android ble蓝牙接收不到数据_Android应用开发之android BLE 4.0 setCharacteristicNotification接收不到数据...

本文将带你了解Android应用开发之android BLE 4.0  setCharacteristicNotification接收不到数据,希望本文对大家学Android有所帮助

问题描述 最近在开发android BLE  读写数据

但是向设备写数据很顺利,但是在接收设备传来的数据时,死活接收不到.

Java代码    /**     * Enables or disables notification on a give characteristic.     *     * @param characteristic Characteristic to act on.     * @param enabled        If true, enable notification.  False otherwise.     */     public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,                                               boolean enabled) {         if (mBluetoothAdapter == null || mBluetoothGatt == null) {             Log.w(TAG, "BluetoothAdapter not initialized");             return;         }         mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);          }     /**

* Enables or disables   notification on a give characteristic.

*

* @param characteristic   Characteristic to act on.

* @param enabled        If true, enable notification.  False otherwise.

*/

public void   setCharacteristicNotification(BluetoothGattCharacteristic   characteristic,

boolean enabled) {

if (mBluetoothAdapter == null   || mBluetoothGatt == null) {

Log.w(TAG,   "BluetoothAdapter not initialized");

return;

}

mBluetoothGatt.setCharacteristicNotification(characteristic,   enabled);

}

上面这样写 是收不到数据的 ,主要是少加了mBluetoothGatt.writeDescriptor(descriptor);

下面是修改后的代码

Java代码       /**      * Enables or disables notification on a give characteristic.      *      * @param characteristic Characteristic to act on.      * @param enabled        If true, enable notification.  False otherwise.      */      public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,                                                boolean enabled) {          if (mBluetoothAdapter == null || mBluetoothGatt == null) {              Log.w(TAG, "BluetoothAdapter not initialized");              return;          }  //        mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);          boolean isEnableNotification =  mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);          if(isEnableNotification) {              List descriptorList = characteristic.getDescriptors();              if(descriptorList != null && descriptorList.size() > 0) {                  for(BluetoothGattDescriptor descriptor : descriptorList) {                      descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);                      mBluetoothGatt.writeDescriptor(descriptor);                  }              }          }      }     /**

* Enables or disables   notification on a give characteristic.

*

* @param characteristic   Characteristic to act on.

* @param enabled        If true, enable notification.  False otherwise.

*/

public void   setCharacteristicNotification(BluetoothGattCharacteristic   characteristic,

boolean enabled) {

if (mBluetoothAdapter == null   || mBluetoothGatt == null) {

Log.w(TAG,   "BluetoothAdapter not initialized");

return;

}

//          mBluetoothGatt.setCharacteristicNotification(characteristic,   enabled);

boolean isEnableNotification   =  mBluetoothGatt.setCharacteristicNotification(characteristic,   enabled);

if(isEnableNotification)   {

List descriptorList = characteristic.getDescriptors();

if(descriptorList != null   && descriptorList.size() > 0) {

for(BluetoothGattDescriptor descriptor : descriptorList) {

descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);

mBluetoothGatt.writeDescriptor(descriptor);

}

}

}

}

如果 不是这种 情况接收不到数据就是另外一种可能了characteristic的 uuid设置不对

你可以看下 boolean isEnableNotification =   mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

返回的是false 还是true,

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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值