溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

openBluetoothAdapter初始化藍(lán)牙適配器的方法

發(fā)布時(shí)間:2022-03-08 09:43:27 來(lái)源:億速云 閱讀:352 作者:iii 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹“openBluetoothAdapter初始化藍(lán)牙適配器的方法”,在日常操作中,相信很多人在openBluetoothAdapter初始化藍(lán)牙適配器的方法問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”openBluetoothAdapter初始化藍(lán)牙適配器的方法”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

藍(lán)牙適配器接口


基礎(chǔ)庫(kù)版本 1.1.0 開(kāi)始支持,低版本需做兼容處理

iOS 微信客戶(hù)端 6.5.6 版本開(kāi)始支持,Android 客戶(hù)端暫不支持

wx.openBluetoothAdapter(OBJECT)


基礎(chǔ)庫(kù)版本 1.1.0 開(kāi)始支持,低版本需做兼容處理

初始化藍(lán)牙適配器

OBJECT參數(shù)說(shuō)明:

參數(shù) 類(lèi)型 必填 說(shuō)明
success Function 成功則返回成功初始化信息
fail Function 接口調(diào)用失敗的回調(diào)函數(shù)
complete Function 接口調(diào)用結(jié)束的回調(diào)函數(shù)(調(diào)用成功、失敗都會(huì)執(zhí)行)

示例代碼:

wx.openBluetoothAdapter({
  success: function (res) {console.log(res)
  }
})

Bug & Tip

  1. tip: 由于系統(tǒng)的問(wèn)題,目前僅在 mac 版的開(kāi)發(fā)工具上支持藍(lán)牙調(diào)試

  2. tip: 基礎(chǔ)庫(kù)版本 1.1.0 開(kāi)始支持,低版本需做兼容處理

wx.closeBluetoothAdapter(OBJECT)


基礎(chǔ)庫(kù)版本 1.1.0 開(kāi)始支持,低版本需做兼容處理

關(guān)閉藍(lán)牙模塊。調(diào)用該方法將斷開(kāi)所有已建立的鏈接并釋放系統(tǒng)資源

OBJECT參數(shù)說(shuō)明:

參數(shù) 類(lèi)型 必填 說(shuō)明
success Function 成功則返回成功關(guān)閉模塊信息
fail Function 接口調(diào)用失敗的回調(diào)函數(shù)
complete Function 接口調(diào)用結(jié)束的回調(diào)函數(shù)(調(diào)用成功、失敗都會(huì)執(zhí)行)

示例代碼:

wx.closeBluetoothAdapter({
  success: function (res) {console.log(res)
  }
})

wx.getBluetoothAdapterState(OBJECT)


基礎(chǔ)庫(kù)版本 1.1.0 開(kāi)始支持,低版本需做兼容處理

獲取本機(jī)藍(lán)牙適配器狀態(tài)

OBJECT參數(shù)說(shuō)明:

參數(shù) 類(lèi)型 必填 說(shuō)明
success Function 成功則返回本機(jī)藍(lán)牙適配器狀態(tài)
fail Function 接口調(diào)用失敗的回調(diào)函數(shù)
complete Function 接口調(diào)用結(jié)束的回調(diào)函數(shù)(調(diào)用成功、失敗都會(huì)執(zhí)行)

success返回參數(shù):

參數(shù) 類(lèi)型 說(shuō)明
discovering Boolean 是否正在搜索設(shè)備
available Boolean 藍(lán)牙適配器是否可用
errMsg String 成功:ok,錯(cuò)誤:詳細(xì)信息
示例代碼:
wx.getBluetoothAdapterState({
  success: function (res) {console.log(res)
  }
})

wx.onBluetoothAdapterStateChange(CALLBACK)


基礎(chǔ)庫(kù)版本 1.1.0 開(kāi)始支持,低版本需做兼容處理

監(jiān)聽(tīng)藍(lán)牙適配器狀態(tài)變化事件

CALLBACK參數(shù)說(shuō)明:

參數(shù) 類(lèi)型 說(shuō)明
available boolean 藍(lán)牙適配器是否可用
discovering boolean 藍(lán)牙適配器是否處于搜索狀態(tài)

