android BLE 4.0 setCharacteristicNotification接收不到数据

转载自:https://347563186.iteye.com/blog/2331399

 

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

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

 

Java代码 

 收藏代码

  1. /** 
  2.     * Enables or disables notification on a give characteristic. 
  3.     * 
  4.     * @param characteristic Characteristic to act on. 
  5.     * @param enabled        If true, enable notification.  False otherwise. 
  6.     */  
  7.    public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,  
  8.                                              boolean enabled) {  
  9.        if (mBluetoothAdapter == null || mBluetoothGatt == null) {  
  10.            Log.w(TAG, "BluetoothAdapter not initialized");  
  11.            return;  
  12.        }  
  13.        mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);  
  14.      
  15.    }  

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

 

下面是修改后的代码

 

Java代码 

 收藏代码

  1.    /** 
  2.      * Enables or disables notification on a give characteristic. 
  3.      * 
  4.      * @param characteristic Characteristic to act on. 
  5.      * @param enabled        If true, enable notification.  False otherwise. 
  6.      */  
  7.     public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,  
  8.                                               boolean enabled) {  
  9.         if (mBluetoothAdapter == null || mBluetoothGatt == null) {  
  10.             Log.w(TAG, "BluetoothAdapter not initialized");  
  11.             return;  
  12.         }  
  13. //        mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);  
  14.         boolean isEnableNotification =  mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);  
  15.         if(isEnableNotification) {  
  16.             List<BluetoothGattDescriptor> descriptorList = characteristic.getDescriptors();  
  17.             if(descriptorList != null && descriptorList.size() > 0) {  
  18.                 for(BluetoothGattDescriptor descriptor : descriptorList) {  
  19.                     descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);  
  20.                     mBluetoothGatt.writeDescriptor(descriptor);  
  21.                 }  
  22.             }  
  23.         }  
  24.     }  

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

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

返回的是false 还是true,

另外还会再写一篇关于蓝牙数据读写的文章

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值