【问题标题】:How to write descriptor when my BluetoothGattCharacteristic does not have one?当我的 BluetoothGattCharacteristic 没有描述符时如何编写描述符?
【发布时间】:2015-12-14 12:49:42
【问题描述】:

我正在制作一个通过蓝牙连接到 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);
        }
    }
}

【问题讨论】:

    标签: java android bluetooth bluetooth-lowenergy texas-instruments


    【解决方案1】:

    您确定远程设备已将 Characteristic Client Config Descriptor 添加到其 GATT 表中以获得您想要通知的特征吗?只需仔细检查它,请执行以下操作:在发现服务的位置设置断点并在调试器中查找特征。查找特征属性,看看特征属性是否有这个值:PROPERTY_NOTIFY

    常数值:16 (0x00000010)

    或者,记录属性值:characteristic.getProperties()

    我从TI wiki page 检查了传感器标签移动服务,只有 数据特征 具有通知访问权限。其余特征只有读/写访问权限。这意味着您将永远无法编写他们的客户端特征配置。描述符,因为它们不包含一个。因此,您将无法从这些特征中获得通知。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多