Skip to content

藍牙-通用

stopBluetoothDevicesDiscovery

TIP

該API使用方法為: wx.stopBluetoothDevicesDiscovery(Object object)

  • 功能說明: 停止搜尋附近的藍牙周邊設備。 若已經找到需要的藍牙設備並不需要繼續搜索時,建議調用該接口停止藍牙蒐索。

  • 參數及說明: Object object。

    内容類型預設值必填說明
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)
  • 錯誤碼

    錯誤碼錯誤資訊說明
    0ok正常
    -1already connect已連接
    10000not init未初始化藍牙適配器
    10001not available當前藍牙適配器不可用
    10002no device沒有找到指定設備
    10003connection fail連接失敗
    10004no service沒有找到指定服務
    10005no characteristic沒有找到指定特徵
    10006no connection當前連接已斷開
    10007property not support當前特徵不支持此操作
    10008system error其餘所有系統上報的异常
    10009system not supportAndroid系統特有,系統版本低於4.3不支持BLE
    10012operate time out連接超時
    10013invalid_data連接deviceId為空或者是格式不正確
  • 示例代碼:

js
wx.stopBluetoothDevicesDiscovery({
  success (res) {
    console.log(res)
  }
})

startBluetoothDevicesDiscovery

TIP

考慮到藍牙功能可以間接進行定位,Android 6.0及以上版本,無定位許可權或定位開關未打開時,無法進行設備蒐索。 這種情況下,Android 8.0.16前,接口調用成功但無法掃描設備; Android 8.0.16及以上版本,會返回錯誤。

TIP

該API使用方法為: wx.startBluetoothDevicesDiscovery(Object object)

  • 功能說明: 開始搜尋附近的藍牙周邊設備。 此操作比較耗費系統資源,請在蒐索到需要的設備後及時調用 wx.stopBluetoothDevicesDiscovery 停止蒐索

  • 參數及說明: Object object。

    内容類型預設值必填說明
    servicesArray.<string>-要蒐索的藍牙設備主服務的UUID清單(支持16/32/128比特UUID)。 某些藍牙設備會廣播自己的主service的UUID。 如果設定此參數,則只蒐索廣播包有對應UUID的主服務的藍牙設備。 建議通過該參數過濾掉周邊不需要處理的其他藍牙設備
    allowDuplicatesKeybooleanfalse是否允許重複上報同一設備。 如果允許重複上報,則wx.onBluetoothDeviceFound 方法會多次上報同一設備,但是RSSI值會有不同。
    intervalnumber0上報設備的間隔,單位ms。 0表示找到新設備立即上報,其他數值根據傳入的間隔上報。
    powerLevelstringmedium掃描模式,越高掃描越快,也越耗電。 僅Android宿主用戶端7.0.12及以上支持,合法值為:
    1.low: 低
    2.medium: 中
    3.high: 高
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)
  • 錯誤碼

    錯誤碼錯誤資訊說明
    0ok正常
    -1already connect已連接
    10000not init未初始化藍牙適配器
    10001not available當前藍牙適配器不可用
    10002no device沒有找到指定設備
    10003connection fail連接失敗
    10004no service沒有找到指定服務
    10005no characteristic沒有找到指定特徵
    10006no connection當前連接已斷開
    10007property not support當前特徵不支持此操作
    10008system error其餘所有系統上報的异常
    10009system not supportAndroid系統特有,系統版本低於4.3不支持BLE
    10012operate time out連接超時
    10013invalid_data連接deviceId為空或者是格式不正確
  • 示例代碼:

js
// Taking the Bluetooth smart light of the host client's hardware platform as an example, the UUID of the main service is FEE7. By passing in this parameter, only devices with a main service UUID of FEE7 will be searched for.
wx.startBluetoothDevicesDiscovery({
  services: ['FEE7'],
  success (res) {
    console.log(res)
  }
})

openBluetoothAdapter

TIP

