# @ohos.bluetooth.connection (Bluetooth connection Module)
The **connection** module provides APIs for operating and managing Bluetooth.
> **NOTE**
>
> The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version.
## Modules to Import
```js
import connection from '@ohos.bluetooth.connection';
```
## connection.pairDevice
pairDevice(deviceId: string, callback: AsyncCallback<void>): void
Pairs a Bluetooth device. This API uses an asynchronous callback to return the result.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ----------------------------------- |
| deviceId | string | Yes | Address of the device to pair, for example, XX:XX:XX:XX:XX:XX.|
| callback | AsyncCallback<void> | Yes | Callback invoked to return the result. If the pairing is successful, **err** is **undefined**. Otherwise, **err** is an error object.|
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900099 | Operation failed. |
**Example**
```js
try {
// The address can be scanned.
connection.pairDevice('XX:XX:XX:XX:XX:XX');
} catch (err) {
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}
```
## connection.pairDevice
pairDevice(deviceId: string): Promise<void>
Pairs a Bluetooth device. This API uses a promise to return the result.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ----------------------------------- |
| deviceId | string | Yes | Address of the device to pair, for example, XX:XX:XX:XX:XX:XX.|
**Return value**
| Type | Description |
| ------------------- | ------------- |
| Promise<void> | Promise used to return the result.|
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900099 | Operation failed. |
**Example**
```js
try {
// The address can be scanned.
connection.pairDevice('XX:XX:XX:XX:XX:XX');
} catch (err) {
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}
```
## connection.pairCredibleDevice
pairCredibleDevice(deviceId: string, transport: BluetoothTransport, callback: AsyncCallback<void>): void
Pairs a trusted device whose address is obtained in a non-Bluetooth scan mode (such as using NFC). This API uses an asynchronous callback to return the result.
**System API**: This is a system API.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ----------------------------------- |
| deviceId | string | Yes | Address of the device to pair, for example, XX:XX:XX:XX:XX:XX.|
| transport | [BluetoothTransport](#bluetoothtransport) | Yes | Device type, for example, a classic Bluetooth device or a Bluetooth low energy (BLE) device.|
| callback | AsyncCallback<void> | Yes | Callback invoked to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object. |
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900099 | Operation failed. |
**Example**
```js
try {
connection.pairCredibleDevice('68:13:24:79:4C:8C', 1, err => {
if (err) {
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
return;
}
console.info('pairCredibleDevice, err: ' + JSON.stringify(err));
});
} catch (err) {
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}
```
## connection.pairCredibleDevice
pairCredibleDevice(deviceId: string, transport: BluetoothTransport): Promise<void>
Pairs a trusted device whose address is obtained in a non-Bluetooth scan mode (such as using NFC). This API uses a promise to return the result.
**System API**: This is a system API.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ----------------------------------- |
| deviceId | string | Yes | Address of the device to pair, for example, XX:XX:XX:XX:XX:XX.|
| transport | [BluetoothTransport](#bluetoothtransport) | Yes | Device type, for example, a classic Bluetooth device or a BLE device.|
**Return value**
| Type | Description |
| ------------------------------------------------- | ------------------- |
| Promise<void> | Promise used to return the result.|
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900099 | Operation failed. |
**Example**
```js
try {
connection.pairCredibleDevice('68:13:24:79:4C:8C', 0).then(() => {
console.info('PairCredibleDevice');
}, err => {
console.error('PairCredibleDevice:errCode' + err.code + ', errMessage: ' + err.message);
});
} catch (err) {
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}
```
## connection.cancelPairedDevice
cancelPairedDevice(deviceId: string, callback: AsyncCallback<void>): void
Cancels a paired device.
**System API**: This is a system API.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ------------------------------------- |
| deviceId | string | Yes | Address of the device to cancel, for example, XX:XX:XX:XX:XX:XX.|
| callback | AsyncCallback<void> | Yes | Callback invoked to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object. |
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900099 | Operation failed. |
**Example**
```js
try {
connection.cancelPairedDevice('XX:XX:XX:XX:XX:XX');
} catch (err) {
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}
```
## connection.cancelPairedDevice
cancelPairedDevice(deviceId: string): Promise<void>
Cancels a paired device. This API uses a promise to return the result.
**System API**: This is a system API.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ------------------------------------- |
| deviceId | string | Yes | Address of the device to cancel, for example, XX:XX:XX:XX:XX:XX.|
**Return value**
| Type | Description |
| ------------------- | ------------- |
| Promise<void> | Promise used to return the result.|
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900099 | Operation failed. |
**Example**
```js
try {
connection.cancelPairedDevice('XX:XX:XX:XX:XX:XX');
} catch (err) {
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}
```
## connection.cancelPairingDevice
cancelPairingDevice(deviceId: string, callback: AsyncCallback<void>): void
Cancels the pairing of a device. This API uses an asynchronous callback to return the result.
**System API**: This is a system API.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ------------------------------------- |
| deviceId | string | Yes | Address of the device to cancel, for example, XX:XX:XX:XX:XX:XX.|
| callback | AsyncCallback<void> | Yes | Callback invoked to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object. |
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900099 | Operation failed. |
**Example**
```js
try {
connection.cancelPairingDevice('XX:XX:XX:XX:XX:XX');
} catch (err) {
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}
```
## connection.cancelPairingDevice
cancelPairingDevice(deviceId: string): Promise<void>
Cancels the pairing of a device. This API uses a promise to return the result.
**System API**: This is a system API.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ------------------------------------- |
| deviceId | string | Yes | Address of the device to cancel, for example, XX:XX:XX:XX:XX:XX.|
**Return value**
| Type | Description |
| ------------------- | ------------- |
| Promise<void> | Promise used to return the result.|
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900099 | Operation failed. |
**Example**
```js
try {
connection.cancelPairingDevice('XX:XX:XX:XX:XX:XX');
} catch (err) {
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}
```
## connection.getRemoteDeviceName
getRemoteDeviceName(deviceId: string): string
Obtains the name of a remote Bluetooth device.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | --------------------------------- |
| deviceId | string | Yes | Address of the remote device, for example, XX:XX:XX:XX:XX:XX.|
**Return value**
| Type | Description |
| ------ | ------------- |
| string | Device name (a string) obtained.|
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900099 | Operation failed. |
**Example**
```js
try {
let remoteDeviceName = connection.getRemoteDeviceName('XX:XX:XX:XX:XX:XX');
} catch (err) {
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}
```
## connection.getRemoteDeviceClass
getRemoteDeviceClass(deviceId: string): DeviceClass
Obtains the class of a remote Bluetooth device.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | --------------------------------- |
| deviceId | string | Yes | Address of the remote device, for example, XX:XX:XX:XX:XX:XX.|
**Return value**
| Type | Description |
| --------------------------- | -------- |
| [DeviceClass](#deviceclass) | Class of the remote device obtained.|
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900099 | Operation failed. |
**Example**
```js
try {
let remoteDeviceClass = connection.getRemoteDeviceClass('XX:XX:XX:XX:XX:XX');
} catch (err) {
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}
```
## connection.getLocalName
getLocalName(): string
Obtains the name of the local Bluetooth device.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Return value**
| Type | Description |
| ------ | --------- |
| string | Name of the local Bluetooth device obtained.|
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900099 | Operation failed. |
**Example**
```js
try {
let localName = connection.getLocalName();
} catch (err) {
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}
```
## connection.getPairedDevices
getPairedDevices(): Array<string>
Obtains the paired devices.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Return value**
| Type | Description |
| ------------------- | ------------- |
| Array<string> | Addresses of the paired Bluetooth devices obtained. |
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900099 | Operation failed. |
**Example**
```js
try {
let devices = connection.getPairedDevices();
} catch (err) {
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}
```
## connection.getProfileConnectionState
getProfileConnectionState(profileId?: ProfileId): ProfileConnectionState
Obtains the connection state of the specified profile.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Parameters**
| Name | Type | Mandatory | Description |
| --------- | --------- | ---- | ------------------------------------- |
| ProfileId | [profileId](js-apis-bluetooth-constant.md#profileid) | No | ID of the target profile, for example, **PROFILE_A2DP_SOURCE**.|
**Return value**
| Type | Description |
| ------------------------------------------------- | ------------------- |
| [ProfileConnectionState](js-apis-bluetooth-constant.md#profileconnectionstate) | Profile connection state obtained.|
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900004 | Profile is not supported. |
|2900099 | Operation failed. |
**Example**
```js
import constant from '@ohos.bluetooth.constant';
try {
let result = connection.getProfileConnectionState(constant.ProfileId.PROFILE_A2DP_SOURCE);
} catch (err) {
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}
```
## connection.setDevicePairingConfirmation
setDevicePairingConfirmation(deviceId: string, accept: boolean): void
Sets the device pairing confirmation.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Parameters**
| Name | Type | Mandatory | Description |
| ------ | ------- | ---- | -------------------------------- |
| deviceId | string | Yes | Address of the remote device, for example, XX:XX:XX:XX:XX:XX.|
| accept | boolean | Yes | Whether to accept the pairing request. The value **true** means to accept the pairing request, and the value **false** means the opposite. |
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900099 | Operation failed. |
**Example**
```js
try {
// Subscribe to the pinRequired event and configure the pairing confirmation after receiving a pairing request from the remote device.
function onReceivePinRequiredEvent(data) { // data is the input parameter for the pairing request.
console.info('pin required = '+ JSON.stringify(data));
connection.setDevicePairingConfirmation(data.deviceId, true);
}
connection.on('pinRequired', onReceivePinRequiredEvent);
} catch (err) {
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}
```
## connection.setDevicePinCode
setDevicePinCode(deviceId: string, code: string, callback: AsyncCallback<void>): void
Sets the PIN for the device when [PinType](#pintype) is **PIN_TYPE_ENTER_PIN_CODE** or **PIN_TYPE_PIN_16_DIGITS**. This API uses an asynchronous callback to return the result.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Parameters**
| Name | Type | Mandatory | Description |
| ------ | ------- | ---- | -------------------------------- |
| deviceId | string | Yes | MAC address of the remote device, for example, XX:XX:XX:XX:XX:XX.|
| code | string | Yes | PIN to set. |
| callback | AsyncCallback<void> | Yes | Callback invoked to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object. |
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900099 | Operation failed. |
**Example**
```js
//callback
try {
connection.setDevicePinCode('11:22:33:44:55:66', '12345', (err, data) => {
console.info('setDevicePinCode,device name err:' + JSON.stringify(err) + ',device name:' + JSON.stringify(data));
});
} catch (err) {
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}
```
## connection.setDevicePinCode
setDevicePinCode(deviceId: string, code: string): Promise<void>
Sets the PIN for the device when [PinType](#pintype) is **PIN_TYPE_ENTER_PIN_CODE** or **PIN_TYPE_PIN_16_DIGITS**. This API uses a promise to return the result.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Parameters**
| Name | Type | Mandatory | Description |
| ------ | ------- | ---- | -------------------------------- |
| deviceId | string | Yes | MAC address of the remote device, for example, XX:XX:XX:XX:XX:XX.|
| code | string | Yes | PIN to set. |
**Return value**
| Type | Description |
| ------------------- | ------------- |
| Promise<void> | Promise used to return the result.|
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900099 | Operation failed. |
**Example**
```js
//promise
try {
connection.setDevicePinCode('11:22:33:44:55:66', '12345').then(() => {
console.info('setDevicePinCode');
}, error => {
console.info('setDevicePinCode: errCode:' + error.code + ',errMessage' + error.message);
})
} catch (err) {
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}
```
## connection.setLocalName
setLocalName(name: string): void
Sets the name of the local Bluetooth device.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Parameters**
| Name | Type | Mandatory | Description |
| ---- | ------ | ---- | --------------------- |
| name | string | Yes | Bluetooth device name to set. It cannot exceed 248 bytes.|
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900099 | Operation failed. |
**Example**
```js
try {
connection.setLocalName('device_name');
} catch (err) {
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}
```
## connection.setBluetoothScanMode
setBluetoothScanMode(mode: ScanMode, duration: number): void
Sets the Bluetooth scan mode so that the device can be discovered by a remote device.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | --------------------- | ---- | ---------------------------- |
| mode | [ScanMode](#scanmode) | Yes | Bluetooth scan mode to set. |
| duration | number | Yes | Duration (in ms) in which the device can be discovered. The value **0** indicates unlimited time.|
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900099 | Operation failed. |
**Example**
```js
try {
// The device can be discovered and connected only when the discoverable and connectable mode is used.
connection.setBluetoothScanMode(connection.ScanMode.SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE, 100);
} catch (err) {
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}
```
## connection.getBluetoothScanMode
getBluetoothScanMode(): ScanMode
Obtains the Bluetooth scan mode.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Return value**
| Type | Description |
| --------------------- | ------- |
| [ScanMode](#scanmode) | Bluetooth scan mode obtained. |
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900099 | Operation failed. |
**Example**
```js
try {
let scanMode = connection.getBluetoothScanMode();
} catch (err) {
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}
```
## connection.startBluetoothDiscovery
startBluetoothDiscovery(): void
Starts discovery of Bluetooth devices.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900099 | Operation failed. |
**Example**
```js
let deviceId;
function onReceiveEvent(data) {
deviceId = data;
}
try {
connection.on('bluetoothDeviceFind', onReceiveEvent);
connection.startBluetoothDiscovery();
} catch (err) {
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}
```
## connection.stopBluetoothDiscovery
stopBluetoothDiscovery(): void
Stops discovery of Bluetooth devices.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900099 | Operation failed. |
**Example**
```js
try {
connection.stopBluetoothDiscovery();
} catch (err) {
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}
```
## connection.getLocalProfileUuids
getLocalProfileUuids(callback: AsyncCallback<Array<ProfileUuids>>): void
Obtains the profile UUIDs of the local device. This API uses an asynchronous callback to return the result.
**System API**: This is a system API.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ----------------------------------- |
| callback | AsyncCallback<Array<[ProfileUuids](js-apis-bluetooth-constant.md#profileuuids)>> | Yes | Callback invoked to return the profile UUIDs obtained. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.|
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900099 | Operation failed. |
**Example**
```js
try {
connection.getLocalProfileUuids('XX:XX:XX:XX:XX:XX', (err, data) => {
console.info('getLocalProfileUuids, err: ' + JSON.stringify(err) + ', data: ' + JSON.stringify(data));
});
} catch (err) {
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}
```
## connection.getLocalProfileUuids
getLocalProfileUuids(): Promise<Array<ProfileUuids>>
Obtains the profile UUIDs of the local device. This API uses a promise to return the result.
**System API**: This is a system API.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Return value**
| Type | Description |
| ------------------- | ------------- |
| Promise<Array<[ProfileUuids](js-apis-bluetooth-constant.md#profileuuids)>> | Promise used to return the profile UUIDs obtained.|
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900099 | Operation failed. |
**Example**
```js
try {
connection.getLocalProfileUuids('XX:XX:XX:XX:XX:XX').then(() => {
console.info('getLocalProfileUuids');
}, err => {
console.error('getLocalProfileUuids: errCode' + err.code + ', errMessage: ' + err.message);
});
} catch (err) {
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}
```
## connection.getRemoteProfileUuids
getRemoteProfileUuids(deviceId: string, callback: AsyncCallback<Array<ProfileUuids>>): void
Obtains the profile UUIDs of a remote Bluetooth device. This API uses an asynchronous callback to return the result.
**System API**: This is a system API.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ----------------------------------- |
| deviceId | string | Yes | Address of the device to pair, for example, XX:XX:XX:XX:XX:XX.|
| callback | AsyncCallback<Array<[ProfileUuids](js-apis-bluetooth-constant.md#profileuuids)>> | Yes | Callback invoked to return the profile UUIDs obtained. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.|
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900099 | Operation failed. |
**Example**
```js
try {
connection.getRemoteProfileUuids('XX:XX:XX:XX:XX:XX', (err, data) => {
console.info('getRemoteProfileUuids, err: ' + JSON.stringify(err) + ', data: ' + JSON.stringify(data));
});
} catch (err) {
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}
```
## connection.getRemoteProfileUuids
getRemoteProfileUuids(deviceId: string): Promise<Array<ProfileUuids>>
Obtains the profile UUIDs of a remote Bluetooth device. This API uses a promise to return the result.
**System API**: This is a system API.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ----------------------------------- |
| deviceId | string | Yes | Address of the device to pair, for example, XX:XX:XX:XX:XX:XX.|
**Return value**
| Type | Description |
| ------------------- | ------------- |
| Promise<Array<[ProfileUuids](js-apis-bluetooth-constant.md#profileuuids)>> | Promise used to return the profile UUIDs obtained.|
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900099 | Operation failed. |
**Example**
```js
try {
connection.getRemoteProfileUuids('XX:XX:XX:XX:XX:XX').then(() => {
console.info('getRemoteProfileUuids');
}, err => {
console.error('getRemoteProfileUuids: errCode' + err.code + ', errMessage: ' + err.message);
});
} catch (err) {
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}
```
## connection.on('bluetoothDeviceFind')
on(type: 'bluetoothDeviceFind', callback: Callback<Array<string>>): void
Subscribes to the discovery of a Bluetooth device.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ----------------------------------- | ---- | -------------------------------------- |
| type | string | Yes | Event type. The value is **bluetoothDeviceFind**, which indicates an event of discovering a Bluetooth device.|
| callback | Callback<Array<string>> | Yes | Callback invoked to return the discovered devices. You need to implement this callback. |
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900099 | Operation failed. |
**Example**
```js
function onReceiveEvent(data) { // data is a set of Bluetooth device addresses.
console.info('bluetooth device find = '+ JSON.stringify(data));
}
try {
connection.on('bluetoothDeviceFind', onReceiveEvent);
} catch (err) {
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}
```
## connection.off('bluetoothDeviceFind')
off(type: 'bluetoothDeviceFind', callback?: Callback<Array<string>>): void
Unsubscribes from the discovery of a Bluetooth device.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ----------------------------------- | ---- | ---------------------------------------- |
| type | string | Yes | Event type. The value is **bluetoothDeviceFind**, which indicates an event of discovering a Bluetooth device. |
| callback | Callback<Array<string>> | No | Callback for the **bluetoothDeviceFind** event. If this parameter is not set, this API unsubscribes from all callbacks corresponding to **type**.|
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900099 | Operation failed. |
**Example**
```js
function onReceiveEvent(data) {
console.info('bluetooth device find = '+ JSON.stringify(data));
}
try {
connection.on('bluetoothDeviceFind', onReceiveEvent);
connection.off('bluetoothDeviceFind', onReceiveEvent);
} catch (err) {
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}
```
## connection.on('bondStateChange')
on(type: 'bondStateChange', callback: Callback<BondStateParam>): void
Subscribes to Bluetooth pairing state changes.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ---------------------------------------- | ---- | ------------------------------------ |
| type | string | Yes | Event type. The value is **bondStateChange**, which indicates a Bluetooth pairing state change event.|
| callback | Callback<[BondStateParam](#BondStateParam)> | Yes | Callback invoked to return the pairing state. You need to implement this callback. |
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900099 | Operation failed. |
**Example**
```js
function onReceiveEvent(data) { // data, as the input parameter of the callback, indicates the pairing state.
console.info('pair state = '+ JSON.stringify(data));
}
try {
connection.on('bondStateChange', onReceiveEvent);
} catch (err) {
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}
```
## connection.off('bondStateChange')
off(type: 'bondStateChange', callback?: Callback<BondStateParam>): void
Unsubscribes from Bluetooth pairing state changes.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type | string | Yes | Event type. The value is **bondStateChange**, which indicates a Bluetooth pairing state change event. |
| callback | Callback<[BondStateParam](#BondStateParam)> | No | Callback for the change of the Bluetooth pairing state. If this parameter is not set, this API unsubscribes from all callbacks corresponding to **type**.|
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900099 | Operation failed. |
**Example**
```js
function onReceiveEvent(data) {
console.info('bond state = '+ JSON.stringify(data));
}
try {
connection.on('bondStateChange', onReceiveEvent);
connection.off('bondStateChange', onReceiveEvent);
} catch (err) {
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}
```
## connection.on('pinRequired')
on(type: 'pinRequired', callback: Callback<PinRequiredParam>): void
Subscribes to the pairing request events of the remote Bluetooth device.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ---------------------------------------- | ---- | -------------------------------- |
| type | string | Yes | Event type. The value **pinRequired** indicates a pairing request event. |
| callback | Callback<[PinRequiredParam](#pinrequiredparam)> | Yes | Callback invoked to return the pairing request. You need to implement this callback.|
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900099 | Operation failed. |
**Example**
```js
function onReceiveEvent(data) { // data is the pairing request parameter.
console.info('pin required = '+ JSON.stringify(data));
}
try {
connection.on('pinRequired', onReceiveEvent);
} catch (err) {
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}
```
## connection.off('pinRequired')
off(type: 'pinRequired', callback?: Callback<PinRequiredParam>): void
Unsubscribes from the pairing request events of the remote Bluetooth device.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type | string | Yes | Event type. The value **pinRequired** indicates a pairing request event. |
| callback | Callback<[PinRequiredParam](#pinrequiredparam)> | No | Callback for the Bluetooth pairing request event. The input parameter is the pairing request parameter. If this parameter is not set, this API unsubscribes from all callbacks corresponding to **type**.|
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900099 | Operation failed. |
**Example**
```js
function onReceiveEvent(data) {
console.info('pin required = '+ JSON.stringify(data));
}
try {
connection.on('pinRequired', onReceiveEvent);
connection.off('pinRequired', onReceiveEvent);
} catch (err) {
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}
```
## BondStateParam
Represents the pairing state parameters.
**System capability**: SystemCapability.Communication.Bluetooth.Core
| Name | Type | Readable | Writable | Description |
| -------- | ------ | ---- | ---- | ----------- |
| deviceId | string | Yes | No | ID of the device to pair.|
| state | BondState | Yes | No | State of the device.|
## PinRequiredParam
Represents the pairing request parameters.
**System capability**: SystemCapability.Communication.Bluetooth.Core
| Name | Type | Readable | Writable | Description |
| -------- | ------ | ---- | ---- | ----------- |
| deviceId | string | Yes | No | ID of the device to pair.|
| pinCode | string | Yes | No | Key for the device pairing. |
| pinType | [PinType](#pintype) | Yes | No | Type of the device to pair.
This is a system API. |
## DeviceClass
Represents the class of a Bluetooth device.
**System capability**: SystemCapability.Communication.Bluetooth.Core
| Name | Type | Readable | Writable | Description |
| --------------- | ----------------------------------- | ---- | ---- | ---------------- |
| majorClass | [MajorClass](js-apis-bluetooth-constant.md#majorclass) | Yes | No | Major class of the Bluetooth device. |
| majorMinorClass | [MajorMinorClass](js-apis-bluetooth-constant.md#majorminorclass) | Yes | No | Major and minor classes of the Bluetooth device.|
| classOfDevice | number | Yes | No | Class of the device. |
## BluetoothTransport
Enumerates the device types.
**System capability**: SystemCapability.Communication.Bluetooth.Core
| Name | Value | Description |
| -------------------------------- | ------ | --------------- |
| TRANSPORT_BR_EDR | 0 | Classic Bluetooth (BR/EDR) device, which is the default value. |
| TRANSPORT_LE | 1 | BLE device. |
## ScanMode
Enumerates the scan modes.
**System capability**: SystemCapability.Communication.Bluetooth.Core
| Name | Value | Description |
| ---------------------------------------- | ---- | --------------- |
| SCAN_MODE_NONE | 0 | No scan mode. |
| SCAN_MODE_CONNECTABLE | 1 | Connectable mode. |
| SCAN_MODE_GENERAL_DISCOVERABLE | 2 | General discoverable mode. |
| SCAN_MODE_LIMITED_DISCOVERABLE | 3 | Limited discoverable mode. |
| SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE | 4 | General connectable and discoverable mode.|
| SCAN_MODE_CONNECTABLE_LIMITED_DISCOVERABLE | 5 | Limited connectable and discoverable mode.|
## BondState
Enumerates the pairing states.
**System capability**: SystemCapability.Communication.Bluetooth.Core
| Name | Value | Description |
| ------------------ | ---- | ------ |
| BOND_STATE_INVALID | 0 | Invalid pairing.|
| BOND_STATE_BONDING | 1 | Pairing. |
| BOND_STATE_BONDED | 2 | Paired. |
## PinType
Enumerates the Bluetooth pairing types.
**System API**: This is a system API.
**System capability**: SystemCapability.Communication.Bluetooth.Core
| Name | Value | Description |
| -------------------------------- | ------ | --------------- |
| PIN_TYPE_ENTER_PIN_CODE | 0 | The user needs to enter the PIN displayed on the peer device.
This is a system API.|
| PIN_TYPE_ENTER_PASSKEY | 1 | The user needs to enter the PASSKEY displayed on the peer device.
This is a system API. |
| PIN_TYPE_CONFIRM_PASSKEY | 2 | The user needs to confirm the PASSKEY displayed on the local device.
This is a system API. |
| PIN_TYPE_NO_PASSKEY_CONSENT | 3 | There is no PASSKEY, and the user needs to accept or reject the pairing request.
This is a system API. |
| PIN_TYPE_NOTIFY_PASSKEY | 4 | The user needs to enter the PASSKEY displayed on the local device on the peer device.
This is a system API. |
| PIN_TYPE_DISPLAY_PIN_CODE | 5 | The user needs to enter the PIN displayed on the peer device for Bluetooth 2.0 devices.
This is a system API. |
| PIN_TYPE_OOB_CONSENT | 6 | The user needs to accept or reject the out of band (OOB) pairing request.
This is a system API. |
| PIN_TYPE_PIN_16_DIGITS | 7 | The user needs to enter the 16-digit PIN displayed on the peer device.
This is a system API. |