藍牙

2024-01-23 16:48 更新

藍牙模塊提供了基礎的傳統(tǒng)藍牙能力以及BLE的掃描、廣播等功能。

說明

本模塊首批接口從API version 9開始支持。后續(xù)版本的新增接口,采用上角標單獨標記接口的起始版本。

導入模塊

  1. import bluetoothManager from '@ohos.bluetoothManager';

bluetoothManager.enableBluetooth

enableBluetooth(): void

開啟藍牙。

需要權限:ohos.permission.DISCOVER_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900099

Operation failed.

示例:

  1. try {
  2. bluetoothManager.enableBluetooth();
  3. } catch (err) {
  4. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  5. }

bluetoothManager.disableBluetooth

disableBluetooth(): void

關閉藍牙。

需要權限:ohos.permission.DISCOVER_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900099

Operation failed.

示例:

  1. try {
  2. bluetoothManager.disableBluetooth();
  3. } catch (err) {
  4. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  5. }

bluetoothManager.getLocalName

getLocalName(): string

獲取藍牙本地設備名稱。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

返回值:

類型

說明

string

藍牙本地設備名稱。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900099

Operation failed.

示例:

  1. try {
  2. let localName = bluetoothManager.getLocalName();
  3. } catch (err) {
  4. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  5. }

bluetoothManager.getState

getState(): BluetoothState

獲取藍牙開關狀態(tài)。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

返回值:

類型

說明

BluetoothState

表示藍牙開關狀態(tài)。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900099

Operation failed.

示例:

  1. try {
  2. let state = bluetoothManager.getState();
  3. } catch (err) {
  4. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  5. }

bluetoothManager.getBtConnectionState

getBtConnectionState(): ProfileConnectionState

獲取藍牙設備的Profile連接狀態(tài)。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

返回值:

類型

說明

ProfileConnectionState

表示藍牙設備的Profile連接狀態(tài)。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900003

Bluetooth switch is off.

2900099

Operation failed.

示例:

  1. try {
  2. let connectionState = bluetoothManager.getBtConnectionState();
  3. } catch (err) {
  4. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  5. }

bluetoothManager.setLocalName

setLocalName(name: string): void

設置藍牙本地設備名稱。

需要權限:ohos.permission.DISCOVER_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

name

string

要設置的藍牙名稱,最大長度為248字節(jié)數。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900003

Bluetooth switch is off.

2900099

Operation failed.

示例:

  1. try {
  2. bluetoothManager.setLocalName('device_name');
  3. } catch (err) {
  4. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  5. }

bluetoothManager.pairDevice

pairDevice(deviceId: string): void

發(fā)起藍牙配對。

需要權限:ohos.permission.DISCOVER_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

deviceId

string

表示配對的遠端設備地址,例如:"XX:XX:XX:XX:XX:XX"。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900003

Bluetooth switch is off.

2900099

Operation failed.

示例:

  1. try {
  2. // 實際的地址可由掃描流程獲取
  3. bluetoothManager.pairDevice("XX:XX:XX:XX:XX:XX");
  4. } catch (err) {
  5. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  6. }

bluetoothManager.getProfileConnectionState

getProfileConnectionState(profileId: ProfileId): ProfileConnectionState

獲取profile的連接狀態(tài)。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

ProfileId

profileId

表示profile的枚舉值,例如:PROFILE_A2DP_SOURCE。

返回值:

類型

說明

ProfileConnectionState

profile的連接狀態(tài)。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900003

Bluetooth switch is off.

2900004

Profile is not supported.

2900099

Operation failed.

示例:

  1. try {
  2. let result = bluetoothManager.getProfileConnectionState(bluetoothManager.ProfileId.PROFILE_A2DP_SOURCE);
  3. } catch (err) {
  4. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  5. }

bluetoothManager.getRemoteDeviceName

getRemoteDeviceName(deviceId: string): string

獲取對端藍牙設備的名稱。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

deviceId

string

表示遠程設備的地址,例如:"XX:XX:XX:XX:XX:XX"。

返回值:

類型

說明

string

以字符串格式返回設備名稱。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900003

Bluetooth switch is off.

2900099

Operation failed.

示例:

  1. try {
  2. let remoteDeviceName = bluetoothManager.getRemoteDeviceName("XX:XX:XX:XX:XX:XX");
  3. } catch (err) {
  4. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  5. }

bluetoothManager.getRemoteDeviceClass

getRemoteDeviceClass(deviceId: string): DeviceClass

獲取對端藍牙設備的類別。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

deviceId

string

表示遠程設備的地址,例如:"XX:XX:XX:XX:XX:XX"。

返回值:

類型

說明

DeviceClass

遠程設備的類別。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900003

Bluetooth switch is off.

2900099

Operation failed.

示例:

  1. try {
  2. let remoteDeviceClass = bluetoothManager.getRemoteDeviceClass("XX:XX:XX:XX:XX:XX");
  3. } catch (err) {
  4. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  5. }

bluetoothManager.getPairedDevices

getPairedDevices(): Array<string>

獲取藍牙配對列表。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

返回值:

類型

說明

Array<string>

已配對藍牙設備的地址列表。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900003

Bluetooth switch is off.

2900099

Operation failed.

示例:

  1. try {
  2. let devices = bluetoothManager.getPairedDevices();
  3. } catch (err) {
  4. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  5. }

bluetoothManager.setBluetoothScanMode

setBluetoothScanMode(mode: ScanMode, duration: number): void

設置藍牙掃描模式,可以被遠端設備發(fā)現(xiàn)。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

mode

ScanMode

藍牙掃描模式。

duration

number

設備可被發(fā)現(xiàn)的持續(xù)時間,單位為毫秒;設置為0則持續(xù)可發(fā)現(xiàn)。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900003

Bluetooth switch is off.

2900099

Operation failed.

示例:

  1. try {
  2. // 設置為可連接可發(fā)現(xiàn)才可被遠端設備掃描到,可以連接。
  3. bluetoothManager.setBluetoothScanMode(bluetoothManager.ScanMode.SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE, 100);
  4. } catch (err) {
  5. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  6. }

bluetoothManager.getBluetoothScanMode

getBluetoothScanMode(): ScanMode

獲取藍牙掃描模式。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

返回值:

類型

說明

ScanMode

藍牙掃描模式。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900003

Bluetooth switch is off.

2900099

Operation failed.

示例:

  1. try {
  2. let scanMode = bluetoothManager.getBluetoothScanMode();
  3. } catch (err) {
  4. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  5. }

bluetoothManager.startBluetoothDiscovery

startBluetoothDiscovery(): void

開啟藍牙掃描,可以發(fā)現(xiàn)遠端設備。

需要權限:ohos.permission.DISCOVER_BLUETOOTH 和 ohos.permission.LOCATION 和 ohos.permission.APPROXIMATELY_LOCATION

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900003

Bluetooth switch is off.

2900099

Operation failed.

示例:

  1. let deviceId;
  2. function onReceiveEvent(data) {
  3. deviceId = data;
  4. }
  5. try {
  6. bluetoothManager.on('bluetoothDeviceFind', onReceiveEvent);
  7. bluetoothManager.startBluetoothDiscovery();
  8. } catch (err) {
  9. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  10. }

bluetoothManager.stopBluetoothDiscovery

stopBluetoothDiscovery(): void

關閉藍牙掃描。

需要權限:ohos.permission.DISCOVER_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900003

Bluetooth switch is off.

2900099

Operation failed.

示例:

  1. try {
  2. bluetoothManager.stopBluetoothDiscovery();
  3. } catch (err) {
  4. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  5. }

bluetoothManager.setDevicePairingConfirmation

setDevicePairingConfirmation(device: string, accept: boolean): void

設置設備配對請求確認。

需要權限:ohos.permission.MANAGE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

device

string

表示遠端設備地址,例如:"XX:XX:XX:XX:XX:XX"。

accept

boolean

接受配對請求設置為true,否則設置為false。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900003

Bluetooth switch is off.

2900099

Operation failed.

示例:

  1. try {
  2. // 訂閱“pinRequired”配對請求事件,收到遠端配對請求后設置配對確認
  3. function onReceivePinRequiredEvent(data) { // data為配對請求的入參,配對請求參數
  4. console.info('pin required = '+ JSON.stringify(data));
  5. bluetoothManager.setDevicePairingConfirmation(data.deviceId, true);
  6. }
  7. bluetoothManager.on("pinRequired", onReceivePinRequiredEvent);
  8. } catch (err) {
  9. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  10. }

bluetoothManager.on('bluetoothDeviceFind')

on(type: "bluetoothDeviceFind", callback: Callback<Array<string>>): void

訂閱藍牙設備發(fā)現(xiàn)上報事件。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

type

string

填寫"bluetoothDeviceFind"字符串,表示藍牙設備發(fā)現(xiàn)事件。

callback

Callback<Array<string>>

表示回調函數的入參,發(fā)現(xiàn)的設備集合。回調函數由用戶創(chuàng)建通過該接口注冊。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900099

Operation failed.

示例:

  1. function onReceiveEvent(data) { // data為藍牙設備地址集合
  2. console.info('bluetooth device find = '+ JSON.stringify(data));
  3. }
  4. try {
  5. bluetoothManager.on('bluetoothDeviceFind', onReceiveEvent);
  6. } catch (err) {
  7. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  8. }

bluetoothManager.off('bluetoothDeviceFind')

off(type: "bluetoothDeviceFind", callback?: Callback<Array<string>>): void

取消訂閱藍牙設備發(fā)現(xiàn)上報事件。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

type

string

填寫"bluetoothDeviceFind"字符串,表示藍牙設備發(fā)現(xiàn)事件。

callback

Callback<Array<string>>

表示取消訂閱藍牙設備發(fā)現(xiàn)事件上報。不填該參數則取消訂閱該type對應的所有回調。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900099

Operation failed.

示例:

  1. function onReceiveEvent(data) {
  2. console.info('bluetooth device find = '+ JSON.stringify(data));
  3. }
  4. try {
  5. bluetoothManager.on('bluetoothDeviceFind', onReceiveEvent);
  6. bluetoothManager.off('bluetoothDeviceFind', onReceiveEvent);
  7. } catch (err) {
  8. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  9. }

bluetoothManager.on('pinRequired')

on(type: "pinRequired", callback: Callback<PinRequiredParam>): void

訂閱遠端藍牙設備的配對請求事件。

需要權限:ohos.permission.DISCOVER_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

type

string

填寫"pinRequired"字符串,表示配對請求事件。

callback

Callback<PinRequiredParam>

