男女午夜刺激视频_久久国产精品高清一区二区三区_国产一区二三区好的_日韩一区二区三区av

歡迎光臨~深圳市山星盛電子科技有限公司-稱重產(chǎn)品官方展示網(wǎng)站
服務(wù)熱線 全國服務(wù)熱線:

0755-2979 1990

公司新聞

微信WEIXIN小程序中連接藍牙電子秤(電子稱品牌:Mount star山星盛),直接通過藍牙獲取當前稱重的重量數(shù)據(jù),然后顯示在界面上

微信小程序低功率電子秤及稱重產(chǎn)品的程序開發(fā),電子磅秤的非標定制使用方法和技術(shù)指導(dǎo),云服務(wù)器代碼說明,APP/APK/小程序簡單功能開發(fā)訂做,直接WIFI及4G/5G移動數(shù)據(jù)直接上傳。

深圳市山星盛電子科技有限公司隨著物聯(lián)網(wǎng)的興起,催生了越來越多的軟件與設(shè)備的對接開發(fā)需求。隨著用戶從APP到微信公眾號再到微信小程序的遷移,小程序憑借開發(fā)簡單,應(yīng)用方便的優(yōu)勢成為了物聯(lián)網(wǎng)開發(fā)軟件的首選。

2018年初至今,越來越多的客戶咨詢我們進行軟件與設(shè)備對接的開發(fā)??蛻魜碓从诟餍懈鳂I(yè),需求也不盡相同。下面我們將為客戶分析部分微信小程序與設(shè)備開發(fā)案例,希望給后續(xù)有開發(fā)需求的客戶帶來一定的幫助。


1、微信小程序與電子秤的對接

如今電子秤的應(yīng)用越來越普及,用戶在稱重時可以快速讀取電子秤的數(shù)據(jù),應(yīng)用領(lǐng)域有礦業(yè),牛奶,機械加工等。但是高頻地讀取和記錄數(shù)據(jù)容易形成造成時間的浪費,同時容易產(chǎn)生錯誤,因此不少用戶尋求更加高效的解決方案。解決方案之一就是微信小程序與電子秤的對接開發(fā),開發(fā)者基于有數(shù)據(jù)接口的電子秤開發(fā)微信小程序的對接應(yīng)用。不但能通過藍牙或網(wǎng)絡(luò)的方式獲取數(shù)據(jù),還能通過微信小程序后臺輕松管理數(shù)據(jù),一鍵發(fā)送數(shù)據(jù),大大提高了電子秤作業(yè)的效率和數(shù)據(jù)正確率。

2、相關(guān)小程序代碼說明

微信小程序連接藍牙電子秤

前情:在微信小程序中連接藍牙電子計重桌秤(電子秤品牌:Mount star山星盛),直接通過藍牙獲取當前稱重的重量數(shù)據(jù),然后顯示在界面上。

###??注意 * 此次,只涉及讀取數(shù)據(jù),沒有寫入數(shù)據(jù),具體 API 查看小程序官方文檔 * 確保手機藍牙已經(jīng)打開,并且可以搜索到該電子秤的藍牙設(shè)備,android 可以搜到,ios 搜不到 --- 但是沒有關(guān)系,小程序里 getBluetoothDevices 可以成功就可以了 * 微信小程序中搜索到的藍牙設(shè)備很多,deviceId 在 android 上顯示為藍牙設(shè)備主服務(wù)的 mac 地址,在 ios 上顯示為藍牙設(shè)備主服務(wù)的 uuid * 最終得到的結(jié)果是 ArrayBuffer 型數(shù)據(jù),需要先轉(zhuǎn)為16進制字符串,再轉(zhuǎn)為10進制數(shù)據(jù) --- 小程序官方文檔上這樣提示,實際并不可行

(1)初始化

初始化藍牙模塊 --- wx.openBluetoothAdapter

