Android应用开发之Android Ble notify功能踩坑(解决)
白羽 2019-01-08 来源 :网络 阅读 4335 评论 0

摘要:本文将带你了解Android应用开发Android Ble notify功能踩坑(解决),希望本文对大家学Android有所帮助。

    本文将带你了解Android应用开发Android Ble notify功能踩坑(解决),希望本文对大家学Android有所帮助。


         

最近在研究Ble蓝牙相关的开发,在和蓝牙通信的时候,主动读取消息是ok的,但是就是无法收到蓝牙主动发送出来的消息,而且IOS是可以接收到消息的。这个就比较尴尬了。

通过查阅文档和网上他人的博客找到开启notify功能的代码:


   

private void setAutoReceiveData(BluetoothGatt gatt)   {

      try {

          BluetoothGattService linkLossService =   gatt.getService(SERVICE_UUID);

          BluetoothGattCharacteristic data =   linkLossService.getCharacteristic(CHARACTERISTIC_UUID);

          BluetoothGattDescriptor descriptor =   data.getDescriptor(CHARACTERISTIC_UUID);

          if (null   != descriptor)   {

              descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);

              bluetoothGatt.writeDescriptor(descriptor);

          }

          bluetoothGatt.setCharacteristicNotification(data,   true);

      } catch (Exception e) {

          BleLogUtils.appendLog("setAutoReceiveData:"   +   e.getMessage());

      }

  }

   

通过serviceId获取到对应的service,并从service中获取到对应的BluetoothGattCharacteristic信息。根据BluetoothGattCharacteristic获取到对应的BluetoothGattDescriptor,但是这里每次获取都是为空的,所以无法进行设置,这样的话Android是无法收到蓝牙发送的消息的。

于是对比目前应用市场上的Ble开发工具,通过其他工具是可以正常接收到蓝牙发送的消息的。

基本上定位到是自己程序的问题后,就开始进行问题调查,知道查到github上一个开源的框架。通过进入内部源码查看才知道,如果获取不到BluetoothGattDescriptor的时候,   这时候可以通过获取系统默认的BluetoothGattDescriptor,进行蓝牙接口设置。修改后的代码如下:


public static final String CLIENT_CHARACTERISTIC_CONFIG = "00002902-0000-1000-8000-00805f9b34fb";

 

private void setAutoReceiveData(BluetoothGatt gatt)   {

        try {

            BluetoothGattService linkLossService =   gatt.getService(SERVICE_UUID);

            BluetoothGattCharacteristic data =   linkLossService.getCharacteristic(CHARACTERISTIC_UUID);

            BluetoothGattDescriptor defaultDescriptor =   data.getDescriptor(UUID.fromString(CLIENT_CHARACTERISTIC_CONFIG));

            if (null !=   defaultDescriptor) {

                defaultDescriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);

                bluetoothGatt.writeDescriptor(defaultDescriptor);

            }

            bluetoothGatt.setCharacteristicNotification(data,   true);

        } catch (Exception e) {

            BleLogUtils.appendLog("setAutoReceiveData:"   +   e.getMessage());

        }

    }

   

问题完美解决,这个还是主要对BLE不熟悉导致的。为什么可以从CLIENT_CHARACTERISTIC_CONFIG   中获取到BluetoothGattDescriptor,这个需要后面继续了解,先记录下自己踩过的坑,让别人少踩坑。

   

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Android频道!


本文由 @白羽 发布于职坐标。未经许可,禁止转载。
喜欢 | 3 不喜欢 | 0
看完这篇文章有何感觉?已经有3人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved