在 BLE android 应用程序中 onCharacteristicRead 没有调用

我正在开发一个与 BLE 设备连接的应用程序,并且我正在遵循谷歌开发者指南。

根据开发人员指南,我已经扫描了 BLE 设备并成功与它们建立了连接,并能够获得可用的服务和特性。

当我尝试使用读取特征值时,我的问题就来了

bluetoothGatt.readCharacteristic(gattCharacteristic)

但它不起作用它没有调用 gatt 回调方法

onCharacteristicRead

这些特征属性是 READ ,因为我没有使用 Descriptor 或任何启用的通知。

我已经尝试了 SO 和 Google 中提到的所有解决方案,但我仍然无法解决这个问题

如果有人可以帮助我,我对这个概念很陌生。

请找到以下代码以供参考:

该类用于蓝牙回调的服务。


白衣非少年
浏览 857回答 1
1回答

隔江千里

尝试对每个特征使用您自己的自定义方法。您需要知道您的服务的 UUID 和您的特征的 UUID: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("UUID of service"));    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("UUID of characteristic"));    if(mBluetoothGatt.readCharacteristic(mReadCharacteristic) == false){        Log.w(TAG, "Failed to read characteristic");    }}public void writeCustomCharacteristic(byte[] value) {    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("UUID of service"));    if(mCustomService == null){        Log.w(TAG, "Custom BLE Service not found");        return;    }    /*get the read characteristic from the service*/    BluetoothGattCharacteristic mWriteCharacteristic = mCustomService.getCharacteristic(UUID.fromString("UUID of characteristic"));    mWriteCharacteristic.setValue(value);    if(mBluetoothGatt.writeCharacteristic(mWriteCharacteristic) == false){        Log.w(TAG, "Failed to write characteristic");    }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java