简体   繁体   English

从未调用Android BLE onCharacteristicRead和onCharacteristicChanged

[英]Android BLE onCharacteristicRead and onCharacteristicChanged never called

I am trying to connect to a Bluetooth LE thermometer. 我正在尝试连接到蓝牙LE温度计。 Connecting to the device is working good. 连接到设备工作正常。 The only part hanging me up is the gattCallBack and it's onCharacteristicChanged / Read. 使我挂断的唯一部分是gattCallBack,它处于onCharacteristicChanged / Read状态。 the 'setNotification' and descriptor 'setValue' and 'writeDescriptor' all return true. 'setNotification'和描述符'setValue'和'writeDescriptor'都返回true。 The onCharacteristicChanged is never called to return a value. 从不调用onCharacteristicChanged返回值。

I used a pretty handy little program from the Play Store called BLE Scanner to help me to give me more information about the device and it's services and characteristics. 我使用了来自Play商店的一个非常方便的小程序,称为BLE Scanner,可以帮助我向我提供有关该设备及其服务和特性的更多信息。

在此处输入图片说明

This is why I simply hard coded service 2, characteristic 0. I just can't seem to figure out why after I writeDescriptor, I never see anything come back. 这就是为什么我只是对服务2(特征0)进行硬编码的原因。我似乎无法弄清楚为什么在我编写了Descriptor之后,再也看不到任何东西。 The interesting thing is, I can use some of the other Characteristics (one being Temperature Interval) and I do receive a response (although the data is garbled.) 有趣的是,我可以使用其他一些特性(一个是温度间隔),并且确实会收到响应(尽管数据有乱码。)

Also, out of curiosity, why are there 2 descriptors on this characteristic? 另外,出于好奇,为什么在这个特征上有两个描述符?

This code is contained in my MainActivity method. 此代码包含在我的MainActivity方法中。 Not sure if that would make a difference here. 不知道这是否会有所作为。 I have looked at and tried several methods posted on here with no luck. 我已经看过并尝试了一些运气不好的方法。

private final BluetoothGattCallback gattCallback = new BluetoothGattCallback()
{
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState)
    { ... }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status)
    {
        mGatt = gatt;

        List<BluetoothGattService> services = mGatt.getServices();
        Log.i("onServicesDiscovered", services.toString());

        BluetoothGattCharacteristic characteristic = services.get(2).getCharacteristics().get(0);

        mGatt.setCharacteristicNotification(characteristic, true);

        BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(CLIENT_CHARACTERISTIC_CONFIG));
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);

        mGatt.writeDescriptor(descriptor);

    }            

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)
    { ... }

    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)
    { ... }
};

UPDATE: I decided to check the the onDescriptorWrite method and Log some information. 更新:我决定检查onDescriptorWrite方法并记录一些信息。

@Override            
public void onDescriptorWrite(BluetoothGatt gatt,  BluetoothGattDescriptor descriptor, int status)
    {
        Log.i("descriptorWRITE", Integer.toString(status));
    }

Interesting thing here is that status is returning 13 which is 'A write operation exceeds the maximum length of the attribute'. 有趣的是,状态返回13,即“写操作超出了属性的最大长度”。

I will be looking into this further. 我将对此进行进一步调查。

I found the problem here. 我在这里发现了问题。 I was assuming that the Thermometer was using the standard BLE service and characteristic setup. 我以为温度计正在使用标准BLE服务和特性设置。 It is not. 它不是。 They created their own Custom Characteristic. 他们创建了自己的自定义特征。 Once I switched to that Characteristic, the 'changed' method started firing. 一旦我切换到该特性,就开始触发“更改”方法。

I think you are missing mGatt.readCharacteristic(characteristic) . 我认为您缺少mGatt.readCharacteristic(characteristic)

When you call it, it will wait for the read to be finished and call onCharacteristicRead . 调用它时,它将等待读取完成并调用onCharacteristicRead

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM