uni-app蓝牙开锁篇

uni-app的api和微信的api其实很相似,用法一样,在这里奉上我之前在项目中实现蓝牙开锁的代码,我会说明每一步的步骤,哪个步骤用哪个api,每个api的详细用法可以去uni-app官网参考文档

  • 蓝牙整个步骤:1初始化蓝牙,2开始搜寻附近的蓝牙外围设备,3监听寻找到新设备的事件,4搜寻到需要的蓝牙,停止搜寻附近的蓝牙外围设备,5连接低功耗蓝牙设备,6获取蓝牙设备所有服务(service),7获取蓝牙设备某个服务中所有特征值(characteristic),8启用低功耗蓝牙设备特征值变化时的 notify 功能,订阅特征值。注意:必须设备的特征值支持 notify 或者 indicate 才可以成功调用,9向低功耗蓝牙设备特征值中写入二进制数据。
          uni.openBluetoothAdapter({//首先初始化蓝牙
                    success(res) {
                      console.log(JSON.stringify(res))
                      uni.startBluetoothDevicesDiscovery({//这里是开启蓝牙搜寻
                        success: (res) => {
                          console.log('startBluetoothDevicesDiscovery success', res)
                          uni.onBluetoothDeviceFound((res) => {//这一步是监听返回的蓝牙设备
                                  console.log(JSON.stringify(res))
                            res.devices.forEach(device => {//这一步就是去筛选找到的蓝牙中,有没有你匹配的名称
                              console.log(JSON.stringify(device))
                              if (device.name == 'XiaoanTech') {
                                        this.DeviceID = device.deviceId
                                        let DeviceID = device.deviceId//这里是拿到的uuid
                                uni.stopBluetoothDevicesDiscovery({//当找到匹配的蓝牙后就关掉蓝牙搜寻,因为蓝牙搜寻很耗性能
                                  success(res) {
                                    console.log(JSON.stringify(res))
                                  }
                                })
                                console.log(DeviceID)
                                uni.createBLEConnection({//连接低功耗蓝牙设备
                                  deviceId:DeviceID,//传入刚刚获取的uuid
                                  success(res) {
                                    console.log(JSON.stringify(res))
                                    //uni.getConnectedBluetoothDevices({
                                    //success(res) {
                                    //console.log(JSON.stringify(res))
                                    //}
                                    //})

                                    setTimeout(function(){//这里为什么要用setTimeout呢,等等下面会解释
                                            uni.getBLEDeviceServices({//获取蓝牙设备所有服务
                                                deviceId:DeviceID,
                                                success(res) {//为什么要用延时,因为不用延时就拿不到所有的服务,在上一步,连接低功耗蓝牙
//设备的时候,需要一个600-1000毫秒的时间后,再去获取设备所有服务,不给延时就会一直返回错误码10004                             
                                                    console.log(JSON.stringify(res))
                                                    uni.getBLEDeviceCharacteristics({//获取蓝牙设备某个服务中所有特征值
                                                        deviceId:DeviceID,
                                                        serviceId:this.ServiceUUID,//这个serviceId可以在上一步获取中拿到,也可以在
//蓝牙文档中(硬件的蓝牙文档)拿到,我这里是通过文档直接赋值上去的,一般有两个,一个是收的uuid,一个是发的uuid,我们这边是发
                                                        success(res) {
                                                            console.log(JSON.stringify(res))
                                                uni.notifyBLECharacteristicValueChange({
                                                  state: true, // 启用 notify 功能
                                                  // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
                                                  deviceId:DeviceID,
                                                  // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
                                                  serviceId:this.ServiceUUID,
                                                  // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
                                                  characteristicId:self.characteristicId,
                                                  success(res) {
                                                    console.log('notifyBLECharacteristicValueChange success', res.errMsg)
                                                            uni.showToast({
                                                                title: '开启蓝牙连接',
                                                                duration: 2000
                                                                        });
                                                },
                                                fail(res) {
                                                        console.log(JSON.stringify(res))
                                                            }
                                                })
                                                },
                                                fail(res){
                                                 console.log(JSON.stringify(res)) 
                                                }
                                                            })
                                            },
                                            fail(res){
                                                    console.log(JSON.stringify(res)) 
                                            }
                                })
                                },1000)
                                  },
                                  fail(res) {
                                    console.log(res)
                                  }
                                })                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
                              }
                            })
                          })
                        }
                      })
                    }, fail(res) {
                      console.log(res)
                      if (res.errCode == 10001) {
                        uni.showToast({
                          title: '蓝牙未打开',
                          duration: 2000,
                        })
                      } else {
                        uni.showToast({
                          title: res.errMsg,
                          duration: 2000,
                        })
                      }
                    }
                  })
  • 蓝牙连接成功后就是发送指令了,发送二进制数据
