微信小程序怎么使用蓝牙小插件
发表于:2025-11-08 作者:千家信息网编辑
千家信息网最后更新 2025年11月08日,今天小编给大家分享一下微信小程序怎么使用蓝牙小插件的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来
千家信息网最后更新 2025年11月08日微信小程序怎么使用蓝牙小插件
今天小编给大家分享一下微信小程序怎么使用蓝牙小插件的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。
bluetooth.js
function BLE(options) { this.options = options || { locator: {} };}function ab2hex(buffer) { const hexArr = Array.prototype.map.call( new Uint8Array(buffer), function (bit) { return ('00' + bit.toString(16)).slice(-2) } ) return hexArr.join('-')};BLE.prototype = { open: function (callback) {//打开适配器 成功 调用callback(); wx.openBluetoothAdapter({ success(res) { if (typeof callback != "undefined") { callback(); } }, fail(res) { console.log(res) } }) }, scan: function (callback) {//扫描设备 成功 调用callback(ls); wx.startBluetoothDevicesDiscovery({ success(res) { wx.getBluetoothDevices({ success(res) { console.log(res); var ls = []; for (var i = 0; i < res.devices.length; i++) { console.log(res.devices[i].name); ls[i] = { deviceId: res.devices[i].deviceId, name: res.devices[i].name }; } if (typeof callback!= "undefined"){ callback(ls); } } }) }, fail(res) { console.log(res) return 0; } }) }, link: function (deviceId, callback) {//连接设备 成功 调用callback(); wx.createBLEConnection({ // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接 deviceId: deviceId, success(res) { console.log(res); wx.getBLEDeviceServices({ // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接 deviceId: deviceId, success(res) { console.log(res); wx.getBLEDeviceCharacteristics({ // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接 deviceId: deviceId, // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取 serviceId: ' ', success(res) { console.log(res) if (typeof callback != "undefined") { callback(); } } }) } }) }, fail(res) { console.log(res) } }) }, write: function (deviceId, value, callback) {//写入数据 成功 调用callback(); const buffer = new ArrayBuffer(value.length); const dataView = new DataView(buffer); for (var i = 0; i < value.length; i++) { dataView.setUint8(i, value[i].charCodeAt()); } console.log(buffer); console.log(buffer); wx.getBLEDeviceServices({ // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接 deviceId: deviceId, success(res) { console.log(res); wx.getBLEDeviceCharacteristics({ // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接 deviceId: deviceId, // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取 serviceId: '4FAFC201-1FB5-459E-8FCC-C5C9C331914B', success(res) { wx.writeBLECharacteristicValue({ deviceId: deviceId, serviceId: '4FAFC201-1FB5-459E-8FCC-C5C9C331914B', characteristicId: 'BEB5483E-36E1-4688-B7F5-EA07361B26B8', value: buffer, success: function (res) { console.log(res); if (typeof callback != "undefined") { callback(); } }, fail: function (res) { console.log(res); } }) } }) } }) }, read: function (deviceId, callback) {//读取数据 成功 调用callback(xmlString); wx.onBLECharacteristicValueChange(function (characteristic) { var board = ab2hex(characteristic.value); var bigData = board.split('-'); var result = []; for (var i = 0; i < bigData.length; i++) { result.push(String.fromCharCode('0X' + bigData[i])); } var xmlString = result.join(''); if (typeof callback != "undefined") { callback(xmlString); } }); wx.readBLECharacteristicValue({ // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接 deviceId: deviceId, // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取 serviceId: '4FAFC201-1FB5-459E-8FCC-C5C9C331914B', // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取 characteristicId: 'BEB5483E-36E1-4688-B7F5-EA07361B26B8', success(res) { // console.log('readBLECharacteristicValue:', res.errCode); console.log(res); }, fail(res) { console.log(res); } }) }, close: function (deviceId, callback) {//关闭蓝牙 成功 调用callback(); wx.closeBLEConnection({ deviceId: deviceId, success(res) { console.log(res); if (typeof callback != "undefined") { callback(); } } }) // wx.closeBluetoothAdapter({ // success(res) { // console.log(res); // if (typeof callback != "undefined"){ // callback(); // } // } // }) }}exports.BLE = BLE;index.js
const Ble = require('../../lib/bluetooch/bluetooch');var ble = new Ble.BLE();以上就是"微信小程序怎么使用蓝牙小插件"这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注行业资讯频道。
设备
成功
链接
接口
知识
篇文章
蓝牙
插件
程序
内容
数据
不同
很大
大部分
就是
更多
知识点
行业
资讯
资讯频道
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
霸州ipfs服务器
努比亚升级服务器
西安沸腾网络技术
哪些服务器可以下载网页
dell服务器远程管理账号
戴尔服务器boot一条
通信类软件开发毕业设计题目
车载视频软件开发
建设互联网科技园
数据库中数组用什么类型
计算机网络安全的特征体现
网络安全研究报告
佛山软件开发工时
网络安全威胁主要有哪些
网络技术入学
163邮箱服务器
数据库复制粘贴后能用吗
linux开启服务器命令
鸿蒙os软件开发
怎么为数据库创建角色
暗黑2重制版ns 与游戏服务器
日本软件开发累吗
VC执行数据库语言失败
软件开发学文学理
河北服务器电源充电方法
外包软件开发公司是什么
计算机网络安全的特征体现
服务器系统盘设置多大合适
数据库安全控制设计
数据库中唯一