Android BLE onCharacteristicRead 和 onCharacteristicChanged 从未调用过

标签 android bluetooth

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

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

enter image description here

这就是为什么我简单地硬编码服务 2,特征 0。我似乎无法弄清楚为什么在我写描述符之后,我再也看不到任何东西回来了。有趣的是,我可以使用其他一些特征(一个是温度间隔)并且我确实收到了响应(尽管数据是乱码。)

另外,出于好奇,为什么这个特征有 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,即“写入操作超出了属性的最大长度”。

我会进一步调查。

最佳答案

我在这里发现了问题。我假设温度计使用的是标准 BLE 服务和特性设置。它不是。他们创建了自己的自定义特征。一旦我切换到该特性,“更改”方法就开始触发。

关于Android BLE onCharacteristicRead 和 onCharacteristicChanged 从未调用过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36659340/

相关文章:

iphone - 远程唤醒应用程序,比如从蓝牙设备

android - 在 setOverScrollMode 中使用反射时遇到问题

php - 如何使用Slim框架获取POST请求实体

iOS Swift 低功耗蓝牙 : Discovering Multiple Characteristics within a Single Service

android - 带有 Inputstream.read 的 Android 2.1 蓝牙 SPP 错误

swift - 使用 Swift 中的按钮通过蓝牙发送数据

ios - 如何获取蓝牙连接设备的电池电量

android - 华为 auth 服务与 .net core 3.1 集成,无法验证 JWT

android - Firestore监听器一次又一次被调用

javascript - 我们如何在我们的 android 应用程序中执行 javascript 函数并获取返回值?