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