微信小程序实现蓝牙BLE(看文章最后一句话)

这是楼主在学校自己开发的用蓝牙小程序控制机械臂的(独立开发的)。
https://pan.baidu.com/s/1AmCW_ARhu--eapzd8AfpHw
提取码:gypw

步骤:
1、wx.openBluetoothAdapter//蓝牙初始化
2、 wx.onBluetoothDeviceFound //监听寻找到新设备的事件
3、 wx.startBluetoothDevicesDiscovery //开始搜寻附近的蓝牙外围设备
4、 wx.getBluetoothDevices//获取在蓝牙模块生效期间所有已发现的蓝牙设备。包括已经和本机处于连接状态的设备。
5、wx.stopBluetoothDevicesDiscovery//停止搜寻附近的蓝牙外围设备。搜索到需要设备时或者连接时候停止搜索
6、wx.createBLEConnection //连接设备
7、 wx.getBLEDeviceServices //获取所有服务列表
8、wx.getBLEDeviceCharacteristics//获取蓝牙设备某个服务中的所有特征值
9、wx.onBLEConnectionStateChange //监听设备连接状态(本人用小米2手环测试经常断)
10、 wx.notifyBLECharacteristicValueChange // 启用低功耗蓝牙设备特征值变化是的notify功能
11、 wx.onBLECharacteristicValueChange//开始监听特征值的变化

12、 wx.writeBLECharacteristicValue//写入特征值
13、wx.readBLECharacteristicValue//读取特征值

以下是demo代码:

onLoad: function (options) {var that=this//蓝牙初始化wx.openBluetoothAdapter({success: function(res) {console.log(res)//监听寻找到新设备的事件wx.onBluetoothDeviceFound(function (res) {// console.log(res)})},fail:function(res){wx.showModal({title: '提示',content: '请检查手机蓝牙是否打开',})}})},

点击搜索设备按钮(lanya):

lanya: function () {wx.showLoading({title: '搜索中',})setTimeout(function(){wx.hideLoading()},1500)//开始搜寻附近的蓝牙外围设备wx.startBluetoothDevicesDiscovery({success: function (res) {console.log('搜索完成')//获取在蓝牙模块生效期间所有已发现的蓝牙设备。包括已经和本机处于连接状态的设备。wx.getBluetoothDevices({success: function (res) {if(res.devices.length==0){wx.showModal({title: '提示',content: '没有搜索到设备,请重试',})wx.navigateTo({url: '../index/index',})}console.log(res)//console.log(res.devices[0].deviceId)// console.log(res.devices[0].name)console.log(res.devices)var data=res.deviceswx.navigateTo({url: '../lanya/lanya?data=' + JSON.stringify(data),})//停止搜寻附近的蓝牙外围设备。若已经找到需要的蓝牙设备并不需要继续搜索时,建议调用该接口停止蓝牙搜索。// wx.stopBluetoothDevicesDiscovery({//   success: function(res) {//    console.log('已经停止搜索')//   },// })},fail:function(res){console.log('没有找到设备')}})},fail: function (fa) {console.log(fa)}})},
onLoad: function (options) {var Id=[]var Name=[]var data = JSON.parse(options.data)console.log(data)var that=thisconsole.log(data)that.setData({array:data})},

点击连接设备按钮:

 sub:function(e){var deviceId = e.currentTarget.idapp.globalData.deviceId=deviceIdconsole.log(e.currentTarget.id)//连接设备wx.createBLEConnection({deviceId:deviceId,success: function(res) {console.log(res)//获取服务列表wx.navigateTo({url: '../res/res?id='+deviceId,})console.log('id:' + deviceId)},fail:function(res){wx.showModal({title: '提示',content: '连接超时,请重试',})// console.log('id:' + deviceId)console.log(res)}})},
  onLoad: function (options) {var deviceId=options.idvar that=this//获取所有服务wx.getBLEDeviceServices({deviceId: deviceId,success: function (res) {// console.log(res)console.log(res.services)that.setData({array:res.services})},})},

进入某个服务

    sub:function(e){var that = thisvar serviceId=e.target.id//获取蓝牙设备某个服务中的所有特征值wx.getBLEDeviceCharacteristics({deviceId:app.globalData.deviceId,serviceId:serviceId,success: function(res) {console.log(res)that.data.characteristics=res.characteristics//获取characteristicwx.navigateTo({url: '../notify/notify?ct=' + JSON.stringify(that.data.characteristics) + '&serviceId=' + serviceId,})},fail:function(res){console.log(res)}})//监听设备连接状态wx.onBLEConnectionStateChange(function (res) {console.log(res)if (res.connected == false) {wx.showModal({title: '提示',content: '设备连接已断开',})}})},
  onLoad: function (options) {var ct = JSON.parse(options.ct)serviceId=options.serviceIdvar that=thisthat.setData({array:ct,serviceId:serviceId,})},

进入某个特征值

 sub:function(e){var that = thisvar cht = e.target.idvar deviceId = app.globalData.deviceId// 启用低功耗蓝牙设备特征值变化是的notify功能wx.notifyBLECharacteristicValueChange({deviceId: deviceId,serviceId: serviceId,characteristicId: cht,state: true,success: function (res) {//开始监听wx.onBLECharacteristicValueChange(function (characteristics) {console.log(characteristics.value)var a = characteristics.valuevar int8array = new Int8Array(a);console.log("监听到特征值更新:" + int8array[0])// var hex = ab2str(a);// console.log("返回的值:  ".hex)})console.log(res)wx.navigateTo({url: '../write/write?cht=' + cht + '&serviceId=' + serviceId + '&deviceId=' + deviceId,})},fail:function(res){console.log(res)},})},
**```
  onLoad: function (options) {var that=thisvar cht = options.chtvar  deviceId=options.deviceIdvar serviceId = options.serviceIdthat.setData({deviceId:deviceId,cht:cht,serviceId:serviceId})},

向该特征值写入数据:**

 formSubmit:function(e){var that = thisvar value=e.detail.value.pswconsole.log(value)//写入数据wx.writeBLECharacteristicValue({deviceId: that.data.deviceId,serviceId: that.data.serviceId,characteristicId: that.data.cht,value: that.str2ab(value),success: function (res) {console.log(res)},fail:function(res){console.log(res)}});wx.readBLECharacteristicValue({deviceId: that.data.deviceId,serviceId: that.data.serviceId,characteristicId: that.data.cht,success: function(e) {console.log(e)},})},

重点写入的字符串要转换成Arraybuffer

  // 字符串转为ArrayBuffer对象,参数为字符串str2ab: function(str) {var buf = new ArrayBuffer(str.length * 2); // 每个字符占用2个字节var bufView = new Uint16Array(buf);for(var i = 0, strLen=str.length; i<strLen; i++) {bufView[i] = str.charCodeAt(i);
}
return buf;
},

**监听的特征值变化回调是ArrayBuffer也要转换为字符串,可是我直接写下标就能获取,写转换获得就是undefined

function ab2str(arrayBuffer) {return String.fromCharCode.apply(null, new Uint8Array(arrayBuffer));
//   let unit8Arr = new Uint8Array(arrayBuffer);
//   let encodedString = String.fromCharCode.apply(null, unit8Arr),
//     decodedString = decodeURIComponent(escape((encodedString)));//没有这一步中文会乱码
//  return decodedString;
}

点赞收藏才发邮件

微信小程序实现蓝牙BLE(demo版)相关推荐

  1. 微信小程序低功耗蓝牙BLE快速开发js

    文章目录 1.前言 2.资料 3.BLE连接流程 BLE连接原理 4.index.js页面加载流程详细说明 完整代码: 1.前言 目的: 1.为了能三分钟快速开发BLE模块,特此做一个笔记,按照笔记的 ...

  2. 微信小程序低功耗蓝牙(BLE)开发总结

    1.准备 低功耗蓝牙模块:(链接)蓝牙模块购买通道 相关参数数据(UUID表): 需要知道服务ID(serviceID) ,响应特征值 ID(notify characteristic UUID),写 ...

  3. 微信小程序入门教程+案例demo

    微信小程序入门教程+案例demo 尊重原创,转载请注明出处:原文查看惊喜更多 http://blog.csdn.net/qq137722697 首先摆在好姿态,--微信小程序开发也就那么回事.你只需要 ...

  4. 在HbuilderX中实现微信小程序下蓝牙连接打印机完整实战案例

    1.基础开发环境,所用到的 Api 以及实现的思路. 应用场景: 商家打印小票,小票包含顾客消费的商品明细信息以及末尾附上二维码,二维码供顾客扫码开票. HbuilderX开发工具: HBuilder ...

  5. 微信小程序:实现计算器-Demo+与发布(编程运算计算器)

    微信小程序:实现计算器-Demo+与发布(编程运算计算器(可以微信小程序直接搜索)) 基本运算符与逻辑运算符,任意进制转换,上不封顶 微信小程序源代码下载:资源下载 使用了JS进制转换接口:JS实现万 ...

  6. 一键搭建微信小程序开发环境 及demo运行(腾讯云上一键搭建node.js服务器环境,PHP,Java,.NET服务类似)

    一.首先准备下本地环境(本地就需要一个微信开发工具) 1.首先得有一个微信小程序账号,登陆微信小程序首页:mp.weixin.qq.com,点击右上角立即注册. 注册登陆后,首页填写一些小程序基本信息 ...

  7. 仿京细菜谱微信小程序源码云开版

    仿京细菜谱微信小程序源码云开版,不需要域名和服务器即可搭建小程序,直接导入开发者工具即可上传审核. 源码下载:仿京细菜谱微信小程序源码云开版-小程序文档类资源-CSDN下载

  8. 微信小程序的考勤管理Demo,包括前后端及数据库等内容

    这是一个微信小程序的考勤管理Demo,包括前后端及数据库等内容.如有错误或建议,欢迎指出. 前端:微信小程序框架 后端:koa框架基于express的新一代框架 文件:url80.ctfile.com ...

  9. 微信小程序客服系统手机版五大功能介绍

    很多朋友小程序上线后,客服消息这块一直没得到解决.小程序客服消息只能在PC端回复,是让众多小程序运营商及商家头疼的问题,因为一个再牛逼的客服,也不可能随时随地都抱着电脑,这就导致很多用户的留言不能及时 ...

最新文章

  1. Android代码混淆工具汇总
  2. 安川机器人焊枪切换设定方法_安川机器人参数更改方法
  3. union和union all有什么区别_Pytorch中Linear与Conv1d(kernel=1)的区别
  4. vue获取前一个页面路由地址
  5. 东方终焉组审核页可做引导页
  6. jmeter 配置 slave 代理压测机
  7. 有哪些在朋友圈发会被秒赞的文案?
  8. C#窗体控件-列表框控件ListBox
  9. Windows 7/Windows Server 2008 R2中创建扩展分区
  10. 阶段5 3.微服务项目【学成在线】_day04 页面静态化_18-页面静态化-模板管理-GridFS研究-取文件...
  11. 2021年华中杯数学建模挑战赛A题马赛克瓷砖选色问题求解全过程文档及程序
  12. pycharm 输入法光标跟随
  13. android短信验证码登录,Android注册登录实时自动获取短信验证码
  14. [codeforces 1293A] ConneR and the A.R.C. Markland-N 不超时的二分/无限长数组map+桶排序
  15. PCB邮票孔的作用及详细设计指南
  16. 学习k-近邻算法简单易懂
  17. 中山大学HCP Lab系列论文:AI解题新突破,神经网络推开数学推理大门
  18. iPhone4/4s 5.1.1版本越狱后无法连接iTunes,出现0xE8000012错误的解决方法
  19. 2022 云栖大会 | 开源人说预约:听百味技术人生,品激荡开源江湖
  20. vmware 连接云服务器协议,vmware怎么连接云服务器

热门文章

  1. [Intervention] Ignored attempt to cancel a touchmove event with cancelable=false
  2. 从0-1做产品快速启动,大型干货案例分享
  3. 计算机教学考核方案,教师计算机使用管理制度和考核方案学校教学管理制度
  4. 原生js实现类似于html模板引擎,JS实现简单的template
  5. XGBoost算法梳理
  6. python+openCV一键换底色,不同底色自动判断
  7. docker apache php-fpm AH01071: Got error 'Primary script unknown\n'
  8. java序列化方案对比
  9. 电脑中病毒,这样处置
  10. 前端优化之if...else判断