表示回調函數的入參,配對請求?;卣{函數由用戶創(chuàng)建通過該接口注冊。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900099

Operation failed.

示例:

  1. function onReceiveEvent(data) { // data為配對請求參數
  2. console.info('pin required = '+ JSON.stringify(data));
  3. }
  4. try {
  5. bluetoothManager.on('pinRequired', onReceiveEvent);
  6. } catch (err) {
  7. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  8. }

bluetoothManager.off('pinRequired')

off(type: "pinRequired", callback?: Callback<PinRequiredParam>): void

取消訂閱遠端藍牙設備的配對請求事件。

需要權限:ohos.permission.DISCOVER_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

type

string

填寫"pinRequired"字符串,表示配對請求事件。

callback

Callback<PinRequiredParam>

表示取消訂閱藍牙配對請求事件上報,入參為配對請求參數。不填該參數則取消訂閱該type對應的所有回調。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900099

Operation failed.

示例:

  1. function onReceiveEvent(data) {
  2. console.info('pin required = '+ JSON.stringify(data));
  3. }
  4. try {
  5. bluetoothManager.on('pinRequired', onReceiveEvent);
  6. bluetoothManager.off('pinRequired', onReceiveEvent);
  7. } catch (err) {
  8. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  9. }

bluetoothManager.on('bondStateChange')

on(type: "bondStateChange", callback: Callback<BondStateParam>): void

訂閱藍牙配對狀態(tài)改變事件。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

type

string

填寫"bondStateChange"字符串,表示藍牙配對狀態(tài)改變事件。

callback

Callback<BondStateParam>

表示回調函數的入參,配對的狀態(tài)。回調函數由用戶創(chuàng)建通過該接口注冊。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900099

Operation failed.

示例:

  1. function onReceiveEvent(data) { // data為回調函數入參,表示配對的狀態(tài)
  2. console.info('pair state = '+ JSON.stringify(data));
  3. }
  4. try {
  5. bluetoothManager.on('bondStateChange', onReceiveEvent);
  6. } catch (err) {
  7. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  8. }

bluetoothManager.off('bondStateChange')

off(type: "bondStateChange", callback?: Callback<BondStateParam>): void

取消訂閱藍牙配對狀態(tài)改變事件。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

type

string

填寫"bondStateChange"字符串,表示藍牙配對狀態(tài)改變事件。

callback

Callback<BondStateParam>

表示取消訂閱藍牙配對狀態(tài)改變事件上報。不填該參數則取消訂閱該type對應的所有回調。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900099

Operation failed.

示例:

  1. function onReceiveEvent(data) {
  2. console.info('bond state = '+ JSON.stringify(data));
  3. }
  4. try {
  5. bluetoothManager.on('bondStateChange', onReceiveEvent);
  6. bluetoothManager.off('bondStateChange', onReceiveEvent);
  7. } catch (err) {
  8. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  9. }

bluetoothManager.on('stateChange')

on(type: "stateChange", callback: Callback<BluetoothState>): void

訂閱藍牙連接狀態(tài)改變事件。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

type

string

填寫"stateChange"字符串,表示藍牙狀態(tài)改變事件。

callback

Callback<BluetoothState>

表示回調函數的入參,藍牙狀態(tài)?;卣{函數由用戶創(chuàng)建通過該接口注冊。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼

錯誤碼ID

錯誤信息

2900099

Operation failed.

示例:

  1. function onReceiveEvent(data) {
  2. console.info('bluetooth state = '+ JSON.stringify(data));
  3. }
  4. try {
  5. bluetoothManager.on('stateChange', onReceiveEvent);
  6. } catch (err) {
  7. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  8. }

bluetoothManager.off('stateChange')

off(type: "stateChange", callback?: Callback<BluetoothState>): void

取消訂閱藍牙連接狀態(tài)改變事件。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

type

string

填寫"stateChange"字符串,表示藍牙狀態(tài)改變事件。

callback

Callback<BluetoothState>

表示取消訂閱藍牙狀態(tài)改變事件上報。不填該參數則取消訂閱該type對應的所有回調。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900099

Operation failed.

示例:

  1. function onReceiveEvent(data) {
  2. console.info('bluetooth state = '+ JSON.stringify(data));
  3. }
  4. try {
  5. bluetoothManager.on('stateChange', onReceiveEvent);
  6. bluetoothManager.off('stateChange', onReceiveEvent);
  7. } catch (err) {
  8. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  9. }

bluetoothManager.sppListen

sppListen(name: string, option: SppOption, callback: AsyncCallback<number>): void

創(chuàng)建一個服務端監(jiān)聽Socket。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

name

string

服務的名稱。

option

SppOption

spp監(jiān)聽配置參數。

callback

AsyncCallback<number>

表示回調函數的入參,服務端Socket的id。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900003

Bluetooth switch is off.

2900004

Profile is not supported.

2900099

Operation failed.

示例:

  1. let serverNumber = -1;
  2. function serverSocket(code, number) {
  3. console.log('bluetooth error code: ' + code.code);
  4. if (code.code == 0) {
  5. console.log('bluetooth serverSocket Number: ' + number);
  6. serverNumber = number;
  7. }
  8. }
  9. let sppOption = {uuid: '00001810-0000-1000-8000-00805F9B34FB', secure: false, type: 0};
  10. try {
  11. bluetoothManager.sppListen('server1', sppOption, serverSocket);
  12. } catch (err) {
  13. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  14. }

bluetoothManager.sppAccept

sppAccept(serverSocket: number, callback: AsyncCallback<number>): void

服務端監(jiān)聽socket等待客戶端連接。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

serverSocket

number

服務端socket的id。

callback

AsyncCallback<number>

表示回調函數的入參,客戶端socket的id。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900003

Bluetooth switch is off.

2900004

Profile is not supported.

2900099

Operation failed.

示例:

  1. let serverNumber = -1;
  2. function serverSocket(code, number) {
  3. console.log('bluetooth error code: ' + code.code);
  4. if (code.code == 0) {
  5. console.log('bluetooth serverSocket Number: ' + number);
  6. serverNumber = number;
  7. }
  8. }
  9. let clientNumber = -1;
  10. function acceptClientSocket(code, number) {
  11. console.log('bluetooth error code: ' + code.code);
  12. if (code.code == 0) {
  13. console.log('bluetooth clientSocket Number: ' + number);
  14. // 獲取的clientNumber用作服務端后續(xù)讀/寫操作socket的id。
  15. clientNumber = number;
  16. }
  17. }
  18. try {
  19. bluetoothManager.sppAccept(serverNumber, acceptClientSocket);
  20. } catch (err) {
  21. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  22. }

bluetoothManager.sppConnect

sppConnect(device: string, option: SppOption, callback: AsyncCallback<number>): void

客戶端向遠端設備發(fā)起spp連接。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

device

string

對端設備地址,例如:"XX:XX:XX:XX:XX:XX"。

option

SppOption

spp客戶端連接配置參數。

callback

AsyncCallback<number>

表示回調函數的入參,客戶端socket的id。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900003

Bluetooth switch is off.

2900004

Profile is not supported.

2900099

Operation failed.

示例:

  1. let clientNumber = -1;
  2. function clientSocket(code, number) {
  3. if (code.code != 0) {
  4. return;
  5. }
  6. console.log('bluetooth serverSocket Number: ' + number);
  7. // 獲取的clientNumber用作客戶端后續(xù)讀/寫操作socket的id。
  8. clientNumber = number;
  9. }
  10. let sppOption = {uuid: '00001810-0000-1000-8000-00805F9B34FB', secure: false, type: 0};
  11. try {
  12. bluetoothManager.sppConnect('XX:XX:XX:XX:XX:XX', sppOption, clientSocket);
  13. } catch (err) {
  14. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  15. }

bluetoothManager.sppCloseServerSocket

sppCloseServerSocket(socket: number): void

關閉服務端監(jiān)聽Socket,入參socket由sppListen接口返回。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

socket

number

服務端監(jiān)聽socket的id。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900099

Operation failed.

示例:

  1. let serverNumber = -1;
  2. function serverSocket(code, number) {
  3. console.log('bluetooth error code: ' + code.code);
  4. if (code.code == 0) {
  5. console.log('bluetooth serverSocket Number: ' + number);
  6. serverNumber = number;
  7. }
  8. }
  9. try {
  10. bluetoothManager.sppCloseServerSocket(serverNumber);
  11. } catch (err) {
  12. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  13. }

bluetoothManager.sppCloseClientSocket

sppCloseClientSocket(socket: number): void

關閉客戶端socket,入參socket由sppAccept或sppConnect接口獲取。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

參數名

類型

必填

說明

socket

number

客戶端socket的id。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900099

Operation failed.

示例:

  1. let clientNumber = -1;
  2. function clientSocket(code, number) {
  3. if (code.code != 0) {
  4. return;
  5. }
  6. console.log('bluetooth serverSocket Number: ' + number);
  7. // 獲取的clientNumber用作客戶端后續(xù)讀/寫操作socket的id。
  8. clientNumber = number;
  9. }
  10. try {
  11. bluetoothManager.sppCloseClientSocket(clientNumber);
  12. } catch (err) {
  13. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  14. }

bluetoothManager.sppWrite

sppWrite(clientSocket: number, data: ArrayBuffer): void

通過socket向遠端發(fā)送數據,入參clientSocket由sppAccept或sppConnect接口獲取 。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

clientSocket

number

客戶端socket的id。

data

ArrayBuffer

寫入的數據。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2901054

IO error.

2900099

Operation failed.

示例:

  1. let clientNumber = -1;
  2. function clientSocket(code, number) {
  3. if (code.code != 0) {
  4. return;
  5. }
  6. console.log('bluetooth serverSocket Number: ' + number);
  7. // 獲取的clientNumber用作客戶端后續(xù)讀/寫操作socket的id。
  8. clientNumber = number;
  9. }
  10. let arrayBuffer = new ArrayBuffer(8);
  11. let data = new Uint8Array(arrayBuffer);
  12. data[0] = 123;
  13. try {
  14. bluetoothManager.sppWrite(clientNumber, arrayBuffer);
  15. } catch (err) {
  16. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  17. }

bluetoothManager.on('sppRead')

on(type: "sppRead", clientSocket: number, callback: Callback<ArrayBuffer>): void

訂閱spp讀請求事件,入參clientSocket由sppAccept或sppConnect接口獲取。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

type

string

填寫"sppRead"字符串,表示spp讀請求事件。

clientSocket

number

客戶端socket的id。

callback

Callback<ArrayBuffer>

表示回調函數的入參,讀取到的數據。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2901054

IO error.

2900099

Operation failed.

示例:

  1. let clientNumber = -1;
  2. function clientSocket(code, number) {
  3. if (code.code != 0) {
  4. return;
  5. }
  6. console.log('bluetooth serverSocket Number: ' + number);
  7. // 獲取的clientNumber用作客戶端后續(xù)讀/寫操作socket的id。
  8. clientNumber = number;
  9. }
  10. function dataRead(dataBuffer) {
  11. let data = new Uint8Array(dataBuffer);
  12. console.log('bluetooth data is: ' + data[0]);
  13. }
  14. try {
  15. bluetoothManager.on('sppRead', clientNumber, dataRead);
  16. } catch (err) {
  17. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  18. }

bluetoothManager.off('sppRead')

off(type: "sppRead", clientSocket: number, callback?: Callback<ArrayBuffer>): void

取消訂閱spp讀請求事件,入參clientSocket由sppAccept或sppConnect接口獲取。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

type

string

填寫"sppRead"字符串,表示spp讀請求事件。

clientSocket

number

客戶端Socket的id。

callback

Callback<ArrayBuffer>

表示取消訂閱spp讀請求事件上報。不填該參數則取消訂閱該type對應的所有回調。

示例:

  1. let clientNumber = -1;
  2. function clientSocket(code, number) {
  3. if (code.code != 0) {
  4. return;
  5. }
  6. console.log('bluetooth serverSocket Number: ' + number);
  7. // 獲取的clientNumber用作客戶端后續(xù)讀/寫操作socket的id。
  8. clientNumber = number;
  9. }
  10. try {
  11. bluetoothManager.off('sppRead', clientNumber);
  12. } catch (err) {
  13. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  14. }

bluetoothManager.getProfileInstance

getProfileInstance(profileId: ProfileId): A2dpSourceProfile | HandsFreeAudioGatewayProfile | HidHostProfile | PanProfile

通過ProfileId,獲取profile的對象實例,API9新增了HidHostProfile,PanProfile。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

profileId

ProfileId

表示profile的枚舉值,例如:PROFILE_A2DP_SOURCE。

返回值:

類型

說明

A2dpSourceProfile或 HandsFreeAudioGatewayProfileHidHostProfilePanProfile

對應的profile的對象實例,當前支持A2dpSourceProfile/HandsFreeAudioGatewayProfile/HidHostProfile/PanProfile。

示例:

  1. try {
  2. let hidHost = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_HID_HOST);
  3. } catch (err) {
  4. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  5. }

bluetoothManager.BLE

bluetoothManager.BLE.createGattServer

createGattServer(): GattServer

創(chuàng)建一個可使用的GattServer實例。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

返回值:

類型

說明

GattServer

server端類,使用server端方法之前需要創(chuàng)建該類的實例進行操作。

示例:

  1. let gattServer = bluetoothManager.BLE.createGattServer();

bluetoothManager.BLE.createGattClientDevice

createGattClientDevice(deviceId: string): GattClientDevice

創(chuàng)建一個可使用的GattClientDevice實例。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

deviceId

string

對端設備地址, 例如:"XX:XX:XX:XX:XX:XX"。

返回值:

類型

說明

GattClientDevice

client端類,使用client端方法之前需要創(chuàng)建該類的實例進行操作。

示例:

  1. try {
  2. let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
  3. } catch (err) {
  4. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  5. }

bluetoothManager.BLE.getConnectedBLEDevices

getConnectedBLEDevices(): Array<string>

獲取和當前設備連接的BLE設備。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

返回值:

類型

說明

Array<string>

返回當前設備作為Server端時連接BLE設備地址集合。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900003

Bluetooth switch is off.

2900099

Operation failed.

示例:

  1. try {
  2. let result = bluetoothManager.BLE.getConnectedBLEDevices();
  3. } catch (err) {
  4. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  5. }

bluetoothManager.BLE.startBLEScan

startBLEScan(filters: Array<ScanFilter>, options?: ScanOptions): void

發(fā)起B(yǎng)LE掃描流程。

需要權限:ohos.permission.DISCOVER_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH 和 ohos.permission.LOCATION 和 ohos.permission.APPROXIMATELY_LOCATION

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

filters

Array<ScanFilter>

表示掃描結果過濾策略集合,如果不使用過濾的方式,該參數設置為null。

options

ScanOptions

表示掃描的參數配置,可選參數。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900003

Bluetooth switch is off.

2900099

Operation failed.

示例:

  1. function onReceiveEvent(data) {
  2. console.info('BLE scan device find result = '+ JSON.stringify(data));
  3. }
  4. try {
  5. bluetoothManager.BLE.on("BLEDeviceFind", onReceiveEvent);
  6. bluetoothManager.BLE.startBLEScan(
  7. [{
  8. deviceId:"XX:XX:XX:XX:XX:XX",
  9. name:"test",
  10. serviceUuid:"00001888-0000-1000-8000-00805f9b34fb"
  11. }],
  12. {
  13. interval: 500,
  14. dutyMode: bluetoothManager.ScanDuty.SCAN_MODE_LOW_POWER,
  15. matchMode: bluetoothManager.MatchMode.MATCH_MODE_AGGRESSIVE,
  16. }
  17. );
  18. } catch (err) {
  19. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  20. }

bluetoothManager.BLE.stopBLEScan

stopBLEScan(): void

停止BLE掃描流程。

需要權限:ohos.permission.DISCOVER_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900003

Bluetooth switch is off.

2900099

Operation failed.

示例:

  1. try {
  2. bluetoothManager.BLE.stopBLEScan();
  3. } catch (err) {
  4. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  5. }

bluetoothManager.BLE.on('BLEDeviceFind')

on(type: "BLEDeviceFind", callback: Callback<Array<ScanResult>>): void

訂閱BLE設備發(fā)現(xiàn)上報事件。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

type

string

填寫"BLEDeviceFind"字符串,表示BLE設備發(fā)現(xiàn)事件。

callback

Callback<Array<ScanResult>>

表示回調函數的入參,發(fā)現(xiàn)的設備集合。回調函數由用戶創(chuàng)建通過該接口注冊。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼

錯誤碼ID

錯誤信息

2900099

Operation failed.

示例:

  1. function onReceiveEvent(data) {
  2. console.info('bluetooth device find = '+ JSON.stringify(data));
  3. }
  4. try {
  5. bluetoothManager.BLE.on('BLEDeviceFind', onReceiveEvent);
  6. } catch (err) {
  7. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  8. }

bluetoothManager.BLE.off('BLEDeviceFind')

off(type: "BLEDeviceFind", callback?: Callback<Array<ScanResult>>): void

取消訂閱BLE設備發(fā)現(xiàn)上報事件。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

type

string

填寫"BLEDeviceFind"字符串,表示BLE設備發(fā)現(xiàn)事件。

callback

Callback<Array<ScanResult>>

表示取消訂閱BLE設備發(fā)現(xiàn)事件上報。不填該參數則取消訂閱該type對應的所有回調。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900099

Operation failed.

示例:

  1. function onReceiveEvent(data) {
  2. console.info('bluetooth device find = '+ JSON.stringify(data));
  3. }
  4. try {
  5. bluetoothManager.BLE.on('BLEDeviceFind', onReceiveEvent);
  6. bluetoothManager.BLE.off('BLEDeviceFind', onReceiveEvent);
  7. } catch (err) {
  8. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  9. }

BaseProfile

profile基類。

getConnectionDevices

getConnectionDevices(): Array<string>

獲取已連接設備列表。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

返回值:

類型

說明

Array<string>

返回已連接設備的地址列表。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900003

Bluetooth switch is off.

2900004

Profile is not supported.

2900099

Operation failed.

示例:

  1. try {
  2. let a2dpSrc = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_A2DP_SOURCE) as bluetoothManager.A2dpSourceProfile;
  3. let retArray = a2dpSrc.getConnectionDevices();
  4. } catch (err) {
  5. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  6. }