該API使用方法為: wx.openBluetoothAdapter(Object object)

TIP

  • 其他藍牙相關API必須在 wx.openBluetoothAdapter 調用之後使用。 否則API會返回錯誤(errCode=10000)。
  • 在用戶藍牙開關未開啟或者手機不支持藍牙功能的情况下,調用wx.openBluetoothAdapter會返回錯誤(errCode=10001),表示手機藍牙功能不可用。
  • 功能說明: 初始化藍牙模組。 iOS上開啟主機/從機(周邊設備)模式時需分別調用一次,並指定對應的mode。

  • 參數及說明: Object object。

    内容類型合法值及說明預設值必填說明
    modestring1.central: 主機模式
    2.peripheral: 從機(周邊設備)模式
    central藍牙模式,可作為主/從設備,僅iOS需要
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)
  • 錯誤碼

    錯誤碼錯誤資訊說明
    0ok正常
    -1already connect已連接
    10000not init未初始化藍牙適配器
    10001not available當前藍牙適配器不可用
    10002no device沒有找到指定設備
    10003connection fail連接失敗
    10004no service沒有找到指定服務
    10005no characteristic沒有找到指定特徵
    10006no connection當前連接已斷開
    10007property not support當前特徵不支持此操作
    10008system error其餘所有系統上報的异常
    10009system not supportAndroid系統特有,系統版本低於4.3不支持BLE
    10012operate time out連接超時
    10013invalid_data連接deviceId為空或者是格式不正確
  • object.fail回呼函數返回的state參數(僅iOS)

    狀態碼說明
    0未知
    1重置中
    2不支持
    3未授權
    4未開啟
  • 示例代碼:

js
wx.openBluetoothAdapter({
  success (res) {
    console.log(res)
  }
})

getConnectedBluetoothDevices

TIP

該API使用方法為: wx.getConnectedBluetoothDevices(Object object)

  • 功能說明: 根據主服務UUID獲取已連接的藍牙設備。

  • 參數及說明: Object object。

    内容類型預設值必填說明
    servicesArray-藍牙設備主服務的UUID清單(支持16/32/128比特UUID)
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)
  • object.success回呼函數參數: Object res。

    内容類型說明
    devicesArray.<Object>蒐索到的設備清單
    結構内容類型說明
    namestring藍牙設備名稱,某些設備可能沒有
    deviceIdstring用於區分設備的id
  • 錯誤碼

    錯誤碼錯誤資訊說明
    0ok正常
    -1already connect已連接
    10000not init未初始化藍牙適配器
    10001not available當前藍牙適配器不可用
    10002no device沒有找到指定設備
    10003connection fail連接失敗
    10004no service沒有找到指定服務
    10005no characteristic沒有找到指定特徵
    10006no connection當前連接已斷開
    10007property not support當前特徵不支持此操作
    10008system error其餘所有系統上報的异常
    10009system not supportAndroid系統特有,系統版本低於4.3不支持BLE
    10012operate time out連接超時
    10013invalid_data連接deviceId為空或者是格式不正確
  • 示例代碼:

js
wx.getConnectedBluetoothDevices({
  services: ['FEE7'],
  success (res) {
    console.log(res)
  }
})

getBluetoothDevices

TIP

該API使用方法為: wx.getBluetoothDevices(Object object)

TIP

