Android BLE低功耗蓝牙开发极简系列(二)之读写操作
来源:互联网 发布:windows是那个公司 编辑:程序博客网 时间:2024/06/05 02:03
简书地址:Android BLE低功耗蓝牙开发极简系列(二)之读写操作
这是Ble极简系列的第二篇文章,上一篇Android BLE低功耗蓝牙开发极简系列(一)之扫描与连接主要是扫描连接,这一篇主要是读写操作。
发现服务
在连接成功后,可以通过Gatt进行discoverServices()。
if (newState == BluetoothProfile.STATE_CONNECTED) {//当蓝牙设备已经连接 //获取ble设备上面的服务 Toast.makeText(MainActivity.this, "连接成功", Toast.LENGTH_SHORT).show(); Log.i("haha", "Attempting to start service discovery:" + mBluetoothGatt.discoverServices()); Log.d("haha", "onConnectionStateChange: " + "连接成功") }
在mGattCallback 回调添加Servicest的相关回调
//发现服务回调。 public void onServicesDiscovered(BluetoothGatt gatt, int status) { Log.d("haha", "onServicesDiscovered: " + "发现服务 : " + status); if (status == BluetoothGatt.GATT_SUCCESS) { //成功 } }
读写开关
当返回的status == BluetoothGatt.GATT_SUCCESS时,进行读写以及通知相关的操作, 调用writeDescriptor(),注意设置setValue为ENABLE_INDICATION_VALUE,否则可能后续读取不到数据。
if (status == BluetoothGatt.GATT_SUCCESS) { //成功 isServiceConnected = true; boolean serviceFound; Log.d("haha", "onServicesDiscovered: " + "发现服务 : " + status) if (mBluetoothGatt != null && isServiceConnected) { BluetoothGattService gattService = mBluetoothGatt.getService(UUID_SERVICE); BluetoothGattCharacteristic characteristic = gattService.getCharacteristic(UUID_NOTIFICATION); boolean b = mBluetoothGatt.setCharacteristicNotification(characteristic, true); if (b) { List<BluetoothGattDescriptor> descriptors = characteristic.getDescriptors(); for (BluetoothGattDescriptor descriptor : descriptors) { boolean b1 = descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); if (b1) { mBluetoothGatt.writeDescriptor(descriptor); Log.d(TAG, "startRead: " + "监听收数据"); } } } }
设置成功,会在onDescriptorWrite方法进行回调,注意UUID_SERVICE,UUID_NOTIFICATION特征值UUID,可以询问公司固件端的开发人员,和开发人员配合修改。
@Override public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { super.onDescriptorWrite(gatt, descriptor, status); Log.d(TAG, "onDescriptorWrite: " + "设置成功"); }
发送数据
public void startSend(View view) { if (mBluetoothGatt != null && isServiceConnected) { BluetoothGattService gattService = mBluetoothGatt.getService(UUID_SERVICE); BluetoothGattCharacteristic characteristic = gattService.getCharacteristic(UUID_WRITE); byte[] bytes = new byte[2]; bytes[0] = 04; bytes[1] = 01; characteristic.setValue(bytes); characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE); mBluetoothGatt.writeCharacteristic(characteristic); }}
读取数据
读取数据在onCharacteristicChanged方法中,注意进制间的转换。
@Override public final void onCharacteristicChanged(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) { byte[] value = characteristic.getValue(); Log.d(TAG, "onCharacteristicChanged: " + value); String s0 = Integer.toHexString(value[0] & 0xFF); String s = Integer.toHexString(value[1] & 0xFF); Log.d(TAG, "onCharacteristicChanged: " + s0 + "、" + s); for (byte b : value) { Log.d(TAG, "onCharacteristicChanged: " + b); } }
断开操作
if (mBluetoothGatt != null) { mBluetoothGatt.close(); }
注意事项
一定要进行读写开关操作,注意descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE),否则可能读取不到数据。
Github
BleDemo
阅读全文
0 0
- Android BLE低功耗蓝牙开发极简系列(二)之读写操作
- Android BLE低功耗蓝牙开发极简系列(一)之扫描与连接
- 【android 蓝牙开发——BLE(低功耗)蓝牙】
- Android ble低功耗蓝牙开发
- Android BLE低功耗蓝牙开发
- Android-BLE低功耗蓝牙开发
- 低功耗蓝牙(BLE)之开发步骤
- Android 低功耗蓝牙(BLE)开发(1)-- 基本概念
- Android 低功耗蓝牙(BLE)开发(2)-- BluetoothAdapter详解
- Android 低功耗蓝牙(BLE)开发(3)-- BluetoothDevice详解
- Android-低功耗蓝牙BLE(Bluetooth Low Energy)开发
- Android蓝牙开发(二) BLE4.0低功耗蓝牙
- 低功耗蓝牙(BLE)
- 低功耗蓝牙(BLE)
- 低功耗蓝牙(BLE)
- 低功耗蓝牙(BLE)
- 【Android应用开发】Android 蓝牙低功耗 (BLE) ( 第一篇 . 概述 . 蓝牙低功耗文档 翻译)
- android 蓝牙4.0 ble 低功耗蓝牙
- 全文检索引擎Solr系列—–全文检索基本原理
- rtmp complex handshake,变更的握手,支持h264/aac
- 常见排序算法
- Android BLE低功耗蓝牙开发极简系列(一)之扫描与连接
- c++ sort函数()用法
- Android BLE低功耗蓝牙开发极简系列(二)之读写操作
- springmvc拦截器与web组件filter使用的粗浅认识
- linux修改系统的yum源为阿里云并添加epel源
- Mybatis-Plus代码生成器使用
- 磁盘及网络IO工作方式解析
- Centos6.5下装ZooKeeper
- R语言之多重共线性的判别以及解决方法
- Linux返回上一次访问的目录
- JSON.parse, JSON.stringify and eval