微信小程序--藍牙連接開發(fā)總結(jié)

這個模塊做了2周,找了很多資料文檔,看示例看別人的demo,最后發(fā)現(xiàn)其實還是得靠自己,不吐槽了,開正文。我實現(xiàn)的小程序模塊自動連接(根據(jù)需要,可改手動),是在小程序初始化完成時開始自動調(diào)用執(zhí)行。

大致流程:

1、 開啟藍牙適配
2、 獲取藍牙適配器狀態(tài),判斷設(shè)備藍牙是否可用。
3、 判斷藍牙適配器可用時開啟掃描藍牙設(shè)備和開啟獲取已連接的藍牙設(shè)備
4、 如果開啟掃描藍牙設(shè)備失敗5s后自動再次開啟掃描
5、 開啟掃描藍牙設(shè)備成功后開啟監(jiān)聽已掃描的設(shè)備
6、 如果已掃描到的新設(shè)備含F(xiàn)eiZhi名(個人產(chǎn)品需要)的設(shè)備則開始連接該設(shè)備
7、 開啟獲取已連接藍牙設(shè)備開啟獲取設(shè)備成功后判斷以獲取的設(shè)備名包含F(xiàn)eiZhi(個人產(chǎn)品需要)字符串的設(shè)備則開始連接該設(shè)備
8、 開始獲取已連接藍牙設(shè)備沒有成功獲取到已連接的藍牙設(shè)備5s后自動重新開啟獲取。
9、 開始連接某設(shè)備時停止掃描設(shè)備,停止循環(huán)獲取已連接設(shè)備。
10、連接成功后停止掃描設(shè)備,停止循環(huán)獲取已連接設(shè)備。

點擊查看:藍牙模塊連接流程圖
1、app.js的onLaunch() 方法里中調(diào)用開啟連接 this.startConnect();彈出提示框,開啟適配,如果失敗提示設(shè)備藍牙不可用,同時開啟藍牙適配器狀態(tài)監(jiān)聽。

startConnect: function () {
    var that = this;
    wx.showLoading({
      title: '開啟藍牙適配'
    });
    wx.openBluetoothAdapter({
      success: function (res) {
        console.log("初始化藍牙適配器");
        console.log(res);
        that.getBluetoothAdapterState();
      },
      fail: function (err) {
        console.log(err);
        wx.showToast({
          title: '藍牙初始化失敗',
          icon: 'success',
          duration: 2000
        })
 
        setTimeout(function () {
          wx.hideToast()
        }, 2000)
      }
    });
    wx.onBluetoothAdapterStateChange(function (res) {
      var available = res.available;
      if (available) {
        that.getBluetoothAdapterState();
      }
    })
  }

2、初始化藍牙適配器成功,調(diào)用this.getBluetoothAdapterState() 獲取本機藍牙適配器狀態(tài),判斷是否可用,available為false則因為用戶沒有開啟系統(tǒng)藍牙。同時判斷程序還沒有開始搜索藍牙設(shè)備,調(diào)用this.startBluetoothDevicesDiscovery();開始掃描附近的藍牙設(shè)備,同時調(diào)用this.getConnectedBluetoothDevices() 開啟獲取本機已配對的藍牙設(shè)備。

startConnect: function () {
    var that = this;
    wx.showLoading({
      title: '開啟藍牙適配'
    });
    wx.openBluetoothAdapter({
      success: function (res) {
        console.log("初始化藍牙適配器");
        console.log(res);
        that.getBluetoothAdapterState();
      },
      fail: function (err) {
        console.log(err);
        wx.showToast({
          title: '藍牙初始化失敗',
          icon: 'success',
          duration: 2000
        })
 
        setTimeout(function () {
          wx.hideToast()
        }, 2000)
      }
    });
 
    wx.onBluetoothAdapterStateChange(function (res) {
      var available = res.available;
      if (available) {
        that.getBluetoothAdapterState();
      }
    })
  }

3、開始搜索藍牙設(shè)備startBluetoothDevicesDiscovery() , 提示藍牙搜索。

startBluetoothDevicesDiscovery: function () {
    var that = this;
    wx.showLoading({
      title: '藍牙搜索'
    });
    wx.startBluetoothDevicesDiscovery({
      services: [],
      allowDuplicatesKey: false,
      success: function (res) {
        if (!res.isDiscovering) {
          that.getBluetoothAdapterState();
        } else {
          that.onBluetoothDeviceFound();
        }
      },
      fail: function (err) {
        console.log(err);
      }
    });
  }

