Android BLE - 阅读自定义服务

时间:2016-08-30 12:35:52

标签: android bluetooth-lowenergy characteristics

我尝试通过BLE通过Android设备与μ控制器通信。我能够编写我的自定义特性(在我的Dev-Kit上切换LED)但无法读取LED状态的值('0x00'表示关闭,'0x01'表示打开)。

当我点击一个项目时点击ExpandableListView时,我想阅读它。目前我在onChildClickListener中实现了它。如果是特征权限"PROPERTY_WRITE" > 0那么它应该写入值。

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

                            mBluetoothLeService.readCustomCharacteristic();
                            mBluetoothLeService.writeCustomCharacteristic(0x01);


                        }
                        return true;
                    }
                    return false;
                } 

但总是无法读懂这个特征。写入LED值并且LED切换为“ON”但不读取LED的值。 我想通过单击列表中的特征来读取打开/关闭LED的值。

我无法弄清楚我做错了什么。它应该是阅读特征,写在文本字段中。在我的BLE-Device上我启用了阅读功能。我可以使用gatttool -> char-read-hnd [hnd] [val]在我的ubuntu-terminal上读取值。

这是我在mBluetoothLeService

readCustomCharacteristic()的实现
public void readCustomCharacteristic() {
        if (mBluetoothAdapter == null || mBluetoothGatt == null) {
            Log.w(TAG, "BluetoothAdapter not initialized");
            return;
        }
        /*check if the service is available on the device*/
        BluetoothGattService mCustomService = mBluetoothGatt.getService(UUID.fromString("edfec62e-9910-0bac-5241-d8bda6932a2f"));
        if(mCustomService == null){
            Log.w(TAG, "Custom BLE Service not found");
            return;
        }
        /*get the read characteristic from the service*/
        BluetoothGattCharacteristic mReadCharacteristic = mCustomService.getCharacteristic(UUID.fromString("18192021-2223-2425-2627-282930313233"));
        mBluetoothGatt.readCharacteristic(mReadCharacteristic);

        if(mBluetoothGatt.readCharacteristic(mReadCharacteristic) == false){
            Log.w(TAG, "Failed to read characteristic");
        }
        return;
    }

1 个答案:

答案 0 :(得分:0)

看起来您可能缺少的是将描述符设置为Notify / indication。我们必须手动执行此操作(iOS不会),但我想这些是我们提供的工具。这是一个可能有用的小片段:

BluetoothGattDescriptor descriptor = characteristic.getDescriptor(DESCRIPTOR_CONFIG_UUID);
bleGatt.setCharacteristicNotification(characteristic, true);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
bleGatt.writeDescriptor(descriptor);

根据您的外围设备,您可能必须将desciptor值设置为ENABLE_INDICATION_VALUE。如果你不知道描述符UUID,你可以做

characteristic.getDescriptors()

获取可用描述符的列表。

在编写描述符之后,你应该在你的gatt回调中调用onDescriptorWrite。如果一切正常,未来的特征变化应该在你的gatt回调中调用onCharacteristicChanged方法。

希望这有帮助!