該接口獲取到的設備清單為藍牙模組生效期間所有蒐索到的藍牙設備,若在藍牙模組使用流程結束後未及時調用 wx.closeBluetoothAdapter 釋放資源,會存在調用該接口會返回之前的藍牙使用流程中蒐索到的藍牙設備,可能設備已經不在用戶身邊,無法連接。

  • 功能說明: 獲取在藍牙模組生效期間所有蒐索到的藍牙設備。 包括已經和本機處於連接狀態的設備。

  • 參數及說明: Object object。

    内容類型預設值必填說明
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)
  • object.success回呼函數參數: Object res。

    内容類型說明
    devicesArray.<Object>UUID對應的已連接設備清單
    結構内容類型說明
    namestring藍牙設備名稱,某些設備可能沒有
    deviceIdstring藍牙設備id
    RSSInumber當前藍牙設備的信號強度,單位: dBm
    advertisDataArrayBuffer當前藍牙設備的廣播數據段中的ManufacturerData數據段
    advertisServiceUUIDsArray.<string>當前藍牙設備的廣播數據段中的ServiceUUIDs數據段
    localNamestring當前藍牙設備的廣播數據段中的LocalName數據段
    serviceDataObject當前藍牙設備的廣播數據段中的ServiceData數據段
    connectableboolean當前藍牙設備是否可連接(Android 8.0以下不支持返回該值)
  • 示例代碼:

js
// ArrayBuffer to 16 progress string example
function ab2hex(buffer) {
  var hexArr = Array.prototype.map.call(
    new Uint8Array(buffer),
    function(bit) {
      return ('00' + bit.toString(16)).slice(-2)
    }
  )
  return hexArr.join('');
}
wx.getBluetoothDevices({
  success: function (res) {
    console.log(res)
    if (res.devices[0]) {
      console.log(ab2hex(res.devices[0].advertisData))
    }
  }
})

getBluetoothAdapterState

TIP

該API使用方法為: wx.getBluetoothAdapterState(Object object)

  • 功能說明: 獲取本機藍牙適配器狀態。

  • 參數及說明: Object object。

    内容類型預設值必填說明
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)
  • object.success回呼函數參數: Object res。

    内容類型說明
    discoveringboolean是否正在蒐索設備
    availableboolean藍牙適配器是否可用
  • 錯誤碼

    錯誤碼錯誤資訊說明
    0ok正常
    -1already connect已連接
    10000not init未初始化藍牙適配器
    10001not available當前藍牙適配器不可用
    10002no device沒有找到指定設備
    10003connection fail連接失敗
    10004no service沒有找到指定服務
    10005no characteristic沒有找到指定特徵
    10006no connection當前連接已斷開
    10007property not support當前特徵不支持此操作
    10008system error其餘所有系統上報的异常
    10009system not supportAndroid系統特有,系統版本低於4.3不支持BLE
    10012operate time out連接超時
    10013invalid_data連接deviceId為空或者是格式不正確
  • 示例代碼:

js
wx.getBluetoothAdapterState({
  success (res) {
    console.log(res)
  }
})

closeBluetoothAdapter

TIP

該API使用方法為: wx.closeBluetoothAdapter(Object object)

  • 功能說明: 關閉藍牙模組。 調用該方法將斷開所有已建立的連接並釋放系統資源。 建議在使用藍牙流程後,與 wx.openBluetoothAdapter成對調用。

  • 參數及說明: Object object。

    内容類型預設值必填說明
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)
  • 錯誤碼

    錯誤碼錯誤資訊說明
    0ok正常
    -1already connect已連接
    10000not init未初始化藍牙適配器
    10001not available當前藍牙適配器不可用
    10002no device沒有找到指定設備
    10003connection fail連接失敗
    10004no service沒有找到指定服務
    10005no characteristic沒有找到指定特徵
    10006no connection當前連接已斷開
    10007property not support當前特徵不支持此操作
    10008system error其餘所有系統上報的异常
    10009system not supportAndroid系統特有,系統版本低於4.3不支持BLE
    10012operate time out連接超時
    10013invalid_data連接deviceId為空或者是格式不正確
  • 示例代碼:

js
wx.closeBluetoothAdapter({
  success (res) {
    console.log(res)
  }
})

onBluetoothDeviceFound

TIP

該API使用方法為: wx.onBluetoothDeviceFound(function listener)

