Change remote device name and class via HCI

1k views Asked by At

I have a bluetooth audio interface which I want to change the name and the CoD (Class of Device) parameter. I have tried doing this using hcitool on Ubuntu, with: sudo hcitool cmd 0x03 0x0024 0x260420 which is supposed to set the CoD to 0x260420 (it's currently 0x260404), but I've had no luck. From what I now understand, I take id that you cannot use hcitool on a Linux computer to send cmd commands to a bluetooth device connected to that computer via bluetooth. Is there a way to achieve this?

Is it at all possible to change a bluetooth devices' configuration remotely in any way?

1

There are 1 answers

0
alpereira7 On

You can try it with a small C script. We will use these two methods you can find in the hci_lib.h header file.

int hci_read_class_of_dev(int dd, uint8_t *cls, int to);
int hci_write_class_of_dev(int dd, uint32_t cls, int to);

Be aware that a Class of Device (CoD) has strict requirements you can find in the official Bluetooth documentation (here). That's why you need sudo to execute.

// Simple program to read and write Class of Device.
// To compile: `gcc -o rw_cod rw_cod.c -lbluetooth`
// To install lbluetooth: `apt-get install libbluetooth-dev`
// Execute with `sudo ./rw_cod`
// Note the original CoD first because it has strict rules, you might need to write it back later.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> // for close()
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>

int main(void){
  int dd = hci_open_dev(hci_devid("DC:A6:32:E4:9C:05"));
  int timeout = 1000;
  uint8_t cls[3] = {0};
  int class;
  int ret;
  uint32_t new_class =  0x260404;

  // Read CoD
  ret = hci_read_class_of_dev(dd, cls, timeout);
  class = cls[0] | cls[1] << 8 | cls[2] << 16;
  printf("Class of Device              = 0x%06x [%d]\n", class, ret);

  // Set CoD
  ret = hci_write_class_of_dev(dd, new_class, 2000);
  printf("Required new Class of Device = 0x%06x [%d]\n", new_class, ret);

  // Check read CoD
  ret = hci_read_class_of_dev(dd, cls, timeout);
  class = cls[0] | cls[1] << 8 | cls[2] << 16;
  printf("Actual new Class of Device   = 0x%06x [%d]\n", class, ret);

  close(dd); 
  return 0;
}

Console returns:

pi@raspberrypi:~/Work_Bluetooth $ sudo ./rw_cod 
Class of Device              = 0x260404 [0]
Required new Class of Device = 0x260420 [0]
Actual new Class of Device   = 0x260420 [0]
pi@raspberrypi:~/Work_Bluetooth $