微信小程序使用藍(lán)牙小插件
本文實(shí)例為大家分享了微信小程序使用藍(lán)牙小插件,供大家參考,具體內(nèi)容如下
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) {//打開(kāi)適配器 成功 調(diào)用callback(); wx.openBluetoothAdapter({ success(res) { if (typeof callback != "undefined") { callback(); } }, fail(res) { console.log(res) } }) }, scan: function (callback) {//掃描設(shè)備 成功 調(diào)用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) {//連接設(shè)備 成功 調(diào)用callback(); wx.createBLEConnection({ // 這里的 deviceId 需要已經(jīng)通過(guò) createBLEConnection 與對(duì)應(yīng)設(shè)備建立鏈接 deviceId: deviceId, success(res) { console.log(res); wx.getBLEDeviceServices({ // 這里的 deviceId 需要已經(jīng)通過(guò) createBLEConnection 與對(duì)應(yīng)設(shè)備建立鏈接 deviceId: deviceId, success(res) { console.log(res); wx.getBLEDeviceCharacteristics({ // 這里的 deviceId 需要已經(jīng)通過(guò) createBLEConnection 與對(duì)應(yīng)設(shè)備建立鏈接 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) {//寫入數(shù)據(jù) 成功 調(diào)用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 需要已經(jīng)通過(guò) createBLEConnection 與對(duì)應(yīng)設(shè)備建立鏈接 deviceId: deviceId, success(res) { console.log(res); wx.getBLEDeviceCharacteristics({ // 這里的 deviceId 需要已經(jīng)通過(guò) createBLEConnection 與對(duì)應(yīng)設(shè)備建立鏈接 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) {//讀取數(shù)據(jù) 成功 調(diào)用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 需要已經(jīng)通過(guò) createBLEConnection 與對(duì)應(yīng)設(shè)備建立鏈接 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) {//關(guān)閉藍(lán)牙 成功 調(diào)用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();
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript 隨機(jī)驗(yàn)證碼的生成實(shí)例代碼
這篇文章主要介紹了JavaScript 隨機(jī)驗(yàn)證碼的生成實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-09-09JS獲取浮動(dòng)(float)元素的style.left值為空的快速解決辦法
這篇文章主要介紹了JS獲取浮動(dòng)(float)元素的style.left值為空的快速解決辦法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02關(guān)閉瀏覽器時(shí)提示onbeforeunload事件
這篇文章主要介紹了關(guān)閉瀏覽器時(shí)提示onbeforeunload事件,有需要的朋友可以參考一下2013-12-12JavaScript 異步調(diào)用框架 (Part 1 - 問(wèn)題 & 場(chǎng)景)
在Ajax應(yīng)用中,調(diào)用XMLHttpRequest是很常見(jiàn)的情況。特別是以客戶端為中心的Ajax應(yīng)用,各種需要從服務(wù)器端獲取數(shù)據(jù)的操作都通過(guò)XHR異步調(diào)用完成。2009-08-08JavaScript本地?cái)?shù)據(jù)存儲(chǔ)sessionStorage與localStorage使用詳解
這篇文章主要介紹了JavaScript本地?cái)?shù)據(jù)存儲(chǔ)sessionStorage與localStorage使用,localStorage:永久存儲(chǔ)在本地,適合保存在本地的數(shù)據(jù)。sessionStorage:會(huì)話級(jí)的存儲(chǔ),敏感帳號(hào)一次登陸2022-10-10js求數(shù)組中全部數(shù)字可拼接出的最大整數(shù)示例代碼
這篇文章主要給大家介紹了利用js如何求數(shù)組中全部數(shù)字可拼接出的最大整數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考借鑒,下面隨著小編一起來(lái)學(xué)習(xí)學(xué)習(xí)吧。2017-08-08小程序?qū)崿F(xiàn)展開(kāi)/收起的效果示例
這篇文章主要介紹了小程序?qū)崿F(xiàn)展開(kāi)/收起的效果示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09javascript實(shí)現(xiàn)非常簡(jiǎn)單的小數(shù)取整功能示例
這篇文章主要介紹了javascript實(shí)現(xiàn)非常簡(jiǎn)單的小數(shù)取整功能,結(jié)合具體實(shí)例形式分析了javascript數(shù)學(xué)運(yùn)算取整相關(guān)操作技巧,需要的朋友可以參考下2017-06-06js用Date對(duì)象的setDate()函數(shù)對(duì)日期進(jìn)行加減操作
在某個(gè)日期上加減天數(shù)來(lái)說(shuō),其實(shí)只要調(diào)用Date對(duì)象的setDate()函數(shù)就可以了,具體方法如下2014-09-09