getDeviceState

getDeviceState(device: string): ProfileConnectionState

獲取設備profile的連接狀態(tài)。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

device

string

遠端設備地址。

返回值:

類型

說明

ProfileConnectionState

返回profile的連接狀態(tài)。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900003

Bluetooth switch is off.

2900004

Profile is not supported.

2900099

Operation failed.

示例:

  1. try {
  2. let a2dpSrc = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_A2DP_SOURCE) as bluetoothManager.A2dpSourceProfile;
  3. let ret = a2dpSrc.getDeviceState('XX:XX:XX:XX:XX:XX');
  4. } catch (err) {
  5. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  6. }

A2dpSourceProfile

使用A2dpSourceProfile方法之前需要創(chuàng)建該類的實例進行操作,通過getProfile()方法構造此實例。

connect

connect(device: string): void

發(fā)起設備的A2dp服務連接請求。

需要權限:ohos.permission.DISCOVER_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

device

string

遠端設備地址。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900003

Bluetooth switch is off.

2900004

Profile is not supported.

2900099

Operation failed.

示例:

  1. try {
  2. let a2dpSrc = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_A2DP_SOURCE) as bluetoothManager.A2dpSourceProfile;
  3. a2dpSrc.connect('XX:XX:XX:XX:XX:XX');
  4. } catch (err) {
  5. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  6. }

disconnect

disconnect(device: string): void

斷開設備的a2dp服務連接。

需要權限:ohos.permission.DISCOVER_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

device

string

遠端設備地址。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900003

Bluetooth switch is off.

2900004

Profile is not supported.

2900099

Operation failed.

示例:

  1. try {
  2. let a2dpSrc = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_A2DP_SOURCE) as bluetoothManager.A2dpSourceProfile;
  3. a2dpSrc.disconnect('XX:XX:XX:XX:XX:XX');
  4. } catch (err) {
  5. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  6. }

on('connectionStateChange')

on(type: "connectionStateChange", callback: Callback<StateChangeParam>): void

訂閱a2dp連接狀態(tài)變化事件。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

type

string

填寫"connectionStateChange"字符串,表示連接狀態(tài)變化事件。

callback

Callback<StateChangeParam>

表示回調函數的入參。

返回值:

示例:

  1. function onReceiveEvent(data) {
  2. console.info('a2dp state = '+ JSON.stringify(data));
  3. }
  4. let a2dpSrc = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_A2DP_SOURCE) as bluetoothManager.A2dpSourceProfile;
  5. a2dpSrc.on('connectionStateChange', onReceiveEvent);

off('connectionStateChange')

off(type: "connectionStateChange", callback?: Callback<StateChangeParam>): void

取消訂閱a2dp連接狀態(tài)變化事件。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

type

string

填寫"connectionStateChange"字符串,表示連接狀態(tài)變化事件。

callback

Callback<StateChangeParam>

表示回調函數的入參。

返回值:

示例:

  1. function onReceiveEvent(data) {
  2. console.info('a2dp state = '+ JSON.stringify(data));
  3. }
  4. let a2dpSrc = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_A2DP_SOURCE) as bluetoothManager.A2dpSourceProfile;
  5. a2dpSrc.on('connectionStateChange', onReceiveEvent);
  6. a2dpSrc.off('connectionStateChange', onReceiveEvent);

getPlayingState

getPlayingState(device: string): PlayingState

獲取設備的播放狀態(tài)。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

device

string

遠端設備地址。

返回值:

類型

說明

PlayingState

遠端設備的播放狀態(tài)。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900003

Bluetooth switch is off.

2900004

Profile is not supported.

2900099

Operation failed.

示例:

  1. try {
  2. let a2dpSrc = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_A2DP_SOURCE) as bluetoothManager.A2dpSourceProfile;
  3. let state = a2dpSrc.getPlayingState('XX:XX:XX:XX:XX:XX');
  4. } catch (err) {
  5. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  6. }

HandsFreeAudioGatewayProfile

使用HandsFreeAudioGatewayProfile方法之前需要創(chuàng)建該類的實例進行操作,通過getProfile()方法構造此實例。

connect

connect(device: string): void

連接設備的HFP服務。

需要權限:ohos.permission.DISCOVER_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

device

string

遠端設備地址。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900003

Bluetooth switch is off.

2900004

Profile is not supported.

2900099

Operation failed.

示例:

  1. try {
  2. let hfpAg = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_HANDS_FREE_AUDIO_GATEWAY) as bluetoothManager.HandsFreeAudioGatewayProfile;
  3. hfpAg.connect('XX:XX:XX:XX:XX:XX');
  4. } catch (err) {
  5. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  6. }

disconnect

disconnect(device: string): void

斷開連接設備的HFP服務。

需要權限:ohos.permission.DISCOVER_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

device

string

遠端設備地址。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900003

Bluetooth switch is off.

2900004

Profile is not supported.

2900099

Operation failed.

示例:

  1. try {
  2. let hfpAg = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_HANDS_FREE_AUDIO_GATEWAY) as bluetoothManager.HandsFreeAudioGatewayProfile;
  3. hfpAg.disconnect('XX:XX:XX:XX:XX:XX');
  4. } catch (err) {
  5. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  6. }

on('connectionStateChange')

on(type: "connectionStateChange", callback: Callback<StateChangeParam>): void

訂閱HFP連接狀態(tài)變化事件。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

type

string

填寫"connectionStateChange"字符串,表示連接狀態(tài)變化事件。

callback

Callback<StateChangeParam>

表示回調函數的入參。

示例:

  1. function onReceiveEvent(data) {
  2. console.info('hfp state = '+ JSON.stringify(data));
  3. }
  4. let hfpAg = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_HANDS_FREE_AUDIO_GATEWAY) as
  5. bluetoothManager.HandsFreeAudioGatewayProfile;
  6. hfpAg.on('connectionStateChange', onReceiveEvent);

off('connectionStateChange')

off(type: "connectionStateChange", callback?: Callback<StateChangeParam>): void

取消訂閱HFP連接狀態(tài)變化事件。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

type

string

填寫"connectionStateChange"字符串,表示連接狀態(tài)變化事件。

callback

Callback<StateChangeParam>

表示回調函數的入參。

示例:

  1. function onReceiveEvent(data) {
  2. console.info('hfp state = '+ JSON.stringify(data));
  3. }
  4. let hfpAg = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_HANDS_FREE_AUDIO_GATEWAY) as
  5. bluetoothManager.HandsFreeAudioGatewayProfile;
  6. hfpAg.on('connectionStateChange', onReceiveEvent);
  7. hfpAg.off('connectionStateChange', onReceiveEvent);

HidHostProfile

使用HidHostProfile方法之前需要創(chuàng)建該類的實例進行操作,通過getProfile()方法構造此實例。