示例代碼:

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

wx.startBluetoothDevicesDiscovery(OBJECT)


基礎(chǔ)庫(kù)版本 1.1.0 開(kāi)始支持,低版本需做兼容處理

開(kāi)始搜尋附近的藍(lán)牙外圍設(shè)備。注意,該操作比較耗費(fèi)系統(tǒng)資源,請(qǐng)?jiān)谒阉鞑⑦B接到設(shè)備后調(diào)用 stop 方法停止搜索。

OBJECT參數(shù)說(shuō)明:

參數(shù) 類(lèi)型 必填 說(shuō)明
services Array 藍(lán)牙設(shè)備主 service 的 uuid 列表
allowDuplicatesKey boolean 是否允許重復(fù)上報(bào)同一設(shè)備, 如果允許重復(fù)上報(bào),則onDeviceFound 方法會(huì)多次上報(bào)同一設(shè)備,但是 RSSI 值會(huì)有不同
interval integer 上報(bào)設(shè)備的間隔,默認(rèn)為0,意思是找到新設(shè)備立即上報(bào),否則根據(jù)傳入的間隔上報(bào)
success Function 成功則返回本機(jī)藍(lán)牙適配器狀態(tài)
fail Function 接口調(diào)用失敗的回調(diào)函數(shù)
complete Function 接口調(diào)用結(jié)束的回調(diào)函數(shù)(調(diào)用成功、失敗都會(huì)執(zhí)行)

services參數(shù)說(shuō)明:某些藍(lán)牙設(shè)備會(huì)廣播自己的主 service 的 uuid。如果這里傳入該數(shù)組,那么根據(jù)該 uuid 列表,只搜索有這個(gè)主服務(wù)的設(shè)備。

success返回參數(shù):

參數(shù) 類(lèi)型 說(shuō)明
errMsg string 成功:ok,錯(cuò)誤:詳細(xì)信息
isDiscovering boolean 當(dāng)前藍(lán)牙適配器是否處于搜索狀態(tài)

示例代碼:

// 以微信硬件平臺(tái)的藍(lán)牙智能燈為例,主服務(wù)的 UUID 是 FEE7。傳入這個(gè)參數(shù),只搜索主服務(wù) UUID 為 FEE7 的設(shè)備wx.startBluetoothDevicesDiscovery({
  services: ['FEE7'],
  success: function (res) {console.log(res)
  }
})

wx.stopBluetoothDevicesDiscovery(OBJECT)


基礎(chǔ)庫(kù)版本 1.1.0 開(kāi)始支持,低版本需做兼容處理

停止搜尋附近的藍(lán)牙外圍設(shè)備。請(qǐng)?jiān)诖_保找到需要連接的設(shè)備后調(diào)用該方法停止搜索。

OBJECT參數(shù)說(shuō)明:

參數(shù) 類(lèi)型 必填 說(shuō)明
success Function 成功則返回本機(jī)藍(lán)牙適配器狀態(tài)
fail Function 接口調(diào)用失敗的回調(diào)函數(shù)
complete Function 接口調(diào)用結(jié)束的回調(diào)函數(shù)(調(diào)用成功、失敗都會(huì)執(zhí)行)

success返回參數(shù):

參數(shù) 類(lèi)型 說(shuō)明
errMsg string 成功:ok,錯(cuò)誤:詳細(xì)信息

adapterState

藍(lán)牙適配器狀態(tài)信息

參數(shù) 類(lèi)型 說(shuō)明
discovering boolean 是否正在搜索設(shè)備
available boolean 藍(lán)牙適配器是否可用

示例代碼:

wx.stopBluetoothDevicesDiscovery({
  success: function (res) {console.log(res)
  }
})

wx.getBluetoothDevices(OBJECT)


基礎(chǔ)庫(kù)版本 1.1.0 開(kāi)始支持,低版本需做兼容處理

獲取所有已發(fā)現(xiàn)的藍(lán)牙設(shè)備,包括已經(jīng)和本機(jī)處于連接狀態(tài)的設(shè)備

