日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

uniapp 蓝牙通讯(搜索/连接蓝牙、读、写)

發布時間:2023/12/16 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 uniapp 蓝牙通讯(搜索/连接蓝牙、读、写) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

小程序點擊藍牙設備進行通訊

1.初始化藍牙設備 | | 提醒用戶打開藍牙設備

player() {var that = this;uni.openBluetoothAdapter({ //調用微信小程序api 打開藍牙適配器接口success: function(res) {// console.log(res)uni.showToast({title: '初始化成功',icon: 'success',duration: 800})that.findBlue(); //2.0},fail: function(res) { //如果手機上的藍牙沒有打開,可以提醒用戶uni.showToast({title: '請打開藍牙',type: 'error',icon: 'none'});}})},

2.搜索周邊設備(此操作比較耗費系統資源,請在搜索并連接到設備后調用 uni.stopBluetoothDevicesDiscovery 方法停止搜索。)

findBlue() {var that = thisuni.startBluetoothDevicesDiscovery({allowDuplicatesKey: false,interval: 0,success: function(res) {uni.showLoading({title: '正在搜索設備',})that.getBlue() //3.0}})},

3.獲取搜索到的設備信息

getBlue() {var that = this//uni.getBluetoothDevices獲取在藍牙模塊生效期間所有已發現的藍牙設備。包括已經和本機處于連接狀態的設備uni.getBluetoothDevices({success: function(res) {uni.hideLoading();// console.log(res)that.BluetoothList = res.devices//將BluetoothList遍歷給用戶,當用戶點擊連接某個藍牙時調用4.0},fail: function() {console.log("搜索藍牙設備失敗")}})},

4.當用戶點擊某個設備時將deviceId進行藍牙連接

connetBlue(deviceId) {// console.log(deviceId)var that = this;uni.createBLEConnection({// 這里的 deviceId 需要已經通過 createBLEConnection 與對應設備建立鏈接deviceId: deviceId, //設備idsuccess: function(res) {uni.showToast({title: '連接成功',icon: 'fails',duration: 800})console.log("連接藍牙成功!-->11111")uni.stopBluetoothDevicesDiscovery({success: function(res) {console.log('連接藍牙成功之后關閉藍牙搜索');}})that.deviceId = deviceId;that.getServiceId() //5.0}})},

5.連接上需要的藍牙設備之后,獲取這個藍牙設備的服務uuid

getServiceId() {var that = thisuni.getBLEDeviceServices({// 這里的 deviceId 需要已經通過 createBLEConnection 與對應設備建立鏈接deviceId: that.deviceId,success: function(res) {console.log(res)//需要什么服務就用對應的services that.readyservices = res.services[0].uuid //因設備而議:該特征值只支持讀that.services = res.services[1].uuid //因設備而議:該特征值支持write和notfy服務that.getCharacteId() //6.0}})},

6.如果一個藍牙設備需要進行數據的寫入以及數據傳輸,就必須具有某些特征值,所以通過上面步驟獲取的id可以查看當前藍牙設備的特征值

getCharacteId() {var that = thisuni.getBLEDeviceCharacteristics({// 這里的 deviceId 需要已經通過 createBLEConnection 與對應設備建立鏈接deviceId: that.deviceId,// 這里的 serviceId 需要在上面的 getBLEDeviceServices 接口中獲取serviceId: that.services,success: function(res) {console.log(res)for (var i = 0; i < res.characteristics.length; i++) { //2個值var model = res.characteristics[i]if (model.properties.write) {//model.uuid:用來寫入的uuid值//this.sendMy()給設備寫入that.sendMy(model.uuid)}if (model.properties.notify) {//model.uuid:用來notify的uuid值that.notifyUuid = model.uuid}}}})},

7.將從后臺服務獲取的指令寫入到藍牙設備當中

//buffer允許寫入的uuidsendMy(buffer) {var that = this//this.string2buffer-->字符串轉換成ArrayBufer(設備接收數據的格式ArrayBufer)var buff = that.string2buffer('3a0a0b0c0d02423122'); //9.0uni.writeBLECharacteristicValue({// 這里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中獲取deviceId: that.deviceId,// 這里的 serviceId 需要在上面的 getBLEDeviceServices 接口中獲取serviceId: that.services,// 這里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中獲取characteristicId: buffer, //第二步寫入的特征值// 這里的value是ArrayBuffer類型value: buff,success: function(res) {//此時設備已接收到你寫入的數據console.log("寫入成功")that.startNotice()},fail: function(err) {console.log(err)},complete: function() {console.log("調用結束");}})},

8.創建鏈接,發送指令啟用notify 功能接收設備返回的數據

startNotice() {var that = this;uni.notifyBLECharacteristicValueChange({state: true, // 啟用 notify 功能// 這里的 deviceId 需要已經通過 createBLEConnection 與對應設備建立鏈接 deviceId: that.deviceId,// 這里的 serviceId 需要在上面的 getBLEDeviceServices 接口中獲取serviceId: that.services,// 這里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中獲取characteristicId: that.notifyUuid, //第一步 開啟監聽 notityid 第二步發送指令 writesuccess(res) {//接收藍牙返回消息uni.onBLECharacteristicValueChange((sjRes)=>{// 此時可以拿到藍牙設備返回來的數據是一個ArrayBuffer類型數據,//所以需要通過一個方法轉換成字符串var nonceId = that.ab2hex(sjRes.value)//10.0console.log(sjRes)console.log('194行'+nonceId)})},fail(err) {console.log(err)}})}

9.將字符串轉換成ArrayBufer

string2buffer(str) {let val = ""if (!str) return;let length = str.length;let index = 0;let array = []while (index < length) {array.push(str.substring(index, index + 2));index = index + 2;}val = array.join(",");// 將16進制轉化為ArrayBufferreturn new Uint8Array(val.match(/[\da-f]{2}/gi).map(function(h) {return parseInt(h, 16)})).buffer},

10.將ArrayBuffer轉換成字符串

ab2hex(buffer) {const hexArr = Array.prototype.map.call(new Uint8Array(buffer),function (bit) {return ('00' + bit.toString(16)).slice(-2)})return hexArr.join('')},

基本完成連接、寫入、讀取返回值…其他操作看官網

總結

以上是生活随笔為你收集整理的uniapp 蓝牙通讯(搜索/连接蓝牙、读、写)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。