BLE GATT写完后没有通知数据

时间:2015-12-10 06:09:26

标签: android bluetooth bluetooth-lowenergy

我有一个自定义服务(0000fff0),并且在服务中有可通知的特征(0000fff1)和可写特征(0000fff2)。 (没有'读'特征) 根据我的蓝牙设备的API文档, 数据包格式如下:

68 xx xxxx xx ... xx 16
(one pair of 'xx' is a byte hex code)
  • 68 - > header(1byte)
  • xx - >功能代码(1字节)
  • xxxx - >数据长度(2字节)
  • ... - > data(... byte)
  • xx - >校验和(1byte)
  • 16 - > tail(1byte)

和写在doc上的示例数据请求代码是

68 06 01 00 00 6f 16
  • 68 - >标题
  • 06 - >功能代码
  • 01 00 - >数据长度
  • 00 - >数据
  • 6f - >校验和
  • 16 - >尾

然后基于文档,我应该从68 86 0e 00 46 00 00 00 00 00 00 00 00 01 00 00 00 00 43 16这样的设备获得响应,但是我没有从通知特性中得到任何信息。

从onCharacteristicWrite回调我也得到GATT_SUCCESS。所以我认为写作不是问题所在。并且通知也适用于其他心率设备(标准HEART_RATE_MEASUREMENT特征)。

该文件没有说明响应数据将通过通知特征。我只是假设它可能在那里。

我想知道API文档上的示例是错误还是我错过了什么?是否存在某种像http通信这样的“请求 - 响应”格式?所以每次我写一些关于可写特征的东西,我都会从设备或类似的东西得到一些回应,......

    private final ExpandableListView.OnChildClickListener servicesListClickListner =
        new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
                                        int childPosition, long id) {
                if (mGattCharacteristics != null) {
                    final BluetoothGattCharacteristic characteristic =
                            mGattCharacteristics.get(groupPosition).get(childPosition);
                    final int charaProp = characteristic.getProperties();
                    if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
                        // If there is an active notification on a characteristic, clear
                        // it first so it doesn't update the data field on the user interface.
                        if (mNotifyCharacteristic != null) {
                            mBluetoothLeService.setCharacteristicNotification(
                                    mNotifyCharacteristic, false);
                            mNotifyCharacteristic = null;
                        }
                        mBluetoothLeService.readCharacteristic(characteristic);
                    }
                    if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
                        mNotifyCharacteristic = characteristic;
                        mBluetoothLeService.setCharacteristicNotification(
                                characteristic, true);
                    }

                    if ((charaProp | BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) {
                        if(SampleGattAttributes.BLACK_WRITE.equals(characteristic.getUuid().toString())){
                            mBluetoothLeService.writeRemoteCharacteristic(characteristic);
                        }
                    }

                        return true;
                }
                return false;
            }
};
public void writeRemoteCharacteristic(BluetoothGattCharacteristic characteristic) {
    if(mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }
    if (SampleGattAttributes.BLACK_WRITE.equals(characteristic.getUuid().toString())) {
        byte[] data = new byte[7];
        data[0] = (byte)0x68;
        data[1] = (byte)0x06;
        data[2] = (byte)0x01;
        data[3] = (byte)0x00;
        data[4] = (byte)0x01;
        data[5] = (byte)0x70;
        data[6] = (byte)0x16;

        characteristic.setValue(data);
        mBluetoothGatt.writeCharacteristic(characteristic);
    }
    mBluetoothGatt.writeCharacteristic(characteristic);
}

private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
    @Override
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status){
        if (status == BluetoothGatt.GATT_SUCCESS) {
            broadcastUpdate(ACTION_DATA_AVAILABLE,characteristic);
        }
    }

...

0 个答案:

没有答案