OBJECT參數(shù)說(shuō)明:

參數(shù) 類(lèi)型 必填 說(shuō)明
success Function 成功則返回本機(jī)藍(lán)牙適配器狀態(tài)
fail Function 接口調(diào)用失敗的回調(diào)函數(shù)
complete Function 接口調(diào)用結(jié)束的回調(diào)函數(shù)(調(diào)用成功、失敗都會(huì)執(zhí)行)

success返回參數(shù):

參數(shù) 類(lèi)型 說(shuō)明
devices Array uuid 對(duì)應(yīng)的的已連接設(shè)備列表
errMsg string 成功:ok,錯(cuò)誤:詳細(xì)信息

device 對(duì)象

藍(lán)牙設(shè)備信息

參數(shù) 類(lèi)型 說(shuō)明
name string 藍(lán)牙設(shè)備名稱(chēng),某些設(shè)備可能沒(méi)有
deviceId string 用于區(qū)分設(shè)備的 id
RSSI int 當(dāng)前藍(lán)牙設(shè)備的信號(hào)強(qiáng)度
advertisData ArrayBuffer 當(dāng)前藍(lán)牙設(shè)備的廣播內(nèi)容(注意:vConsole 無(wú)法打印出 ArrayBuffer 類(lèi)型數(shù)據(jù))

示例代碼:

wx.getBluetoothDevices({
  success: function (res) {console.log(res)
  }
})

Bug & Tip

  1. tip: Mac系統(tǒng)可能無(wú)法獲取advertisDataRSSI,請(qǐng)使用真機(jī)調(diào)試

  2. tip: 開(kāi)發(fā)者工具和 Android 上獲取到的deviceId為設(shè)備 MAC 地址,iOS 上則為設(shè)備 uuid。因此deviceId不能硬編碼到代碼中

wx.getConnectedBluetoothDevices(OBJECT)


基礎(chǔ)庫(kù)版本 1.1.0 開(kāi)始支持,低版本需做兼容處理

根據(jù) uuid 獲取處于已連接狀態(tài)的設(shè)備

OBJECT參數(shù)說(shuō)明:

參數(shù) 類(lèi)型 必填 說(shuō)明
services Array 藍(lán)牙設(shè)備主 service 的 uuid 列表
success Function 成功則返回本機(jī)藍(lán)牙適配器狀態(tài)
fail Function 接口調(diào)用失敗的回調(diào)函數(shù)
complete Function 接口調(diào)用結(jié)束的回調(diào)函數(shù)(調(diào)用成功、失敗都會(huì)執(zhí)行)

success返回參數(shù):

參數(shù) 類(lèi)型 說(shuō)明
devices Array 搜索到的設(shè)備列表
errMsg string 成功:ok,錯(cuò)誤:詳細(xì)信息

device對(duì)象

藍(lán)牙設(shè)備信息

參數(shù) 類(lèi)型 說(shuō)明
name string 藍(lán)牙設(shè)備名稱(chēng),某些設(shè)備可能沒(méi)有
deviceId string 用于區(qū)分設(shè)備的 id

示例代碼:

wx.getConnectedBluetoothDevices({
  success: function (res) {console.log(res)
  }
})

Bug & Tip

  1. tip: 開(kāi)發(fā)者工具和 Android 上獲取到的deviceId為設(shè)備 MAC 地址,iOS 上則為設(shè)備 uuid。因此deviceId不能硬編碼到代碼中

wx.onBluetoothDeviceFound(CALLBACK)


基礎(chǔ)庫(kù)版本 1.1.0 開(kāi)始支持,低版本需做兼容處理

監(jiān)聽(tīng)尋找到新設(shè)備的事件

CALLBACK參數(shù)說(shuō)明:

參數(shù) 類(lèi)型 說(shuō)明
devices Array 新搜索到的設(shè)備列表

device對(duì)象

