为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

白衣非少年 2022-05-25 16:01:26
我正在开发一个与 BLE 设备连接的应用程序,并且我正在遵循谷歌开发者指南。根据开发人员指南,我已经扫描了 BLE 设备并成功与它们建立了连接,并能够获得可用的服务和特性。当我尝试使用读取特征值时,我的问题就来了bluetoothGatt.readCharacteristic(gattCharacteristic)但它不起作用它没有调用 gatt 回调方法onCharacteristicRead这些特征属性是 READ ,因为我没有使用 Descriptor 或任何启用的通知。我已经尝试了 SO 和 Google 中提到的所有解决方案,但我仍然无法解决这个问题如果有人可以帮助我,我对这个概念很陌生。请找到以下代码以供参考:该类用于蓝牙回调的服务。
查看完整描述

1 回答

?
隔江千里

TA贡献1906条经验 获得超10个赞

尝试对每个特征使用您自己的自定义方法。您需要知道您的服务的 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");

    }

}


查看完整回答
反对 回复 2022-05-25
  • 1 回答
  • 0 关注
  • 856 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号