我正在创建一个通过蓝牙连接到sensortag cc2650的应用程序,但是当我尝试将值写入其特征的描述符时,该特性没有一个,我该如何配置它?
private final BluetoothGattCallback btleGattCallback = new BluetoothGattCallback() {
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
byte[] data = characteristic.getValue();
Log.v("valores", String.valueOf(data));
}
@Override
public void onConnectionStateChange(final BluetoothGatt gatt, final int status, final int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
bluetoothGatt.discoverServices();
}
}
@Override
public void onServicesDiscovered(final BluetoothGatt gatt, final int status) {
List<BluetoothGattService> services = bluetoothGatt.getServices();
for (BluetoothGattService service : services) {
if (service.getUuid().compareTo(movementServiceUUID) == 0) {
writeCharacteristic(service.getCharacteristics(), service);
break;
}
}
}
};
这是我连接到蓝牙设备并传递回调的部分
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ItemListView item = adapter.getItem(position);
bluetoothGatt = devicesMap.get(item.getId()).connectGatt(MainActivity.this, false, btleGattCallback);
}
});
这是连接后的回调
private void writeCharacteristic(List<BluetoothGattCharacteristic> list, BluetoothGattService service) {
for (BluetoothGattCharacteristic charac : list) {
if (charac.getUuid().compareTo(movementDataUUID) == 0) {
bluetoothGatt.setCharacteristicNotification(charac, true);
//This descriptor ok
BluetoothGattDescriptor descriptor = charac.getDescriptor(this.movementDataDescriptorUUID);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
bluetoothGatt.writeDescriptor(descriptor);
}
if (charac.getUuid().compareTo(movementConfigUUID) == 0) {
bluetoothGatt.setCharacteristicNotification(charac, true);
//This descriptor is null
BluetoothGattDescriptor descriptor = charac.getDescriptor(this.movementConfigUUID);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
bluetoothGatt.writeDescriptor(descriptor);
}
if (charac.getUuid().compareTo(movementPeriodUUID) == 0) {
bluetoothGatt.setCharacteristicNotification(charac, true);
//This descriptor is null
BluetoothGattDescriptor descriptor = charac.getDescriptor(this.movementPeriodUUID);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
bluetoothGatt.writeDescriptor(descriptor);
}
}
}
答案 0 :(得分:0)
您确定远程设备是否已将特性客户端配置描述符添加到其GATT表中,以了解您想要通知的特征?只需要仔细检查它,请执行以下操作:在发现服务的位置设置断点,并在调试器中查找特性。查找特征属性,并查看特征属性是否具有此值:PROPERTY_NOTIFY
常数值:16(0x00000010)
或者,记录属性值:characteristic.getProperties()
我从TI wiki page检查了传感器标签移动服务,只有数据特征具有通知访问权限。其余特性仅具有读/写访问权限。这意味着您永远无法编写客户端特性配置。描述符因为它们不包括一个。因此,您将无法通知这些特征。