千家信息网

微信小程序怎么使用蓝牙小插件

发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,今天小编给大家分享一下微信小程序怎么使用蓝牙小插件的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来
千家信息网最后更新 2026年11月01日微信小程序怎么使用蓝牙小插件

今天小编给大家分享一下微信小程序怎么使用蓝牙小插件的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

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安全错误 数据库的锁怎样保障安全 互联网科技 深圳市人人互联网科技有限公司 互联网科技工作委员会 新华互联网科技学校 互联网科技作文 天津摩尔互联网科技 友谊互联网络科技 深圳艾佳互联网科技 居家互联网科技有限公司 互联网科技股午盘股票涨跌 成都互联网科技公司100强 互联网科技人工智能实验室 乌鲁木齐新华智原互联网科技学校 深圳艾果互联网通讯科技 21世纪互联网科技成就 互联网与数字科技关系 半导体行业属于互联网高科技类吗 扫呗互联网科技有限公司 云南互联网科技学费 互联网科技和医药行业 广州紫鲸互联网科技有限公司 互联网与科技金融数据 互联网科技论文 富国互联网科技创新基金 随着互联网科技发展 西安小鹰速贷互联网科技有限公司 互联网科技的全部知识 每天都有哪些好的互联网科技 科技互联网公司两年变化 高科技和互联网的现代国家
0