可能是由于GattCallback注册了不同的UUID导致,建议检查Descriptor UUID、Characteristic UUID和Service UUID是否与设备的UUID匹配。此外,也可以尝试使用BluetoothGatt#readCharacteristic(BluetoothGattCharacteristic)和BluetoothGatt#writeCharacteristic(BluetoothGattCharacteristic)直接读写特征值并检查是否会触发相应的回调。以下是一个示例代码:
private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
// ...
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
List services = gatt.getServices();
for (BluetoothGattService service : services) {
Log.d(TAG, "getService(): " + service.getUuid());
List characteristics = service.getCharacteristics();
for (BluetoothGattCharacteristic characteristic : characteristics) {
Log.d(TAG, "getCharcteristic(): " + characteristic.getUuid());
List descriptors = characteristic.getDescriptors();
for (BluetoothGattDescriptor descriptor : descriptors) {
Log.d(TAG, "getDescriptor(): " + descriptor.getUuid());
}
}
}
}
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
// ...
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
// ...
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
// ...
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
// ...
}
};
// 读取特征值
BluetoothGattCharacteristic characteristic = gatt.getService(serviceUuid)
.getCharacteristic(characteristicUuid);
gatt.readCharacteristic(characteristic);
// 写入特征值
BluetoothGattCharacteristic characteristic = gatt.getService(serviceUuid)
.getCharacteristic(characteristicUuid);
characteristic.setValue(value);
gatt.writeCharacteristic(characteristic);