4、獲取已配對的藍牙設(shè)備。此方法特別說明參數(shù)services(Array)是必填的,但是官方示例中以及各種坑爹demo里從沒見過有誰填寫,但是不填寫這個屬性此方法無法獲取到任何已配對設(shè)備。如果要調(diào)用此方法則是需要連接特定設(shè)備,并且知道該設(shè)備的一個主服務serviceId。如果未知可以先手動連接一次想要連接的設(shè)備,然后獲取service列表,記錄屬性primary為true的值至少一個。

getConnectedBluetoothDevices: function () {
    var that = this;
    wx.getConnectedBluetoothDevices({
      services: [that.serviceId],
      success: function (res) {
        console.log("獲取處于連接狀態(tài)的設(shè)備", res);
        var devices = res['devices'], flag = false, index = 0, conDevList = [];
        devices.forEach(function (value, index, array) {
          if (value['name'].indexOf('FeiZhi') != -1) {
            // 如果存在包含F(xiàn)eiZhi字段的設(shè)備
            flag = true;
            index += 1;
            conDevList.push(value['deviceId']);
            that.deviceId = value['deviceId'];
            return;
          }
        });
        if (flag) {
          this.connectDeviceIndex = 0;
          that.loopConnect(conDevList);
        } else {
          if (!this.getConnectedTimer) {
            that.getConnectedTimer = setTimeout(function () {
              that.getConnectedBluetoothDevices();
            }, 5000);
          }
        }
      },
      fail: function (err) {
        if (!this.getConnectedTimer) {
          that.getConnectedTimer = setTimeout(function () {
            that.getConnectedBluetoothDevices();
          }, 5000);
        }
      }
    });
  }

5、開啟藍牙搜索功能失敗,則回到第2步重新檢查藍牙是適配器是否可用,開啟藍牙搜索功能成功后開啟發(fā)現(xiàn)附近藍牙設(shè)備事件監(jiān)聽。this.onBluetoothDeviceFound()

onBluetoothDeviceFound: function () {
    var that = this;
    console.log('onBluetoothDeviceFound');
    wx.onBluetoothDeviceFound(function (res) {
      console.log('new device list has founded')
      console.log(res);
      if (res.devices[0]) {
        var name = res.devices[0]['name'];
        if (name != '') {
          if (name.indexOf('FeiZhi') != -1) {
            var deviceId = res.devices[0]['deviceId'];
            that.deviceId = deviceId;
            console.log(that.deviceId);
            that.startConnectDevices();
          }
        }
      }
    })
  }

此方法可自定義過濾一些無效的藍牙設(shè)備比如name為空的,個人產(chǎn)品開發(fā)中需要過濾devices name 不含有FeiZhi字符串的設(shè)備。

6、在第5步中發(fā)現(xiàn)了某個想配對的設(shè)備,則獲取到該設(shè)備的deviceId,然后開始配對該設(shè)備 this.startConnectDevices()。

startConnectDevices: function (ltype, array) {
    var that = this;
    clearTimeout(that.getConnectedTimer);
    that.getConnectedTimer = null;
    clearTimeout(that.discoveryDevicesTimer);
    that.stopBluetoothDevicesDiscovery();
    this.isConnectting = true;
    wx.createBLEConnection({
      deviceId: that.deviceId,
      success: function (res) {
        if (res.errCode == 0) {
          setTimeout(function () {
            that.getService(that.deviceId);
          }, 5000)
        }
      },
      fail: function (err) {
        console.log('連接失敗:', err);
        if (ltype == 'loop') {
          that.connectDeviceIndex += 1;
          that.loopConnect(array);
        } else {
          that.startBluetoothDevicesDiscovery();
          that.getConnectedBluetoothDevices();
        }
      },
      complete: function () {
        console.log('complete connect devices');
        this.isConnectting = false;
      }
    });
  }

開啟連接后為了避免出現(xiàn)沖突,一旦開啟連接則終止掃描附近藍牙設(shè)備,終止讀取本機已配對設(shè)備。

7、連接成功后根據(jù)deiviceId獲取設(shè)備的所有服務。this.getService(deviceId);

getService: function (deviceId) {
    var that = this;
    // 監(jiān)聽藍牙連接
    wx.onBLEConnectionStateChange(function (res) {
      console.log(res);
    });
    // 獲取藍牙設(shè)備service值
    wx.getBLEDeviceServices({
      deviceId: deviceId,
      success: function (res) {
        that.getCharacter(deviceId, res.services);
      }
    })
  }

8、讀取服務的特征值。