// 定義數(shù)據(jù)data: {    devices: [],    // 搜索到的藍牙設(shè)備 deviceId 數(shù)組
    deviceId: '',    // 目標藍牙設(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初始化藍牙模塊失?。?#39;, err)            if (err.errCode === 10001) {  // 當前藍牙適配器不可用
                wx.onBluetoothAdapterStateChange( res => {                    if (res.available) {                        this.startBluetoothDevicesDiscovery()
                    }
              })
           }            /* 
            wx.onBluetoothAdapterStateChange({
                success: res => {
                  console.log('onBlueToothAdapterStateChange success 監(jiān)聽藍牙適配器變化: ', res);
                  this.startBluetoothDevicesDiscovery();
                },
                fail: err => {
                     console.log('onBlueToothAdapterStateChange fail: ', err)
                 }
            })
            */
        }
    })
}


###(2)搜索藍牙設(shè)備 ####搜尋附近的藍牙外圍設(shè)備 --- wx.startBluetoothDevicesDiscovery * 入?yún)?services 作用要搜索的藍牙設(shè)備主 service 的 uuid 列表,某些藍牙設(shè)備會廣播自己的主 service 的 uuid,如果設(shè)置此參數(shù),則只搜索廣播包括對應(yīng) uuid 的主服務(wù)的藍牙設(shè)備,可以通過該參數(shù)過濾掉周邊不需要處理的其他藍牙設(shè)備 * 入?yún)?allowDuplicatesKey 作用是否允許重復(fù)上報同一設(shè)備,如果允許重復(fù)上報,則 wx.onBlueToothDeviceFound 方法會多次上報同一設(shè)備,但是 RSSI 值會有不同,默認為 false eg: services: ['FEE7'] 主服務(wù)的 UUID 是 FEE7,傳入這個參數(shù),只搜索主服務(wù) UUID 為 FEE7 的設(shè)備,該設(shè)備是微信硬件平臺的藍牙智能燈

?? 此操作比較耗費系統(tǒng)資源,需要在搜索并連接到設(shè)備后調(diào)用 wx.stopBluetoothDevicesDiscovery 方法停止搜索

