android 蓝牙开锁功能,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))
}
})
},
文章有缺陷,因为蓝牙的指令文档被我搞不见了,只能通过理解写给大家看,希望能帮到大家,谢谢
android 蓝牙开锁功能,uni-app蓝牙开锁篇相关推荐
- uni.app蓝牙扫描不到设备
只能发现ble蓝牙,也就是蓝牙4.0以上的,我们用的蓝牙模块hc05,sim800c的蓝牙,都是3.0的,所以搜不到... uni蓝牙开发参考资料 https://www.codeleading.co ...
- 【Android】Java多功能通讯录APP项目设计
❥ 每每学习一门课程,都少不了一门课设布置下来 ❥<移动互联开发>课开了8天,要设计一款App,果然大学即自学,而且一学期好几个 (555 ❥ 下面把最近开发的Android Studio ...
- 关于uni.app蓝牙开发问题汇总
1.onBLECharacteristicValueChange 数据返回两次相同数据 2.7以前的版本有此BUG ,更新至2.9.8即可
- 蓝牙技术|蓝牙 Auracast 广播功能发布,蓝牙音频体验发生改变
蓝牙技术联盟SIG宣布了 Auracast 功能,这是一种即将推出的蓝牙广播功能,号称将推动新的无线音频体验,将为用户带来和朋友.家人共享音频的能力.该功能以前被称为 Audio Sharing,但从 ...
- 生活小程序摩拜单车为啥投开锁功能?生活小程序是趋势?
这段时间,我和家里人一直都在使用摩拜单车这个目前在生活小程序中最火的小程序,几乎现在武汉大街小巷都有摩拜单车的身影!我妈也是下班天天骑单车,我也开心不少.而且我妈还告诉我让我去下载个APP叫亿里财,这 ...
- Android 10.0 app添加校验锁(输入密码才能进入app)
1.概述 最近有客户有需求要求给app添加锁,就是点击app 图标时,会弹出Dialog,需要输入密码才能进入app中,就是应用校验锁, 最开始想到在Launcher3中实现,但是如果更换了默认Lau ...
- 配置MyBatis Plus 的乐观锁功能
配置MyBatis Plus 的乐观锁功能 一. 回顾CAS乐观锁 CAS: 二. 配置MyBatis Plus 的乐观锁 (1)数据库:添加version版本号 (2)实体类:添加version版本 ...
- iphone开蓝牙wifi上网慢_为啥我开锁总是比别人慢?
说起手机的蓝牙功能,大家都不陌生,在还没有wifi,网速超级慢,流量也贵得害怕的年代. 蓝牙传送是两台手机之间传输文件最快也最方便的方式,堪称当年手机界的"黑科技"功能.多年过去后 ...
- android音频杂音问题_三星手机与iOS13 双蓝牙音频输出功能体验报告 「Soomal」
在WWDC2019上,iOS13功能介绍中似乎并没有提到蓝牙双音频的输出功能,也包括在年中发布各个版本的iOS13 Beta里都没有相关功能.直到iOS13.1,蓝牙双音频功能突然出现,iOS13称之 ...
最新文章
- Web 开发最有用的 jQuery 插件集锦
- python转换维度
- 连接虚拟机mysql无法访问_连接虚拟机mysql无法访问,报错编号1130的解决方法
- 基于verilog流水灯设计
- c++中vector使用说明
- Mask RCNN笔记
- 服务器启动时Webapp的web.xml中配置的加载顺序
- 计算机的数据库应用领域,【信息管理论文】信息管理中计算机数据库技术的应用(共4410字)...
- [原]MS SQL表字段自增相关的脚本
- php中 被遗忘的函数
- 想起JDK1.4的死机事故
- 2022年基站行业研究报告
- 如何把图纸转换为t3格式_天正cad转t3格式
- 下行文格式图片_下行文标准模版
- 数据分析统计工具有哪些?
- 怎么样使prestashop 运行速度更快
- 微信小程序实现在线画板
- 上海市城镇生育保险办法
- 全球与中国压电比例阀市场深度研究分析报告
- 使用Android studio 开发xposed插件