harmony 鸿蒙@system.bluetooth (Bluetooth)

2022-08-09 浏览 (955)

@system.bluetooth (Bluetooth)

NOTE

  • The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.

  • The APIs provided by this module are no longer maintained since API version 7. You are advised to use profile APIs of @ohos.bluetooth.ble.

Modules to Import

import bluetooth from '@system.bluetooth';

bluetooth.startBLEScan(OBJECT)

Scans for Bluetooth Low Energy (BLE) devices nearby. This operation consumes system resources. Call bluetooth.stopBLEScan to stop the scan after a BLE device is detected and connected.

System capability: SystemCapability.Communication.Bluetooth.Lite

Parameters Table 1 StartBLEScanOptions

NameTypeMandatoryDescription
intervalnumberNoInterval for reporting device information, in milliseconds. The default value is 0, which means to report the detected device immediately and report other information at the given interval.
successFunctionNoCalled when the operation is successful.
failFunctionNoCalled when the operation fails.
completeFunctionNoCalled when the execution is complete.

Example

bluetooth.startBLEScan({
  interval:0,
  success() {
    console.log('call bluetooth.startBLEScan success.');
  },
  fail(code, data) {
    console.log('call bluetooth.startBLEScan failed, code:' + code + ', data:' + data);
  },
  complete() {
    console.log('call bluetooth.startBLEScan complete.');
  }
});

bluetooth.stopBLEScan(OBJECT)

Stops scanning for BLE devices nearby. This API is used with bluetooth.startBLEScan(OBJECT) in pairs.

System capability: SystemCapability.Communication.Bluetooth.Lite

Parameters Table 2 StopBLEScanOptions

NameTypeMandatoryDescription
successFunctionNoCalled when the operation is successful.
failFunctionNoCalled when the operation fails.
completeFunctionNoCalled when the execution is complete.

Example

bluetooth.stopBLEScan({
  success() {
    console.log('call bluetooth.stopBLEScan success.');
  },
  fail(data, code) {
    console.log('call bluethooth.stopBLEScan fail, code:' + code + ', data:' + data);
  },
  complete() {
    console.log('call bluethooth.stopBLEScan complete.');
  }
});

bluetooth.subscribeBLEFound(OBJECT)

Subscribes to the newly detected BLE device. If this API is called multiple times, the last call takes effect.

System capability: SystemCapability.Communication.Bluetooth.Lite

Parameters Table 3 SubscribeBLEFoundOptions

NameTypeMandatoryDescription
successFunctionYesCalled to report the newly detected device.
failFunctionNoCalled when the operation fails.

Table 4 Return value in success

NameTypeDescription
devicesArray<BluetoothDevice>List of the newly detected BLE devices.

Table 5 BluethoothDevice

NameTypeDescription
addrTypestringDevice address type, which can be:
public: a public address
random: a random address
addrstringMAC address of the device.
rssinumberReceived signal strength indicator (RSSl) of the device.
txpowerstringtxpower field in the Bluetooth advertising data.
datahex stringBluetooth advertising data (including advertising data and scan response data), in a hexadecimal string.

Example

bluetooth.subscribeBLEFound({
  success(data) {
    console.log('call bluetooth.subscribeBLEFound success, data: ${data}.');
  },
  fail(data, code) {
    console.log('call bluetooth.startBLEScan failed, code:' + code + ', data:' + data);
  }
});

bluetooth.unsubscribeBLEFound()

Unsubscribes from the newly detected devices.

System capability: SystemCapability.Communication.Bluetooth.Lite

Example

bluetooth.unsubscribeBLEFound();

Common Error Codes

Error CodeDescription
1100The Bluetooth adapter is not initialized.
1101The Bluetooth adapter is unavailable.
1102The specified device is not found.
1103Connection failed.
1104The specified service is not found.
1105The specified characteristic value is not found.
1106The Bluetooth device is disconnected.
1107The characteristic value does not support this operation.
1108Other exceptions reported by the system.
1109The system version does not support BLE.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/0042d8b17bc94570826918b5d2c0a664