js-apis-system-bluetooth.md 5.2 KB
Newer Older
A
Annie_wang 已提交
1 2 3
# Bluetooth


4
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br/>
A
Annie_wang 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
>
> - The APIs of this module are no longer maintained since API version 7. You are advised to use [`@ohos.bluetooth`](js-apis-bluetooth.md).
>
> - 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.


## Modules to Import


```
import bluetooth from '@system.bluetooth';
```


## bluetooth.startBLEScan(OBJECT)

A
Annie_wang 已提交
21
Scans for Bluetooth Low Energy (BLE) devices nearby. This operation consumes system resources. Call [bluetooth.stopBLEScan](#bluetoothstopblescanobject) to stop the scan after a BLE device is detected and connected.
A
Annie_wang 已提交
22 23 24 25 26 27

**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH and ohos.permission.LOCATION

**System capability**: SystemCapability.Communication.Bluetooth.Lite

**Parameters**
A
Annie_wang 已提交
28

A
Annie_wang 已提交
29 30 31 32
**Table 1** StartBLEScanOptions

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
33
| interval | number | No| Interval 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.|
A
Annie_wang 已提交
34 35
| success | Function | No| Called when the operation is successful.|
| fail | Function | No| Called when the operation fails.|
A
Annie_wang 已提交
36
| complete | Function | No| Called when the execution is complete.|
A
Annie_wang 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

**Example**

  ```
  bluetooth.startBLEScan({
    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)](#bluetoothstartblescanobject) in pairs.

**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH and ohos.permission.LOCATION

**System capability**: SystemCapability.Communication.Bluetooth.Lite

**Parameters**
A
Annie_wang 已提交
64

A
Annie_wang 已提交
65 66 67 68 69 70
**Table 2** StopBLEScanOptions

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| success | Function | No| Called when the operation is successful.|
| fail | Function | No| Called when the operation fails.|
A
Annie_wang 已提交
71
| complete | Function | No| Called when the execution is complete.|
A
Annie_wang 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

**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)

A
Annie_wang 已提交
92
Subscribes to the newly detected BLE device. If this API is called multiple times, the last call takes effect.
A
Annie_wang 已提交
93 94 95 96 97 98

**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH and ohos.permission.LOCATION

**System capability**: SystemCapability.Communication.Bluetooth.Lite

**Parameters**
A
Annie_wang 已提交
99

A
Annie_wang 已提交
100 101
**Table 3** SubscribeBLEFoundOptions

A
Annie_wang 已提交
102
| Name| Type| Mandatory| Description|
A
Annie_wang 已提交
103
| -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
104
| success | Function | Yes| Called to report the newly detected device.|
A
Annie_wang 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
| fail | Function | No| Called when the operation fails.|

**Table 4** Return value in success

| Name| Type| Description|
| -------- | -------- | -------- |
| devices | Array&lt;BluetoothDevice&gt; | List of the newly detected BLE devices.|

**Table 5** BluethoothDevice

| Name| Type| Description|
| -------- | -------- | -------- |
| addrType | string | Device address type, which can be:<br>-&nbsp;**public**: a public address<br>-&nbsp;**random**: a random address|
| addr | string | MAC address of the device.|
| rssi | number | Received signal strength indicator (RSSl) of the device.|
| txpower | string | **txpower** field in the Bluetooth advertising data.|
| data | hex&nbsp;string | Bluetooth advertising data (including advertising data and scan response data), in a hexadecimal string.|

**Example**

  ```
  bluetooth.startaBLEScan({
    success() {
      bluetooth.subscribeBLEFound({
        success(data) {
          const [device] = data.devices;
          if (!!device) {
            bluetooth.stopBLEScan();
          }
        }
      });
    },
    fail(code, data) {
      console.log('Failed to start BLE device scan, code: ${code}, data: ${data}');
    }
  });
  ```


## bluetooth.unsubscribeBLEFound()

Unsubscribes from the newly detected devices.

**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH and ohos.permission.LOCATION

**System capability**: SystemCapability.Communication.Bluetooth.Lite

**Example**

  ```
  bluetooth.unsubscribeBLEFound();
  ```


## Common Error Codes

| Error Code| Description|
| -------- | -------- |
| 1100 | The Bluetooth adapter is not initialized.|
| 1101 | The Bluetooth adapter is unavailable.|
| 1102 | The specified device is not found.|
| 1103 | Connection failed.|
| 1104 | The specified service is not found.|
A
Annie_wang 已提交
168
| 1105 | The specified characteristic value is not found.|
A
Annie_wang 已提交
169 170 171 172
| 1106 | The Bluetooth device is disconnected.|
| 1107 | The characteristic value does not support this operation.|
| 1108 | Other exceptions reported by the system.|
| 1109 | The system version does not support BLE.|