国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

微信小程序藍(lán)牙功能全套開發(fā)流程介紹

這篇具有很好參考價值的文章主要介紹了微信小程序藍(lán)牙功能全套開發(fā)流程介紹。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

一、開發(fā)流程

  1. 流程圖:流程圖作者原文章

微信小程序藍(lán)牙功能全套開發(fā)流程介紹
  1. 實(shí)現(xiàn)模塊順序

1.1初始化藍(lán)牙模塊(打開藍(lán)牙適配器)

初次加載,自動獲取獲取系統(tǒng)信息,檢查藍(lán)牙適配器是否可用

初始化藍(lán)牙,提示打開GPS和藍(lán)牙,開始自動搜索藍(lán)牙設(shè)備

微信小程序藍(lán)牙功能全套開發(fā)流程介紹
微信小程序藍(lán)牙功能全套開發(fā)流程介紹

1.2搜索周圍藍(lán)牙

開始搜索藍(lán)牙設(shè)備,定時1s獲取搜索到的設(shè)備

把搜索到的設(shè)備保存在一個數(shù)組內(nèi),渲染在頁面

微信小程序藍(lán)牙功能全套開發(fā)流程介紹

1.3監(jiān)聽搜索設(shè)備

監(jiān)聽5s后停止搜索,并把新設(shè)備push到數(shù)組進(jìn)行渲染

顯示設(shè)備名稱和連接按鈕

1.4連接目標(biāo)設(shè)備

點(diǎn)擊連接按鈕創(chuàng)建連接,獲取設(shè)備信息

連接成功停止搜索,獲取已連接藍(lán)牙的服務(wù)

微信小程序藍(lán)牙功能全套開發(fā)流程介紹

1.5獲取服務(wù)、特征值

連接成功獲取藍(lán)牙設(shè)備服務(wù)和特征值(是否能讀寫)

微信小程序藍(lán)牙功能全套開發(fā)流程介紹

1.6開啟notify,監(jiān)聽特征值變化

開啟監(jiān)聽功能,發(fā)送指令后接收設(shè)備響應(yīng)的數(shù)據(jù)

微信小程序藍(lán)牙功能全套開發(fā)流程介紹

1.7發(fā)送指令、讀寫數(shù)據(jù)

設(shè)備特征值中寫入數(shù)據(jù),必須設(shè)備的特征支持 write

ArrayBuffer轉(zhuǎn)16進(jìn)制字符串

微信小程序藍(lán)牙功能全套開發(fā)流程介紹

1.8斷開連接

清空設(shè)備名、設(shè)備id,關(guān)閉藍(lán)牙模塊

微信小程序藍(lán)牙功能全套開發(fā)流程介紹

二、實(shí)現(xiàn)模塊代碼

  1. 初始化藍(lán)牙模塊(打開藍(lán)牙適配器)


openBluetoothAdapter() {
        let that = this;
        uni.openBluetoothAdapter({ //初始化藍(lán)牙模塊
          success(res) {
            console.log(res, '初始化藍(lán)牙成功');
            uni.onBluetoothAdapterStateChange((res) => { // 監(jiān)聽藍(lán)牙適配器狀態(tài)變化
              if (!res.available) {
                uni.showModal({
                  title: '溫馨提示',
                  content: '藍(lán)牙適配器不可用,請重新啟動',
                  showCancel: false
                });
              }
            });
            that.searchBlue(); //開始搜索
          },
          fail(res) {
            console.log(res, '初始化藍(lán)牙失敗');
            uni.showToast({
              title: '請檢查手機(jī)藍(lán)牙是否打開',
              icon: 'none'
            });
          }
        });
      },
  1. 開始搜索周圍藍(lán)牙


searchBlue() {
        let that = this;
        uni.startBluetoothDevicesDiscovery({
          // services: [],
          success: (res) => {
            console.log(res, "開始搜索設(shè)備");
            uni.showLoading({
              title: '正在搜索設(shè)備'
            });
            that.getBluetoothDevices();
          },
          fail: (res) => {
            console.log(res, '搜索失敗');
            uni.showToast({
              title: '搜索藍(lán)牙設(shè)備失敗!',
              icon: 'none'
            });
          }
        });
      },
  1. 獲取搜索到的設(shè)備


