dlt.startBluetoothDevicesDiscovery
Introduction
Start scanning for Bluetooth devices.
Usage Restrictions
- Prerequisite: Initialize the Bluetooth module using dlt.openBluetoothAdapter.
- After discovering devices, call dlt.stopBluetoothDevicesDiscovery to stop the scan.
Parameters
Object object
Property | Type | Default | Required | Description |
---|---|---|---|---|
serviceUUIDS | string[] | Yes | A list of UUIDs of the main services of Bluetooth devices to search for (supports 16/32/128-bit UUIDs). Some Bluetooth devices broadcast the UUID of their main service. If you set this parameter, the scan will only search for Bluetooth devices that advertise the specified UUIDs in their broadcast packets. It is recommended to use this parameter to filter out other Bluetooth devices that do not need to be processed. | |
powerLevel | number | No | Scanning level. The higher the level, the more frequently the scan occurs, and the more power is consumed. | |
success | function | No | The callback function for a successful API call. | |
fail | function | No | The callback function for a failed API call. | |
complete | function | No | The callback function that is called when the API call ends (whether it succeeds or fails). |
object.success Callback Function
Parameters
Object res
Property | Type | Description |
---|---|---|
success | string | true - Success |
object.fail Callback Function
Parameters
Object res
Property | Type | Description |
---|---|---|
success | string | false - Failure |
errCode | string | Error code |
Explanation of Error Codes
Error Code | Description |
---|---|
-1 | Failed |
0 | Normal |
10000 | Bluetooth adapter uninitialized |
10001 | Bluetooth not available |
10002 | Device not found |
10003 | Connection failed |
10004 | Already connected |
10005 | Characteristic not found |
10006 | Descriptor not found |
10007 | Location access required |
10008 | System reports an exception |
10013 | Invalid data |
10014 | Permission not granted |
10015 | Adapter already initialized |
Sample Code
dlt.startBluetoothDevicesDiscovery({
serviceUUIDS: [],
powerLevel: 1,
success: function (res) {
console.log('success', res);
dlt.stopBluetoothDevicesDiscovery();
},
});
In the sample code provided, dlt.startBluetoothDevicesDiscovery
is used to start scanning for Bluetooth devices. It specifies an empty array for serviceUUIDS
, meaning it will scan for all Bluetooth devices. When the operation is successful, the success callback function is called, and it logs the result to the console. It also calls dlt.stopBluetoothDevicesDiscovery
to stop the scan when the operation succeeds.