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的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)) } }) },文章有缺陷,因为蓝牙的指令文档被我搞不见了,只能通过理解写给大家看,希望能帮到大家,谢谢
关跳转到新页面并关闭当前页(不闪屏)
在你需要的地方执行close方法就可以了 A页面→B页面(close)→C页面 C页面(mui.back())→A页面
我自己加了延时关闭 可以根据自己情况调整
function close() {
if(!this.isBrowser) {
var ws = plus.webview.currentWebview();
plus.webview.hide(ws);
setTimeout(function() {
plus.webview.close(ws);
}, 1000)
} else {
history.back();
}
} 在你需要的地方执行close方法就可以了 A页面→B页面(close)→C页面 C页面(mui.back())→A页面
我自己加了延时关闭 可以根据自己情况调整
function close() {
if(!this.isBrowser) {
var ws = plus.webview.currentWebview();
plus.webview.hide(ws);
setTimeout(function() {
plus.webview.close(ws);
}, 1000)
} else {
history.back();
}
} 收起阅读 »
仿qq阅读页模板修改版~点击任意地方展开收起上下悬浮菜单
演示图片
需要演示地址和模板下载的请访问
https://jgo.wodemo.net/entry/523537
免费获取和下载啦。
本人自己修改的 QQ阅读页模板啦。
点击任意地方展开~顶部+底部悬浮
再次点击任意地方收起~顶部+底部悬浮
如果任何不懂,欢迎联系qq 1274003856
本模板可以适用于~影视软件开发模板
等各种软件模板开发的。
uni-app h5页面在chrome浏览器上跨域问题,不需要安装插件,只需要在终端输入一行代码解决
终端里输入命令:
open -a "/Applications/Google Chrome.app" --args --disable-web-security --user-data-dir=/Users/你电脑的名称/chromeDevUserData/
此方法可以解决chrome跨域,但有个问题每次关闭chrome后又会出现跨域,需要终端里重新输这条命令。
终端里输入命令:
open -a "/Applications/Google Chrome.app" --args --disable-web-security --user-data-dir=/Users/你电脑的名称/chromeDevUserData/
此方法可以解决chrome跨域,但有个问题每次关闭chrome后又会出现跨域,需要终端里重新输这条命令。
uni-app ios 苹果真机运行
博客地址:https://www.cnblogs.com/yunsun/p/11506444.html
博客地址:https://www.cnblogs.com/yunsun/p/11506444.html
ipa上传反馈修改info.plist用途字符串问题方案
很多开发者上传ipa后,收到苹果的反馈邮件说请修改应用程序info.plist文件中相关用途字符串!
意思就是需要对请求的权限进行详细说明,比如使用到了定位,相册,通讯录等权限,要把为什么使用这些权限做下详细描述!
反馈翻译截图例子!
权限描述举例说明
比如一个外卖应用,获取定位后需要展示附近的美食信息。那么,相应的定位权限描述,应当是类似“获取定位信息用于为用户提供附近的美食信息”这样的描述。
而不应当是,“获取用户当前位置信息”这种没有明确描述定位用处的信息。
HBuilderX开发工具修改入口!
打开manifest.json文件
很多开发者上传ipa后,收到苹果的反馈邮件说请修改应用程序info.plist文件中相关用途字符串!
意思就是需要对请求的权限进行详细说明,比如使用到了定位,相册,通讯录等权限,要把为什么使用这些权限做下详细描述!
反馈翻译截图例子!
权限描述举例说明
比如一个外卖应用,获取定位后需要展示附近的美食信息。那么,相应的定位权限描述,应当是类似“获取定位信息用于为用户提供附近的美食信息”这样的描述。
而不应当是,“获取用户当前位置信息”这种没有明确描述定位用处的信息。
HBuilderX开发工具修改入口!
打开manifest.json文件
监听所有应用推送信息
监听所有应用推送信息:https://ext.dcloud.net.cn/plugin?id=756
监听所有应用推送信息:https://ext.dcloud.net.cn/plugin?id=756
uni-app在h5端使用tabBar的页面下用uni.redirectTo跳转会导致tabBar导航栏不隐藏
在h5端,如果是用到tabBar做导航栏的话,用uni.redirectTo跳转的时候,tabBar导航栏是不会隐藏的,android和ios端倒没有这个情况,不知道是属于bug呢还是属于其他的
在h5端,如果是用到tabBar做导航栏的话,用uni.redirectTo跳转的时候,tabBar导航栏是不会隐藏的,android和ios端倒没有这个情况,不知道是属于bug呢还是属于其他的
DCloud助力,Vue官网有免费中文视频教程了!
Vue官网,是广大前端开发者学习Vue.js最重要的阵地。官网上有详细的文档、及部分视频教程。但之前这些视频都是英文的,对国内开发者的入门学习不太友好。
为了让更多开发者低门槛进入vue生态,DCloud与Vue官方合作,全新录制Vue中文视频教程,现已正式上线并更新到Vue官网,供大家免费观看学习。
更细致的是,在很多API文档旁,都提供了这个API对应视频讲解的链接。
该系列视频免费、且没有广告。

除了观看视频讲解外,学习者还可点击右上角按钮,
使用HBuilderX打开课程源码,亲自动手编码,更深入的掌握Vue.js的具体用法。在HBuilderX中可以方便的对Vue的语法进行
代码提示、转到定义,能在代码助手右侧看到API的详细文档链接。
通过HBuilderX右上角的预览,开发者可以实时了解自己代码的运行情况,还可以查看log和debug打断点。
Vue.js是我们中国人创造的世界级前端框架,在国内,它已经远远超过react和angular。下图为今年的百度指数对比:
欢迎广大开发者积极拥抱
Vue,共同促进生态的繁荣发展!
Vue官网,是广大前端开发者学习Vue.js最重要的阵地。官网上有详细的文档、及部分视频教程。但之前这些视频都是英文的,对国内开发者的入门学习不太友好。
为了让更多开发者低门槛进入vue生态,DCloud与Vue官方合作,全新录制Vue中文视频教程,现已正式上线并更新到Vue官网,供大家免费观看学习。
更细致的是,在很多API文档旁,都提供了这个API对应视频讲解的链接。
该系列视频免费、且没有广告。

除了观看视频讲解外,学习者还可点击右上角按钮,
使用HBuilderX打开课程源码,亲自动手编码,更深入的掌握Vue.js的具体用法。在HBuilderX中可以方便的对Vue的语法进行
代码提示、转到定义,能在代码助手右侧看到API的详细文档链接。
通过HBuilderX右上角的预览,开发者可以实时了解自己代码的运行情况,还可以查看log和debug打断点。
Vue.js是我们中国人创造的世界级前端框架,在国内,它已经远远超过react和angular。下图为今年的百度指数对比:
欢迎广大开发者积极拥抱
Vue,共同促进生态的繁荣发展! 收起阅读 »











