java - 安卓蓝牙 : descriptor/characteristic write fails with GATT_WRITE_NOT_PERMITTED

标签 java android bluetooth bluetooth-lowenergy

我正在尝试从我的移动设备(扫描仪)写入已连接的可穿戴设备(广播设备)中的描述符。

在可穿戴设备上,我将描述符定义为

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) {

}

在可穿戴 logcat 中,我看到 gatts_write_attr_perm_check - GATT_WRITE_NOT_PERMIT。

当我尝试写入特征时,同样的事情发生了。

奇怪的是,使用 iOS 扫描仪时写入成功。这意味着可穿戴部分是正确的。

我在 list 中有权限:

<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" />

使用的设备: 联系 5x 6.0.1 API 23, 摩托 360 5.1.1 API 22

有谁知道如何解决这个描述符/特征写入问题?

最佳答案

From my mobile device(scanner) I am trying to write to a descriptor in a connected wearable(broadcaster).

在这种情况下,它应该是移动设备(中央)和连接的可穿戴设备(外围设备)。 scanner和broadcaster实际上并没有建立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");
       }

所以,问题是记录的 perm 不符合 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);

关于java - 安卓蓝牙 : descriptor/characteristic write fails with GATT_WRITE_NOT_PERMITTED,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39289594/

相关文章:

android - 无法从加载程序 dalvik.system.PathClassLoader 加载 avutil

java - 当方向改变时将对象从一个 fragment 传递到另一个 fragment

java-me - 如何在J2ME蓝牙OBEX中实现ACTION(移动/重命名、设置权限)操作?

android - 我可以绑定(bind)低功耗蓝牙设备吗?

Java 保护应用程序不写入磁盘

Android 蓝牙连接问题

java - runOnUiThread 如何关闭/关闭线程

java - 使用来自蓝牙连接的传入数据来处理特定对象? C# Windows 窗体应用程序和 Java Android 应用程序

java - 使用 Guice 进行可选绑定(bind)以避免用户绑定(bind)

java - 如何在 Java 7 中使用集合文字?