目录

Bluetooth - General

has.startBluetoothDevicesDiscovery

has.startBluetoothDevicesDiscovery(Object object)

Starts searching for nearby Bluetooth devices. This operation consumes system resources. Call has.stopBluetoothDevicesDiscovery to stop the scan after a BLE device is detected and connected.

Since: 1.0.4

Parameters

The input parameter is an object containing the following fields.

Parameter Type Default Value Mandatory Description
services Array<string> - No

UUID list of the primary services to be searched for. Some Bluetooth devices broadcast their primary service UUIDs. If this parameter is set, only the Bluetooth devices that broadcast the required primary service UUIDs are searched for. It is recommended that this parameter be used to filter out Bluetooth devices that do not need to be processed.

Since: 1.0.18

allowDuplicatesKey boolean false No Whether the same device can be repeatedly reported. If repeated reporting is allowed, the has.onBluetoothDeviceFound method will report the same device multiple times, but the RSSI values will be different.
interval number 0 No Device reporting interval, in ms. The value 0 indicates that a new device is immediately reported once being found; any other values indicate that devices are reported by the specified interval.
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
12003301 startBluetoothDevicesDiscovery error.
12003020 adapter not init.
12003010 adapter not available.

Example:

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

has.stopBluetoothDevicesDiscovery

has.stopBluetoothDevicesDiscovery (Object object)

Stops searching for nearby Bluetooth devices. If the required Bluetooth device has been found and no further search is necessary, it is recommended that this API be called to stop the Bluetooth search.

Since: 1.0.4

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
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
12003401 stopBluetoothDevicesDiscovery error.
12003020 adapter not init.

Example:

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

has.openBluetoothAdapter

has.openBluetoothAdapter(Object object)

Initializes the Bluetooth module.

Since: 1.0.4

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

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
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
103

1. authorize error.

2. has no permission.

12003010 openBluetoothAdapter error, adapter not available.
12003001 openBluetoothAdapter error.

Example:

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

has.closeBluetoothAdapter

has.closeBluetoothAdapter(Object object)

Disables the Bluetooth module. Calling this API will disconnect all established connections and release relevant system resources. It is recommended that this API be used with has.openBluetoothAdapter.

Since: 1.0.4

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
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
12003101 closeBluetoothAdapter error.

Example:

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

has.onBluetoothDeviceFound

has.onBluetoothDeviceFound(function callback)

Enables listening for new device found events.

If a device is returned in the callback of has.onBluetoothDeviceFound, the device is added to the device array obtained by callinghas.getBluetoothDevices.

Since: 1.0.4

Parameters

Parameter Type Mandatory Description
callback function Yes Callback for the new device found event.

Return value of callback

Parameter Type Description
devices Array<Object> List of the newly detected BLE devices.

devices parameters

Parameter Type Description
name string Name of a Bluetooth device. Some devices may have no names.
deviceId string Bluetooth device ID.
RSSI number

Signal strength of the current Bluetooth device, in dBm.

Since: 1.0.18

advertisData ArrayBuffer

ManufacturerData segment in the broadcast data segment of the current Bluetooth device.

Since: 1.0.18

advertisServiceUUIDs Array<string>

ServiceUUIDs segment in the broadcast data segment of the current Bluetooth device.

Since: 1.0.18

localName string

LocalName segment in the broadcast data segment of the current Bluetooth device.

Since: 1.0.18

serviceData Object

ServiceData segment in the broadcast data segment of the current Bluetooth device.

Since: 1.0.18

connectable boolean

Whether the current Bluetooth device can be connected.

Since: 1.0.18

Example:

// Convert an ArrayBuffer into a hexadecimal string.
function ab2hex(buffer) {
  const hexArr = Array.prototype.map.call(
    new Uint8Array(buffer),
    function (bit) {
      return ('00' + bit.toString(16)).slice(-2)
    }
  );
  return hexArr.join('');
};

has.onBluetoothDeviceFound(function (res) {
  const devices = res.devices;
  console.info('new device list found');
  console.info(ab2hex(devices[0].advertisData));
});

has.offBluetoothDeviceFound

has.offBluetoothDeviceFound()

Removes all listener functions for new devices found events.

Since: 1.0.4

Example:

has.offBluetoothDeviceFound();

has.onBluetoothAdapterStateChange

has.onBluetoothAdapterStateChange(function callback)

Enables listening for Bluetooth adapter status change events.

Since: 1.0.4

Parameters

Parameter Type Mandatory Description
callback function Yes Callback for the Bluetooth adapter status change event.

Return value of callback

Parameter Type Description
available boolean Whether the Bluetooth adapter is available.
discovering boolean Whether a device is being searched.

Example:

has.onBluetoothAdapterStateChange(function(res) {
  console.info('adapterState changed, now is', res);
});

has.offBluetoothAdapterStateChange

has.offBluetoothAdapterStateChange()

Removes all listener functions for Bluetooth adapter status change events.

Since: 1.0.4

Example:

has.offBluetoothAdapterStateChange();

has.getBluetoothDevices

has.getBluetoothDevices(Object object)

Obtains all Bluetooth devices found during the effective period of the Bluetooth module, including the devices that are connected to the current device.

Since: 1.0.4

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
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

Attribute Type Description
devices Array<Object> Bluetooth device list.

devices parameters

Parameter Type Description
name string Name of a Bluetooth device. Some devices may have no names.
deviceId string Bluetooth device ID.
RSSI number

Signal strength of the current Bluetooth device, in dBm.

Since: 1.0.18

advertisData ArrayBuffer

ManufacturerData segment in the broadcast data segment of the current Bluetooth device.

Since: 1.0.18

advertisServiceUUIDs Array<string>

ServiceUUIDs segment in the broadcast data segment of the current Bluetooth device.

Since: 1.0.18

localName string

LocalName segment in the broadcast data segment of the current Bluetooth device.

Since: 1.0.18

serviceData Object

ServiceData segment in the broadcast data segment of the current Bluetooth device.

Since: 1.0.18

connectable boolean

Whether the current Bluetooth device can be connected.

Since: 1.0.18

Error code information

Error Code Error Message
12004501 getBluetoothDevices error.
12003020 adapter not init.
12003010 adapter not available.

Example:

// Convert an ArrayBuffer into a hexadecimal string.
function ab2hex(buffer) {
  const hexArr = Array.prototype.map.call(
    new Uint8Array(buffer),
    function (bit) {
      return ('00' + bit.toString(16)).slice(-2);
    }
  );
  return hexArr.join('');
};

has.getBluetoothDevices({
  success: (res) => {
    console.info('getBluetoothDevices success', res);
    if (res.devices[0]) {
      console.info(ab2hex(res.devices[0].advertisData));
    }
  },
  fail: (err) => {
    console.error('getBluetoothDevices fail', err);
  },
  complete: (res) => {
    console.info('getBluetoothDevices complete', res);
  }
});

has.getBluetoothAdapterState

has.getBluetoothAdapterState(Object object)

Obtains the status of the local Bluetooth adapter.

Since: 1.0.4

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
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
available boolean Whether the Bluetooth adapter is available.
discovering boolean Whether a device is being searched.

Error code information

Error Code Error Message
12003020 getBluetoothAdapterState error, adapter not init.

Example:

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