Android BLE通知:方法setCharacteristicNotification就够了吗?

时间:2015-05-03 15:37:42

标签: android bluetooth-lowenergy android-bluetooth

我正在开发两个应用程序外围和一个核心角色。

周边

2特征:

  • 使用write_noresponse属性的一个(称为pippo)
  • 具有读取和通知属性的一个(称为paperino)

ý

private void addServiceToGattServer() {

        Service = new BluetoothGattService(
                UUID.fromString(CostantUUID.xxx),
                BluetoothGattService.SERVICE_TYPE_PRIMARY);

        paperino = new BluetoothGattCharacteristic(
                UUID.fromString(CostantUUID.xxx),
                BluetoothGattCharacteristic.PROPERTY_NOTIFY|BluetoothGattCharacteristic.PROPERTY_READ,BluetoothGattCharacteristic.PERMISSION_READ);


        clientCharacteristiConfiguration = new BluetoothGattDescriptor(UUID.fromString(CostantUUID.clientCharacteristiConfiguration), BluetoothGattDescriptor.PERMISSION_WRITE);
        paperino.addDescriptor(clientCharacteristiConfiguration);

        Service.addCharacteristic(paperino);


        pippo = new BluetoothGattCharacteristic(
                UUID.fromString(CostantUUID.userCommands),
                BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE ,
                BluetoothGattCharacteristic.PERMISSION_WRITE);

        ebikeService.addCharacteristic(pippo);

        mGattServer.addService(Service);
    }



@Override
        public void onDescriptorWriteRequest(BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value)
        {
            if(responseNeeded)
              mGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, value);
                ................
                ...............
                paperino.setValue(payload);
                mGattServer.notifyCharacteristicChanged(device,paperino, false);
            }

客户端

我已经使setCharacteristicNotification变得简单,并为特色Paperino写了一个描述符:

    bluetoothGatt.setCharacteristicNotification(characteristic, true);

    /**
     * @author I
     * I abilitate the cliatcharacteristicconfiguration descriptor
     */

    BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(CostantUUid.clientCharacteristiConfiguration));
    if(descriptor != null)
    {
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        bluetoothGatt.writeDescriptor(descriptor);
    }

通过这种方式,所有功能都能正常工作,但我想知道:在externall端添加描述符并从客户端写入它真的是必要的吗?

我试图不使用它并且它不起作用。但是在互联网上,教程永远不会写出描述符......

什么是真的?

此外,如果我做第一个notifyCharacteristicChanged(device,paperino,false);在onConnectionStateChange回调中

@Override
    public void onConnectionStateChange(final BluetoothDevice device,
            int status, int newState) {
        super.onConnectionStateChange(device, status, newState);
        Log.d(TAG, "onConnectionStateChange status=" + status + "->"
                + newState);

        message=myHandler.obtainMessage();

if(newState == BluetoothProfile.STATE_CONNECTED)
            {
                disconnected=false;
                Bundle f=new Bundle();
                f.putString("connectionStatus", (String)(Utils.getStateDescription(newState)));
                message.setData(f);
                myHandler.sendMessage(message);
                connectedDevice=device;

                paperino.setValue(examplemsg);
                        mGattServer.notifyCharacteristicChanged(device, paperino,false);
            }

在服务器端通知" examplemessage"没有到达客户端。我必须第一次调用onDescriptorWrite中的notifyCharacteristicChanged ...

调用notifycharacteristicChanged可能为时尚早?

1 个答案:

答案 0 :(得分:3)

您必须写入描述符,它是蓝牙规范的一部分。 iOS实现抽象出来为您提供帮助。然而,Android实现需要手动写入描述符。

我认为我之前写的这个答案仍然适用:Any way to implement BLE notifications in Android-L preview