Bluetooth classic. How to set device class?

Projdysvit
Posts: 2
Joined: Fri Jul 14, 2023 2:37 pm

Bluetooth classic. How to set device class?

Postby Projdysvit » Fri Jul 14, 2023 3:06 pm

Hello.
I'm trying to set the device class to "Headphones".
  1. // init buf
  2.  
  3. setPackSize(buf, 4 + 3);
  4. uint8ToBuf(buf, PACK_TYPE_CMD);
  5. setOpCode(buf, HCI_OGF_CONTROLLER, HCI_OCF_WRITE_DEVICE_CLASS);
  6. //data
  7. uint8ToBuf(buf, 3);
  8. uint8ToBuf(buf, 0b00100000);
  9. uint8ToBuf(buf, 0b00000100);
  10. uint8ToBuf(buf, 0b00011000);
  11.  
  12. // send packet

But during the scan, the controller provides the following information
inq res.png
inq res.png (48.48 KiB) Viewed 929 times
I think the problem is in the packet data. Most likely, there is some information missing for the controller. But I can't find any hints on the net about what a properly formed package should look like for this command. Can someone share this information with me? Thanks!

bidrohini
Posts: 202
Joined: Thu Oct 27, 2022 12:55 pm

Re: Bluetooth classic. How to set device class?

Postby bidrohini » Sun Jul 16, 2023 3:27 pm

Try the following code snippet:

Code: Select all

// Assuming buf is a buffer where you construct the HCI command

// Initialize the buffer with the necessary parameters
setPackSize(buf, 9); // Command opcode (2 bytes) + Parameter length (1 byte) + Parameters (6 bytes)

// Set the HCI Command OpCode to write the class of device
setOpCode(buf, HCI_OGF_CONTROLLER, HCI_OCF_WRITE_CLASS_OF_DEVICE);

// Set the parameter length (6 bytes)
uint8ToBuf(buf, 6);

// Set the parameters for the device class (in this case, "Headphones")
uint32_t deviceClass = 0x240404; // Headphones: Major Service Class (0x2400) + Audio (0x04) + Headphones (0x04)
uint24ToBuf(buf, deviceClass); // Helper function to write a 24-bit value to the buffer

// Now you can send the packet (HCI command) to the Bluetooth controller

Please note the following changes in the code:

The parameter length is set to 6 because the device class is 3 bytes long (24 bits), and we need an additional byte for the HCI OpCode.
The device class value is set to 0x240404, where 0x2400 represents the Major Service Class (Audio) and 0x04 represents the specific device class (Headphones).
Remember that the actual implementation of the functions setPackSize, uint8ToBuf, setOpCode, and uint24ToBuf will depend on your specific Bluetooth stack or platform. Make sure these functions are correctly implemented and handle the buffer manipulation and data conversion properly.

After sending this HCI command, the Bluetooth controller should set the device class to "Headphones," and during scanning, other devices should see it as a headphone device.

Projdysvit
Posts: 2
Joined: Fri Jul 14, 2023 2:37 pm

Re: Bluetooth classic. How to set device class?

Postby Projdysvit » Tue Jul 25, 2023 6:03 pm

bidrohini wrote:
Sun Jul 16, 2023 3:27 pm
The parameter length is set to 6 because the device class is 3 bytes long (24 bits), and we need an additional byte for the HCI OpCode.
Sorry, but ChatGPT is a very bad helper in this case.
1 oct - packet type.
2 oct - OGF+OCF(op.code)
1 oct - params len.
3 oct - data len.
1 + 2 + 1 + 3 = 7.
Am I not understanding something correctly? Thank you!

Who is online

Users browsing this forum: No registered users and 88 guests