详解蓝牙BluetoothGattCallback用法之BLE数据接收
来源:互联网 发布:centos6 离线安装mysql 编辑:程序博客网 时间:2024/05/22 10:40
详解蓝牙BluetoothGattCallback用法之BLE数据接收
这篇继续讲解数据接收,想要接收到数据必须先要使能通知
@Override public void onServicesDiscovered(BluetoothGatt gatt, int status) {//发现服务,在蓝牙连接的时候会调用 List<BluetoothGattService> list = mBluetoothGatt.getServices(); for (BluetoothGattService bluetoothGattService:list){ String str = bluetoothGattService.getUuid().toString(); Log.e("onServicesDisc中中中", " :" + str); List<BluetoothGattCharacteristic> gattCharacteristics = bluetoothGattService .getCharacteristics(); for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) { Log.e("onServicesDisc中中中", " :" + gattCharacteristic.getUuid()); if("0000ffe1-0000-1000-8000-00805f9b34fb".equals(gattCharacteristic.getUuid().toString())){ linkLossService=bluetoothGattService; alertLevel=gattCharacteristic; Log.e("daole",alertLevel.getUuid().toString()); } } } enableNotification(true,gatt,alertLevel);//必须要有,否则接收不到数据 //Log.e("onServicesDisc中中中", " :" + mBluetoothGatt.getServices().toString()); }
就是 {enableNotification(true,gatt,alertLevel);//必须要有,否则接收不到数据} 这句话,这个必须要有不然接收数据的那个回调函数会毫无反应,不要问为什么,程序主要还是测试,都是泪,为了做一个工具用了好久。
private void enableNotification(boolean enable, BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { if (gatt == null || characteristic == null) return; //这一步必须要有 否则收不到通知 gatt.setCharacteristicNotification(characteristic, enable);}
这些都做完了以后,在发送了指令后,如果有数据返回,就会接收到数据
@Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {// Characteristic 改变,数据接收会调用 Log.e("CharacteristicChanged中", "数据接收了哦"+bytesToHexString(characteristic.getValue())); tx_receive.append(bytesToHexString(characteristic.getValue()) + "\n"); }
tx_receive.append(bytesToHexString(characteristic.getValue()) + “\n”);这个是将接收的数据在textview中显示出来。append是追加显示,有的手机可能不能在第一时间显示出数据,那么你就滑动一下listview啥的,数据就会显示了。
数据写入状态
@Override public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {//发送数据时调用 Log.e("onCharacteristicWrite中", "数据发送了哦"); Log.e("onCharacteristicWrite中", bytesToHexString(characteristic.getValue())); if(status == BluetoothGatt.GATT_SUCCESS){//写入成功 Log.e("onCharacteristicWrite中", "写入成功"); tx_display.append("写入成功"); }else if (status == BluetoothGatt.GATT_FAILURE){ Log.e("onCharacteristicWrite中", "写入失败"); }else if (status == BluetoothGatt.GATT_WRITE_NOT_PERMITTED){ Log.e("onCharacteristicWrite中", "没权限"); } }
这个是显示写入状态的回调函数,写入成功后就会显示的,这个
boolean status = mBluetoothGatt.writeCharacteristic(alertLevel);
也可以显示写入成功没有。
关于数据接收与发送的我已经写完了,我所有的地方都没有用到广播和服务,使用我的代码的时候一定要注意。
如果有什么更好的想法,本人能力也有限,如有错误,也欢迎指正。
源码(http://download.csdn.net/download/uu13567/9983668
阅读全文
0 0
- 详解蓝牙BluetoothGattCallback用法之BLE数据接收
- 详解蓝牙BluetoothGattCallback用法之BLE数据发送
- iOS蓝牙4.0(BLE)-后台接收数据
- iOS蓝牙4.0(BLE)-后台接收数据
- android 蓝牙Ble详解
- 蓝牙BLE 广播数据
- Android蓝牙BLE之RSSI数据的读写(1)
- Android蓝牙BLE之RSSI数据的读写(1)
- iOS 蓝牙4.0(BLE)后台或者锁屏也可以接收数据的方法
- Android安卓蓝牙BLE 扫描连接接收数据 封装好的类
- Android 蓝牙BLE开发详解
- Android蓝牙BLE开发详解
- Android ble onCharacteristicChanged()无法触发BluetoothGattCallback
- 【Android(BLE)】Android(BLE)之启动蓝牙
- 蓝牙之二十二-BLE
- 蓝牙后台接收数据
- [蓝牙]iOS学习之蓝牙4.0 BLE
- 蓝牙小试牛刀之蓝牙4.0BLE开发
- python pip
- 简易菜单界面
- ASIC/SoC验证
- Hibernate之树状结构设计
- python为什么函数里的代码会比全局书写运行更快
- 详解蓝牙BluetoothGattCallback用法之BLE数据接收
- Set,List,Map,Vector,ArrayList的区别
- 项目启动会应该注意的几点
- 9.17总结_NOIP模拟赛
- The Suspects 并查集
- iOS中HTTPS证书验证浅析
- Node-red学习第7篇--用mqtt节点发送传感器数据到邮箱
- Tensorflow的梯度异步更新
- Hadoop和大数据:60款顶级开源工具