startBluetoothDevicesDiscovery() {
    wx.startBluetoothDevicesDiscovery({        success: res => {            console.log('startBluetoothDevicesDiscovery開始搜索外圍設(shè)備成功:', res)            this.getBluetoothDevices()  // (3) 獲取藍牙列表
         },        fail: err => {            console.log('startBluetoothDevicesDiscovery搜索外圍設(shè)備失?。?#39;, err)
        }
    })
}


###(3)獲取藍牙設(shè)備 ####獲取在藍牙模塊生效期間所有已發(fā)現(xiàn)的藍牙設(shè)備,包括已經(jīng)連接成功的藍牙設(shè)備 --- wx.getBluetoothDevices ``` getBluetoothDevices() { wx.getBluetoothDevices({ success: res => { console.log('getBluetoothDevices獲取藍牙設(shè)備成功:', res) this.setData({ devices: res. devices || [] // uuid 對應(yīng)的的已連接設(shè)備列表 }) this.createBLEConnection(); // (4) 與目標設(shè)備建立連接 }, fail: err => { console.log('getBluetoothDevices獲取藍牙設(shè)備失?。?#39;, err) } }) } ``` ####這里還可以用 wx.onBluetoothDeviceFound(),但是相較于 wx.getBluetoothDevices(),這個只會監(jiān)聽尋找新設(shè)備,因而在一次編譯中,不方便同一個藍牙設(shè)備的復(fù)用 ``` // 監(jiān)聽尋找新設(shè)備 onBluetoothDeviceFound() { let that = this; wx.onBluetoothDeviceFound(res => { console.log('onBluetoothDeviceFound success 監(jiān)聽尋找新設(shè)備: ', res); (res.devices || []).forEach(item => { if(item.name == 'KunHong') { that.setData({ deviceId: item.deviceId || '' }) that.createBLEConnection(that.data.deviceId); } }) }) }, ```
###(4)建立連接 ####與目標藍牙設(shè)備建立連接,需要是低功耗藍牙設(shè)備 --- wx.createBLEConnection ?? 如果微信小程序此前搜索過某個藍牙設(shè)備,并成功建立連接,可直接傳入之前搜索獲取的 deviceId 直接嘗試連接該設(shè)備,不用重新搜索 ``` 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與目標藍牙連接成功:', res) this.getBLEDeviceServices() // (5)獲取服務(wù) }, fail: err => { console.log('createBLEConnection與目標藍牙連接失?。?#39;, err) } }) } ```
###(5)獲取藍牙設(shè)備服務(wù) ####獲取藍牙設(shè)備所有主服務(wù)的 uuid --- wx.getBLEDeviceServices * 入?yún)?deviceId 為 wx.getBluetoothDevices 中獲取的目標藍牙設(shè)備的 deviceId

??開發(fā)過程中,主服務(wù) serviceId 和 主服務(wù)的特征值 characteristics 都是選取的實際操作過程中,得到的類似于該目標藍牙設(shè)備的 id,但是小程序官方文檔的 demo,遍歷了所有的列表(serviceId 和 characteristics),需要區(qū)分一下

getBLEDeviceServices() {
    wx.getBLEDeviceServices({        deviceId: this.data.deviceId,        success: res => {            console.log('getBLEDeviceServices獲取藍牙設(shè)備服務(wù)', res)            // getBluetoothDevices 獲取的有 deviceId 和 advertisServiceUUIDs,可以在這里獲取的服務(wù)列表中選擇一個一樣的作為后續(xù) API 請求的服務(wù)id,這個 id 需要滿足是否可讀
            this.setData({                 services: res.services,                 serviceId: res.services[0].uuid    // 假設(shè)是第一個
            })            this.getBLEDeviceCharacteristics()    // (6) 獲取特征值

            // 官方 demo
            for(var i = 0; i < res.services.length; i++) {                // 該服務(wù)是否為主服務(wù)
                if(res.services[i].isPrimary) {                    this.getBLEDeviceCharacteristics(res.services[i].uuid)
                }
            }
        },        fail: err => {            console.log('getBLEDeviceServices獲取藍牙設(shè)備服務(wù)失敗:', err)
        }
    })
}


###(6)獲取特征值 ####獲取藍牙設(shè)備某個服務(wù)中所有特征值 --- wx.getBLEDeviceCharacteristics * 入?yún)?deviceId 為 wx.getBluetoothDevices 中獲取的目標藍牙設(shè)備的 deviceId * 入?yún)?serviceId 為藍牙服務(wù) uuid ,通過 wx.getBLEDeviceServices 獲取 ``` 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 }) (res.characteristics || []).forEach(item => { if(item.properties.read) { wx.readBLECharacteristicValue({ deviceId: this.data.deviceId, serviceId: serviceid, characteristicId: res.characteristicId[i].uuid }) } if(item.properties.notify || item.properties.indicate) { // 開啟通知 wx.notifyBLECharacteristicValueChange({ state: true, deviceId, serviceId, characteristicId: item.uuid, success(res) { console.log('notifyBLECharacteristicValueChange success state: ', res.errMsg) that.setData({ notifyFlag: true }) } }) } }) }, fail: err => { console.log('getBLEDeviceCharacteristics獲取藍牙服務(wù)特征值失?。?#39;, err) } }) this.onBLECharacteristicValueChange() // (8)監(jiān)聽特征值變化 } ```
###(7)啟用 notify 功能 ####啟用低功耗藍牙特征值變化時的 notify 功能,訂閱特征值 ??必須設(shè)備的特征值支持 notify 或者 indicate 才可以成功啟用 ``` 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 ??必須先啟用 notifyBLECharacteristicValueChange 接口才能接收到設(shè)備推送的 notification(通知) ``` // 先監(jiān)聽一下,保證第一時間獲取數(shù)據(jù) onBLECharacteristicValueChange() { wx.onBLECharacteristicValueChange( characteristic => { console.log('onBLECharacteristicValueChange從目標藍牙設(shè)備監(jiān)聽到的數(shù)據(jù)值:', characteristic) this.setData({ value: this.ab2hex(characteristic.value) // (10) 轉(zhuǎn)為 16 進制 })

    //     獲取最終結(jié)果 監(jiān)聽值是否發(fā)生變化,變化時獲取最新值 避免一直監(jiān)聽,數(shù)據(jù)改變量較大
    let result = (this.ab2Str(characteristic.value) || '').split(' ').reverse()[1];    if(this.data.weight == result) {       return;
    } else {        this.setData({
          weight: result,
        })
      }
})

}