TIP

  • 若在wx.onBluetoothDeviceFound回檔了某個設備,則此設備會添加到wx.getBluetoothDevices 接口獲取到的數組中。
  • 藍牙設備在被蒐索到時,系統返回的name欄位一般為廣播包中的LocalName欄位中的設備名稱,而如果與藍牙設備建立連接,系統返回的name欄位會改為從藍牙設備上獲取到的GattName。 若需要動態改變設備名稱並展示,建議使用localName欄位。
  • Android下部分機型需要有位置許可權才能蒐索到設備,需留意是否開啟了位置許可權。
  • 功能說明: 監聽蒐索到新設備的事件。

  • 參數及說明: function listener,蒐索到新設備的事件的監聽函數,參數object res如下:

    内容類型說明
    devicesArray.<Object>新蒐索到的設備清單
  • devices結構内容

    結構内容類型說明
    namestring藍牙設備名稱,某些設備可能沒有
    deviceIdstring藍牙設備id
    RSSInumber當前藍牙設備的信號強度,單位: dBm
    advertisDataArrayBuffer當前藍牙設備的廣播數據段中的ManufacturerData數據段
    advertisServiceUUIDsArray.<string>當前藍牙設備的廣播數據段中的ServiceUUIDs數據段
    localNamestring當前藍牙設備的廣播數據段中的LocalName數據段
    serviceDataObject當前藍牙設備的廣播數據段中的ServiceData數據段
    connectableboolean當前藍牙設備是否可連接(Android 8.0以下不支持返回該值)
  • 示例代碼:

js
// ArrayBuffer to 16 progress string example
function ab2hex(buffer) {
  var hexArr = Array.prototype.map.call(
    new Uint8Array(buffer),
    function(bit) {
      return ('00' + bit.toString(16)).slice(-2)
    }
  )
  return hexArr.join('');
}
wx.onBluetoothDeviceFound(function(res) {
  var devices = res.devices;
  console.log('new device list has founded')
  console.dir(devices)
  console.log(ab2hex(devices[0].advertisData))
})

offBluetoothDeviceFound

TIP

該API使用方法為: wx.offBluetoothDeviceFound()

  • 功能說明: 移除蒐索到新設備的事件的全部監聽函數。

  • 示例代碼:

js
wx.offBluetoothDeviceFound()

onBluetoothAdapterStateChange

TIP

該API使用方法為: wx.onBluetoothAdapterStateChange(function listener)

  • 功能說明: 監聽藍牙適配器狀態變化事件。

  • 參數及說明: Object res 參數, function listener, 藍牙適配器狀態變化事件的監聽函數。

    内容類型說明
    availableboolean藍牙適配器是否可用
    discoveringboolean藍牙適配器是否處於蒐索狀態
  • 示例代碼:

js
wx.onBluetoothAdapterStateChange(function (res) {
  console.log('adapterState changed, now is', res)
})

offBluetoothDeviceFound

TIP

該API使用方法為: wx.offBluetoothAdapterStateChange()

  • 功能說明: 移除藍牙適配器狀態變化事件的全部監聽函數。

  • 示例代碼:

js
wx.offBluetoothAdapterStateChange()

makeBluetoothPair

TIP

該API使用方法為: wx.makeBluetoothPair(Object object)

  • 功能說明: 藍牙配對接口,僅Android支持。 通常情况下(需要指定pin碼或者密碼時)系統會接管配對流程,直接調用wx.createBLEConnection即可。 該接口只應當在開發者不想讓用戶手動輸入pin碼且真機驗證確認可以正常生效情况下用。
  • 參數及說明: Object object。
    内容類型預設值必填說明
    deviceIdstring-藍牙設備id
    pinstring-pin碼,Base64格式
    timeoutnumber20000超時時間,單位: ms
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)

isBluetoothDevicePaired

TIP

該API使用方法為: wx.isBluetoothDevicePaired(Object object)

  • 功能說明: 査詢藍牙設備是否配對,僅Android支持。
  • 參數及說明: Object object。
    内容類型預設值必填說明
    deviceIdstring-藍牙設備id
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)