1

BLE 経由で Android デバイスを介して µ-controller と通信しようとしています。カスタム特性 (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デバイスでは、読み取りを有効にしました。を使用して、ubuntu-terminal で値を読み取ることができますgatttool -> char-read-hnd [hnd] [val]

これはreadCustomCharacteristic()mBluetoothLeServiceの私の実装です

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;
    }
4

1 に答える 1

0

It looks like what you're probably missing is setting the descriptor to Notify/indicate. It's a pain that we have to do this manually (iOS doesn't), but I guess these are the tools we're given. Here's a little snippet that might help:

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

Depending on your peripheral, you might have to set the desciptor value to ENABLE_INDICATION_VALUE instead. If you don't know the descriptor UUID, you can do

characteristic.getDescriptors()

to get a list of the available descriptors.

After writing the descriptor, you should get a call to onDescriptorWrite in your gatt callback. If everything works correctly, and future characteristic changes should call the onCharacteristicChanged method in your gatt callback.

Hope this helps!

于 2016-08-30T15:30:52.170 に答える