微信小程序更新--測試API之藍牙

接着上一篇來繼續測:微信小程序藍牙的API每個測試了一下發現跟以前的寫Android的藍牙調取是一樣的,打開流程:先打開藍牙的適配器,然後確定下本機的藍牙狀態,然後開始搜索藍牙,搜索完成以後關閉搜索。開始連接,根據搜索到的mac 適配連接。在服務裏監聽連接斷開的狀態。Android 的可以看這裏:藍牙的使用。先看一下打印的log吧。

大概說明一下:打log 要這樣寫 console.log("初始化藍牙適配器",res),我一直都是 寫成console.log("初始化藍牙適配器"+res)。

搞的每次出不來。

流程是這樣的:先初始化藍牙適配器,然後獲取本機藍牙適配器的狀態,然後開始搜索,當停止搜索以後在開始搜索,就會觸發藍牙是配置狀態變化的事件,搜索完成以後獲取所有已經發現的藍牙設備,就可以將devices中的設備Array取出來了。然後就可以得到所有已經連接的設備了,至於鏈接功能,還沒有真機可測,所以沒有測試。




我的電腦上藍牙連接的設備:



// pages/bluetooth/bluetooth.js
Page({
  data:{},
  onLoad:function(options){
    // 頁面初始化 options爲頁面跳轉所帶來的參數
  },
  //初始化藍牙適配器
  openBluetooth:function(){
    wx.openBluetoothAdapter({
      success: function(res){
        console.log(res.errMsg)
        // success
        wx.showToast({
          title:"初始化藍牙適配器成功",
          duration:2000
        })
      },
    })
  },
//關閉藍牙模塊
closeBluetooth:function(){
 wx.openBluetoothAdapter()
 
  wx.closeBluetoothAdapter({
    success: function(res){
      // success
       console.log("success"+res)
    }
  })
},
//獲取本機藍牙適配器狀態
getBluetoothAdapterState:function(){
wx.getBluetoothAdapterState({
  success: function(res){
    // success
     console.log("res:"+res)
     console.log("errMsg:"+res.errMsg)
  }
})
},
//監聽藍牙適配器狀態變化事件
  onBluetoothAdapterStateChange:function(){
    wx.onBluetoothAdapterStateChange(function(res) {
      console.log(`adapterState changed, now is`, res)
  })
},
 // 開始搜尋附近的藍牙外圍設備
  startBluetoothDevicesDiscovery:function(){
    wx.startBluetoothDevicesDiscovery({
      success: function (res) {
      console.log(res)
    }
  })
},
 // 停止搜尋附近的藍牙外圍設備
  stopBluetoothDevicesDiscovery:function(){
    wx.stopBluetoothDevicesDiscovery({
      success: function (res) {
      console.log(res)
    }
  })
},
  //獲取所有已發現的藍牙設備
  getBluetoothDevices:function(){
    wx.getBluetoothDevices({
      success: function(res){
        // success
        console.log(res)
      },
    })
  },
  //監聽尋找到新設備的事件
  onBluetoothDeviceFound:function(){
    wx.onBluetoothDeviceFound(function(res) {
      // callback
       console.log(res)
    })
  },
  //根據 uuid 獲取處於已連接狀態的設備
  getConnectedBluetoothDevices:function(){
  wx.getConnectedBluetoothDevices({
  success: function (res) {
    console.log(res)
  }
})
},
//連接低功耗藍牙設備
createBLEConnection:function(){
  wx.createBLEConnection({
    deviceId: 'AC:BC:32:C1:47:80',
    success: function(res){
      // success
      console.log(res)
    },
    fail: function(res) {
      // fail
    },
    complete: function(res) {
      // complete
    }
  })
},
//斷開與低功耗藍牙設備的連接
closeBLEConnection:function(){
  wx.closeBLEConnection({
    deviceId: 'AC:BC:32:C1:47:80',
    success: function (res) {
    console.log(res)
  }
})
},
//監聽低功耗藍牙連接的錯誤事件,包括設備丟失,連接異常斷開等等
onBLEConnectionStateChanged:function(){
  wx.onBLEConnectionStateChanged(function(res) {
  console.log(`device ${res.deviceId} state has changed, connected: ${res.connected}`)
})
},
//獲取藍牙設備所有 service(服務)
getBLEDeviceServices:function(){
  wx.getBLEDeviceServices({
    deviceId: '48:3B:38:88:E3:83',
    success: function(res){
      // success
       console.log('device services:', res.services.serviceId)
    },
    fail: function(res) {
      // fail
    },
    complete: function(res) {
      // complete
    }
  })
},
//獲取藍牙設備所有 characteristic(特徵值)
getBLEDeviceCharacteristics:function(){
  wx.getBLEDeviceCharacteristics({
    deviceId: '48:3B:38:88:E3:83',
    serviceId: 'serviceId',
    success: function(res){
      // success
    },
    fail: function(res) {
      // fail
    },
    complete: function(res) {
      // complete
    }
  })
}
})




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章