Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android可读取自定义服务_Android_Bluetooth Lowenergy_Characteristics - Fatal编程技术网

Android可读取自定义服务

Android可读取自定义服务,android,bluetooth-lowenergy,characteristics,Android,Bluetooth Lowenergy,Characteristics,我尝试通过android设备通过BLE与µ控制器通信。我能够写入自定义特性(在我的开发工具包上切换LED),但无法读取LED状态值(“0x00”表示关闭,“0x01”表示打开) 当我点击一个项目时,我想在点击ExpandableListView时阅读它。目前,我在onChildClickListener中实现了它。如果特征权限“属性_WRITE”>0,则应写入该值 private final ExpandableListView.OnChildClickListener servicesList

我尝试通过android设备通过BLE与µ控制器通信。我能够写入自定义特性(在我的开发工具包上切换LED),但无法读取LED状态值(“0x00”表示关闭,“0x01”表示打开)

当我点击一个项目时,我想在点击ExpandableListView时阅读它。目前,我在onChildClickListener中实现了它。如果特征权限
“属性_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将切换为“开”,但不会读取LED值。 我想通过单击列表中的特征来读取打开/关闭LED的值

我不知道我做错了什么。它应该读取特征,并将其写入文本字段。在我的BLE设备上,我已启用读取。我可以使用ubuntu终端使用
gatttool->char read hnd[hnd][val]
读取值

这是我在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;
    }

看起来您可能缺少的是将描述符设置为通知/指示。我们不得不手动操作(iOS没有),这是一种痛苦,但我想这就是我们所拥有的工具。下面是一个可能会有所帮助的小片段:

BluetoothGattDescriptor descriptor = characteristic.getDescriptor(DESCRIPTOR_CONFIG_UUID);
bleGatt.setCharacteristicNotification(characteristic, true);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
bleGatt.writeDescriptor(descriptor);
根据您的外设,您可能需要设置desciptor值以启用\u指示\u值。如果您不知道描述符UUID,可以这样做

characteristic.getDescriptors()
获取可用描述符的列表

在编写描述符之后,您应该在gatt回调中获得对onDescriptorWrite的调用。如果一切正常,并且将来的特性更改应该在gatt回调中调用onCharacteristicChanged方法


希望这有帮助

看起来您可能缺少的是将描述符设置为通知/指示。我们不得不手动操作(iOS没有),这是一种痛苦,但我想这就是我们所拥有的工具。下面是一个可能会有所帮助的小片段:

BluetoothGattDescriptor descriptor = characteristic.getDescriptor(DESCRIPTOR_CONFIG_UUID);
bleGatt.setCharacteristicNotification(characteristic, true);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
bleGatt.writeDescriptor(descriptor);
根据您的外设,您可能需要设置desciptor值以启用\u指示\u值。如果您不知道描述符UUID,可以这样做

characteristic.getDescriptors()
获取可用描述符的列表

在编写描述符之后,您应该在gatt回调中获得对onDescriptorWrite的调用。如果一切正常,并且将来的特性更改应该在gatt回调中调用onCharacteristicChanged方法


希望这有帮助

你的陈述是错误的。您必须打开BLE设备上的通知或指示才能使用它。iOS不会自动开启,尽管设备可以通过固件设计开启。在您提供的源代码示例中,什么是
toBeNotified
?感谢您的回答!在我的外设代码中,我为我的服务设置了一个单独的UUID,为我的特征设置了一个UUID(128位)。我没有设置通知描述符之类的东西。我使用了一个示例,并遵循了设备制造商提供的教程,没有特征配置描述符。但我甚至不能用描述符读出预先定义的“定制服务和特性”。此字符的权限为读/写/写\u无\u响应。android开发者的标准示例代码也不起作用。我只知道一位同事告诉我的关于iOS的事情,如果我弄错了,很抱歉。的确,它确实需要在固件级别启用。不幸的是,Android似乎并不在意它已经在硬件层面启用了。我正在将代码转换为一个更通用的代码段,但我忽略了那个变量。“我已经把它修好了。”烟头,这很有趣。当我遇到这个问题时,我正在使用一个内部硬件,在那里我找不到描述符UUID。因此,我编写了characteristic.getDescriptors代码。如果我是你,我会在你的第二段代码中插入
mReadCharacteristic.getDescriptors()
,然后打印出结果列表以查看哪些描述符可用。我如何打印它们。。?这是一个
列表
如何打印它们或使它们在logcat中可见?您的陈述是错误的。您必须打开BLE设备上的通知或指示才能使用它。iOS不会自动开启,尽管设备可以通过固件设计开启。在您提供的源代码示例中,什么是
toBeNotified
?感谢您的回答!在我的外设代码中,我为我的服务设置了一个单独的UUID,为我的特征设置了一个UUID(128位)。我没有设置通知描述符之类的东西。我使用了一个示例,并遵循了设备制造商提供的教程,没有特征配置描述符。但我甚至不能用描述符读出预先定义的“定制服务和特性”。此字符的权限为读/写/写\u无\u响应。android开发者的标准示例代码也不起作用。我只知道一位同事告诉我的关于iOS的事情,如果我弄错了,很抱歉。的确,它确实需要在固件级别启用。不幸的是,Android似乎并不在意它已经在硬件层面启用了。我正在将代码转换为一个更通用的代码段,但我忽略了那个变量。“我已经把它修好了。”烟头,这很有趣。当我遇到这个问题时,我正在使用一个内部硬件,在那里我找不到描述符UUID。因此,我编写了characteristic.getDescriptors代码。如果我是你,我会在你的第二段代码中插入
mReadCharacteristic.getDescriptors()
,然后打印出结果列表以查看哪些描述符可用。我如何打印它们。。?这是一个
列表
如何打印它们或使它们在logc中可见