接口聲明
{ "name": "system.bluetooth" }
導入模塊
import bluetooth from '@system.bluetooth' 或 var bluetooth = require("@system.bluetooth")
接口定義
bluetooth.openAdapter(OBJECT)
初始化藍牙模塊
參數:
參數名 | 類型 | 必填 | 說明 |
---|
operateAdapter | Boolean | 否 | 是否打開系統藍牙開關。設置為 true,在系統藍牙開關關閉的情況下會彈框提示是否打開。默認值 false。 |
success | Function | 否 | 成功回調。 |
fail | Function | 否 | 失敗回調。 |
complete | Function | 否 | 執(zhí)行結束后的回調。 |
示例:
bluetooth.openAdapter({
success: function() {
console.log('success')
},
fail: function(data, code) {
console.log(`handling fail, code = ${code}`)
},
complete: function() {
console.log('complete')
}
})
bluetooth.closeAdapter(OBJECT)
關閉藍牙模塊。調用該方法將斷開所有已建立的連接并釋放系統資源。建議在使用藍牙流程后,與 bluetooth.openAdapter 成對調用。
參數:
參數名 | 類型 | 必填 | 說明 |
---|
operateAdapter | Boolean | 否 | 是否關閉系統藍牙開關。設置為 true,調用時會關閉系統藍牙開關。默認值 false。 |
success | Function | 否 | 成功回調。 |
fail | Function | 否 | 失敗回調。 |
complete | Function | 否 | 執(zhí)行結束后的回調。 |
示例:
bluetooth.closeAdapter({
success: function() {
console.log('success')
},
fail: function(data, code) {
console.log(`handling fail, code = ${code}`)
},
complete: function() {
console.log('complete')
}
})
bluetooth.getAdapterState(OBJECT)
獲取本機藍牙適配器狀態(tài)。
OBJECT 參數:
參數名 | 類型 | 必填 | 說明 |
---|
success | Function | 否 | 成功回調。 |
fail | Function | 否 | 失敗回調。 |
complete | Function | 否 | 執(zhí)行結束后的回調 |
success 返回值:
參數名 | 類型 | 描述 |
---|
available | Boolean | 藍牙適配器是否可用 |
discovering | Boolean | 是否正在搜索設備 |
示例:
bluetooth.getAdapterState({
success: function(data) {
console.log(
`handling adapter state, available = ${data.available}, discovering = ${
data.discovering
}`
)
},
fail: function(data, code) {
console.log(`handling fail, code = ${code}`)
},
complete: function() {
console.log('complete')
}
})
bluetooth.onadapterstatechange = function(data)
監(jiān)聽藍牙適配器狀態(tài)變化事件
data 返回值:
參數名 | 類型 | 描述 |
---|
available | Boolean | 藍牙適配器是否可用 |
discovering | Boolean | 藍牙適配器是否處于搜索狀態(tài) |
示例:
bluetooth.onadapterstatechange = function(data) {
console.log('adapterState changed, now is', data.available)
}
bluetooth.startDevicesDiscovery(OBJECT)
開始搜尋附近的藍牙外圍設備。此操作比較耗費系統資源,請在搜索并連接到設備后調用 bluetooth.stopDevicesDiscovery 方法停止搜索。
OBJECT 參數:
參數名 | 類型 | 必填 | 說明 |
---|
services | String[] | 否 | 要搜索的主 service 的 uuid 列表。某些藍牙設備會廣播自己的主 service 的 uuid。如果設置此參數,則只搜索廣播包有對應 uuid 的主服務的藍牙設備。建議主要通過該參數過濾掉周邊不需要處理的其他藍牙設備。 |
allowDuplicatesKey | Boolean | 否 | 默認值為 false。是否允許重復上報同一設備。如果允許重復上報,則 bluetooth.ondevicefound 方法會多次上報同一設備,但是 RSSI 值會有不同。 |
interval | Number | 否 | 單位毫秒,默認值為 0。上報設備的間隔。0 表示找到新設備立即上報,其他數值根據傳入的間隔上報。 |
success | Function | 否 | 成功回調。 |
fail | Function | 否 | 失敗回調。 |
complete | Function | 否 | 執(zhí)行結束后的回調。 |
示例:
bluetooth.startDevicesDiscovery({
services: ['FEE7'],
success: function() {
console.log('success')
}
})
bluetooth.stopDevicesDiscovery(OBJECT)
停止搜尋附近的藍牙外圍設備。若已經找到需要的藍牙設備并不需要繼續(xù)搜索時,建議調用該接口停止藍牙搜索。
OBJECT 參數:
參數名 | 類型 | 必填 | 說明 |
---|
success | Function | 否 | 成功回調。 |
fail | Function | 否 | 失敗回調。 |
complete | Function | 否 | 執(zhí)行結束后的回調。 |
示例:
bluetooth.stopDevicesDiscovery({
success: function() {
console.log('success')
},
fail: function(data, code) {
console.log(`handling fail, code = ${code}`)
},
complete: function() {
console.log('complete')
}
})
bluetooth.getDevices(OBJECT)
獲取在藍牙模塊生效期間所有已發(fā)現的藍牙設備。包括已經和本機處于連接狀態(tài)的設備。
OBJECT 參數:
參數名 | 類型 | 必填 | 說明 |
---|
success | Function | 否 | 成功回調。 |
fail | Function | 否 | 失敗回調。 |
complete | Function | 否 | 執(zhí)行結束后的回調。 |
success 返回值:
參數名 | 類型 | 描述 |
---|
devices | Object[] | 藍牙模塊生效期間已發(fā)現的藍牙設備 |
devices 返回值:
參數名 | 類型 | 說明 |
---|
name | String | 藍牙設備名稱,某些設備可能沒有 |
deviceId | String | 用于區(qū)分設備的 id |
RSSI | Number | 當前藍牙設備的信號強度 |
advertisData | ArrayBuffer | 當前藍牙設備的廣播數據段中的 ManufacturerData 數據段 |
advertisServiceUUIDs | String[] | 當前藍牙設備的廣播數據段中的 ServiceUUIDs 數據段 |
localName | String | 當前藍牙設備的廣播數據段中的 LocalName 數據段 |
serviceData | Object | 當前藍牙設備的廣播數據段中的 ServiceData 數據段,key 為 uuid 的 String 值,value 為對應的 ServiceData 的 ArrayBuffer |
示例:
function ab2hex(buffer) {
var hexArr = Array.prototype.map.call(new Uint8Array(buffer), function(bit) {
return ('00' + bit.toString(16)).slice(-2)
})
return hexArr.join('')
}
bluetooth.getDevices({
success: function(data) {
console.log('get device list has founded')
data.devices.forEach(device => {
console.log(`handling devive:${JSON.stringify(device)}`)
console.log(`handling advertisData = ${ab2hex(device.advertisData)}`)
for (let key in device.serviceData) {
console.log(
`handling serviceData: uuid = ${key}, serviceData = ${ab2hex(
device.serviceData[key]
)}`
)
}
})
},
fail: function(data, code) {
console.log(`handling fail, code = ${code}`)
},
complete: function() {
console.log('complete')
}
})
bluetooth.ondevicefound = function(data)
監(jiān)聽尋找到新設備的事件
data 返回值:
參數名 | 類型 | 描述 |
---|
devices | Object[] | 新搜索到的設備列表,devices 返回值見 getDevices |
示例:
function ab2hex(buffer) {
var hexArr = Array.prototype.map.call(new Uint8Array(buffer), function(bit) {
return ('00' + bit.toString(16)).slice(-2)
})
return hexArr.join('')
}
bluetooth.ondevicefound = function(data) {
console.log('new device list has founded')
data.devices.forEach(device => {
console.log(`handling find new devive:${JSON.stringify(device)}`)
console.log(`handling advertisData = ${ab2hex(device.advertisData)}`)
for (let key in device.serviceData) {
console.log(
`handling serviceData: uuid = ${key}, serviceData = ${ab2hex(
device.serviceData[key]
)}`
)
}
})
}
bluetooth.getConnectedDevices(OBJECT)
根據 uuid 獲取處于已連接狀態(tài)的設備。
OBJECT 參數:
參數名 | 類型 | 必填 | 說明 |
---|
success | Function | 否 | 成功回調。 |
fail | Function | 否 | 失敗回調。 |
complete | Function | 否 | 執(zhí)行結束后的回調。 |
services | String[] | 是 | 藍牙設備主 service 的 uuid 列表 |
success 返回值:
參數名 | 類型 | 描述 |
---|
devices | Object[] | uuid 對應的的已連接設備列表 |
devices 返回值:
參數名 | 類型 | 說明 |
---|
name | String | 藍牙設備名稱,某些設備可能沒有 |
deviceId | String | 用于區(qū)分設備的 id |
示例:
bluetooth.getConnectedDevices({
success: function(data) {
console.log(data)
if (data.devices[0]) {
console.log(data.devices[0].name)
}
},
fail: function(data, code) {
console.log(`handling fail, code = ${code}`)
},
complete: function() {
console.log('complete')
}
})
bluetooth.createBLEConnection(OBJECT)
連接低功耗藍牙設備。若快應用有搜索過某個藍牙設備,并成功建立連接,可直接傳入之前搜索獲取的 deviceId 直接嘗試連接該設備,無需進行搜索操作。
OBJECT 參數:
參數名 | 類型 | 必填 | 說明 |
---|
deviceId | String | 是 | 用于區(qū)分設備的 id |
timeout | Number | 否 | 超時時間,單位 ms,不填表示不會超時 |
success | Function | 否 | 成功回調。 |
fail | Function | 否 | 失敗回調。 |
complete | Function | 否 | 執(zhí)行結束后的回調。 |
示例:
bluetooth.createBLEConnection({
deviceId: deviceId,
success: function() {
console.log('success')
},
fail: function(data, code) {
console.log(`handling fail, code = ${code}`)
},
complete: function() {
console.log('complete')
}
})
bluetooth.closeBLEConnection (OBJECT)
斷開與低功耗藍牙設備的連接。
OBJECT 參數:
參數名 | 類型 | 必填 | 說明 |
---|
deviceId | String | 是 | 用于區(qū)分設備的 id |
success | Function | 否 | 成功回調。 |
fail | Function | 否 | 失敗回調。 |
complete | Function | 否 | 執(zhí)行結束后的回調。 |
示例:
bluetooth.closeBLEConnection({
deviceId: deviceId,
success: function() {
console.log('success')
},
fail: function(data, code) {
console.log(`handling fail, code = ${code}`)
},
complete: function() {
console.log('complete')
}
})
bluetooth.getBLEDeviceServices(OBJECT)
獲取藍牙設備所有服務(service)。
OBJECT 參數:
參數名 | 類型 | 必填 | 說明 |
---|
deviceId | String | 是 | 藍牙設備 id |
success | Function | 否 | 成功回調。 |
fail | Function | 否 | 失敗回調。 |
complete | Function | 否 | 執(zhí)行結束后的回調。 |
success 返回值:
參數名 | 類型 | 描述 |
---|
services | Object[] | 設備服務列表 |
services 返回值:
參數名 | 類型 | 說明 |
---|
uuid | String | 藍牙設備服務的 uuid |
isPrimary | Boolean | 該服務是否為主服務 |
示例:
bluetooth.getBLEDeviceServices({
deviceId: deviceId,
success: function(data) {
data.services.forEach(service => {
console.log(
`handling device services: uuid = ${service.uuid}, isPrimary = ${
service.isPrimary
}`
)
})
},
fail: function(data, code) {
console.log(`handling fail, code = ${code}`)
},
complete: function() {
console.log('complete')
}
})
bluetooth.getBLEDeviceCharacteristics(OBJECT)
獲取藍牙設備某個服務中所有特征值(characteristic)。
OBJECT 參數:
參數名 | 類型 | 必填 | 說明 |
---|
success | Function | 否 | 成功回調。 |
fail | Function | 否 | 失敗回調。 |
complete | Function | 否 | 執(zhí)行結束后的回調。 |
deviceId | String | 是 | 藍牙設備 id |
serviceId | String | 是 | 藍牙服務 uuid,需要使用 getBLEDeviceServices 獲取 |
success 返回值:
參數名 | 類型 | 描述 |
---|
characteristics | Object[] | 設備服務列表 |
characteristics 返回值:
參數名 | 類型 | 說明 |
---|
uuid | String | 藍牙設備特征值的 uuid |
properties | Object | 該特征值支持的操作類型 |
properties 返回值:
參數名 | 類型 | 說明 |
---|
read | Boolean | 該特征值是否支持 read 操作 |
write | Boolean | 該特征值是否支持 write 操作 |
notify | Boolean | 該特征值是否支持 notify 操作 |
indicate | Boolean | 該特征值是否支持 indicate 操作 |
示例:
bluetooth.getBLEDeviceCharacteristics({
deviceId: deviceId,
serviceId: serviceId,
success: function(data) {
data.characteristics.forEach(characteristic => {
console.log(
`handling device characteristic : uuid = ${
characteristic.uuid
}, can read = ${characteristic.properties.read}`
)
})
},
fail: function(data, code) {
console.log(`handling fail, code = ${code}`)
},
complete: function() {
console.log('complete')
}
})
bluetooth.readBLECharacteristicValue(OBJECT)
讀取低功耗藍牙設備的特征值的二進制數據值。注意:必須設備的特征值支持 read 才可以成功調用。
OBJECT 參數:
參數名 | 類型 | 必填 | 說明 |
---|
deviceId | String | 是 | 藍牙設備 id |
serviceId | String | 是 | 藍牙特征值對應服務的 uuid |
characteristicId | String | 是 | 藍牙特征值的 uuid |
success | Function | 否 | 成功回調。 |
fail | Function | 否 | 失敗回調。 |
complete | Function | 否 | 執(zhí)行結束后的回調。 |
示例:
bluetooth.readBLECharacteristicValue({
// 這里的 deviceId 需要已經通過 createBLEConnection 與對應設備建立鏈接
deviceId: deviceId,
// 這里的 serviceId 需要在 getBLEDeviceServices 接口中獲取
serviceId: serviceId,
// 這里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中獲取
characteristicId: characteristicId,
success: function() {
// 執(zhí)行操作成功,讀取的值會在onblecharacteristicvaluechange 接口中上報
console.log('success')
}
})
bluetooth.writeBLECharacteristicValue(OBJECT)
向低功耗藍牙設備特征值中寫入二進制數據。注意:必須設備的特征值支持 write 才可以成功調用。
OBJECT 參數:
參數名 | 類型 | 必填 | 說明 |
---|
deviceId | String | 是 | 藍牙設備 id |
serviceId | String | 是 | 藍牙特征值對應服務的 uuid |
characteristicId | String | 是 | 藍牙特征值的 uuid |
value | ArrayBuffer | 是 | 藍牙設備特征值對應的二進制值 |
success | Function | 否 | 成功回調。 |
fail | Function | 否 | 失敗回調。 |
complete | Function | 否 | 執(zhí)行結束后的回調。 |
示例:
bluetooth.writeBLECharacteristicValue({
// 這里的 deviceId 需要在 getBluetoothDevices 或 onBluetoothDeviceFound接口中獲取
deviceId: deviceId,
// 這里的 serviceId 需要在 getBLEDeviceServices 接口中獲取
serviceId: serviceId,
// 這里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中獲取
characteristicId: characteristicId,
// 這里的value是ArrayBuffer類型
value: buffer,
success: function() {
console.log('success')
},
fail: function(data, code) {
console.log(`handling fail, code = ${code}`)
},
complete: function() {
console.log('complete')
}
})
bluetooth.notifyBLECharacteristicValueChange(OBJECT)
啟用低功耗藍牙設備特征值變化時的 notify 功能,訂閱特征值。注意:必須設備的特征值支持 notify 或者 indicate 才可以成功調用。另外,必須先啟用 notifyBLECharacteristicValueChange 才能監(jiān)聽到設備 characteristicValueChange 事件
OBJECT 參數:
參數名 | 類型 | 必填 | 說明 |
---|
deviceId | String | 是 | 藍牙設備 id |
serviceId | String | 是 | 藍牙特征值對應服務的 uuid |
characteristicId | String | 是 | 藍牙特征值的 uuid |
state | Boolean | 是 | 是否啟用 notify |
success | Function | 否 | 成功回調。 |
fail | Function | 否 | 失敗回調。 |
complete | Function | 否 | 執(zhí)行結束后的回調。 |
示例:
bluetooth.notifyBLECharacteristicValueChange({
// 啟用 notify 功能
state: true,
// 這里的 deviceId 需要已經通過 createBLEConnection 與對應設備建立鏈接
deviceId: deviceId,
// 這里的 serviceId 需要在 getBLEDeviceServices 接口中獲取
serviceId: serviceId,
// 這里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中獲取
characteristicId: characteristicId,
success: function() {
console.log('success')
},
fail: function(data, code) {
console.log(`handling fail, code = ${code}`)
},
complete: function() {
console.log('complete')
}
})
bluetooth.onblecharacteristicvaluechange = function(data)
監(jiān)聽低功耗藍牙設備的特征值變化。必須先啟用 notifyBLECharacteristicValueChange 接口才能接收到設備推送的 notification。
data 返回值:
參數名 | 類型 | 描述 |
---|
deviceId | String | 藍牙設備 id |
serviceId | String | 藍牙特征值對應服務的 uuid |
characteristicId | String | 藍牙特征值的 uuid |
value | ArrayBuffer | 特征值最新的值 |
示例:
function ab2hex(buffer) {
var hexArr = Array.prototype.map.call(new Uint8Array(buffer), function(bit) {
return ('00' + bit.toString(16)).slice(-2)
})
return hexArr.join('')
}
bluetooth.onblecharacteristicvaluechange = function(data) {
console.log(
`handling characteristic value change: deviceId = ${
data.deviceId
}, serviceId = ${data.serviceId}, characteristicId = ${
data.characteristicId
}, value = ${ab2hex(data.value)}`
)
}
bluetooth.onbleconnectionstatechange = function(data)
監(jiān)聽低功耗藍牙連接狀態(tài)的改變事件。包括開發(fā)者主動連接或斷開連接,設備丟失,連接異常斷開等等
data 返回值:
參數名 | 類型 | 描述 |
---|
deviceId | String | 藍牙設備 id |
connected | Boolean | 是否處于已連接狀態(tài) |
示例:
bluetooth.onbleconnectionstatechange = function(data) {
console.log(
`handling device state change: deviceId = ${data.deviceId}, connected = ${
data.connected
}`
)
}
狀態(tài)碼
錯誤碼 | 錯誤信息 | 描述 |
---|
0 | ok | 正常 |
10000 | not init | 未初始化藍牙適配器 |
10001 | not available | 當前系統藍牙未打開 |
10002 | no device | 沒有找到指定設備 |
10003 | connection fail | 連接失敗 |
10004 | no service | 沒有找到指定服務 |
10005 | no characteristic | 沒有找到指定特征值 |
10006 | no connection | 當前連接已斷開 |
10007 | property not support | 當前特征值不支持此操作 |
10008 | system error | 其余所有系統上報的異常 |
10009 | system not support | 系統版本低于 4.3 不支持 BLE |
10010 1060+ | location not turned on | 定位開關未打開 |
后臺運行限制
禁止使用。后臺運行詳細用法參見 后臺運行腳本。
更多建議: