在WeChat微信小程序中連接藍牙電子計重秤(電子秤品牌:山星盛MOUNT STAR),直接通過藍牙獲取當(dāng)前稱重的重量數(shù)據(jù),然后顯示在weixin界面上
。山星盛電子科技是一家衡器硬件設(shè)備提供商,我們專業(yè)提供ERP管理系統(tǒng)PC,APP,APK,POS,PDA,安卓The android和蘋果apple手機IOS及android操作系統(tǒng),電腦端微軟Microsoft安裝版及文本直接傳送的電子秤山星盛電子科技是一家衡器硬件設(shè)備提供商 騰訊微信小程序電子稱 前情:在微信小程序中連接藍牙電子計重桌秤,(電子秤品牌:山星盛MOUNT STAR) 此次 確保手機藍牙已經(jīng)打開 微信小程序中搜索到的藍牙設(shè)備很多 最終得到的結(jié)果是 ArrayBuffer 型數(shù)據(jù) 入?yún)?services 作用要搜索的藍牙設(shè)備主 service 的 uuid 列表 入?yún)?allowDuplicatesKey 作用是否允許重復(fù)上報同一設(shè)備 ?? 此操作比較耗費系統(tǒng)資源 ?? 如果微信小程序此前搜索過某個藍牙設(shè)備 入?yún)?deviceId 為 wx.getBluetoothDevices 中獲取的目標(biāo)藍牙設(shè)備的 deviceId ??開發(fā)過程中 入?yún)?deviceId 為 wx.getBluetoothDevices 中獲取的目標(biāo)藍牙設(shè)備的 deviceId 入?yún)?serviceId 為藍牙服務(wù) uuid ,通過 wx.getBLEDeviceServices 獲取 ??必須設(shè)備的特征值支持 notify 或者 indicate 才可以成功啟用 ??必須先啟用 notifyBLECharacteristicValueChange 接口才能接收到設(shè)備推送的 notification(通知) ??必須目標(biāo)藍牙設(shè)備的特征值支持 read 才可以成功調(diào)用 ACSII碼16進制轉(zhuǎn)換代碼 如以上轉(zhuǎn)換都不能成功 ab2Weight(abValue) { let characteristicValue = this.ab2hex(abValue); let strValue = this.hexCharCodeToStr(characteristicValue) return strValue // let weightValue = }, ab2hex(buffer) { let hexArr = Array.prototype.map.call( new Uint8Array(buffer), function (bit) { return ('00' + bit.toString(16)).slice(-2) } ) return hexArr.join(''); }, hexCharCodeToStr(hexCharCodeStr) { var trimedStr = hexCharCodeStr.trim(); var rawStr = trimedStr.substr(0, 2).toLowerCase() === "0x" ? trimedStr.substr(2) : trimedStr; var len = rawStr.length; if (len % 2 !== 0) { alert("Illegal Format ASCII Code!"); return ""; } var curCharCode; var resultStr = []; for (var i = 0; i < len; i = i + 2) { curCharCode = parseInt(rawStr.substr(i, 2), 16); // ASCII Code Value resultStr.push(String.fromCharCode(curCharCode)); } return resultStr.join(""); } 來源: 我們專業(yè)提供電子秤硬件稱重設(shè)備:服務(wù)電話:0755-23035550 直線:18923420600/15307550221
ps:記錄的時候??注意
(1)初始化
初始化藍牙模塊 --- wx.openBluetoothAdapter
// 定義數(shù)據(jù)data: {
devices: [], // 搜索到的藍牙設(shè)備 deviceId 數(shù)組
deviceId: '', // 目標(biāo)藍牙設(shè)備 deviceId
services: [] // 設(shè)備服務(wù)列表 serviceId 數(shù)組
serviceId: '',
characteristics: [] // 特征值列表
characteristicId: '' // 選擇某一個特征值
value: '' // 16 進制數(shù)據(jù)值}// 藍牙 API 調(diào)用步驟openBluetoothAdapter() {
wx.openBluetoothAdapter({ // (1)
success: res => { console.log('openBluetoothAdapter初始化藍牙模塊成功:', res) this.startBluetoothDevicesDiscovery() // (2) 開始搜索
},
fail: err => { console.log('openBluetoothAdapter初始化藍牙模塊失敗:', err) if (err.errCode === 10001) { // 當(dāng)前藍牙適配器不可用
wx.onBluetoothAdapterStateChange( res => { if (res.available) { this.startBluetoothDevicesDiscovery()
}
})
}
}
})
}
(2)搜索藍牙設(shè)備
搜尋附近的藍牙外圍設(shè)備 --- wx.startBluetoothDevicesDiscovery
eg: services: ['FEE7'] 主服務(wù)的 UUID 是 FEE7startBluetoothDevicesDiscovery() {
wx.startBluetoothDevicesDiscovery({
success: res => { console.log('startBluetoothDevicesDiscovery開始搜索外圍設(shè)備成功:', res) this.getBluetoothDevices() // (3) 獲取藍牙列表
},
fail: err => { console.log('startBluetoothDevicesDiscovery搜索外圍設(shè)備失敗:', err)
}
})
}
(3)獲取藍牙設(shè)備
獲取在藍牙模塊生效期間所有已發(fā)現(xiàn)的藍牙設(shè)備
getBluetoothDevices() {
wx.getBluetoothDevices({
success: res => { console.log('getBluetoothDevices獲取藍牙設(shè)備成功:', res) this.setData({
devices: res. devices || [] // uuid 對應(yīng)的的已連接設(shè)備列表
}) this.createBLEConnection(); // (4) 與目標(biāo)設(shè)備建立連接
},
fail: err => { console.log('getBluetoothDevices獲取藍牙設(shè)備失?div id="jpandex" class="focus-wrap mb20 cf">。?#39;, err)
}
})
}
(4)建立連接
與目標(biāo)藍牙設(shè)備建立連接,需要是低功耗藍牙設(shè)備 --- wx.createBLEConnection
createBLEConnection() { // 如果是第一次建立連接,可以通過名稱匹配,獲取相應(yīng)設(shè)備的 deviceId
let devices = this.data.devices;
devices.forEach(item => { if(item.name == 'kunHong') { this.setData({
deviceId: item.deviceId
})
}
}) // 建立連接
wx.createBLEConnection({
deviceId: this.data.deviceId,
success: res => { console.log('createBLEConnection與目標(biāo)藍牙連接成功:', res) this.getBLEDeviceServices() // (5)獲取服務(wù)
},
fail: err => { console.log('createBLEConnection與目標(biāo)藍牙連接失敗:', err)
}
})
}
(5)獲取藍牙設(shè)備服務(wù)
獲取藍牙設(shè)備所有主服務(wù)的 uuid --- wx.getBLEDeviceServices
getBLEDeviceServices() {
wx.getBLEDeviceServices({
deviceId: this.data.deviceId,
success: res => { console.log('getBLEDeviceServices獲取藍牙設(shè)備服務(wù)', res) // getBluetoothDevices 獲取的有 deviceId 和 advertisServiceUUIDs,可以在這里獲取的服務(wù)列表中選擇一個一樣的作為后續(xù) API 請求的服務(wù)id
(6)獲取特征值
獲取藍牙設(shè)備某個服務(wù)中所有特征值 --- wx.getBLEDeviceCharacteristics
getBLEDeviceCharacteristics(serviceId) {
wx.getBLEDeviceCharacteristics({
deviceId: this.data.deviceId,
serviceId: this.data.serviceId,
success: res => {
console.log('getBLEDeviceCharacteristics獲取藍牙服務(wù)特征值成功:', res) this.setData({
characteristics: res. characteristics,
characteristics: res. characteristics[0].uuid
}) this.notifyBLECharacteristicValueChange(); // (7)啟用 notify 功能
// 官方 demo
for(var i = 0; i < res.characteristics.length; i++) { // 是否可讀
if(res.characteristics[i].read) { // 讀取數(shù)據(jù)
wx.readBLECharacteristicValue({
deviceId: this.data.deviceId,
serviceId: serviceid,
characteristicId: res.characteristicId[i].uuid
})
}, if(res.characteristics[i].properties.notify || res.characteristics[i].properties.indicate) { // 啟用功能
wx.notifyBLECharacteristicValueChange({
deviceId,
serviceId,
characteristicId: item.uuid,
state: true,
})
}
}
},
fail: err => {
console.log('getBLEDeviceCharacteristics獲取藍牙服務(wù)特征值失?div id="m50uktp" class="box-center"> 。?#39;, err)
}
}) this.onBLECharacteristicValueChange() // (8)監(jiān)聽特征值變化
this.readBLECharacteristicValue(); // (9)讀取數(shù)據(jù)}
(7)啟用 notify 功能
啟用低功耗藍牙特征值變化時的 notify 功能
notifyBLECharacteristicValueChange() {
wx.notifyBLECharacteristicValueChange({
deviceId: this.data.deviceId,
serviceId: this.data.serviceId,
characteristicId: this.data. characteristicId,
state: true // 是否啟用 notify (四個字段全部必填)
})
}
(8)監(jiān)聽特征值變化
監(jiān)聽低功耗藍牙設(shè)備特征值的變化事件 --- wx.onBLECharacteristicValueChange
// 先監(jiān)聽一下,保證第一時間獲取數(shù)據(jù)onBLECharacteristicValueChange() { wx.onBLECharacteristicValueChange( characteristic => { console.log('onBLECharacteristicValueChange從目標(biāo)藍牙設(shè)備監(jiān)聽到的數(shù)據(jù)值:', characteristic) this.setData({ value: this.ab2hex(abcharacteristic.value) // (10) 轉(zhuǎn)為 16 進制
})
})
}
(9)讀取數(shù)據(jù)
讀取低功耗藍牙設(shè)備的特征值的二進制數(shù)據(jù)值 --- wx.readBLECharacteristicValue
readBLECharacteristicValue() {
wx.readBLECharacteristicValue({
deviceId: this.data.deviceId,
serviceId: this.data.serviceId,
characteristicId: this.data.charecteristicId,
success: res => {
console.log('readBLECharacteristicValue讀取特征值成功:', res)
},
fail: err => {
console.log('readBLECharacteristicValue讀取特征值失?div id="m50uktp" class="box-center"> 。?#39;, err)
}
})
}
(10)轉(zhuǎn)為 16 進制
官方文檔中介紹了 ArrayBuffer 轉(zhuǎn)為 16 進制的方法
<br>###(10)轉(zhuǎn)為 16 進制####官方文檔中介紹了 ArrayBuffer 轉(zhuǎn)為 16 進制的方法
// ArrayBuffer轉(zhuǎn)16進制字符串示例
ab2hex(buffer) {
let hexArr = Array.prototype.map.call(
new Uint8Array(buffer),
function(bit) {
return ('00' + bit.toString(16)).slice(-2)
}
)
return hexArr.join('');
}
<br>###(11)值轉(zhuǎn)換####官方文檔介紹的方法似乎有點不適用哎,試下這個
ab2Str(arrayBuffer){
let unit8Arr = new Uint8Array(arrayBuffer);
let encodedString = String.fromCharCode.apply(null, unit8Arr);
return encodedString;
}
http://www.dxalxbgw.cn/new/WeChat-scale.html
在線詢價