Android BLE onCharacteristicRead和onCharacteristicChanged从未调用

时间:2016-04-16 01:57:34

标签: android bluetooth

我正在尝试连接蓝牙LE温度计。连接到设备工作正常。让我失望的唯一部分是gattCallBack,它是onCharacteristicChanged / Read。 'setNotification'和描述符'setValue'和'writeDescriptor'都返回true。永远不会调用onCharacteristicChanged来返回值。

我在Play商店使用了一个名为BLE Scanner的非常方便的小程序,帮助我向我提供有关该设备及其服务和特性的更多信息。

enter image description here

这就是为什么我只是硬编码服务2,特征0.我只是无法弄清楚为什么在writeDescriptor之后,我再也看不到任何东西了。有趣的是,我可以使用其他一些特性(一个是温度间隔),我确实收到了一个响应(虽然数据是乱码。)

另外,出于好奇,为什么这个特征上有2个描述符?

此代码包含在我的MainActivity方法中。不确定这是否会有所作为。我已经看过并尝试了几种方法,但没有运气。

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)
    { ... }
};

更新: 我决定检查onDescriptorWrite方法并记录一些信息。

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

这里有趣的是状态返回13,即'写操作超过属性的最大长度'。

我将进一步研究这个问题。

2 个答案:

答案 0 :(得分:4)

我在这里发现了问题。我假设温度计正在使用标准的BLE服务和特性设置。它不是。他们创造了自己的自定义特征。一旦我切换到那个特征,“改变了”#39;方法开始射击。

答案 1 :(得分:0)

我认为你遗失了mGatt.readCharacteristic(characteristic)

当您调用它时,它将等待读取完成并调用onCharacteristicRead