SendChange: function () {//开锁
                      var self = this
                    
                      // 向蓝牙设备发送一个0x00的16进制数据
                                let buffer = new ArrayBuffer(8)
                                let dataView = new DataView(buffer)
                                dataView.setUint8(0, 0x20)//开锁指令
                                dataView.setUint8(1, 0x05)//字节
                                dataView.setUint8(2, 0x0A)//指令
                                dataView.setUint8(3, 0x0A)//指令
                                dataView.setUint8(4, 0x05)//指令
                                dataView.setUint8(5, 0x05)//指令
                                dataView.setUint8(6, 0x00)//指令
                               //算法,逢10进1,A到F(或a~f)表示,其中:A~F表示10~15
                                //20等于32,32+05+10+10+5+5+0 = 67
                                dataView.setUint8(7, 0x43)
                                uni.writeBLECharacteristicValue({
                                    // 这里的 deviceId 需要在 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
                                    deviceId:self.DeviceID,
                                    // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
                                    serviceId:self.ServiceUUID,
                                    // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
                                    characteristicId:self.characteristicId,
                                    // 这里的value是ArrayBuffer类型
                                    value: buffer,
                                    success(res) {
                                        console.log('writeBLECharacteristicValue success', res.errMsg)
                                        uni.showToast({
                                                title: '开锁',
                                                duration: 2000
                                        });
                                    },
                                    fail(res) {
                                        console.log(JSON.stringify(res))
                                        console.log(JSON.stringify(buffer))
                                    }
                                })
                    },
                    CloseChange: function () {//关锁
                      var self = this
                    
                      // 向蓝牙设备发送一个0x00的16进制数据
                                let buffer = new ArrayBuffer(8)
                                let dataView = new DataView(buffer)
                                dataView.setUint8(0, 0x20)
                                dataView.setUint8(1, 0x05)
                                dataView.setUint8(2, 0x0A)
                                dataView.setUint8(3, 0x0A)
                                dataView.setUint8(4, 0x05)
                                dataView.setUint8(5, 0x05)
                                dataView.setUint8(6, 0x01)
                                dataView.setUint8(7, 0x44)
                                uni.writeBLECharacteristicValue({
                                    // 这里的 deviceId 需要在 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
                                    deviceId:self.DeviceID,
                                    // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
                                    serviceId:self.ServiceUUID,
                                    // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
                                    characteristicId:self.characteristicId,
                                    // 这里的value是ArrayBuffer类型
                                    value: buffer,
                                    success(res) {
                                        console.log('writeBLECharacteristicValue success', res.errMsg)
                                        uni.showToast({
                                                title: '关锁',
                                                duration: 2000
                                        });
                                    },
                                    fail(res) {
                                        console.log(JSON.stringify(res))
                                        console.log(JSON.stringify(buffer))
                                    }
                                })
                    },

文章有缺陷,因为蓝牙的指令文档被我搞不见了,只能通过理解写给大家看,希望能帮到大家,谢谢

你可能感兴趣的:(uni-app蓝牙开锁篇)