getBluetoothDevices() {
        let that = this;
        setTimeout(() => {
          uni.getBluetoothDevices({ // 獲取搜索到的設(shè)備
            success: (res) => {
              console.log(res, "搜索到的設(shè)備");
              let devicesListArr = [];
              if (res.devices.length > 0) {
                uni.hideLoading(res.devices);
                res.devices.forEach((device) => {
                  if (!device.name && !device.localName) {
                    return;
                  } else if (device.name.substring(0, 2) == 'AA') {
                    devicesListArr.push(device);
                    that.devicesList = devicesListArr;
                    that.isHideList = true;
                    that.watchBluetoothFound(); // 監(jiān)聽搜索到的新設(shè)備
                  } else {
                    return
                  }
                });
              } else {
                uni.hideLoading();
                uni.showModal({
                  title: '溫馨提示',
                  content: '無法搜索到藍(lán)牙設(shè)備,請重試',
                  showCancel: false
                });
                uni.closeBluetoothAdapter({ //關(guān)閉藍(lán)牙模塊
                  success: (res) => {
                    console.log(res, "關(guān)閉藍(lán)牙模塊");
                  }
                });
              }
            }
          });
        }, 2000);
      },
  1. 監(jiān)聽搜索設(shè)備


watchBluetoothFound() {
        let that = this;
        uni.onBluetoothDeviceFound(function(res) { // 監(jiān)聽搜索到的新設(shè)備
          if (String(res.devices.name).substring(0, 2) == 'AA') {
            devicesListArr.push(res.devices);
            that.devicesList = devicesListArr;
            console.log(res.devices, "監(jiān)聽新設(shè)備");
          }
        })
      },
  1. 連接目標(biāo)設(shè)備


createBLEConnection(e) {
        let that = this;
        let deviceId = e.currentTarget.dataset.id;
        let connectName = e.currentTarget.dataset.name;
        console.log("正在連接" + connectName);
        uni.showLoading({
          title: '連接中...'
        });
        uni.createBLEConnection({
          deviceId,
          success: (res) => {
            uni.hideLoading();
            that.stopBluetoothDevicesDiscovery(); // 連接成功,停止搜索
            console.log(res, "連接成功");
            if (res.errCode == 0) {
              that.deviceId = deviceId;
              that.connectName = connectName;
              that.isHideConnect = true;
              that.getBLEDeviceServices(deviceId); // 獲取服務(wù)
            } else if (res.errCode == 10012) {
              uni.showToast({
                title: '連接超時,請重試!'
              });
            }
          },
          fail(error) {
            uni.hideLoading();
            console.log(error, '連接失敗');
            uni.showToast({
              title: '連接失?。?
            });
          }
        });
      },
  1. 獲取設(shè)備所有服務(wù)


getBLEDeviceServices(deviceId) {
        let that = this;
        // let serviceId;
        uni.getBLEDeviceServices({
          deviceId,
          success: (res) => {
            console.log(res.services, "獲取設(shè)備服務(wù)");
            for (let i = 0; i < res.services.length; i++) {
              let isPrimary = res.services[i].isPrimary
              let uuid = res.services[i].uuid.substring(4, 8) == 'FE60' 
              if (isPrimary && uuid) {
                this.getBLEDeviceCharacteristics(deviceId, res.services[i].uuid)
                return
              }
            }
          }
        });
      },
  1. 獲取特征值


getBLEDeviceCharacteristics(deviceId, serviceId) {
        let that = this;
        uni.getBLEDeviceCharacteristics({ 
          deviceId,
          serviceId,
          success: (res) => {
            console.log(res.characteristics, '獲取特征值成功')
            let characteristicId = res.characteristics[0].uuid;
            let writeNews = {
              deviceId,
              serviceId,
              characteristicId
            };
            that.writeNews = writeNews;
            let notifyId = res.characteristics[1].uuid;
            that.notifyBLECharacteristicValueChange(serviceId, notifyId);
          },
          fail(err) {
            console.log(err, "獲取失敗");
          }
        });
      },
  1. 啟用監(jiān)聽特征值變化


notifyBLECharacteristicValueChange(serviceId, characteristicId) {
        let that = this;
        uni.notifyBLECharacteristicValueChange({ // 啟用監(jiān)聽設(shè)備特征值變化 
          type: 'notification',
          state: true, // 啟用 notify 功能
          deviceId: that.writeNews.deviceId,
          serviceId,
          characteristicId,
          success(res) {
            console.log(res, '啟用監(jiān)聽成功');
            that.onBLECharacteristicValueChange()
          },
          fail: (err) => {
            console.log(err, "啟用監(jiān)聽失敗");
          }
        });
      },
  1. 監(jiān)聽特征值變化


onBLECharacteristicValueChange() {
        let that = this;
        uni.onBLECharacteristicValueChange(res => {
          console.log(res, '監(jiān)聽變化成功');
          let resHex = that.ab2hex(res.value)
          console.log('從機(jī)響應(yīng)的數(shù)據(jù):', resHex)
        })
      },
  1. 發(fā)送、讀取數(shù)據(jù)


