在我的 android 应用程序中,BLE 连接成功,一旦 BLE 设备连接到 Android 手机。如何以编程方式更改连接的 BLE 设备名称?示例代码如下
private static final UUID Device_Name_UUID = UUID.fromString("00002a00-0000-1000-8000-00805f9b34fb");
private static final UUID Write_UUID = UUID.fromString("00001800-0000-1000-8000-00805f9b34fb");
public void Device_Name(){
BluetoothGattService Name_Service = mBluetoothGatt.getService(Write_UUID );
if(Name_Service == null) {
Log.d(TAG, "Name_Service service not found!");
return;
}
BluetoothGattCharacteristic DeviceName = Name_Service.getCharacteristic(Device_Name_UUID);
if(DeviceName == null) {
Log.d(TAG, "DeviceName charateristic not found!");
return;
}
}
Log.v(TAG, "readCharacteristic(DeviceName) = " + mBluetoothGatt.readCharacteristic(DeviceName));
String i = "123";
DeviceName.setValue(i);
Log.v(TAG, "writeCharacteristic(DeviceName) = " + mBluetoothGatt.writeCharacteristic(DeviceName));
Log.v(TAG, "writeCharacteristic(DeviceName) = " + mBluetoothGatt.writeCharacteristic(DeviceName));
此处mBluetoothGatt.writeCharacteristic(DeviceName);方法总是返回false。
经过一些研究,我找到了以下解决方案
需要修改CC2541 keyfob的固件,在keyfobdemo.c中的KeyFobApp_Init()中添加如下代码,添加设备名称的写入权限:
uint8 devNamePermission = GATT_PERMIT_READ|GATT_PERMIT_WRITE;
GGS_SetParameter( GGS_W_PERMIT_DEVICE_NAME_ATT, sizeof ( uint8 ), &devNamePermission );
现在我的问题是,我们需要在哪里添加这些权限?
谁能告诉我如何添加这些权限?
提前致谢
最佳答案
顾名思义:keyfobdemo.c 是一个 .c 文件,因此 TI 告诉您必须在 KeyFobDemo 工作区中添加这些权限,因此您必须对 CC2541 套件进行编程才能更改名称。你不能在 Android 中做到这一点。
你所要做的就是:
从德州仪器网站下载 BLE 堆栈: http://www.ti.com/tool/ble-stack
然后在堆栈中,打开 Projects\ble\KeyFob\CC2541DB\KeyFobDemo.eww 文件。
CC2540 套件的编程需要名为 IAR Embedded Workbench 的 IDE。您可以下载它并获得 30 天试用期: http://www.iar.com/Products/IAR-Embedded-Workbench/8051/
然后打开工程,在APP文件夹中找到keyfobdemo.c文件。从第 200-213 行有一个名为 deviceName[] 的字符数组,它实际上将广告名称定义为“Keyfobdemo”。您只需将其更改为具有正确十六进制值的所需名称,以及数组的长度。 然后在第 236 行,您还必须更改 attDeviceName[] 数组,因为此参数定义了设备处于连接状态后的名称。
关于android - 如何以编程方式更改 Android 中连接的 BLE 设备名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27855620/