參數(shù) 類(lèi)型 說(shuō)明
deviceId string 藍(lán)牙設(shè)備 id,參考 device 對(duì)象
name string 藍(lán)牙設(shè)備名稱(chēng),參考 device 對(duì)象
RSSI int 當(dāng)前藍(lán)牙設(shè)備的信號(hào)強(qiáng)度
advertisData ArrayBuffer 當(dāng)前藍(lán)牙設(shè)備的廣播內(nèi)容(注意:vConsole 無(wú)法打印出 ArrayBuffer 類(lèi)型數(shù)據(jù))

示例代碼:

wx.onBluetoothDeviceFound(function(devices) {  console.log('new device list has founded')  console.dir(devices)
})

Bug & Tip

  1. tip: Mac系統(tǒng)可能無(wú)法獲取advertisDataRSSI,請(qǐng)使用真機(jī)調(diào)試

  2. tip: 開(kāi)發(fā)者工具和 Android 上獲取到的deviceId為設(shè)備 MAC 地址,iOS 上則為設(shè)備 uuid。因此deviceId不能硬編碼到代碼中

低功耗藍(lán)牙接口

wx.createBLEConnection(OBJECT)


基礎(chǔ)庫(kù)版本 1.1.0 開(kāi)始支持,低版本需做兼容處理

連接低功耗藍(lán)牙設(shè)備

OBJECT參數(shù)說(shuō)明:

參數(shù) 類(lèi)型 必填 說(shuō)明
deviceId string 藍(lán)牙設(shè)備 id,參考 getDevices 接口
success Function 成功則返回本機(jī)藍(lán)牙適配器狀態(tài)
fail Function 接口調(diào)用失敗的回調(diào)函數(shù)
complete Function 接口調(diào)用結(jié)束的回調(diào)函數(shù)(調(diào)用成功、失敗都會(huì)執(zhí)行)

success返回參數(shù):

參數(shù) 類(lèi)型 說(shuō)明
errMsg string 成功:ok,錯(cuò)誤:詳細(xì)信息

示例代碼:

wx.createBLEConnection({  // 這里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中獲取
  deviceId: deviceId,
  success: function (res) {console.log(res)
  }
})

wx.closeBLEConnection(OBJECT)


基礎(chǔ)庫(kù)版本 1.1.0 開(kāi)始支持,低版本需做兼容處理

斷開(kāi)與低功耗藍(lán)牙設(shè)備的連接

OBJECT參數(shù)說(shuō)明:

參數(shù) 類(lèi)型 必填 說(shuō)明
deviceId string 藍(lán)牙設(shè)備 id,參考 getDevices 接口
success Function 成功則返回本機(jī)藍(lán)牙適配器狀態(tài)
fail Function 接口調(diào)用失敗的回調(diào)函數(shù)
complete Function 接口調(diào)用結(jié)束的回調(diào)函數(shù)(調(diào)用成功、失敗都會(huì)執(zhí)行)

success返回參數(shù):

參數(shù) 類(lèi)型 說(shuō)明
errMsg string 成功:ok,錯(cuò)誤:詳細(xì)信息

示例代碼:

wx.closeBLEConnection({
  success: function (res) {console.log(res)
  }
})

wx.getBLEDeviceServices(OBJECT)


基礎(chǔ)庫(kù)版本 1.1.0 開(kāi)始支持,低版本需做兼容處理

獲取藍(lán)牙設(shè)備所有 service(服務(wù))

OBJECT參數(shù)說(shuō)明:

參數(shù) 類(lèi)型 必填 說(shuō)明
deviceId string 藍(lán)牙設(shè)備 id,參考 getDevices 接口
success Function 成功則返回本機(jī)藍(lán)牙適配器狀態(tài)
fail Function 接口調(diào)用失敗的回調(diào)函數(shù)
complete Function 接口調(diào)用結(jié)束的回調(diào)函數(shù)(調(diào)用成功、失敗都會(huì)執(zhí)行)

success返回參數(shù):

參數(shù) 類(lèi)型 說(shuō)明
services array 設(shè)備服務(wù)列表
errMsg string 成功:ok,錯(cuò)誤:詳細(xì)信息

service對(duì)象

藍(lán)牙設(shè)備service(服務(wù))信息