writeBLECharacteristicValue(order) { // 向藍(lán)牙設(shè)備發(fā)送一個0x00的16進(jìn)制數(shù)據(jù)
        let that = this;
        let msg = order;
        let buffer = new ArrayBuffer(msg.length / 2); // 定義 buffer 長度
        let dataView = new DataView(buffer); // 從二進(jìn)制ArrayBuffer對象中讀寫多種數(shù)值類型
        let ind = 0;
        for (var i = 0, len = msg.length; i < len; i += 2) {
          let code = parseInt(msg.substr(i, 2), 16)
          dataView.setUint8(ind, code)
          ind++
        }
        console.log('主機(jī)寫入的指令:', that.ab2hex(buffer));
        uni.writeBLECharacteristicValue({
          deviceId: that.writeNews.deviceId,
          serviceId: that.writeNews.serviceId,
          characteristicId: that.writeNews.characteristicId,
          value: buffer,
          writeType: 'writeNoResponse',
          success: (res) => {
            console.log(res, '寫入數(shù)據(jù)成功');
            that.onBLECharacteristicValueChange()
          },
          fail: (err) => {
            console.log(err);
          }
        })
      },
  1. 斷開連接


closeBLEConnection() {
        let that = this;
        console.log(that);
        uni.closeBLEConnection({
          deviceId: this.deviceId,
          success: () => {
            uni.showToast({
              title: '已斷開連接'
            });
            that.deviceId = '';
            that.connectName = '';
            that.isHideConnect = false;
          }
        });
        that.closeBluetoothAdapter();
      },
  1. 關(guān)閉藍(lán)牙模塊


closeBluetoothAdapter() {
        let that = this;
        uni.closeBluetoothAdapter({
          success: (res) => {
            console.log(res);
            that.devicesList = [];
            that.isHideList = false;
            that.isHideConnect = false;
          }
        });
      },
  1. 停止搜索設(shè)備


stopBluetoothDevicesDiscovery() {
        uni.stopBluetoothDevicesDiscovery({
          success(res) {
            console.log(res);
          }
        });
      },
  1. ArrayBuffer轉(zhuǎn)16進(jìn)制字符串


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

三、遇到的問題以及解決

問題1:onBLECharacteristicValueChange監(jiān)聽不到響應(yīng)數(shù)據(jù)?

解決方法:

1、給onBLECharacteristicValueChange添加延時器;

2、給notifyBLECharacteristicValueChange添加type: 'notification';

3、給writeBLECharacteristicValue添加 writeType: 'writeNoResponse';

4、更換手機(jī)設(shè)備:Android、IOS設(shè)備;

5、查看特征值:read、notify、write、writeNoResponse;

6、分包發(fā)送:藍(lán)牙BLE最大支持20個字節(jié)發(fā)送,因此超過20個字節(jié)需要分包發(fā)送;

7、遵循硬件文檔:使用指定服務(wù),寫入、接收分別使用的特征值(成功解決);文章來源地址http://www.zghlxwxcb.cn/news/detail-482615.html