on('connectionStateChange')

on(type: "connectionStateChange", callback: Callback<StateChangeParam>): void

訂閱HidHost連接狀態(tài)變化事件。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

type

string

填寫"connectionStateChange"字符串,表示連接狀態(tài)變化事件。

callback

Callback<StateChangeParam>

表示回調函數的入參。

示例:

  1. function onReceiveEvent(data) {
  2. console.info('hidHost state = '+ JSON.stringify(data));
  3. }
  4. let hidHost = bluetoothManager.getProfileInst(bluetoothManager.ProfileId.PROFILE_HID_HOST) as bluetoothManager.HidHostProfile;
  5. hidHost.on('connectionStateChange', onReceiveEvent);

off('connectionStateChange')

off(type: "connectionStateChange", callback?: Callback<StateChangeParam>): void

取消訂閱HidHost連接狀態(tài)變化事件。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

type

string

填寫"connectionStateChange"字符串,表示連接狀態(tài)變化事件。

callback

Callback<StateChangeParam>

表示回調函數的入參。

示例:

  1. function onReceiveEvent(data) {
  2. console.info('hidHost state = '+ JSON.stringify(data));
  3. }
  4. let hidHost = bluetoothManager.getProfileInst(bluetoothManager.ProfileId.PROFILE_HID_HOST) as bluetoothManager.HidHostProfile;
  5. hidHost.on('connectionStateChange', onReceiveEvent);
  6. hidHost.off('connectionStateChange', onReceiveEvent);

PanProfile

使用PanProfile方法之前需要創(chuàng)建該類的實例進行操作,通過getProfile()方法構造此實例。

on('connectionStateChange')

on(type: "connectionStateChange", callback: Callback<StateChangeParam>): void

訂閱Pan連接狀態(tài)變化事件。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

type

string

填寫"connectionStateChange"字符串,表示連接狀態(tài)變化事件。

callback

Callback<StateChangeParam>

表示回調函數的入參。

示例:

  1. function onReceiveEvent(data) {
  2. console.info('pan state = '+ JSON.stringify(data));
  3. }
  4. let panProfile = bluetoothManager.getProfileInst(bluetoothManager.ProfileId.PROFILE_PAN_NETWORK) as bluetoothManager.PanProfile;
  5. panProfile.on('connectionStateChange', onReceiveEvent);

off('connectionStateChange')

off(type: "connectionStateChange", callback?: Callback<StateChangeParam>): void

取消訂閱Pan連接狀態(tài)變化事件。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

type

string

填寫"connectionStateChange"字符串,表示連接狀態(tài)變化事件。

callback

Callback<StateChangeParam>

表示回調函數的入參。

示例:

  1. function onReceiveEvent(data) {
  2. console.info('pan state = '+ JSON.stringify(data));
  3. }
  4. let panProfile = bluetoothManager.getProfileInst(bluetoothManager.ProfileId.PROFILE_PAN_NETWORK) as bluetoothManager.PanProfile;
  5. panProfile.on('connectionStateChange', onReceiveEvent);
  6. panProfile.off('connectionStateChange', onReceiveEvent);

GattServer

server端類,使用server端方法之前需要創(chuàng)建該類的實例進行操作,通過createGattServer()方法構造此實例。

startAdvertising

startAdvertising(setting: AdvertiseSetting, advData: AdvertiseData, advResponse?: AdvertiseData): void

開始發(fā)送BLE廣播。

需要權限:ohos.permission.DISCOVER_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

setting

AdvertiseSetting

BLE廣播的相關參數。

advData

AdvertiseData

BLE廣播包內容。

advResponse

AdvertiseData

BLE回復掃描請求回復響應。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900003

Bluetooth switch is off.

2900099

Operation failed.

示例:

  1. let manufactureValueBuffer = new Uint8Array(4);
  2. manufactureValueBuffer[0] = 1;
  3. manufactureValueBuffer[1] = 2;
  4. manufactureValueBuffer[2] = 3;
  5. manufactureValueBuffer[3] = 4;
  6. let serviceValueBuffer = new Uint8Array(4);
  7. serviceValueBuffer[0] = 4;
  8. serviceValueBuffer[1] = 6;
  9. serviceValueBuffer[2] = 7;
  10. serviceValueBuffer[3] = 8;
  11. console.info('manufactureValueBuffer = '+ JSON.stringify(manufactureValueBuffer));
  12. console.info('serviceValueBuffer = '+ JSON.stringify(serviceValueBuffer));
  13. let gattServer = bluetoothManager.BLE.createGattServer();
  14. try {
  15. gattServer.startAdvertising({
  16. interval:150,
  17. txPower:60,
  18. connectable:true,
  19. },{
  20. serviceUuids:["00001888-0000-1000-8000-00805f9b34fb"],
  21. manufactureData:[{
  22. manufactureId:4567,
  23. manufactureValue:manufactureValueBuffer.buffer
  24. }],
  25. serviceData:[{
  26. serviceUuid:"00001888-0000-1000-8000-00805f9b34fb",
  27. serviceValue:serviceValueBuffer.buffer
  28. }],
  29. },{
  30. serviceUuids:["00001889-0000-1000-8000-00805f9b34fb"],
  31. manufactureData:[{
  32. manufactureId:1789,
  33. manufactureValue:manufactureValueBuffer.buffer
  34. }],
  35. serviceData:[{
  36. serviceUuid:"00001889-0000-1000-8000-00805f9b34fb",
  37. serviceValue:serviceValueBuffer.buffer
  38. }],
  39. });
  40. } catch (err) {
  41. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  42. }

stopAdvertising

stopAdvertising(): void

停止發(fā)送BLE廣播。

需要權限:ohos.permission.DISCOVER_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900003

Bluetooth switch is off.

2900099

Operation failed.

示例:

  1. let server = bluetoothManager.BLE.createGattServer();
  2. try {
  3. server.stopAdvertising();
  4. } catch (err) {
  5. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  6. }

addService

addService(service: GattService): void

server端添加服務。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

service

GattService

服務端的service數據。BLE廣播的相關參數

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900003

Bluetooth switch is off.

2900099

Operation failed.

示例:

  1. // 創(chuàng)建descriptors
  2. let descriptors = [];
  3. let arrayBuffer = new ArrayBuffer(8);
  4. let descV = new Uint8Array(arrayBuffer);
  5. descV[0] = 11;
  6. let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  7. characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
  8. descriptorUuid: '00002902-0000-1000-8000-00805F9B34FB', descriptorValue: arrayBuffer};
  9. descriptors[0] = descriptor;
  10. // 創(chuàng)建characteristics
  11. let characteristics = [];
  12. let arrayBufferC = new ArrayBuffer(8);
  13. let cccV = new Uint8Array(arrayBufferC);
  14. cccV[0] = 1;
  15. let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  16. characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', characteristicValue: arrayBufferC, descriptors:descriptors};
  17. let characteristicN = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  18. characteristicUuid: '00001821-0000-1000-8000-00805F9B34FB', characteristicValue: arrayBufferC, descriptors:descriptors};
  19. characteristics[0] = characteristic;
  20. // 創(chuàng)建gattService
  21. let gattService = {serviceUuid:'00001810-0000-1000-8000-00805F9B34FB', isPrimary: true, characteristics:characteristics, includeServices:[]};
  22. let gattServer = bluetoothManager.BLE.createGattServer();
  23. try {
  24. gattServer.addService(gattService);
  25. } catch (err) {
  26. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  27. }

removeService

removeService(serviceUuid: string): void

刪除已添加的服務。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

serviceUuid

string

service的UUID,例如“00001810-0000-1000-8000-00805F9B34FB”。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900003

Bluetooth switch is off.

2900004

Profile is not supported.

2900099

Operation failed.

示例:

  1. let server = bluetoothManager.BLE.createGattServer();
  2. try {
  3. server.removeService('00001810-0000-1000-8000-00805F9B34FB');
  4. } catch (err) {
  5. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  6. }

close

close(): void

關閉服務端功能,去注冊server在協(xié)議棧的注冊,調用該接口后GattServer實例將不能再使用。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900003

Bluetooth switch is off.

2900099

Operation failed.

示例:

  1. let server = bluetoothManager.BLE.createGattServer();
  2. try {
  3. server.close();
  4. } catch (err) {
  5. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  6. }

notifyCharacteristicChanged

notifyCharacteristicChanged(deviceId: string, notifyCharacteristic: NotifyCharacteristic): void

server端特征值發(fā)生變化時,主動通知已連接的client設備。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

deviceId

string

接收通知的client端設備地址,例如“XX:XX:XX:XX:XX:XX”。

notifyCharacteristic

NotifyCharacteristic

通知的特征值數據。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900003

Bluetooth switch is off.

2900099

Operation failed.

示例:

  1. // 創(chuàng)建descriptors
  2. let descriptors = [];
  3. let arrayBuffer = new ArrayBuffer(8);
  4. let descV = new Uint8Array(arrayBuffer);
  5. descV[0] = 11;
  6. let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  7. characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
  8. descriptorUuid: '00002902-0000-1000-8000-00805F9B34FB', descriptorValue: arrayBuffer};
  9. descriptors[0] = descriptor;
  10. let arrayBufferC = new ArrayBuffer(8);
  11. let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  12. characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', characteristicValue: arrayBufferC, descriptors:descriptors};
  13. let notifyCharacteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  14. characteristicUuid: '00001821-0000-1000-8000-00805F9B34FB', characteristicValue: characteristic.characteristicValue, confirm: false};
  15. let server = bluetoothManager.BLE.createGattServer();
  16. try {
  17. server.notifyCharacteristicChanged('XX:XX:XX:XX:XX:XX', notifyCharacteristic);
  18. } catch (err) {
  19. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  20. }

sendResponse

sendResponse(serverResponse: ServerResponse): void

server端回復client端的讀寫請求。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

serverResponse

ServerResponse

server端回復的響應數據。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900003

Bluetooth switch is off.

2900099

Operation failed.

示例:

  1. /* send response */
  2. let arrayBufferCCC = new ArrayBuffer(8);
  3. let cccValue = new Uint8Array(arrayBufferCCC);
  4. cccValue[0] = 1123;
  5. let serverResponse = {
  6. "deviceId": "XX:XX:XX:XX:XX:XX",
  7. "transId": 0,
  8. "status": 0,
  9. "offset": 0,
  10. "value": arrayBufferCCC,
  11. };
  12. let gattServer = bluetoothManager.BLE.createGattServer();
  13. try {
  14. gattServer.sendResponse(serverResponse);
  15. } catch (err) {
  16. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  17. }

on('characteristicRead')

on(type: "characteristicRead", callback: Callback<CharacteristicReadRequest>): void

server端訂閱特征值讀請求事件。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

type

string

填寫"characteristicRead"字符串,表示特征值讀請求事件。

callback

Callback<CharacteristicReadRequest>

表示回調函數的入參,client端發(fā)送的讀請求數據。

示例:

  1. let arrayBufferCCC = new ArrayBuffer(8);
  2. let cccValue = new Uint8Array(arrayBufferCCC);
  3. cccValue[0] = 1123;
  4. function ReadCharacteristicReq(CharacteristicReadRequest) {
  5. let deviceId = CharacteristicReadRequest.deviceId;
  6. let transId = CharacteristicReadRequest.transId;
  7. let offset = CharacteristicReadRequest.offset;
  8. let characteristicUuid = CharacteristicReadRequest.characteristicUuid;
  9. let serverResponse = {deviceId: deviceId, transId: transId, status: 0, offset: offset, value:arrayBufferCCC};
  10. try {
  11. gattServer.sendResponse(serverResponse);
  12. } catch (err) {
  13. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  14. }
  15. }
  16. let gattServer = bluetoothManager.BLE.createGattServer();
  17. gattServer.on("characteristicRead", ReadCharacteristicReq);

off('characteristicRead')

off(type: "characteristicRead", callback?: Callback<CharacteristicReadRequest>): void