參數(shù) 類(lèi)型 說(shuō)明
uuid string 藍(lán)牙設(shè)備服務(wù)的 uuid
isPrimary boolean 該服務(wù)是否為主服務(wù)

示例代碼:

wx.getBLEDeviceServices({  // 這里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中獲取
  deviceId: deviceId,
  success: function (res) {console.log('device services:', res.services)
  }
})

wx.getBLEDeviceCharacteristics(OBJECT)


基礎(chǔ)庫(kù)版本 1.1.0 開(kāi)始支持,低版本需做兼容處理

獲取藍(lán)牙設(shè)備所有 characteristic(特征值)

OBJECT參數(shù)說(shuō)明:

參數(shù) 類(lèi)型 必填 說(shuō)明
deviceId string 藍(lán)牙設(shè)備 id,參考 device 對(duì)象
serviceId string 藍(lán)牙服務(wù) uuid
success Function 成功則返回本機(jī)藍(lán)牙適配器狀態(tài)
fail Function 接口調(diào)用失敗的回調(diào)函數(shù)
complete Function 接口調(diào)用結(jié)束的回調(diào)函數(shù)(調(diào)用成功、失敗都會(huì)執(zhí)行)

success返回參數(shù):

參數(shù) 類(lèi)型 說(shuō)明
characteristics array 設(shè)備特征值列表
errMsg string 成功:ok,錯(cuò)誤:詳細(xì)信息

characteristic對(duì)象

藍(lán)牙設(shè)備characteristic(特征值)信息

參數(shù) 類(lèi)型 說(shuō)明
uuid string 藍(lán)牙設(shè)備特征值的 uuid
properties object 該特征值支持的操作類(lèi)型

properties對(duì)象

參數(shù) 類(lèi)型 說(shuō)明
read boolean 該特征值是否支持 read 操作
write boolean 該特征值是否支持 write 操作
notify boolean 該特征值是否支持 notify 操作
indicate boolean 該特征值是否支持 indicate 操作

示例代碼:

wx.getBLEDeviceCharacteristics({  // 這里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中獲取
  deviceId: deviceId,  // 這里的 serviceId 需要在上面的 getBLEDeviceServices 接口中獲取
  serviceId: serviceId,
  success: function (res) {console.log('device getBLEDeviceCharacteristics:', res.characteristics)
  }
})

wx.readBLECharacteristicValue(OBJECT)


基礎(chǔ)庫(kù)版本 1.1.0 開(kāi)始支持,低版本需做兼容處理

讀取低功耗藍(lán)牙設(shè)備的特征值的二進(jìn)制數(shù)據(jù)值。注意:必須設(shè)備的特征值支持read才可以成功調(diào)用,具體參照 characteristic 的 properties 屬性

OBJECT參數(shù)說(shuō)明:

參數(shù) 類(lèi)型 必填 說(shuō)明
deviceId string 藍(lán)牙設(shè)備 id,參考 device 對(duì)象
serviceId string 藍(lán)牙特征值對(duì)應(yīng)服務(wù)的 uuid
characteristicId string 藍(lán)牙特征值的 uuid
success Function 成功則返回本機(jī)藍(lán)牙適配器狀態(tài)
fail Function 接口調(diào)用失敗的回調(diào)函數(shù)
complete Function 接口調(diào)用結(jié)束的回調(diào)函數(shù)(調(diào)用成功、失敗都會(huì)執(zhí)行)

success返回參數(shù):

參數(shù) 類(lèi)型 說(shuō)明
characteristic object 設(shè)備特征值信息
errMsg string 成功:ok,錯(cuò)誤:詳細(xì)信息

characteristic對(duì)象

藍(lán)牙設(shè)備characteristic(特征值)信息

參數(shù) 類(lèi)型 說(shuō)明
characteristicId string 藍(lán)牙設(shè)備特征值的 uuid
serviceId object 藍(lán)牙設(shè)備特征值對(duì)應(yīng)服務(wù)的 uuid
value ArrayBuffer 藍(lán)牙設(shè)備特征值對(duì)應(yīng)的二進(jìn)制值(注意:vConsole 無(wú)法打印出 ArrayBuffer 類(lèi)型數(shù)據(jù))