到了這里,關(guān)于微信小程序藍(lán)牙功能全套開發(fā)流程介紹的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 微信小程序藍(lán)牙授權(quán)完整流程

    微信小程序藍(lán)牙授權(quán)完整流程

    ? ? ? ? 1.1 authorize: ????????????????提前向用戶發(fā)起授權(quán)請求。調(diào)用后會立刻彈窗詢問用戶是否同意授權(quán)小程序使用某項(xiàng)功能或獲取用戶的某些數(shù)據(jù),但不會實(shí)際調(diào)用對應(yīng)接口。如果用戶之前已經(jīng)同意授權(quán),則不會出現(xiàn)彈窗,直接返回成功。更多用法詳見 用戶授權(quán)。

    2024年04月27日
    瀏覽(103)
  • 微信小程序——實(shí)現(xiàn)藍(lán)牙設(shè)備搜索及連接功能

    微信小程序——實(shí)現(xiàn)藍(lán)牙設(shè)備搜索及連接功能

    ?作者簡介:2022年 博客新星 第八 。熱愛國學(xué)的Java后端開發(fā)者,修心和技術(shù)同步精進(jìn)。 ??個人主頁:Java Fans的博客 ??個人信條:不遷怒,不貳過。小知識,大智慧。 ??當(dāng)前專欄:微信小程序?qū)W習(xí)分享 ?特色專欄:國學(xué)周更-心性養(yǎng)成之路 ??本文內(nèi)容:微信小程序——實(shí)

    2024年02月08日
    瀏覽(26)
  • 微信小程序:BLE藍(lán)牙開發(fā)

    一、添加藍(lán)牙權(quán)限: 1.添加藍(lán)牙權(quán)限(工程/app.json): 二、實(shí)現(xiàn)掃描/連接/接收BLE設(shè)備數(shù)據(jù): 1.實(shí)現(xiàn)BLE藍(lán)牙設(shè)備掃描: 2.實(shí)現(xiàn)連接設(shè)備/接收數(shù)據(jù): 3.調(diào)用例子:

    2024年02月12日
    瀏覽(106)
  • 微信小程序低功耗藍(lán)牙BLE快速開發(fā)js

    微信小程序低功耗藍(lán)牙BLE快速開發(fā)js

    目的: 1、為了能三分鐘快速開發(fā)BLE模塊,特此做一個筆記,按照筆記的順序開發(fā),能夠簡單、快速、規(guī)范。 2、如果以后覺得有必要改動的地方就在這里更改。 3、主要是記錄BLE連接的步驟。 https://note.youdao.com/ynoteshare/index.html?id=d662c9c1c58121ec28901d78d9aa5e80 比較完整的微信小程

    2024年02月10日
    瀏覽(109)
  • 微信小程序基礎(chǔ)功能及技術(shù)棧實(shí)現(xiàn)介紹

    番茄鐘功能 : 前端(Vant Weapp):使用Vant Weapp提供的倒計(jì)時組件或者自己開發(fā)一個正計(jì)時的組件來實(shí)現(xiàn)計(jì)時功能,同時可以用它的列表、排行榜組件等來展示各科目的學(xué)習(xí)時間和學(xué)習(xí)時長排行。 后端(PHP+ThinkPHP6):ThinkPHP6作為一個功能強(qiáng)大的PHP框架,可以方便地處理學(xué)習(xí)時

    2024年02月11日
    瀏覽(88)
  • 微信小程序用什么工具開發(fā)(微信小程序開發(fā)工具介紹)

    微信小程序用什么工具開發(fā)(微信小程序開發(fā)工具介紹)

    有很多人在開發(fā)小程序之前都會去了解微信小程序開發(fā)工具,想知道微信小程序用什么工具開發(fā)。時至今日,隨著互聯(lián)網(wǎng)技術(shù)的發(fā)展,現(xiàn)在開發(fā)微信小程序也能使用多種不同的工具,讓我們來了解一下吧。 一、微信開發(fā)者工具 這是微信官方提供的微信小程序開發(fā)工具,可以

    2024年02月11日
    瀏覽(95)
  • 微信小程序從開發(fā)—到上線的流程講解

    微信小程序從開發(fā)—到上線的流程講解

    1.下載微信小程序開發(fā)工具:在微信官方文檔中進(jìn)入小程序模塊、選擇工具-下載、選擇穩(wěn)定版,根據(jù)自己的操作系統(tǒng)選擇適合的版本。 2.購買云服務(wù)器:在阿里云或騰訊云等服務(wù)器廠商中,根據(jù)自己的需求買一個服務(wù)器并進(jìn)行服務(wù)器的一些配置,建議選購的時候選擇 Linux寶塔

    2024年02月05日
    瀏覽(91)
  • 微信小程序 云開發(fā) 聊天功能

    微信小程序 云開發(fā) 聊天功能

    。功能要求為:一對一聊天,可以發(fā)送文字,圖片,語音,文件,視頻,含消息列表頁。 暑假沒事干來寫篇博客復(fù)盤一下??蚣芎蜆邮讲糠志褪遣捎昧薱olorUI 的組件,沒啥好說的,這方面我也不會 ?這是聊天內(nèi)容部分 淺淺地小講一下 scroll-into-view string 值應(yīng)為某子元素id(id不

    2024年02月09日
    瀏覽(88)
  • 【聯(lián)機(jī)對戰(zhàn)】微信小程序聯(lián)機(jī)游戲開發(fā)流程詳解

    【聯(lián)機(jī)對戰(zhàn)】微信小程序聯(lián)機(jī)游戲開發(fā)流程詳解

    現(xiàn)有一個微信小程序叫中國象棋項(xiàng)目,棋盤類的單機(jī)游戲看著有缺少了什么,現(xiàn)在給補(bǔ)上了,加個聯(lián)機(jī)對戰(zhàn)的功能,增加了可玩性,對新手來說,實(shí)現(xiàn)聯(lián)機(jī)游戲還是有難度的,那要怎么實(shí)現(xiàn)的呢,接下來給大家講一下。 考慮到搭建聯(lián)機(jī)游戲的服務(wù)器成本不小,第一個想法是用

    2024年02月04日
    瀏覽(258)
  • 微信小程序發(fā)布上線全流程(注冊/開發(fā)/上傳審核)

    以下是微信小程序發(fā)布上線的詳細(xì)流程: 確認(rèn)小程序信息:在微信公眾平臺注冊并登錄后,進(jìn)入小程序管理后臺,在“開發(fā)”-“開發(fā)設(shè)置”中填寫小程序基本信息和配置,包括小程序名稱、圖標(biāo)設(shè)計(jì)、類目選擇等。此外,需要在小程序管理后臺中配置小程序服務(wù)類目和資質(zhì)

    2024年02月10日
    瀏覽(163)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包