server端取消訂閱特征值讀請求事件。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

type

string

填寫"characteristicRead"字符串,表示特征值讀請求事件。

callback

Callback<CharacteristicReadRequest>

表示取消訂閱特征值讀請求事件上報。不填該參數則取消訂閱該type對應的所有回調。

示例:

  1. let gattServer = bluetoothManager.BLE.createGattServer();
  2. gattServer.off("characteristicRead");

on('characteristicWrite')

on(type: "characteristicWrite", callback: Callback<CharacteristicWriteRequest>): void

server端訂閱特征值寫請求事件。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

type

string

填寫"characteristicWrite"字符串,表示特征值寫請求事件。

callback

Callback<CharacteristicWriteRequest>

表示回調函數的入參,client端發(fā)送的寫請求數據。

示例:

  1. let arrayBufferCCC = new ArrayBuffer(8);
  2. let cccValue = new Uint8Array(arrayBufferCCC);
  3. function WriteCharacteristicReq(CharacteristicWriteRequest) {
  4. let deviceId = CharacteristicWriteRequest.deviceId;
  5. let transId = CharacteristicWriteRequest.transId;
  6. let offset = CharacteristicWriteRequest.offset;
  7. let isPrep = CharacteristicWriteRequest.isPrep;
  8. let needRsp = CharacteristicWriteRequest.needRsp;
  9. let value = new Uint8Array(CharacteristicWriteRequest.value);
  10. let characteristicUuid = CharacteristicWriteRequest.characteristicUuid;
  11. cccValue[0] = value[0];
  12. let serverResponse = {deviceId: deviceId, transId: transId, status: 0, offset: offset, value:arrayBufferCCC};
  13. try {
  14. gattServer.sendResponse(serverResponse);
  15. } catch (err) {
  16. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  17. }
  18. }
  19. let gattServer = bluetoothManager.BLE.createGattServer();
  20. gattServer.on("characteristicWrite", WriteCharacteristicReq);

off('characteristicWrite')

off(type: "characteristicWrite", callback?: Callback<CharacteristicWriteRequest>): void

server端取消訂閱特征值寫請求事件。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

type

string

填寫"characteristicWrite"字符串,表示特征值寫請求事件。

callback

Callback<CharacteristicWriteRequest>

表示取消訂閱特征值寫請求事件上報。不填該參數則取消訂閱該type對應的所有回調。

示例:

  1. let gattServer = bluetoothManager.BLE.createGattServer();
  2. gattServer.off("characteristicWrite");

on('descriptorRead')

on(type: "descriptorRead", callback: Callback<DescriptorReadRequest>): void

server端訂閱描述符讀請求事件。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

type

string

填寫"descriptorRead"字符串,表示描述符讀請求事件。

callback

Callback<DescriptorReadRequest>

表示回調函數的入參,client端發(fā)送的讀請求數據。

示例:

  1. let arrayBufferDesc = new ArrayBuffer(8);
  2. let descValue = new Uint8Array(arrayBufferDesc);
  3. descValue[0] = 1101;
  4. function ReadDescriptorReq(DescriptorReadRequest) {
  5. let deviceId = DescriptorReadRequest.deviceId;
  6. let transId = DescriptorReadRequest.transId;
  7. let offset = DescriptorReadRequest.offset;
  8. let descriptorUuid = DescriptorReadRequest.descriptorUuid;
  9. let serverResponse = {deviceId: deviceId, transId: transId, status: 0, offset: offset, value:arrayBufferDesc};
  10. try {
  11. gattServer.sendResponse(serverResponse);
  12. } catch (err) {
  13. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  14. }
  15. }
  16. let gattServer = bluetoothManager.BLE.createGattServer();
  17. gattServer.on("descriptorRead", ReadDescriptorReq);

off('descriptorRead')

off(type: "descriptorRead", callback?: Callback<DescriptorReadRequest>): void

server端取消訂閱描述符讀請求事件。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

type

string

填寫"descriptorRead"字符串,表示描述符讀請求事件。

callback

Callback<DescriptorReadRequest>

表示取消訂閱描述符讀請求事件上報。不填該參數則取消訂閱該type對應的所有回調。

示例:

  1. let gattServer = bluetoothManager.BLE.createGattServer();
  2. gattServer.off("descriptorRead");

on('descriptorWrite')

on(type: "descriptorWrite", callback: Callback<DescriptorWriteRequest>): void

server端訂閱描述符寫請求事件。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

type

string

填寫"descriptorWrite"字符串,表示描述符寫請求事件。

callback

Callback<DescriptorWriteRequest>

表示回調函數的入參,client端發(fā)送的寫請求數據。

示例:

  1. let arrayBufferDesc = new ArrayBuffer(8);
  2. let descValue = new Uint8Array(arrayBufferDesc);
  3. function WriteDescriptorReq(DescriptorWriteRequest) {
  4. let deviceId = DescriptorWriteRequest.deviceId;
  5. let transId = DescriptorWriteRequest.transId;
  6. let offset = DescriptorWriteRequest.offset;
  7. let isPrep = DescriptorWriteRequest.isPrep;
  8. let needRsp = DescriptorWriteRequest.needRsp;
  9. let value = new Uint8Array(DescriptorWriteRequest.value);
  10. let descriptorUuid = DescriptorWriteRequest.descriptorUuid;
  11. descValue[0] = value[0];
  12. let serverResponse = {deviceId: deviceId, transId: transId, status: 0, offset: offset, value:arrayBufferDesc};
  13. try {
  14. gattServer.sendResponse(serverResponse);
  15. } catch (err) {
  16. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  17. }
  18. }
  19. let gattServer = bluetoothManager.BLE.createGattServer();
  20. gattServer.on("descriptorRead", WriteDescriptorReq);

off('descriptorWrite')

off(type: "descriptorWrite", callback?: Callback<DescriptorWriteRequest>): void

server端取消訂閱描述符寫請求事件。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

type

string

填寫"descriptorWrite"字符串,表示描述符寫請求事件。

callback

Callback<DescriptorWriteRequest>

表示取消訂閱描述符寫請求事件上報。不填該參數則取消訂閱該type對應的所有回調。

示例:

  1. let gattServer = bluetoothManager.BLE.createGattServer();
  2. gattServer.off("descriptorWrite");

on('connectStateChange')

on(type: "connectStateChange", callback: Callback<BLEConnectChangedState>): void

server端訂閱BLE連接狀態(tài)變化事件。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

type

string

填寫"connectStateChange"字符串,表示BLE連接狀態(tài)變化事件。

callback

Callback<BLEConnectChangedState>

表示回調函數的入參,連接狀態(tài)。

示例:

  1. function Connected(BLEConnectChangedState) {
  2. let deviceId = BLEConnectChangedState.deviceId;
  3. let status = BLEConnectChangedState.state;
  4. }
  5. let gattServer = bluetoothManager.BLE.createGattServer();
  6. gattServer.on("connectStateChange", Connected);

off('connectStateChange')

off(type: "connectStateChange", callback?: Callback<BLEConnectChangedState>): void

server端取消訂閱BLE連接狀態(tài)變化事件。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

type

string

填寫"connectStateChange"字符串,表示BLE連接狀態(tài)變化事件。

callback

Callback<BLEConnectChangedState>

表示取消訂閱BLE連接狀態(tài)變化事件。不填該參數則取消訂閱該type對應的所有回調。

示例:

  1. let gattServer = bluetoothManager.BLE.createGattServer();
  2. gattServer.off("connectStateChange");

GattClientDevice

client端類,使用client端方法之前需要創(chuàng)建該類的實例進行操作,通過createGattClientDevice(deviceId: string)方法構造此實例。

connect

connect(): void

client端發(fā)起連接遠端藍牙低功耗設備。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900003

Bluetooth switch is off.

2900099

Operation failed.

示例:

  1. try {
  2. let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
  3. device.connect();
  4. } catch (err) {
  5. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  6. }

disconnect

disconnect(): void

client端斷開與遠端藍牙低功耗設備的連接。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900003

Bluetooth switch is off.

2900099

Operation failed.

示例:

  1. try {
  2. let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
  3. device.disconnect();
  4. } catch (err) {
  5. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  6. }

close

close(): void

關閉客戶端功能,注銷client在協(xié)議棧的注冊,調用該接口后GattClientDevice實例將不能再使用。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900003

Bluetooth switch is off.

2900099

Operation failed.

示例:

  1. try {
  2. let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
  3. device.close();
  4. } catch (err) {
  5. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  6. }

getServices

getServices(callback: AsyncCallback<Array<GattService>>): void

client端獲取藍牙低功耗設備的所有服務,即服務發(fā)現(xiàn) 。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

callback

AsyncCallback<Array<GattService>>

client進行服務發(fā)現(xiàn),通過注冊回調函數獲取。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900099

Operation failed.

示例:

  1. // callkback 模式
  2. function getServices(code, gattServices) {
  3. if (code.code == 0) {
  4. let services = gattServices;
  5. console.log('bluetooth code is ' + code.code);
  6. console.log("bluetooth services size is ", services.length);
  7. for (let i = 0; i < services.length; i++) {
  8. console.log('bluetooth serviceUuid is ' + services[i].serviceUuid);
  9. }
  10. }
  11. }
  12. try {
  13. let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
  14. device.connect();
  15. device.getServices(getServices);
  16. } catch (err) {
  17. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  18. }

getServices

getServices(): Promise<Array<GattService>>

client端獲取藍牙低功耗設備的所有服務,即服務發(fā)現(xiàn)。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

返回值:

類型

說明

Promise<Array<GattService>>

client進行服務發(fā)現(xiàn),通過promise形式獲取。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900099

Operation failed.

示例:

  1. // Promise 模式
  2. try {
  3. let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
  4. device.connect();
  5. device.getServices().then(result => {
  6. console.info("getServices successfully:" + JSON.stringify(result));
  7. });
  8. } catch (err) {
  9. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  10. }

readCharacteristicValue

readCharacteristicValue(characteristic: BLECharacteristic, callback: AsyncCallback<BLECharacteristic>): void

client端讀取藍牙低功耗設備特定服務的特征值。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

characteristic

BLECharacteristic

待讀取的特征值。

callback

AsyncCallback<BLECharacteristic>

client讀取特征值,通過注冊回調函數獲取。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼

錯誤碼ID

錯誤信息

2900001

Service stopped.

2901000

Read forbidden.

2900099

Operation failed.

示例:

  1. function readCcc(code, BLECharacteristic) {
  2. if (code.code != 0) {
  3. return;
  4. }
  5. console.log('bluetooth characteristic uuid: ' + BLECharacteristic.characteristicUuid);
  6. let value = new Uint8Array(BLECharacteristic.characteristicValue);
  7. console.log('bluetooth characteristic value: ' + value[0] +','+ value[1]+','+ value[2]+','+ value[3]);
  8. }
  9. let descriptors = [];
  10. let bufferDesc = new ArrayBuffer(8);
  11. let descV = new Uint8Array(bufferDesc);
  12. descV[0] = 11;
  13. let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  14. characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
  15. descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', descriptorValue: bufferDesc};
  16. descriptors[0] = descriptor;
  17. let bufferCCC = new ArrayBuffer(8);
  18. let cccV = new Uint8Array(bufferCCC);
  19. cccV[0] = 1;
  20. let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  21. characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
  22. characteristicValue: bufferCCC, descriptors:descriptors};
  23. try {
  24. let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
  25. device.readCharacteristicValue(characteristic, readCcc);
  26. } catch (err) {
  27. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  28. }

readCharacteristicValue

readCharacteristicValue(characteristic: BLECharacteristic): Promise<BLECharacteristic>

client端讀取藍牙低功耗設備特定服務的特征值。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

characteristic

BLECharacteristic

待讀取的特征值。

返回值:

類型

說明

Promise<BLECharacteristic>

client讀取特征值,通過promise形式獲取。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900001

