我正在尝试从移动设备(扫描仪)写入已连接可穿戴设备(广播器)中的描述符。
在可穿戴设备上,我将描述符定义为
readCharacteristic = new BluetoothGattCharacteristic(Constants.READ_CHAR_UUID,
BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_NOTIFY,
BluetoothGattCharacteristic.PERMISSION_READ);
readCharacteristic.addDescriptor(new BluetoothGattDescriptor(Constants.NOTIFY_DESC_UUID,
BluetoothGattDescriptor.PERMISSION_READ | BluetoothGattDescriptor.PERMISSION_WRITE));
在手机上,我尝试用
BluetoothGattDescriptor descriptor = readCharacteristic.getDescriptor(Constants.NOTIFY_DESC_UUID);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE)
不幸的是,这失败,状态为GATT_WRITE_NOT_PERMITTED
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
}
在可穿戴的日志猫中,我看到gatts_write_attr_perm_check-GATT_WRITE_NOT_PERMIT。
当我尝试写特征时,也会发生同样的事情。
奇怪的是,使用iOS扫描仪时写入成功。这意味着可穿戴部件是正确的。
我具有清单中的权限:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
使用的设备:
Nexus 5x 6.0.1 API 23,
Moto 360 5.1.1 API 22
有谁知道如何解决此描述符/特征写入问题?
最佳答案
我正在尝试从移动设备(扫描仪)写入描述符
连接的可穿戴设备(广播者)。
在这种情况下,它应该是移动设备(中央)并连接可穿戴设备(外围)。扫描器和广播者实际上未建立BLE连接。
关于写入失败,当应用程序尝试将数据写入数据库时,在功能gatts_write_attr_perm_check
处将其拒绝。代码如下所示:
else if (!(perm & GATT_WRITE_ALLOWED))
{
status = GATT_WRITE_NOT_PERMIT;
GATT_TRACE_ERROR( "gatts_write_attr_perm_check -GATT_WRITE_NOT_PERMIT");
}
因此,问题在于录制的烫发不符合GATT_WRITE_ALLOWED的条件,即
> (GATT_PERM_WRITE | GATT_PERM_WRITE_ENCRYPTED |\
> GATT_PERM_WRITE_ENC_MITM | GATT_PERM_WRITE_SIGNED |\
> GATT_PERM_WRITE_SIGNED_MITM)
因此,您可以尝试以下方法:
1)取消配对设备,然后再次配对设备。
2)启用高安全等级,例如启用MITM。
我想您想启用通知吗?我无法获取您拥有的所有代码,但您也可以尝试一下(来自Android网站的报价):
private BluetoothGatt mBluetoothGatt;
BluetoothGattCharacteristic characteristic;
boolean enabled;
...
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
...
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);