<br>###(9)讀取數(shù)據(jù)####讀取低功耗藍牙設(shè)備的特征值的二進制數(shù)據(jù)值 --- wx.readBLECharacteristicValue??必須目標藍牙設(shè)備的特征值支持 read 才可以成功調(diào)用,并且單獨使用 readBLECharacteristicValue 并不能獲取到真正的特征值,只能返回獲取特征值的狀態(tài),即是否成功獲取到值,真正的值需要使用 wx.onBLECharacteristicValueChange() 執(zhí)行回調(diào)才可以在 wx.onBLECharacteristicValueChange() 這個 API 中獲得讀取到的特征值

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讀取特征值失?。?#39;, err)
}
})
}

<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;
}


微信小程序藍牙模塊

藍牙部分知識

  • 關(guān)于Service:

每個設(shè)備包含有多個Service,每個Service對應(yīng)一個uuid

  • 關(guān)于Characteristic

每個Service包含多個Characteristic,每個Characteristic對應(yīng)一個uuid

  • 如何得到數(shù)據(jù)

我們想要的數(shù)據(jù)是包含在每一個Characteristic

 

微信小程序目前提供的藍牙API:

1.操作藍牙適配器的4個API  

復(fù)制代碼

wx.openBluetoothAdapter //初始化藍牙適配器wx.closeBluetoothAdapter //關(guān)閉藍牙模塊wx.getBluetoothAdapterState //獲取本機藍牙適配器狀態(tài)wx.onBluetoothAdapterStateChange //監(jiān)聽藍牙適配器狀態(tài)變化事件

復(fù)制代碼

 

2.連接前使用的共有4個,分別是

wx.startBluetoothDevicesDiscovery //開始搜尋附近的藍牙外圍設(shè)備wx.stopBluetoothDevicesDiscovery //停止搜尋附近的藍牙外圍設(shè)備wx.getBluetoothDevices //獲取所有已發(fā)現(xiàn)的藍牙設(shè)備wx.onBluetoothDeviceFound //監(jiān)聽尋找到新設(shè)備的事件

3.連接和斷開時使用的共有2個,分別是

wx.createBLEConnection //連接低功耗藍牙設(shè)備wx.closeBLEConnection //斷開與低功耗藍牙設(shè)備的連接

4.連接成功后使用的共有8個,分別是

復(fù)制代碼

wx.getConnectedBluetoothDevices //根據(jù) uuid 獲取處于已連接狀態(tài)的設(shè)備wx.getBLEDeviceServices //獲取藍牙設(shè)備所有 service(服務(wù))wx.getBLEDeviceCharacteristics //獲取藍牙設(shè)備所有 characteristic(特征值)wx.readBLECharacteristicValue //讀取低功耗藍牙設(shè)備的特征值的二進制數(shù)據(jù)值wx.writeBLECharacteristicValue //向低功耗藍牙設(shè)備特征值中寫入二進制數(shù)據(jù)wx.notifyBLECharacteristicValueChange //啟用低功耗藍牙設(shè)備特征值變化時的 notify 功能wx.onBLECharacteristicValueChange //監(jiān)聽低功耗藍牙設(shè)備的特征值變化wx.onBLEConnectionStateChange //監(jiān)聽低功耗藍牙連接的錯誤事件


詳細參數(shù)請見小程序開發(fā)文檔

開發(fā)指南

小程序提供了一個簡單、高效的應(yīng)用開發(fā)框架和豐富的組件及API,幫助開發(fā)者在微信中開發(fā)具有原生 APP 體驗的服務(wù)。

本章分主題的介紹了小程序的開發(fā)語言、框架、能力、調(diào)試等內(nèi)容,幫助開發(fā)者快速全面的了解小程序開發(fā)的方方面面。

想要更具體了解關(guān)于框架、組件、API的詳細內(nèi)容,請參考對應(yīng)的參考文檔:


小程序電子秤,微信電子秤,微信小程序稱重,WEIXIN電子秤,MOUNT STAR電子秤,小程序藍牙電子秤,數(shù)據(jù)通訊電子秤,電子磅秤低功率藍牙,Bluetooth scale

用手機掃描二維碼關(guān)閉
二維碼