【问题标题】:Change remote device name and class via HCI通过 HCI 更改远程设备名称和类别
【发布时间】:2021-01-30 10:19:29
【问题描述】:

我有一个蓝牙音频接口,我想更改其名称和 CoD(设备类别)参数。我曾尝试在 Ubuntu 上使用 hcitool 执行此操作,其中:sudo hcitool cmd 0x03 0x0024 0x260420 应该将 CoD 设置为 0x260420(当前为 0x260404),但我没有运气。根据我现在的理解,我认为您不能在 Linux 计算机上使用 hcitool 将cmd 命令发送到通过蓝牙连接到该计算机的蓝牙设备。有没有办法做到这一点?

是否有可能以任何方式远程更改蓝牙设备的配置?

【问题讨论】:

    标签: linux bluetooth bluez hci


    【解决方案1】:

    你可以用一个小的C 脚本试试。我们将使用这两种方法,您可以在hci_lib.h 头文件中找到。

    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);
    

    请注意,设备类别 (CoD) 有严格的要求,您可以在官方蓝牙文档 (here) 中找到。这就是为什么你需要sudo 来执行。

    // 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;
    }
    

    控制台返回:

    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 $ 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-13
      • 1970-01-01
      • 2017-01-04
      • 2018-10-23
      • 1970-01-01
      • 2012-09-23
      • 2011-07-08
      相关资源
      最近更新 更多