Service stopped.

2901000

Read forbidden.

2900099

Operation failed.

示例:

  1. let descriptors = [];
  2. let bufferDesc = new ArrayBuffer(8);
  3. let descV = new Uint8Array(bufferDesc);
  4. descV[0] = 11;
  5. let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  6. characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
  7. descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', descriptorValue: bufferDesc};
  8. descriptors[0] = descriptor;
  9. let bufferCCC = new ArrayBuffer(8);
  10. let cccV = new Uint8Array(bufferCCC);
  11. cccV[0] = 1;
  12. let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  13. characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
  14. characteristicValue: bufferCCC, descriptors:descriptors};
  15. try {
  16. let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
  17. device.readCharacteristicValue(characteristic);
  18. } catch (err) {
  19. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  20. }

readDescriptorValue

readDescriptorValue(descriptor: BLEDescriptor, callback: AsyncCallback<BLEDescriptor>): void

client端讀取藍牙低功耗設備特定的特征包含的描述符。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

descriptor

BLEDescriptor

待讀取的描述符。

callback

AsyncCallback<BLEDescriptor>

client讀取描述符,通過注冊回調函數獲取。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900001

Service stopped.

2901000

Read forbidden.

2900099

Operation failed.

示例:

  1. function readDesc(code, BLEDescriptor) {
  2. if (code.code != 0) {
  3. return;
  4. }
  5. console.log('bluetooth descriptor uuid: ' + BLEDescriptor.descriptorUuid);
  6. let value = new Uint8Array(BLEDescriptor.descriptorValue);
  7. console.log('bluetooth descriptor value: ' + value[0] +','+ value[1]+','+ value[2]+','+ value[3]);
  8. }
  9. let bufferDesc = new ArrayBuffer(8);
  10. let descV = new Uint8Array(bufferDesc);
  11. descV[0] = 11;
  12. let descriptor = {
  13. serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  14. characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
  15. descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB',
  16. descriptorValue: bufferDesc
  17. };
  18. try {
  19. let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
  20. device.readDescriptorValue(descriptor, readDesc);
  21. } catch (err) {
  22. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  23. }

readDescriptorValue

readDescriptorValue(descriptor: BLEDescriptor): Promise<BLEDescriptor>

client端讀取藍牙低功耗設備特定的特征包含的描述符。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

descriptor

BLEDescriptor

待讀取的描述符。

返回值:

類型

說明

Promise<BLEDescriptor>

client讀取描述符,通過promise形式獲取。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼

錯誤碼ID

錯誤信息

2900001

Service stopped.

2901000

Read forbidden.

2900099

Operation failed.

示例:

  1. let bufferDesc = new ArrayBuffer(8);
  2. let descV = new Uint8Array(bufferDesc);
  3. descV[0] = 11;
  4. let descriptor = {
  5. serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  6. characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
  7. descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB',
  8. descriptorValue: bufferDesc
  9. };
  10. try {
  11. let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
  12. device.readDescriptorValue(descriptor);
  13. } catch (err) {
  14. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  15. }

writeCharacteristicValue

writeCharacteristicValue(characteristic: BLECharacteristic): void

client端向低功耗藍牙設備寫入特定的特征值。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

characteristic

BLECharacteristic

藍牙設備特征對應的二進制值及其它參數。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900001

Service stopped.

2901001

Write forbidden.

2900099

Operation failed.

示例:

  1. let descriptors = [];
  2. let bufferDesc = new ArrayBuffer(8);
  3. let descV = new Uint8Array(bufferDesc);
  4. descV[0] = 11;
  5. let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  6. characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
  7. descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', descriptorValue: bufferDesc};
  8. descriptors[0] = descriptor;
  9. let bufferCCC = new ArrayBuffer(8);
  10. let cccV = new Uint8Array(bufferCCC);
  11. cccV[0] = 1;
  12. let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  13. characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
  14. characteristicValue: bufferCCC, descriptors:descriptors};
  15. try {
  16. let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
  17. device.writeCharacteristicValue(characteristic);
  18. } catch (err) {
  19. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  20. }

writeDescriptorValue

writeDescriptorValue(descriptor: BLEDescriptor): void

client端向低功耗藍牙設備特定的描述符寫入二進制數據。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

descriptor

BLEDescriptor

藍牙設備描述符的二進制值及其它參數。

boolean

寫描述符操作成功返回true,操作失敗返回false。

  

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼

錯誤碼ID

錯誤信息

2900001

Service stopped.

2901001

Write forbidden.

2900099

Operation failed.

示例:

  1. let bufferDesc = new ArrayBuffer(8);
  2. let descV = new Uint8Array(bufferDesc);
  3. descV[0] = 22;
  4. let descriptor = {
  5. serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  6. characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
  7. descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB',
  8. descriptorValue: bufferDesc
  9. };
  10. try {
  11. let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
  12. device.writeDescriptorValue(descriptor);
  13. } catch (err) {
  14. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  15. }

setBLEMtuSize

setBLEMtuSize(mtu: number): void

client協(xié)商遠端藍牙低功耗設備的最大傳輸單元(Maximum Transmission Unit, MTU),調用connect接口連接成功后才能使用。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

mtu

number

設置范圍為22~512字節(jié)。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900099

Operation failed.

示例:

  1. try {
  2. let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
  3. device.setBLEMtuSize(128);
  4. } catch (err) {
  5. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  6. }

setNotifyCharacteristicChanged

setNotifyCharacteristicChanged(characteristic: BLECharacteristic, enable: boolean): void

向服務端發(fā)送設置通知此特征值請求。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

characteristic

BLECharacteristic

藍牙低功耗特征。

enable

boolean

啟用接收notify設置為true,否則設置為false。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900099

Operation failed.

示例:

  1. // 創(chuàng)建descriptors
  2. let descriptors = [];
  3. let arrayBuffer = new ArrayBuffer(8);
  4. let descV = new Uint8Array(arrayBuffer);
  5. descV[0] = 11;
  6. let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  7. characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
  8. descriptorUuid: '00002902-0000-1000-8000-00805F9B34FB', descriptorValue: arrayBuffer};
  9. descriptors[0] = descriptor;
  10. let arrayBufferC = new ArrayBuffer(8);
  11. let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  12. characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', characteristicValue: arrayBufferC, descriptors:descriptors};
  13. try {
  14. let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
  15. device.setNotifyCharacteristicChanged(characteristic, false);
  16. } catch (err) {
  17. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  18. }

on('BLECharacteristicChange')

on(type: "BLECharacteristicChange", callback: Callback<BLECharacteristic>): void

訂閱藍牙低功耗設備的特征值變化事件。需要先調用setNotifyCharacteristicChanged接口才能接收server端的通知。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

type

string

填寫"BLECharacteristicChange"字符串,表示特征值變化事件。

callback

Callback<BLECharacteristic>

表示藍牙低功耗設備的特征值變化事件的回調函數。

示例:

  1. function CharacteristicChange(CharacteristicChangeReq) {
  2. let serviceUuid = CharacteristicChangeReq.serviceUuid;
  3. let characteristicUuid = CharacteristicChangeReq.characteristicUuid;
  4. let value = new Uint8Array(CharacteristicChangeReq.characteristicValue);
  5. }
  6. try {
  7. let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
  8. device.on('BLECharacteristicChange', CharacteristicChange);
  9. } catch (err) {
  10. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  11. }

off('BLECharacteristicChange')

off(type: "BLECharacteristicChange", callback?: Callback<BLECharacteristic>): void

取消訂閱藍牙低功耗設備的特征值變化事件。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

type

string

填寫"BLECharacteristicChange"字符串,表示特征值變化事件。

callback

Callback<BLECharacteristic>

表示取消訂閱藍牙低功耗設備的特征值變化事件。不填該參數則取消訂閱該type對應的所有回調。

示例:

  1. try {
  2. let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
  3. device.off('BLECharacteristicChange');
  4. } catch (err) {
  5. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  6. }

on('BLEConnectionStateChange')

on(type: "BLEConnectionStateChange", callback: Callback<BLEConnectChangedState>): void

client端訂閱藍牙低功耗設備的連接狀態(tài)變化事件。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

type

string

填寫"BLEConnectionStateChange"字符串,表示連接狀態(tài)變化事件。

callback

Callback<BLEConnectChangedState>

表示連接狀態(tài),已連接或斷開。

示例:

  1. function ConnectStateChanged(state) {
  2. console.log('bluetooth connect state changed');
  3. let connectState = state.state;
  4. }
  5. try {
  6. let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
  7. device.on('BLEConnectionStateChange', ConnectStateChanged);
  8. } catch (err) {
  9. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  10. }

off('BLEConnectionStateChange')

off(type: "BLEConnectionStateChange", callback?: Callback<BLEConnectChangedState>): void

取消訂閱藍牙低功耗設備的連接狀態(tài)變化事件。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

type

string

填寫"BLEConnectionStateChange"字符串,表示連接狀態(tài)變化事件。

callback

Callback<BLEConnectChangedState>

表示取消訂閱藍牙低功耗設備的連接狀態(tài)變化事件。不填該參數則取消訂閱該type對應的所有回調。

示例:

  1. try {
  2. let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
  3. device.off('BLEConnectionStateChange');
  4. } catch (err) {
  5. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  6. }

getDeviceName

getDeviceName(callback: AsyncCallback<string>): void

client獲取遠端藍牙低功耗設備名。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

callback

AsyncCallback<string>

client獲取對端server設備名,通過注冊回調函數獲取。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900099

Operation failed.

示例:

  1. // callback
  2. try {
  3. let gattClient = bluetoothManager.BLE.createGattClientDevice("XX:XX:XX:XX:XX:XX");
  4. gattClient.connect();
  5. let deviceName = gattClient.getDeviceName((err, data)=> {
  6. console.info('device name err ' + JSON.stringify(err));
  7. console.info('device name' + JSON.stringify(data));
  8. })
  9. } catch (err) {
  10. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  11. }

getDeviceName

getDeviceName(): Promise<string>

client獲取遠端藍牙低功耗設備名。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

返回值:

類型

說明

Promise<string>

client獲取對端server設備名,通過promise形式獲取。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼

錯誤碼ID

錯誤信息

2900001

Service stopped.

2900099

Operation failed.

示例:

  1. // promise
  2. try {
  3. let gattClient = bluetoothManager.BLE.createGattClientDevice("XX:XX:XX:XX:XX:XX");
  4. gattClient.connect();
  5. let deviceName = gattClient.getDeviceName().then((data) => {
  6. console.info('device name' + JSON.stringify(data));
  7. })
  8. } catch (err) {
  9. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  10. }

getRssiValue

getRssiValue(callback: AsyncCallback<number>): void

client獲取遠端藍牙低功耗設備的信號強度 (Received Signal Strength Indication, RSSI),調用connect接口連接成功后才能使用。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

參數:

參數名

類型

必填

說明

callback

AsyncCallback<number>

返回信號強度,單位 dBm,通過注冊回調函數獲取。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼。

錯誤碼ID

錯誤信息

2900099

Operation failed.

示例:

  1. // callback
  2. try {
  3. let gattClient = bluetoothManager.BLE.createGattClientDevice("XX:XX:XX:XX:XX:XX");
  4. gattClient.connect();
  5. let rssi = gattClient.getRssiValue((err, data)=> {
  6. console.info('rssi err ' + JSON.stringify(err));
  7. console.info('rssi value' + JSON.stringify(data));
  8. })
  9. } catch (err) {
  10. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  11. }

getRssiValue

getRssiValue(): Promise<number>

client獲取遠端藍牙低功耗設備的信號強度 (Received Signal Strength Indication, RSSI),調用connect接口連接成功后才能使用。

需要權限:ohos.permission.USE_BLUETOOTH

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

返回值:

類型

說明

Promise<number>

返回信號強度,單位 dBm,通過promise形式獲取。

