目录

Bluetooth - BLE

has.createBLEConnection

has.createBLEConnection(Object object)

Connects to a Bluetooth low energy (BLE) device.

If an atomic service has found a Bluetooth device during search and successfully established a connection to it, the atomic service can then directly pass the device ID obtained from the previous search to connect to the device.

Since: 1.0.4

Required permissions:

  1. ohos.permission.ACCESS_BLUETOOTH, which needs to be declared in module.json5.

  2. scope.bluetooth, which is applied for using has.authorize().

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
deviceId string Yes Bluetooth device ID.
success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

Error code information

Error Code Error Message
12002110 deviceId is invalid.
12002101

create ble connection error.

For details, see Bluetooth Error Codes.

Example:

has.createBLEConnection({
  deviceId: '',
  success: () => {
    console.info('createBLEConnection success');
  },
  fail: (err) => {
    console.error('createBLEConnection fail', err);
  },
  complete: (res) => {
    console.info('createBLEConnection complete', res);
  }
});

has.closeBLEConnection

has.closeBLEConnection(Object object)

Disconnects a BLE device.

Since: 1.0.4

Required permissions:

  1. ohos.permission.ACCESS_BLUETOOTH, which needs to be declared in module.json5.

  2. scope.bluetooth, which is applied for using has.authorize().

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
deviceId string Yes Bluetooth device ID.
success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

Error code information

Error Code Error Message
12002401 close ble connection error.
12002110 deviceId is invalid. For details, see Universal Error Codes.

Example:

has.closeBLEConnection({
  deviceId: '',
  success: () => {
    console.info('closeBLEConnection success');
  },
  fail: (err) => {
    console.error('closeBLEConnection fail', err);
  },
  complete: (res) => {
    console.info('closeBLEConnection complete', res);
  }
});

has.getConnectedBluetoothDevices

has.getConnectedBluetoothDevices(Object object)

Obtains the connected Bluetooth device based on the UUID list of the primary service.

Since: 1.0.12

Required permissions:

  1. ohos.permission.ACCESS_BLUETOOTH, which needs to be declared in module.json5.

  2. scope.bluetooth, which is applied for using has.authorize().

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
services Array<string> Yes UUID list of the primary service of the Bluetooth device.
success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

Return value of success

Parameter Type Description
devices Object List of the found devices.

devices parameters

Parameter Type Description
name string Name of a Bluetooth device.
deviceId string Unique ID of a Bluetooth device.

Error code information

Error Code Error Message
12002901

1. get connected devices failed.

2. get connected devices error.

3. services is empty.

For details, see Bluetooth Error Codes.

Example:

has.getConnectedBluetoothDevices({
  services: [''],
  success: (res) => {
    console.info('getConnectedBluetoothDevices success', res);
  },
  fail: (err) => {
    console.error('getConnectedBluetoothDevices fail', err);
  },
  complete: (res) => {
    console.info('getConnectedBluetoothDevices complete', res);
  }
});

has.onBLEConnectionStateChange

has.onBLEConnectionStateChange(function callback)

Listens for BLE connection status change events.

Since: 1.0.12

Required permissions:

  1. ohos.permission.ACCESS_BLUETOOTH, which needs to be declared in module.json5.

  2. scope.bluetooth, which is applied for using has.authorize().

Parameters

Parameter Type Mandatory Description
callback function Yes Listener function for the BLE connection status change event.

Return value of callback

Parameter Type Description
deviceId string Bluetooth device ID.
connected boolean Whether the Bluetooth device is connected.

Example:

has.onBLEConnectionStateChange(function(res) {
  console.info(`device ${res.deviceId} state has changed, connected: ${res.connected}`);
});

has.offBLEConnectionStateChange

has.offBLEConnectionStateChange(function callback)

Removes the listener function for BLE connection status change events.

Since: 1.0.12

Required permissions:

  1. ohos.permission.ACCESS_BLUETOOTH, which needs to be declared in module.json5.

  2. scope.bluetooth, which is applied for using has.authorize().

Parameters

Parameter Type Mandatory Description
callback function No Callback for the BLE connection status change event.

Example:

let listener = function(res) {
  console.info(`device ${res.deviceId} state has changed, connected: ${res.connected}`);
};
has.onBLEConnectionStateChange(listener);
has.offBLEConnectionStateChange(listener);
has.offBLEConnectionStateChange();

has.getBLEDeviceServices

has.getBLEDeviceServices(Object object)

Obtains all services of a BLE device.

Since: 1.0.12

Required permissions:

  1. ohos.permission.ACCESS_BLUETOOTH, which needs to be declared in module.json5.

  2. scope.bluetooth, which is applied for using has.authorize().

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
deviceId string Yes Bluetooth device ID.
success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

Return value of success

Parameter Type Description
services Object Service list.

services parameters

Parameter Type Description
uuid string Service UUID.
isPrimary boolean Whether the service is the primary one.

Error code information

Error Code Error Message
12002110 deviceId is invalid.
12002201

1. get device services error.

2. get devices failed.

3. get devices error.

For details, see Bluetooth Error Codes.

Example:

has.getBLEDeviceServices({
  deviceId: 'deviceId',
  success: (res) => {
    console.info('getBLEDeviceServices success', res);
  },
  fail: (err) => {
    console.error('getBLEDeviceServices fail', err);
  },
  complete: (res) => {
    console.info('getBLEDeviceServices complete', res);
  }
});

has.getBLEDeviceCharacteristics

has.getBLEDeviceCharacteristics(Object object)

Obtains all characteristics of a service of a Bluetooth Low Energy device.

Since: 1.0.12

Required permissions:

  1. ohos.permission.ACCESS_BLUETOOTH, which needs to be declared in module.json5.

  2. scope.bluetooth, which is applied for using has.authorize().

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
deviceId string Yes Bluetooth device ID.
serviceId string Yes Bluetooth service UUID.
success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

Return value of success

Parameter Type Description
characteristics Object Device characteristic list.

characteristics parameters

Parameter Type Description
uuid string Characteristic UUID.
properties Object Operations supported by the characteristic.

properties parameters

Parameter Type Description
read boolean Whether the characteristic supports the read operation.
write boolean Whether the characteristic supports the write operation.
notify boolean Whether the characteristic supports the notify operation.
indicate boolean Whether the characteristic supports the indicate operation.
writeNoResponse boolean Whether the characteristic supports the write operation without a response.
writeDefault boolean Whether the characteristic supports the write operation with a response.

Error code information

Error Code Error Message
12002501

1. get characteristics failed.

2. get characteristics error.

12002201

get device services error.

For details, see Bluetooth Error Codes.

Example:

has.getBLEDeviceCharacteristics({
  deviceId: 'deviceId',
  serviceId: 'serviceId',
  success: (res) => {
    console.info('getBLEDeviceCharacteristics success', res);
  },
  fail: (err) => {
    console.error('getBLEDeviceCharacteristics fail', err);
  },
  complete: (res) => {
    console.info('getBLEDeviceCharacteristics complete', res);
  }
});

has.readBLECharacteristicValue

has.readBLECharacteristicValue(Object object)

Reads the characteristic value (binary) of a BLE device.

Before using this API, call has.getBLEDeviceCharacteristics to check whether the characteristic supports the read operation.

Since: 1.0.12

Required permissions:

  1. ohos.permission.ACCESS_BLUETOOTH, which needs to be declared in module.json5.

  2. scope.bluetooth, which is applied for using has.authorize().

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
deviceId string Yes Bluetooth device ID.
serviceId string Yes Service UUID of the characteristic.
characteristicId string Yes Characteristic UUID.
success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

Error code information

Error Code Error Message
102

1. serviceId is invalid.

2. characteristicId is invalid.

12002701

1. device is not found.

2. read characteristic value failed.

3. read characteristic value error.

4. the characteristic value cannot be read.

12002110

deviceId is invalid.

For details, see Universal Error Codes.

Example:

has.readBLECharacteristicValue({
  deviceId: 'deviceId',
  serviceId: 'serviceId',
  characteristicId: 'characteristicId',
  success: () => {
    console.info('readBLECharacteristicValue success');
  },
  fail: (err) => {
    console.error('readBLECharacteristicValue fail', err);
  },
  complete: (res) => {
    console.info('readBLECharacteristicValue complete', res);
  }
});

has.writeBLECharacteristicValue

has.writeBLECharacteristicValue(Object object)

Writes the characteristic value (binary) of a BLE device.

Before using this API, call has.getBLEDeviceCharacteristics to check whether the characteristic supports the write operation.

Since: 1.0.12

Required permissions:

  1. ohos.permission.ACCESS_BLUETOOTH, which needs to be declared in module.json5.

  2. scope.bluetooth, which is applied for using has.authorize().

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
deviceId string Yes Bluetooth device ID.
serviceId string Yes Service UUID of the characteristic.
characteristicId string Yes Characteristic UUID.
value ArrayBuffer Yes Binary value mapping the characteristic of a Bluetooth device.
writeType string No

Write mode of the Bluetooth characteristic.

The value write means to forcibly write with a response. If the write mode is not supported, an error is reported.

The value writeNoResponse means to forcibly write without a response. If the write mode is not supported, an error is reported.

success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

Error code information

Error Code Error Message
102

1. serviceId is invalid.

2. characteristicId is invalid.

12002601

1. device is not found.

2. write characteristic value error.

12002110

deviceId is invalid.

For more error information, see:

Universal Error Codes

Bluetooth Error Codes

Example:

let buffer = new ArrayBuffer(1);
let dataView = new DataView(buffer);
dataView.setUint8(0, 0);

has.writeBLECharacteristicValue({
  deviceId: 'deviceId',
  serviceId: 'serviceId',
  characteristicId: 'characteristicId',
  value: buffer,
  success: () => {
    console.info('writeBLECharacteristicValue success');
  },
  fail: (err) => {
    console.error('writeBLECharacteristicValue fail', err);
  },
  complete: (res) => {
    console.info('writeBLECharacteristicValue complete', res);
  }
});

has.notifyBLECharacteristicValueChange

has.notifyBLECharacteristicValueChange(Object object)

Enables the notify function for characteristic changes of a BLE device.

Before using this API, call has.getBLEDeviceCharacteristics to check whether the characteristic supports the notify or indicate function.

Since: 1.0.12

Required permissions:

  1. ohos.permission.ACCESS_BLUETOOTH, which needs to be declared in module.json5.

  2. scope.bluetooth, which is applied for using has.authorize().

Parameters

The input parameter is an object containing the following fields.

Parameter Type Default Value Mandatory Description
deviceId string - Yes Bluetooth device ID.
serviceId string - Yes Service UUID of the characteristic.
characteristicId string - Yes Characteristic UUID.
state boolean - Yes Whether to enable the notify function.
type string indication No

Characteristic subscription type. The options are as follows:

notification: No response from the peer device is required.

indication: A response from the peer device is required.

success function - No Callback triggered when the API call is successful.
fail function - No Callback triggered when the API call fails.
complete function - No Callback triggered when the API call is complete, regardless of success or failure.

Error code information

Error Code Error Message
101 characteristic is not found.
102

1. serviceId is invalid.

2. characteristicId is invalid.

12002801

1. device is not found.

2. notify ble characteristic failed.

3. get characteristic failed.

4. notify ble characteristic error.

5. characteristic is invalid.

12002110

deviceId is invalid.

For more error information, see:

Universal Error Codes

Bluetooth Error Codes

Example:

has.notifyBLECharacteristicValueChange({
  deviceId: 'deviceId',
  serviceId: 'serviceId',
  characteristicId: 'characteristicId',
  state: true,
  success: () => {
    console.info('notifyBLECharacteristicValueChange success');
  },
  fail: (err) => {
    console.error('notifyBLECharacteristicValueChange fail', err);
  },
  complete: (res) => {
    console.info('notifyBLECharacteristicValueChange complete', res);
  }
});

has.onBLECharacteristicValueChange

has.onBLECharacteristicValueChange(function callback)

Enables listening for characteristic changes of a BLE device.

The application can receive notifications from the device only after calling has.notifyBLECharacteristicValueChange.

Since: 1.0.12

Required permissions:

  1. ohos.permission.ACCESS_BLUETOOTH, which needs to be declared in module.json5.

  2. scope.bluetooth, which is applied for using has.authorize().

Parameters

Parameter Type Mandatory Description
callback function Yes Listener function for the characteristic value change event.

Return value of callback

Parameter Type Description
deviceId string Bluetooth device ID.
serviceId string Service UUID of the characteristic.
characteristicId string Characteristic UUID.
value ArrayBuffer Latest characteristic value.

Example:

function ab2hex(buffer) {
  let hexArr = Array.prototype.map.call(
  new Uint8Array(buffer),
  function(bit) {
    return ('00' + bit.toString(16)).slice(-2)
  }
 )
  return hexArr.join('');
};
has.onBLECharacteristicValueChange(
  function(res) {
    console.info(`characteristic ${res.characteristicId} has changed, now is ${res.value}`);
    console.info(ab2hex(res.value));
  }
);

has.offBLECharacteristicValueChange

has.offBLECharacteristicValueChange()

Removes all listener functions for characteristic changes of a BLE device.

Since: 1.0.12

Required permissions:

  1. ohos.permission.ACCESS_BLUETOOTH, which needs to be declared in module.json5.

  2. scope.bluetooth, which is applied for using has.authorize().

Example:

has.offBLECharacteristicValueChange();