Cr明仔
Cr明仔
  • 发布:2019-09-12 11:47
  • 更新:2024-04-18 13:26
  • 阅读:13784

uni-app蓝牙开锁篇

分类: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))  
                                    }  
                                })  
                    },

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

10 关注 分享
[已删除] 一路格桑花 牛儿哒哒 一枚小前端 许香香 1***@qq.com [已删除] 1***@qq.com 1***@qq.com w***@163.com

要回复文章请先登录注册

w***@163.com

w***@163.com

好文章,正在学这个编程。最终目的就是想弄个能连接BLE蓝牙,WIFI的APP。关注一下以后来取经。
2024-04-18 13:26
[已删除]

[已删除]

复制的同学把复制的代码里面的this都改一下,,因为有些this不是vue对象
2022-05-06 10:23
[已删除]

[已删除]

回复 9***@qq.com :
把复制的代码里面的this都改一下,,因为有些this不是vue对象
2022-05-06 10:22
4***@qq.com

4***@qq.com

回复 1***@163.com :
按这个可以 https://juejin.cn/post/7093171532318375950
2022-05-06 09:02
4***@qq.com

4***@qq.com

回复 1***@163.com :
可以,notify为false只是不能接收消息而已
2022-05-06 09:00
1***@163.com

1***@163.com

获取特征值返回notify:false,indicate:false,发送写入指令可以吗?
2022-02-10 13:08
1***@163.com

1***@163.com

为什么我也是这么写的但是不可以?
2022-02-10 13:06
9***@qq.com

9***@qq.com

为什么我一直卡在监听返回设备那一步 它一直不能与我自己要求的设备匹配
2020-12-11 17:41
5***@qq.com

5***@qq.com

大佬这种是需要连接蓝牙,有没有做过不连接,通过向外广播数据的方式开锁呢
2020-09-07 16:50
许香香

许香香

字节如何转化成字符串,求解大佬
2020-05-22 13:36