錯誤碼

以下錯誤碼的詳細介紹請參見藍牙服務子系統(tǒng)錯誤碼

錯誤碼ID

錯誤信息

2900099

Operation failed.

示例:

  1. // promise
  2. try {
  3. let gattClient = bluetoothManager.BLE.createGattClientDevice("XX:XX:XX:XX:XX:XX");
  4. let rssi = gattClient.getRssiValue().then((data) => {
  5. console.info('rssi' + JSON.stringify(data));
  6. })
  7. } catch (err) {
  8. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  9. }

ScanMode

枚舉,掃描模式。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

名稱

說明

SCAN_MODE_NONE

0

沒有掃描模式。

SCAN_MODE_CONNECTABLE

1

可連接掃描模式。

SCAN_MODE_GENERAL_DISCOVERABLE

2

general發(fā)現(xiàn)模式。

SCAN_MODE_LIMITED_DISCOVERABLE

3

limited發(fā)現(xiàn)模式。

SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE

4

可連接general發(fā)現(xiàn)模式。

SCAN_MODE_CONNECTABLE_LIMITED_DISCOVERABLE

5

可連接limited發(fā)現(xiàn)模式。

BondState

枚舉,配對狀態(tài)。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

名稱

說明

BOND_STATE_INVALID

0

無效的配對。

BOND_STATE_BONDING

1

正在配對。

BOND_STATE_BONDED

2

已配對。

SppOption

描述spp的配置參數。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

名稱

類型

可讀

可寫

說明

uuid

string

spp單據的uuid。

secure

boolean

是否是安全通道。

type

SppType

Spp鏈路類型。

SppType

枚舉,Spp鏈路類型。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

名稱

說明

SPP_RFCOMM

0

表示rfcomm鏈路類型。

GattService

描述service的接口參數定義。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

名稱

類型

可讀

可寫

說明

serviceUuid

string

特定服務(service)的UUID,例如:00001888-0000-1000-8000-00805f9b34fb。

isPrimary

boolean

如果是主服務設置為true,否則設置為false。

characteristics

Array<BLECharacteristic>

當前服務包含的特征列表。

includeServices

Array<GattService>

當前服務依賴的其它服務。

BLECharacteristic

描述characteristic的接口參數定義 。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

名稱

類型

可讀

可寫

說明

serviceUuid

string

特定服務(service)的UUID,例如:00001888-0000-1000-8000-00805f9b34fb。

characteristicUuid

string

特定特征(characteristic)的UUID,例如:00002a11-0000-1000-8000-00805f9b34fb。

characteristicValue

ArrayBuffer

特征對應的二進制值。

descriptors

Array<BLEDescriptor>

特定特征的描述符列表。

BLEDescriptor

描述descriptor的接口參數定義 。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

名稱

類型

可讀

可寫

說明

serviceUuid

string

特定服務(service)的UUID,例如:00001888-0000-1000-8000-00805f9b34fb。

characteristicUuid

string

特定特征(characteristic)的UUID,例如:00002a11-0000-1000-8000-00805f9b34fb。

descriptorUuid

string

描述符(descriptor)的UUID,例如:00002902-0000-1000-8000-00805f9b34fb。

descriptorValue

ArrayBuffer

描述符對應的二進制值。

NotifyCharacteristic

描述server端特征值變化時發(fā)送的特征通知參數定義。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

名稱

類型

可讀

可寫

說明

serviceUuid

string

特定服務(service)的UUID,例如:00001888-0000-1000-8000-00805f9b34fb。

characteristicUuid

string

特定特征(characteristic)的UUID,例如:00002a11-0000-1000-8000-00805f9b34fb。

characteristicValue

ArrayBuffer

特征對應的二進制值。

confirm

boolean

如果是notification則對端回復確認設置為true,如果是indication則對端不需要回復確認設置為false。

CharacteristicReadRequest

描述server端訂閱后收到的特征值讀請求事件參數結構。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

名稱

類型

可讀

可寫

說明

deviceId

string

表示發(fā)送特征值讀請求的遠端設備地址,例如:"XX:XX:XX:XX:XX:XX"。

transId

number

表示讀請求的傳輸ID,server端回復響應時需填寫相同的傳輸ID。

offset

number

表示讀特征值數據的起始位置。例如:k表示從第k個字節(jié)開始讀,server端回復響應時需填寫相同的offset。

characteristicUuid

string

特定特征(characteristic)的UUID,例如:00002a11-0000-1000-8000-00805f9b34fb。

serviceUuid

string

特定服務(service)的UUID,例如:00001888-0000-1000-8000-00805f9b34fb。

CharacteristicWriteRequest

描述server端訂閱后收到的特征值寫請求事件參數結構。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

名稱

類型

可讀

可寫

說明

deviceId

string

表示發(fā)送特征值寫請求的遠端設備地址,例如:"XX:XX:XX:XX:XX:XX"。

transId

number

表示寫請求的傳輸ID,server端回復響應時需填寫相同的傳輸ID。

offset

number

表示寫特征值數據的起始位置。例如:k表示從第k個字節(jié)開始寫,server端回復響應時需填寫相同的offset。

descriptorUuid

string

表示描述符(descriptor)的UUID,例如:00002902-0000-1000-8000-00805f9b34fb。

characteristicUuid

string

特定特征(characteristic)的UUID,例如:00002a11-0000-1000-8000-00805f9b34fb。

serviceUuid

string

特定服務(service)的UUID,例如:00001888-0000-1000-8000-00805f9b34fb。

DescriptorReadRequest

描述server端訂閱后收到的描述符讀請求事件參數結構。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

名稱

類型

可讀

可寫

說明

deviceId

string

表示發(fā)送描述符讀請求的遠端設備地址,例如:"XX:XX:XX:XX:XX:XX"。

transId

number

表示讀請求的傳輸ID,server端回復響應時需填寫相同的傳輸ID。

offset

number

表示讀描述符數據的起始位置。例如:k表示從第k個字節(jié)開始讀,server端回復響應時需填寫相同的offset。

descriptorUuid

string

表示描述符(descriptor)的UUID,例如:00002902-0000-1000-8000-00805f9b34fb。

characteristicUuid

string

特定特征(characteristic)的UUID,例如:00002a11-0000-1000-8000-00805f9b34fb。

serviceUuid

string

特定服務(service)的UUID,例如:00001888-0000-1000-8000-00805f9b34fb。

DescriptorWriteRequest

描述server端訂閱后收到的描述符寫請求事件參數結構。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

名稱

類型

可讀

可寫

說明

deviceId

string

表示發(fā)送描述符寫請求的遠端設備地址,例如:"XX:XX:XX:XX:XX:XX"。

transId

number

表示寫請求的傳輸ID,server端回復響應時需填寫相同的傳輸ID。

offset

number

表示寫描述符數據的起始位置。例如:k表示從第k個字節(jié)開始寫,server端回復響應時需填寫相同的offset。

isPrep

boolean

表示寫請求是否立即執(zhí)行。

needRsp

boolean

表示是否要給client端回復響應。

value

ArrayBuffer

表示寫入的描述符二進制數據。

descriptorUuid

string

表示描述符(descriptor)的UUID,例如:00002902-0000-1000-8000-00805f9b34fb。

characteristicUuid

string

特定特征(characteristic)的UUID,例如:00002a11-0000-1000-8000-00805f9b34fb。

serviceUuid

string

特定服務(service)的UUID,例如:00001888-0000-1000-8000-00805f9b34fb。

ServerResponse

描述server端回復client端讀/寫請求的響應參數結構。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

名稱

類型

可讀

可寫

說明

deviceId

string

表示遠端設備地址,例如:"XX:XX:XX:XX:XX:XX"。

transId

number

表示請求的傳輸ID,與訂閱的讀/寫請求事件攜帶的ID保持一致。

status

number

表示響應的狀態(tài),設置為0即可,表示正常。

offset

number

表示請求的讀/寫起始位置,與訂閱的讀/寫請求事件攜帶的offset保持一致。

value

ArrayBuffer

表示回復響應的二進制數據。

BLEConnectChangedState

描述Gatt profile連接狀態(tài) 。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

名稱

類型

可讀

可寫

說明

deviceId

string

表示遠端設備地址,例如:"XX:XX:XX:XX:XX:XX"。

state

ProfileConnectionState

表示BLE連接狀態(tài)的枚舉。

ProfileConnectionState

枚舉,藍牙設備的profile連接狀態(tài)。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

名稱

說明

STATE_DISCONNECTED

0

表示profile已斷連。

STATE_CONNECTING

1

表示profile正在連接。

STATE_CONNECTED

2

表示profile已連接。

STATE_DISCONNECTING

3

表示profile正在斷連。

ScanFilter

掃描過濾參數。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

名稱

類型

可讀

可寫

說明

deviceId

string

表示過濾的BLE設備地址,例如:"XX:XX:XX:XX:XX:XX"。

name

string

表示過濾的BLE設備名。

serviceUuid

string

表示過濾包含該UUID服務的設備,例如:00001888-0000-1000-8000-00805f9b34fb。

serviceUuidMask

string

表示過濾包含該UUID服務掩碼的設備,例如:FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF。

serviceSolicitationUuid

string

表示過濾包含該UUID服務請求的設備,例如:00001888-0000-1000-8000-00805F9B34FB。

serviceSolicitationUuidMask

string

表示過濾包含該UUID服務請求掩碼的設備,例如:FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF。

serviceData

ArrayBuffer

表示過濾包含該服務相關數據的設備,例如:[0x90,0x00,0xF1,0xF2]。

serviceDataMask

ArrayBuffer

表示過濾包含該服務相關數據掩碼的設備,例如:[0xFF,0xFF,0xFF,0xFF]。

manufactureId

number

表示過濾包含該制造商ID的設備,例如:0x0006。

manufactureData

ArrayBuffer

表示過濾包含該制造商相關數據的設備,例如:[0x1F,0x2F,0x3F]。

manufactureDataMask

ArrayBuffer

表示過濾包含該制造商相關數據掩碼的設備,例如:[0xFF,0xFF,0xFF]。

ScanOptions

掃描的配置參數。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

名稱

類型

可讀

可寫

說明

interval

number

表示掃描結果上報延遲時間,默認值為0。

dutyMode

ScanDuty

表示掃描模式,默認值為SCAN_MODE_LOW_POWER。

matchMode

MatchMode

表示硬件的過濾匹配模式,默認值為MATCH_MODE_AGGRESSIVE。

ScanDuty

枚舉,掃描模式。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

名稱

說明

SCAN_MODE_LOW_POWER

0

表示低功耗模式,默認值。

SCAN_MODE_BALANCED

1

表示均衡模式。

SCAN_MODE_LOW_LATENCY

2

表示低延遲模式。

MatchMode

枚舉,硬件過濾匹配模式。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

名稱

說明

MATCH_MODE_AGGRESSIVE

1

表示硬件上報掃描結果門限較低,比如掃描到的功率較低或者一段時間掃描到的次數較少也觸發(fā)上報,默認值。

MATCH_MODE_STICKY

2

表示硬件上報掃描結果門限較高,更高的功率門限以及掃描到多次才會上報。

ScanResult

掃描結果上報數據。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

名稱

類型

可讀

可寫

說明

deviceId

string

表示掃描到的設備地址,例如:"XX:XX:XX:XX:XX:XX"。

rssi

number

表示掃描到的設備的rssi值。

data

ArrayBuffer

表示掃描到的設備發(fā)送的廣播包。

BluetoothState

枚舉,藍牙開關狀態(tài)。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

名稱

說明

STATE_OFF

0

表示藍牙已關閉。

STATE_TURNING_ON

1

表示藍牙正在打開。

STATE_ON

2

表示藍牙已打開。

STATE_TURNING_OFF

3

表示藍牙正在關閉。

STATE_BLE_TURNING_ON

4

表示藍牙正在打開LE-only模式。

STATE_BLE_ON

5

