赞
踩
在使用uni-app过程中,网上很少有介绍BLE的read服务
开发蓝牙很多小伙伴刚开始一头雾水,不知道从何下手,网上可以查的资料少之又少,所以写这篇文章来总结一下BLE低功耗蓝牙开发流程,话不多说,仔细看!!
经过测试,完成了uni-app的read服务,留作笔记
(一)初始化蓝牙 uni.openBluetoothAdapter(OBJECT)
- uni.openBluetoothAdapter({
- success: e => {
- console.log('初始化蓝牙成功:' + e.errMsg);
- console.log(JSON.stringify(e));
- this.isStop = false;
- this.$set(this.disabled, 0, true);
- this.$set(this.disabled, 1, false);
- this.$set(this.disabled, 10, false);
- this.getBluetoothAdapterState();
- },}
(二)开始搜索蓝牙设备 uni.startBluetoothDevicesDiscovery(OBJECT)
- uni.startBluetoothDevicesDiscovery({
- success: e => {
- console.log('开始搜索蓝牙设备:' + e.errMsg);
- this.searchLoad = true;
- this.$set(this.disabled, 1, true);
- this.$set(this.disabled, 2, false);
- this.$set(this.disabled, 3, false);
- this.onBluetoothDeviceFound();
- },
(三)发现外围设备 uni.onBluetoothDeviceFound(CALLBACK)
- uni.onBluetoothDeviceFound(devices => {
- console.log('开始监听寻找到新设备的事件');
- // this.$set(this.disabled, 3, false);
- this.getBluetoothDevices();
(四)停止搜寻附近的蓝牙外围设备
uni.stopBluetoothDevicesDiscovery(OBJECT)
- uni.stopBluetoothDevicesDiscovery({
- success: e => {
- console.log('停止搜索蓝牙设备:' + e.errMsg);
- if (types) {
- this.$set(this.disabled, 1, true);
- } else {
- this.$set(this.disabled, 1, false);
- }
- this.$set(this.disabled, 2, true);
- // this.$set(this.disabled, 3, true);
- this.searchLoad = false;
- },
(五)连接蓝牙设备uni.createBLEConnection(OBJECT)
连接低功耗蓝牙设备。
若APP在之前已有搜索过某个蓝牙设备,并成功建立连接,可直接传入之前搜索获取的 deviceId 直接尝试连接该设备,无需进行搜索操作。
注意
请保证尽量成对的调用 createBLEConnection
和 closeBLEConnection
接口。安卓如果多次调用 createBLEConnection
创建连接,有可能导致系统持有同一设备多个连接的实例,导致调用 closeBLEConnection
的时候并不能真正的断开与设备的连接。
蓝牙连接随时可能断开,建议监听 uni.onBLEConnectionStateChange
回调事件,当蓝牙设备断开时按需执行重连操作
若对未连接的设备或已断开连接的设备调用数据读写操作的接口,会返回 10006 错误,建议进行重连操作。
示例代码
- uni.createBLEConnection({
- // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
- deviceId,
- success(res) {
- console.log(res)
- }
- })
(六)获取服务uni.getBLEDeviceServices(OBJECT)
获取蓝牙设备所有服务(service)。
示例代码
- uni.getBLEDeviceServices({
- // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
- deviceId,
- success(res) {
- console.log('device services:', res.services)
- }
- })
(七)获取特征值
获取蓝牙设备某个服务中所有特征值(characteristic)。
示例代码
- uni.getBLEDeviceCharacteristics({
- // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
- deviceId,
- // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
- serviceId,
- success(res) {
- console.log('device getBLEDeviceCharacteristics:', res.characteristics)
- }
- })
(八)notify功能uni.notifyBLECharacteristicValueChange(OBJECT)
启用低功耗蓝牙设备特征值变化时的 notify 功能,订阅特征值。注意:必须设备的特征值支持 notify 或者 indicate 才可以成功调用。另外,必须先启用 notifyBLECharacteristicValueChange
才能监听到设备 characteristicValueChange
事件。
注意
订阅操作成功后需要设备主动更新特征值的 value,才会触发 uni.onBLECharacteristicValueChange
回调。
安卓平台上,在调用 notifyBLECharacteristicValueChange
成功后立即调用 writeBLECharacteristicValue
接口,在部分机型上会发生 10008 系统错误
示例代码
- uni.notifyBLECharacteristicValueChange({
- state: true, // 启用 notify 功能
- // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
- deviceId,
- // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
- serviceId,
- // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
- characteristicId,
- success(res) {
- console.log('notifyBLECharacteristicValueChange success', res.errMsg)
- }
- })
(九)监听特征值变化uni.onBLECharacteristicValueChange(CALLBACK)
监听低功耗蓝牙设备的特征值变化事件。必须先启用 notifyBLECharacteristicValueChange
接口才能接收到设备推送的 notification。
示例代码
- // ArrayBuffer转16进度字符串示例
- function ab2hex(buffer) {
- const hexArr = Array.prototype.map.call(
- new Uint8Array(buffer),
- function (bit) {
- return ('00' + bit.toString(16)).slice(-2)
- }
- )
- return hexArr.join('')
- }
- uni.onBLECharacteristicValueChange(function (res) {
- console.log(`characteristic ${res.characteristicId} has changed, now is ${res.value}`)
- console.log(ab2hex(res.value))
- })
(十)写数据
uni.writeBLECharacteristicValue(OBJECT)
向低功耗蓝牙设备特征值中写入二进制数据。注意:必须设备的特征值支持 write 才可以成功调用。
注意
并行调用多次会存在写失败的可能性。
APP不会对写入数据包大小做限制,但系统与蓝牙设备会限制蓝牙4.0单次传输的数据大小,超过最大字节数后会发生写入错误,建议每次写入不超过20字节。
若单次写入数据过长,iOS 上存在系统不会有任何回调的情况(包括错误回调)。
安卓平台上,在调用 notifyBLECharacteristicValueChange
成功后立即调用 writeBLECharacteristicValue
接口,在部分机型上会发生 10008 系统错误
示例代码
- // 向蓝牙设备发送一个0x00的16进制数据
- const buffer = new ArrayBuffer(1)
- const dataView = new DataView(buffer)
- dataView.setUint8(0, 0)
- uni.writeBLECharacteristicValue({
- // 这里的 deviceId 需要在 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
- deviceId,
- // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
- serviceId,
- // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
- characteristicId,
- // 这里的value是ArrayBuffer类型
- value: buffer,
- success(res) {
- console.log('writeBLECharacteristicValue success', res.errMsg)
- }
- })
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
(十一)读数据
uni.readBLECharacteristicValue(OBJECT)
读取低功耗蓝牙设备的特征值的二进制数据值。注意:必须设备的特征值支持 read 才可以成功调用。
注意
并行调用多次会存在读失败的可能性。
接口读取到的信息需要在 onBLECharacteristicValueChange
方法注册的回调中获取。
示例代码
- // 必须在这里的回调才能获取
- uni.onBLECharacteristicValueChange(function (characteristic) {
- console.log('characteristic value comed:', characteristic)
- })
- uni.readBLECharacteristicValue({
- // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
- deviceId,
- // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
- serviceId,
- // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
- characteristicId,
- success(res) {
- console.log('readBLECharacteristicValue:', res.errCode)
- }
- })
断开与低功耗蓝牙设备的连接。
示例代码
- uni.closeBLEConnection({
- deviceId,
- success(res) {
- console.log(res)
- }
- })
本次就到这里了,如果对你有帮助,请点击喜欢
源码见下一篇博客。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。