getCharacter: function (deviceId, services) {
    var that = this;
    services.forEach(function (value, index, array) {
      if (value == that.serviceId) {
        that.serviceId = array[index];
      }
    });
    wx.getBLEDeviceCharacteristics({
      deviceId: deviceId,
      serviceId: that.serviceId,
      success: function (res) {
        that.writeBLECharacteristicValue(deviceId, that.serviceId, that.characterId_write);
        that.openNotifyService(deviceId, that.serviceId, that.characterId_read);
      },
      fail: function (err) {
        console.log(err);
      },
      complete: function () {
        console.log('complete');
      }
    })
  }

9、如果掃描到的設(shè)備中沒有想要連接的設(shè)備,可以嘗試使用系統(tǒng)藍牙手動配對,然后再小程序中調(diào)用getConnectedBluetoothDevices() 獲取本機已配對的藍牙設(shè)備,然后過濾設(shè)備(可能獲取多個已配對的藍牙設(shè)備)。將以獲取的藍牙設(shè)備deviceId放入到一個數(shù)組中調(diào)用自定義方法this.loopConnect(); 思路:通過遞歸調(diào)用獲取已配對藍牙設(shè)備的deviceId,如果獲取到了就去連接,devicesId[x] 為空說明上傳調(diào)用getConnectedBluetoothDevices()時獲取到的已配對設(shè)備全部連接失敗了。則開啟重新獲取已配對藍牙設(shè)備,并開啟掃描附近藍牙設(shè)備。

loopConnect: function (devicesId) {
    var that = this;
    var listLen = devicesId.length;
    if (devicesId[this.connectDeviceIndex]) {
      this.deviceId = devicesId[this.connectDeviceIndex];
      this.startConnectDevices('loop', devicesId);
    } else {
      console.log('已配對的設(shè)備小程序藍牙連接失敗');
      that.startBluetoothDevicesDiscovery();
      that.getConnectedBluetoothDevices();
    }
  }

10、**startConnectDevices(’loop’, array)方法,是當獲取已配對藍牙設(shè)備進行連接時這樣調(diào)用。其中的處理邏輯上文已經(jīng)貼出,意思就是在連接失敗后fail方法里累加一個全局變量,然后回調(diào)loopConnect(array)方法。

11、手動連接,上文介紹的方法是為了直接自動連接,如果不需要自動連接,可在使用方法getBluetoothDevices() 將會獲取到已掃描到的藍牙設(shè)備的列表,可以做個頁面顯示出設(shè)備名,點擊該設(shè)備開始連接。

注意:
1、that.serviceId 是在初始化時設(shè)置的,由于對需要連接設(shè)備的主服務serivceId和各種特征值都是已知的因此可以這樣做。如果不可知可以做一個掃描方法自己檢查特征值的用途。
2、 連接成功后的writeBLECharacteristicValue和openNotifyService操作需要注意,如果同時開啟這兩項操作要先調(diào)用wirte再開啟notify(原因未知,個人心得)。
3、經(jīng)人提醒還可以再完善一下在onBlueToothAdapterStateChange()**可以監(jiān)聽藍牙適配器狀態(tài),以此判斷連接過程中或連接后用戶開關(guān)了設(shè)備藍牙,如果判斷到關(guān)了藍牙提示請開啟,如果監(jiān)聽到開啟了,就重新回到第1步。

最后本文屬于個人開發(fā)者的一點總結(jié),歡迎留言指導討論,也可以加入小程序聯(lián)盟群(536592077)一起探討學習。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • Guide to BluetoothSecurity原文 本出版物可免費從以下網(wǎng)址獲得:https://doi.o...
    公子小水閱讀 8,797評論 0 6
  • Android平臺支持藍牙網(wǎng)絡協(xié)議棧,實現(xiàn)藍牙設(shè)備之間數(shù)據(jù)的無線傳輸。本文檔描述了怎樣利用android平臺提供的...
    Camming閱讀 3,492評論 0 3
  • 藍牙 注:本文翻譯自https://developer.android.com/guide/topics/conn...
    RxCode閱讀 9,035評論 11 99
  • 普通藍牙設(shè)備官方文檔 Android 平臺包含藍牙網(wǎng)絡堆棧支持,憑借此支持,設(shè)備能以無線方式與其他藍牙設(shè)備交換數(shù)據(jù)...
    sydMobile閱讀 69,842評論 5 43
  • 公司的項目最近需要用到藍牙開發(fā)的相關(guān)內(nèi)容,因此特地查閱了Google官方文檔的內(nèi)容并進行二次整理,希望能對需要學習...
    Chuckiefan閱讀 32,724評論 44 123

友情鏈接更多精彩內(nèi)容