• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @system.bluetooth (Bluetooth)
2
3
4> **NOTE**
5>
6> - The APIs of this module are no longer maintained since API version 7. You are advised to use [`@ohos.bluetooth`](js-apis-bluetooth.md).
7>
8> - 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.
9
10
11## Modules to Import
12
13
14```
15import bluetooth from '@system.bluetooth';
16```
17
18## bluetooth.startBLEScan(OBJECT)
19
20Scans 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.
21
22**System capability**: SystemCapability.Communication.Bluetooth.Lite
23
24**Parameters**
25
26**Table 1** StartBLEScanOptions
27
28| Name| Type| Mandatory| Description|
29| -------- | -------- | -------- | -------- |
30| 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.|
31| success | Function | No| Called when the operation is successful.|
32| fail | Function | No| Called when the operation fails.|
33| complete | Function | No| Called when the execution is complete.|
34
35**Example**
36
37  ```
38  bluetooth.startBLEScan({
39    interval:0,
40    success() {
41      console.log('call bluetooth.startBLEScan success.');
42    },
43    fail(code, data) {
44      console.log('call bluetooth.startBLEScan failed, code:' + code + ', data:' + data);
45    },
46    complete() {
47      console.log('call bluetooth.startBLEScan complete.');
48    }
49  });
50  ```
51
52
53## bluetooth.stopBLEScan(OBJECT)
54
55Stops scanning for BLE devices nearby. This API is used with [bluetooth.startBLEScan(OBJECT)](#bluetoothstartblescanobject) in pairs.
56
57**System capability**: SystemCapability.Communication.Bluetooth.Lite
58
59**Parameters**
60
61**Table 2** StopBLEScanOptions
62
63| Name| Type| Mandatory| Description|
64| -------- | -------- | -------- | -------- |
65| success | Function | No| Called when the operation is successful.|
66| fail | Function | No| Called when the operation fails.|
67| complete | Function | No| Called when the execution is complete.|
68
69**Example**
70
71  ```
72  bluetooth.stopBLEScan({
73    success() {
74      console.log('call bluetooth.stopBLEScan success.');
75    },
76    fail(data, code) {
77      console.log('call bluethooth.stopBLEScan fail, code:' + code + ', data:' + data);
78    },
79    complete() {
80      console.log('call bluethooth.stopBLEScan complete.');
81    }
82  });
83  ```
84
85
86## bluetooth.subscribeBLEFound(OBJECT)
87
88Subscribes to the newly detected BLE device. If this API is called multiple times, the last call takes effect.
89
90**System capability**: SystemCapability.Communication.Bluetooth.Lite
91
92**Parameters**
93**Table 3** SubscribeBLEFoundOptions
94
95| Name| Type| Mandatory| Description|
96| -------- | -------- | -------- | -------- |
97| success | Function | Yes| Called to report the newly detected device.|
98| fail | Function | No| Called when the operation fails.|
99
100**Table 4** Return value in success
101
102| Name| Type| Description|
103| -------- | -------- | -------- |
104| devices | Array<BluetoothDevice> | List of the newly detected BLE devices.|
105
106**Table 5** BluethoothDevice
107
108| Name| Type| Description|
109| -------- | -------- | -------- |
110| addrType | string | Device address type, which can be:<br>-&nbsp;**public**: a public address<br>-&nbsp;**random**: a random address|
111| addr | string | MAC address of the device.|
112| rssi | number | Received signal strength indicator (RSSl) of the device.|
113| txpower | string | **txpower** field in the Bluetooth advertising data.|
114| data | hex&nbsp;string | Bluetooth advertising data (including advertising data and scan response data), in a hexadecimal string.|
115
116**Example**
117
118  ```
119  bluetooth.subscribeBLEFound({
120    success(data) {
121      console.log('call bluetooth.subscribeBLEFound success, data: ${data}.');
122    },
123    fail(data, code) {
124      console.log('call bluetooth.startBLEScan failed, code:' + code + ', data:' + data);
125    }
126  });
127  ```
128
129
130## bluetooth.unsubscribeBLEFound()
131
132Unsubscribes from the newly detected devices.
133
134**System capability**: SystemCapability.Communication.Bluetooth.Lite
135
136**Example**
137
138  ```
139  bluetooth.unsubscribeBLEFound();
140  ```
141
142
143## Common Error Codes
144
145| Error Code| Description|
146| -------- | -------- |
147| 1100 | The Bluetooth adapter is not initialized.|
148| 1101 | The Bluetooth adapter is unavailable.|
149| 1102 | The specified device is not found.|
150| 1103 | Connection failed.|
151| 1104 | The specified service is not found.|
152| 1105 | The specified characteristic value is not found.|
153| 1106 | The Bluetooth device is disconnected.|
154| 1107 | The characteristic value does not support this operation.|
155| 1108 | Other exceptions reported by the system.|
156| 1109 | The system version does not support BLE.|
157