表示藍牙正處于LE-only模式。

STATE_BLE_TURNING_OFF

6

表示藍牙正在關閉LE-only模式。

AdvertiseSetting

描述藍牙低功耗設備發(fā)送廣播的參數。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

名稱

類型

可讀

可寫

說明

interval

number

表示廣播間隔,最小值設置32個slot表示20ms,最大值設置16384個slot,默認值設置為1600個slot表示1s。

txPower

number

表示發(fā)送功率,最小值設置-127,最大值設置1,默認值設置-7,單位dbm。

connectable

boolean

表示是否是可連接廣播,默認值設置為true。

AdvertiseData

描述BLE廣播數據包的內容。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

名稱

類型

可讀

可寫

說明

serviceUuids

Array<string>

表示要廣播的服務 UUID 列表。

manufactureData

Array<ManufactureData>

表示要廣播的廣播的制造商信息列表。

serviceData

Array<ServiceData>

表示要廣播的服務數據列表。

ManufactureData

描述BLE廣播數據包的內容。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

名稱

類型

可讀

可寫

說明

manufactureId

number

表示制造商的ID,由藍牙SIG分配。

manufactureValue

ArrayBuffer

表示制造商發(fā)送的制造商數據。

ServiceData

描述廣播包中服務數據內容。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

名稱

類型

可讀

可寫

說明

serviceUuid

string

表示服務的UUID。

serviceValue

ArrayBuffer

表示服務數據。

PinRequiredParam

描述配對請求參數。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

名稱

類型

可讀

可寫

說明

deviceId

string

表示要配對的設備ID。

pinCode

string

表示要配對的密鑰。

BondStateParam

描述配對狀態(tài)參數。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

名稱

類型

可讀

可寫

說明

deviceId

string

表示要配對的設備ID。

state

BondState

表示配對設備的狀態(tài)。

StateChangeParam

描述profile狀態(tài)改變參數。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

名稱

類型

可讀

可寫

說明

deviceId

string

表示藍牙設備地址。

state

ProfileConnectionState

表示藍牙設備的profile連接狀態(tài)。

DeviceClass

描述藍牙設備的類別。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

名稱

類型

可讀

可寫

說明

majorClass

MajorClass

表示藍牙設備主要類別的枚舉。

majorMinorClass

MajorMinorClass

表示主要次要藍牙設備類別的枚舉。

classOfDevice

number

表示設備類別。

MajorClass

枚舉,藍牙設備主要類別。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

名稱

說明

MAJOR_MISC

0x0000

表示雜項設備。

MAJOR_COMPUTER

0x0100

表示計算機設備。

MAJOR_PHONE

0x0200

表示手機設備。

MAJOR_NETWORKING

0x0300

表示網絡設備。

MAJOR_AUDIO_VIDEO

0x0400

表示音頻和視頻設備。

MAJOR_PERIPHERAL

0x0500

表示外圍設備。

MAJOR_IMAGING

0x0600

表示成像設備。

MAJOR_WEARABLE

0x0700

表示可穿戴設備。

MAJOR_TOY

0x0800

表示玩具設備。

MAJOR_HEALTH

0x0900

表示健康設備。

MAJOR_UNCATEGORIZED

0x1F00

表示未分類設備。

MajorMinorClass

枚舉,主要次要藍牙設備類別。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

名稱

說明

COMPUTER_UNCATEGORIZED

0x0100

表示未分類計算機設備。

COMPUTER_DESKTOP

0x0104

表示臺式計算機設備。

COMPUTER_SERVER

0x0108

表示服務器設備。

COMPUTER_LAPTOP

0x010C

表示便攜式計算機設備。

COMPUTER_HANDHELD_PC_PDA

0x0110

表示手持式計算機設備。

COMPUTER_PALM_SIZE_PC_PDA

0x0114

表示掌上電腦設備。

COMPUTER_WEARABLE

0x0118

表示可穿戴計算機設備。

COMPUTER_TABLET

0x011C

表示平板電腦設備。

PHONE_UNCATEGORIZED

0x0200

表示未分類手機設備。

PHONE_CELLULAR

0x0204

表示便攜式手機設備。

PHONE_CORDLESS

0x0208

表示無線電話設備。

PHONE_SMART

0x020C

表示智能手機設備。

PHONE_MODEM_OR_GATEWAY

0x0210

表示調制解調器或網關手機設備。

PHONE_ISDN

0x0214

表示ISDN手機設備。

NETWORK_FULLY_AVAILABLE

0x0300

表示網絡完全可用設備。

NETWORK_1_TO_17_UTILIZED

0x0320

表示使用網絡1到17設備。

NETWORK_17_TO_33_UTILIZED

0x0340

表示使用網絡17到33設備。

NETWORK_33_TO_50_UTILIZED

0x0360

表示使用網絡33到50設備。

NETWORK_60_TO_67_UTILIZED

0x0380

表示使用網絡60到67設備。

NETWORK_67_TO_83_UTILIZED

0x03A0

表示使用網絡67到83設備。

NETWORK_83_TO_99_UTILIZED

0x03C0

表示使用網絡83到99設備。

NETWORK_NO_SERVICE

0x03E0

表示網絡無服務設備。

AUDIO_VIDEO_UNCATEGORIZED

0x0400

表示未分類音頻視頻設備。

AUDIO_VIDEO_WEARABLE_HEADSET

0x0404

表示可穿戴式音頻視頻設備。

AUDIO_VIDEO_HANDSFREE

0x0408

表示免提音頻視頻設備。

AUDIO_VIDEO_MICROPHONE

0x0410

表示麥克風音頻視頻設備。

AUDIO_VIDEO_LOUDSPEAKER

0x0414

表示揚聲器音頻視頻設備。

AUDIO_VIDEO_HEADPHONES

0x0418

表示頭戴式音頻視頻設備。

AUDIO_VIDEO_PORTABLE_AUDIO

0x041C

表示便攜式音頻視頻設備。

AUDIO_VIDEO_CAR_AUDIO

0x0420

表示汽車音頻視頻設備。

AUDIO_VIDEO_SET_TOP_BOX

0x0424

表示機頂盒音頻視頻設備。

AUDIO_VIDEO_HIFI_AUDIO

0x0428

表示高保真音響設備。

AUDIO_VIDEO_VCR

0x042C

表示錄像機音頻視頻設備。

AUDIO_VIDEO_VIDEO_CAMERA

0x0430

表示照相機音頻視頻設備。

AUDIO_VIDEO_CAMCORDER

0x0434

表示攝像機音頻視頻設備。

AUDIO_VIDEO_VIDEO_MONITOR

0x0438

表示監(jiān)視器音頻視頻設備。

AUDIO_VIDEO_VIDEO_DISPLAY_AND_LOUDSPEAKER

0x043C

表示視頻顯示器和揚聲器設備。

AUDIO_VIDEO_VIDEO_CONFERENCING

0x0440

表示音頻視頻會議設備。

AUDIO_VIDEO_VIDEO_GAMING_TOY

0x0448

表示游戲玩具音頻視頻設備。

PERIPHERAL_NON_KEYBOARD_NON_POINTING

0x0500

表示非鍵盤非指向外圍設備。

PERIPHERAL_KEYBOARD

0x0540

表示外設鍵盤設備。

PERIPHERAL_POINTING_DEVICE

0x0580

表示定點裝置外圍設備。

PERIPHERAL_KEYBOARD_POINTING

0x05C0

表示鍵盤指向外圍設備。

PERIPHERAL_UNCATEGORIZED

0x0500

表示未分類外圍設備。

PERIPHERAL_JOYSTICK

0x0504

表示周邊操縱桿設備。

PERIPHERAL_GAMEPAD

0x0508

表示周邊游戲板設備。

PERIPHERAL_REMOTE_CONTROL

0x05C0

表示遠程控制外圍設備。

PERIPHERAL_SENSING_DEVICE

0x0510

表示外圍傳感設備設備。

PERIPHERAL_DIGITIZER_TABLET

0x0514

表示外圍數字化儀平板電腦設備。

PERIPHERAL_CARD_READER

0x0518

表示外圍讀卡器設備。

PERIPHERAL_DIGITAL_PEN

0x051C

表示外設數碼筆設備。

PERIPHERAL_SCANNER_RFID

0x0520

表示射頻識別掃描儀外圍設備。

PERIPHERAL_GESTURAL_INPUT

0x0522

表示手勢輸入外圍設備。

IMAGING_UNCATEGORIZED

0x0600

表示未分類的圖像設備。

IMAGING_DISPLAY

0x0610

表示圖像顯示設備。

IMAGING_CAMERA

0x0620

表示成像照相機設備。

IMAGING_SCANNER

0x0640

表示成像掃描儀設備。

IMAGING_PRINTER

0x0680

表示成像打印機設備。

WEARABLE_UNCATEGORIZED

0x0700

表示未分類的可穿戴設備。

WEARABLE_WRIST_WATCH

0x0704

表示可穿戴腕表設備。

WEARABLE_PAGER

0x0708

表示可穿戴尋呼機設備。

WEARABLE_JACKET

0x070C

表示夾克可穿戴設備。

WEARABLE_HELMET

0x0710

表示可穿戴頭盔設備。

WEARABLE_GLASSES

0x0714

表示可穿戴眼鏡設備。

TOY_UNCATEGORIZED

0x0800

表示未分類的玩具設備。

TOY_ROBOT

0x0804

表示玩具機器人設備。

TOY_VEHICLE

0x0808

表示玩具車設備。

TOY_DOLL_ACTION_FIGURE

0x080C

表示人形娃娃玩具設備。

TOY_CONTROLLER

0x0810

表示玩具控制器設備。

TOY_GAME

0x0814

表示玩具游戲設備。

HEALTH_UNCATEGORIZED

0x0900

表示未分類健康設備。

HEALTH_BLOOD_PRESSURE

0x0904

表示血壓健康設備。

HEALTH_THERMOMETER

0x0908

表示溫度計健康設備。

HEALTH_WEIGHING

0x090C

表示體重健康設備。

HEALTH_GLUCOSE

0x0910

表示葡萄糖健康設備。

HEALTH_PULSE_OXIMETER

0x0914

表示脈搏血氧儀健康設備。

HEALTH_PULSE_RATE

0x0918

表示脈搏率健康設備。

HEALTH_DATA_DISPLAY

0x091C

表示數據顯示健康設備。

HEALTH_STEP_COUNTER

0x0920

表示階梯計數器健康設備。

HEALTH_BODY_COMPOSITION_ANALYZER

0x0924

表示身體成分分析儀健康設備。

HEALTH_PEAK_FLOW_MONITOR

0x0928

表示濕度計健康設備。

HEALTH_MEDICATION_MONITOR

0x092C

表示藥物監(jiān)視儀健康設備。

HEALTH_KNEE_PROSTHESIS

0x0930

表示膝蓋假肢健康設備。

HEALTH_ANKLE_PROSTHESIS

0x0934

表示腳踝假肢健康設備。

HEALTH_GENERIC_HEALTH_MANAGER

0x0938

表示通用健康管理設備。

HEALTH_PERSONAL_MOBILITY_DEVICE

0x093C

表示個人移動健康設備。

PlayingState

枚舉,藍牙A2DP 播放狀態(tài)。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

名稱

說明

STATE_NOT_PLAYING

0x0000

表示未播放。

STATE_PLAYING

0x0001

表示正在播放。

ProfileId

藍牙profile枚舉,API9新增PROFILE_HID_HOST,PROFILE_PAN_NETWORK。

系統(tǒng)能力:SystemCapability.Communication.Bluetooth.Core。

名稱

說明

PROFILE_A2DP_SOURCE

1

表示A2DP profile。

PROFILE_HANDS_FREE_AUDIO_GATEWAY

4

表示HFP profile。

PROFILE_HID_HOST

6

表示HID profile。

PROFILE_PAN_NETWORK

7

表示PAN profile。

以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號