这个问题也是我遇到的,通过第三方工具我排除了uuid错误的问题,发送给蓝牙设备的指令也成功了,但是就是收不到返回的,第三方的工具是能收到的,又排除了设备的问题,确定是自己代码的问题之后,看了一些博客之后也有人遇到这个问题,我在我的demo中也修改了,可以看到:
private void displayGattServices(List<BluetoothGattService> gattServices) {
Iterator localIterator1 = gattServices.iterator();
while (localIterator1.hasNext()) {
BluetoothGattService localBluetoothGattService = (BluetoothGattService) localIterator1
.next();
if (localBluetoothGattService.getUuid().toString()
.equalsIgnoreCase(SERVICE_UUID)) {
List localList = localBluetoothGattService.getCharacteristics();
Iterator localIterator2 = localList.iterator();
while (localIterator2.hasNext()) {
BluetoothGattCharacteristic localBluetoothGattCharacteristic = (BluetoothGattCharacteristic)
localIterator2
.next();
if (localBluetoothGattCharacteristic.getUuid().toString()
.equalsIgnoreCase(NOTIFICATION_UUID)) {
bleGattCharacteristic = localBluetoothGattCharacteristic;
mBluetoothGatt.setCharacteristicNotification(bleGattCharacteristic, true);
/*//notifiction默认是关闭的 需要设置0x01打开
List<BluetoothGattDescriptor> descriptors = localBluetoothGattCharacteristic.getDescriptors();
for (int i = 0; i < descriptors.size(); i++) {
if (descriptors.get(i).getUuid().toString().equals(DISENABLE)) {
BluetoothGattDescriptor bluetoothGattDescriptor = descriptors.get(i);
bluetoothGattDescriptor.setValue(BluetoothGattDescriptor.PERMISSION_READ);
mBluetoothGatt.writeDescriptor(bluetoothGattDescriptor);
}
}*/
BluetoothGattDescriptor descriptor = localBluetoothGattCharacteristic
.getDescriptor(UUID.fromString(NOTIFICATION_UUID));
if (descriptor == null) {
descriptor = new BluetoothGattDescriptor(
UUID.fromString(NOTIFICATION_UUID),
BluetoothGattDescriptor.PERMISSION_WRITE);
}
descriptor
.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
break;
}
}
break;
}
}
}
主要就是我注释掉的这段代码,在查找服务的方法,notifi默认是关闭的,我设置为0x01就打开了,如果0x01你们没用的话,再问下硬件工程师,然后解开上面的注释,把下面的注释掉就可以正常收发指令了
demo:https://github.com/duoshine/BlueToothConnect-master