当前位置:   article > 正文

在uniAPP中编写和低功耗蓝牙BLE通讯的程序-微信小程序-安卓app实测_uni.onblecharacteristicvaluechange

uni.onblecharacteristicvaluechange

在使用uni-app过程中,网上很少有介绍BLE的read服务

开发蓝牙很多小伙伴刚开始一头雾水,不知道从何下手,网上可以查的资料少之又少,所以写这篇文章来总结一下BLE低功耗蓝牙开发流程,话不多说,仔细看!!

经过测试,完成了uni-app的read服务,留作笔记

(一)初始化蓝牙 uni.openBluetoothAdapter(OBJECT)

  1. uni.openBluetoothAdapter({
  2. success: e => {
  3. console.log('初始化蓝牙成功:' + e.errMsg);
  4. console.log(JSON.stringify(e));
  5. this.isStop = false;
  6. this.$set(this.disabled, 0, true);
  7. this.$set(this.disabled, 1, false);
  8. this.$set(this.disabled, 10, false);
  9. this.getBluetoothAdapterState();
  10. },}

(二)开始搜索蓝牙设备 uni.startBluetoothDevicesDiscovery(OBJECT)

  1. uni.startBluetoothDevicesDiscovery({
  2. success: e => {
  3. console.log('开始搜索蓝牙设备:' + e.errMsg);
  4. this.searchLoad = true;
  5. this.$set(this.disabled, 1, true);
  6. this.$set(this.disabled, 2, false);
  7. this.$set(this.disabled, 3, false);
  8. this.onBluetoothDeviceFound();
  9. },

(三)发现外围设备 uni.onBluetoothDeviceFound(CALLBACK)

  1. uni.onBluetoothDeviceFound(devices => {
  2. console.log('开始监听寻找到新设备的事件');
  3. // this.$set(this.disabled, 3, false);
  4. this.getBluetoothDevices();

(四)停止搜寻附近的蓝牙外围设备 

uni.stopBluetoothDevicesDiscovery(OBJECT)

  1. uni.stopBluetoothDevicesDiscovery({
  2. success: e => {
  3. console.log('停止搜索蓝牙设备:' + e.errMsg);
  4. if (types) {
  5. this.$set(this.disabled, 1, true);
  6. } else {
  7. this.$set(this.disabled, 1, false);
  8. }
  9. this.$set(this.disabled, 2, true);
  10. // this.$set(this.disabled, 3, true);
  11. this.searchLoad = false;
  12. },

(五)连接蓝牙设备uni.createBLEConnection(OBJECT)

连接低功耗蓝牙设备。

若APP在之前已有搜索过某个蓝牙设备,并成功建立连接,可直接传入之前搜索获取的 deviceId 直接尝试连接该设备,无需进行搜索操作。

注意

  • 请保证尽量成对的调用 createBLEConnection 和 closeBLEConnection 接口。安卓如果多次调用 createBLEConnection 创建连接,有可能导致系统持有同一设备多个连接的实例,导致调用 closeBLEConnection 的时候并不能真正的断开与设备的连接。

  • 蓝牙连接随时可能断开,建议监听 uni.onBLEConnectionStateChange 回调事件,当蓝牙设备断开时按需执行重连操作

  • 若对未连接的设备或已断开连接的设备调用数据读写操作的接口,会返回 10006 错误,建议进行重连操作。

示例代码

  1. uni.createBLEConnection({
  2. // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
  3. deviceId,
  4. success(res) {
  5. console.log(res)
  6. }
  7. })

(六)获取服务uni.getBLEDeviceServices(OBJECT)

获取蓝牙设备所有服务(service)。

示例代码

  1. uni.getBLEDeviceServices({
  2. // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
  3. deviceId,
  4. success(res) {
  5. console.log('device services:', res.services)
  6. }
  7. })

(七)获取特征值

uni.getBLEDeviceCharacteristics(OBJECT)

获取蓝牙设备某个服务中所有特征值(characteristic)。

示例代码

  1. uni.getBLEDeviceCharacteristics({
  2. // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
  3. deviceId,
  4. // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
  5. serviceId,
  6. success(res) {
  7. console.log('device getBLEDeviceCharacteristics:', res.characteristics)
  8. }
  9. })

(八)notify功能uni.notifyBLECharacteristicValueChange(OBJECT)

启用低功耗蓝牙设备特征值变化时的 notify 功能,订阅特征值。注意:必须设备的特征值支持 notify 或者 indicate 才可以成功调用。另外,必须先启用 notifyBLECharacteristicValueChange 才能监听到设备 characteristicValueChange 事件。

注意

  • 订阅操作成功后需要设备主动更新特征值的 value,才会触发 uni.onBLECharacteristicValueChange 回调。

  • 安卓平台上,在调用 notifyBLECharacteristicValueChange 成功后立即调用 writeBLECharacteristicValue 接口,在部分机型上会发生 10008 系统错误

示例代码

  1. uni.notifyBLECharacteristicValueChange({
  2. state: true, // 启用 notify 功能
  3. // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
  4. deviceId,
  5. // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
  6. serviceId,
  7. // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
  8. characteristicId,
  9. success(res) {
  10. console.log('notifyBLECharacteristicValueChange success', res.errMsg)
  11. }
  12. })

(九)监听特征值变化uni.onBLECharacteristicValueChange(CALLBACK)

监听低功耗蓝牙设备的特征值变化事件。必须先启用 notifyBLECharacteristicValueChange 接口才能接收到设备推送的 notification。

示例代码

  1. // ArrayBuffer转16进度字符串示例
  2. function ab2hex(buffer) {
  3. const hexArr = Array.prototype.map.call(
  4. new Uint8Array(buffer),
  5. function (bit) {
  6. return ('00' + bit.toString(16)).slice(-2)
  7. }
  8. )
  9. return hexArr.join('')
  10. }
  11. uni.onBLECharacteristicValueChange(function (res) {
  12. console.log(`characteristic ${res.characteristicId} has changed, now is ${res.value}`)
  13. console.log(ab2hex(res.value))
  14. })

(十)写数据

uni.writeBLECharacteristicValue(OBJECT)

向低功耗蓝牙设备特征值中写入二进制数据。注意:必须设备的特征值支持 write 才可以成功调用。

注意

  • 并行调用多次会存在写失败的可能性。

  • APP不会对写入数据包大小做限制,但系统与蓝牙设备会限制蓝牙4.0单次传输的数据大小,超过最大字节数后会发生写入错误,建议每次写入不超过20字节。

  • 若单次写入数据过长,iOS 上存在系统不会有任何回调的情况(包括错误回调)。

  • 安卓平台上,在调用 notifyBLECharacteristicValueChange 成功后立即调用 writeBLECharacteristicValue 接口,在部分机型上会发生 10008 系统错误

示例代码

  1. // 向蓝牙设备发送一个0x0016进制数据
  2. const buffer = new ArrayBuffer(1)
  3. const dataView = new DataView(buffer)
  4. dataView.setUint8(0, 0)
  5. uni.writeBLECharacteristicValue({
  6. // 这里的 deviceId 需要在 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
  7. deviceId,
  8. // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
  9. serviceId,
  10. // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
  11. characteristicId,
  12. // 这里的value是ArrayBuffer类型
  13. value: buffer,
  14. success(res) {
  15. console.log('writeBLECharacteristicValue success', res.errMsg)
  16. }
  17. })

(十一)读数据

uni.readBLECharacteristicValue(OBJECT)

读取低功耗蓝牙设备的特征值的二进制数据值。注意:必须设备的特征值支持 read 才可以成功调用。

注意

  • 并行调用多次会存在读失败的可能性。

  • 接口读取到的信息需要在 onBLECharacteristicValueChange 方法注册的回调中获取。

示例代码

  1. // 必须在这里的回调才能获取
  2. uni.onBLECharacteristicValueChange(function (characteristic) {
  3. console.log('characteristic value comed:', characteristic)
  4. })
  5. uni.readBLECharacteristicValue({
  6. // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
  7. deviceId,
  8. // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
  9. serviceId,
  10. // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
  11. characteristicId,
  12. success(res) {
  13. console.log('readBLECharacteristicValue:', res.errCode)
  14. }
  15. })


 

(十二)断开设备

uni.closeBLEConnection(OBJECT)

断开与低功耗蓝牙设备的连接。

示例代码

  1. uni.closeBLEConnection({
  2. deviceId,
  3. success(res) {
  4. console.log(res)
  5. }
  6. })

本次就到这里了,如果对你有帮助,请点击喜欢

源码见下一篇博客。

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号