示例代碼:

// 必須在這里的回調(diào)才能獲取wx.onBLECharacteristicValueChange(function(characteristic) {  console.log('characteristic value comed:', characteristic)
})

wx.readBLECharacteristicValue({  // 這里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中獲取
  deviceId: deviceId,  // 這里的 serviceId 需要在上面的 getBLEDeviceServices 接口中獲取
  serviceId: serviceId,  // 這里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中獲取
  characteristicId: characteristicId,
  success: function (res) {console.log('readBLECharacteristicValue:', res.characteristic.value)
  }
})

Bug & Tip

  1. tip: 并行調(diào)用多次讀寫(xiě)接口存在讀寫(xiě)失敗的可能性。

  2. tip:read接口讀取到的信息需要在onBLECharacteristicValueChange方法注冊(cè)的回調(diào)中獲取。

wx.writeBLECharacteristicValue(OBJECT)


基礎(chǔ)庫(kù)版本 1.1.0 開(kāi)始支持,低版本需做兼容處理

向低功耗藍(lán)牙設(shè)備特征值中寫(xiě)入二進(jìn)制數(shù)據(jù)。注意:必須設(shè)備的特征值支持write才可以成功調(diào)用,具體參照 characteristic 的 properties 屬性

tips: 并行調(diào)用多次讀寫(xiě)接口存在讀寫(xiě)失敗的可能性

OBJECT參數(shù)說(shuō)明:

參數(shù) 類(lèi)型 必填 說(shuō)明
deviceId string 藍(lán)牙設(shè)備 id,參考 device 對(duì)象
serviceId string 藍(lán)牙特征值對(duì)應(yīng)服務(wù)的 uuid
characteristicId string 藍(lán)牙特征值的 uuid
value ArrayBuffer 藍(lán)牙設(shè)備特征值對(duì)應(yīng)的二進(jìn)制值(注意:vConsole 無(wú)法打印出 ArrayBuffer 類(lèi)型數(shù)據(jù))
success Function 成功則返回本機(jī)藍(lán)牙適配器狀態(tài)
fail Function 接口調(diào)用失敗的回調(diào)函數(shù)
complete Function 接口調(diào)用結(jié)束的回調(diào)函數(shù)(調(diào)用成功、失敗都會(huì)執(zhí)行)

success返回參數(shù):

參數(shù) 類(lèi)型 說(shuō)明
errMsg string 成功:ok,錯(cuò)誤:詳細(xì)信息

示例代碼:

// 這里的回調(diào)可以獲取到 write 導(dǎo)致的特征值改變wx.onBLECharacteristicValueChange(function(characteristic) {  console.log('characteristic value changed:', characteristic)
})// 向藍(lán)牙設(shè)備發(fā)送一個(gè)0x00的16進(jìn)制數(shù)據(jù)let buffer = new ArrayBuffer(1)let dataView = new DataView(buffer)
dataView.setUint8(0, 0)

wx.writeBLECharacteristicValue({  // 這里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中獲取
  deviceId: deviceId,  // 這里的 serviceId 需要在上面的 getBLEDeviceServices 接口中獲取
  serviceId: serviceId,  // 這里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中獲取
  characteristicId: characteristicId,  // 這里的value是ArrayBuffer類(lèi)型
  value: buffer,
  success: function (res) {console.log('writeBLECharacteristicValue success', res.errMsg)
  }
})

wx.notifyBLECharacteristicValueChanged(OBJECT)


基礎(chǔ)庫(kù)版本 1.1.0 開(kāi)始支持,低版本需做兼容處理

啟用低功耗藍(lán)牙設(shè)備特征值變化時(shí)的 notify 功能。注意:必須設(shè)備的特征值支持notify才可以成功調(diào)用,具體參照 characteristic 的 properties 屬性

另外,必須先啟用notify才能監(jiān)聽(tīng)到設(shè)備 characteristicValueChange 事件

OBJECT參數(shù)說(shuō)明:

參數(shù) 類(lèi)型 必填 說(shuō)明
deviceId string 藍(lán)牙設(shè)備 id,參考 device 對(duì)象
serviceId string 藍(lán)牙特征值對(duì)應(yīng)服務(wù)的 uuid
characteristicId string 藍(lán)牙特征值的 uuid
state boolean true: 啟用 notify; false: 停用 notify
success Function 成功則返回本機(jī)藍(lán)牙適配器狀態(tài)
fail Function 接口調(diào)用失敗的回調(diào)函數(shù)
complete Function 接口調(diào)用結(jié)束的回調(diào)函數(shù)(調(diào)用成功、失敗都會(huì)執(zhí)行)

success返回參數(shù):

參數(shù) 類(lèi)型 說(shuō)明
errMsg string 成功:ok,錯(cuò)誤:詳細(xì)信息

示例代碼:

wx.notifyBLECharacteristicValueChanged({
  state: true, // 啟用 notify 功能
  // 這里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中獲取
  deviceId: deviceId,  // 這里的 serviceId 需要在上面的 getBLEDeviceServices 接口中獲取
  serviceId: serviceId,  // 這里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中獲取
  characteristicId: characteristicId,
  success: function (res) {console.log('notifyBLECharacteristicValueChanged success', res.errMsg)
  }
})

wx.onBLEConnectionStateChanged(CALLBACK)


基礎(chǔ)庫(kù)版本 1.1.0 開(kāi)始支持,低版本需做兼容處理

監(jiān)聽(tīng)低功耗藍(lán)牙連接的錯(cuò)誤事件,包括設(shè)備丟失,連接異常斷開(kāi)等等。

CALLBACK參數(shù)說(shuō)明:

參數(shù) 類(lèi)型 說(shuō)明
deviceId string 藍(lán)牙設(shè)備 id,參考 device 對(duì)象
connected boolean 連接目前的狀態(tài)

示例代碼:

wx.onBLEConnectionStateChanged(function(res) {  // 該方法回調(diào)中可以用于處理連接意外斷開(kāi)等異常情況
  console.log(`device ${res.deviceId} state has changed, connected: ${res.connected}`)
})

wx.onBLECharacteristicValueChange(CALLBACK)


基礎(chǔ)庫(kù)版本 1.1.0 開(kāi)始支持,低版本需做兼容處理

監(jiān)聽(tīng)低功耗藍(lán)牙設(shè)備的特征值變化。必須先啟用notify接口才能接收到設(shè)備推送的notification。

CALLBACK參數(shù)說(shuō)明:

參數(shù) 類(lèi)型 說(shuō)明
deviceId string 藍(lán)牙設(shè)備 id,參考 device 對(duì)象
serviceId string 特征值所屬服務(wù) uuid
characteristicId string 特征值 uuid
value ArrayBuffer 特征值最新的值(注意:vConsole 無(wú)法打印出 ArrayBuffer 類(lèi)型數(shù)據(jù))

示例代碼:

wx.onBLECharacteristicValueChange(function(res) {  console.log(`characteristic ${res.characteristicId} has changed, now is ${res.value}`)
})

藍(lán)牙錯(cuò)誤碼(errCode)列表

錯(cuò)誤碼 說(shuō)明 備注
0 ok 正常
10000 not init 未初始化藍(lán)牙適配器
10001 not available 當(dāng)前藍(lán)牙適配器不可用
10002 no device 沒(méi)有找到指定設(shè)備
10003 connection fail 連接失敗
10004 no service 沒(méi)有找到指定服務(wù)
10005 no characteristic 沒(méi)有找到指定特征值
10006 no connection 當(dāng)前連接已斷開(kāi)
10007 property not support 當(dāng)前特征值不支持此操作
10008 system error 其余所有系統(tǒng)上報(bào)的異常
10009 system not support Android 系統(tǒng)特有,系統(tǒng)版本低于 4.3 不支持BLE
10010 no descriptor 沒(méi)有找到指定描述符

到此,關(guān)于“openBluetoothAdapter初始化藍(lán)牙適配器的方法”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI