@ohos.bluetooth.connection (Bluetooth Connection Module) (System API)
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.
- This topic describes only the system APIs provided by the module. For details about its public APIs, see @ohos.bluetooth.connection (Bluetooth Connection Module).
Modules to Import
import connection from '@ohos.bluetooth.connection';
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 | 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.
ID | Error Message |
---|---|
2900001 | Service stopped. |
2900003 | Bluetooth switch is off. |
2900099 | Operation failed. |
Example
import { BusinessError } from '@ohos.base';
try {
connection.pairCredibleDevice('68:13:24:79:4C:8C', connection.BluetoothTransport
.TRANSPORT_BR_EDR, (err: BusinessError) => {
if (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
return;
}
console.info('pairCredibleDevice, err: ' + JSON.stringify(err));
});
} catch (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).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 | 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.
ID | Error Message |
---|---|
2900001 | Service stopped. |
2900003 | Bluetooth switch is off. |
2900099 | Operation failed. |
Example
import { BusinessError } from '@ohos.base';
try {
connection.pairCredibleDevice('68:13:24:79:4C:8C', 0).then(() => {
console.info('PairCredibleDevice');
}, (err: BusinessError) => {
console.error('PairCredibleDevice:errCode' + err.code + ', errMessage: ' + err.message);
});
} catch (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).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.
ID | Error Message |
---|---|
2900001 | Service stopped. |
2900003 | Bluetooth switch is off. |
2900099 | Operation failed. |
Example
import { BusinessError } from '@ohos.base';
try {
connection.cancelPairedDevice('XX:XX:XX:XX:XX:XX');
} catch (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).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.
ID | Error Message |
---|---|
2900001 | Service stopped. |
2900003 | Bluetooth switch is off. |
2900099 | Operation failed. |
Example
import { BusinessError } from '@ohos.base';
try {
connection.cancelPairedDevice('XX:XX:XX:XX:XX:XX');
} catch (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).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.
ID | Error Message |
---|---|
2900001 | Service stopped. |
2900003 | Bluetooth switch is off. |
2900099 | Operation failed. |
Example
import { BusinessError } from '@ohos.base';
try {
connection.cancelPairingDevice('XX:XX:XX:XX:XX:XX');
} catch (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).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.
ID | Error Message |
---|---|
2900001 | Service stopped. |
2900003 | Bluetooth switch is off. |
2900099 | Operation failed. |
Example
import { BusinessError } from '@ohos.base';
try {
connection.cancelPairingDevice('XX:XX:XX:XX:XX:XX');
} catch (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).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>> | 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.
ID | Error Message |
---|---|
2900001 | Service stopped. |
2900003 | Bluetooth switch is off. |
2900099 | Operation failed. |
Example
import { BusinessError } from '@ohos.base';
try {
connection.getLocalProfileUuids('XX:XX:XX:XX:XX:XX', (err: BusinessError, data: Array<connection.ProfileUuids>) => {
console.info('getLocalProfileUuids, err: ' + JSON.stringify(err) + ', data: ' + JSON.stringify(data));
});
} catch (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).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>> | Promise used to return the profile UUIDs obtained. |
Error codes
For details about the error codes, see Bluetooth Error Codes.
ID | Error Message |
---|---|
2900001 | Service stopped. |
2900003 | Bluetooth switch is off. |
2900099 | Operation failed. |
Example
import { BusinessError } from '@ohos.base';
try {
connection.getLocalProfileUuids('XX:XX:XX:XX:XX:XX').then(() => {
console.info('getLocalProfileUuids');
}, (err: BusinessError) => {
console.error('getLocalProfileUuids: errCode' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
});
} catch (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).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>> | 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.
ID | Error Message |
---|---|
2900001 | Service stopped. |
2900003 | Bluetooth switch is off. |
2900099 | Operation failed. |
Example
import { BusinessError } from '@ohos.base';
try {
connection.getRemoteProfileUuids('XX:XX:XX:XX:XX:XX', (err: BusinessError, data: Array<connection.ProfileUuids>) => {
console.info('getRemoteProfileUuids, err: ' + JSON.stringify(err) + ', data: ' + JSON.stringify(data));
});
} catch (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).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>> | Promise used to return the profile UUIDs obtained. |
Error codes
For details about the error codes, see Bluetooth Error Codes.
ID | Error Message |
---|---|
2900001 | Service stopped. |
2900003 | Bluetooth switch is off. |
2900099 | Operation failed. |
Example
import { BusinessError } from '@ohos.base';
try {
connection.getRemoteProfileUuids('XX:XX:XX:XX:XX:XX').then(() => {
console.info('getRemoteProfileUuids');
}, (err: BusinessError) => {
console.error('getRemoteProfileUuids: errCode' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
});
} catch (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}
connection.connectAllowedProfiles11+
connectAllowedProfiles(deviceId: string, callback: AsyncCallback<void>): void
Connects all profiles allowed for a remote device. 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 target remote device, for example, 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.
ID | Error Message |
---|---|
201 | Permission denied. |
202 | Non-system applications are not allowed to use system APIs. |
401 | Invalid parameter. |
801 | Capability not supported. |
2900001 | Service stopped. |
2900003 | Bluetooth switch is off. |
2900099 | Operation failed. |
Example
import { BusinessError } from '@ohos.base';
try {
connection.connectAllowedProfiles('68:13:24:79:4C:8C', (err: BusinessError) => {
if (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
return;
}
console.info('connectAllowedProfiles, err: ' + JSON.stringify(err));
});
} catch (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}
connection.connectAllowedProfiles11+
connectAllowedProfiles(deviceId: string): Promise<void>
Connects all profiles allowed for a remote device. 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 target remote device, for example, XX:XX:XX:XX:XX. |
Return value
Type | Description |
---|---|
Promise<void> | Promise used to return the result. If the operation is successful, true is returned; otherwise, false is returned. |
Error codes
For details about the error codes, see Bluetooth Error Codes.
ID | Error Message |
---|---|
201 | Permission denied. |
202 | Non-system applications are not allowed to use system APIs. |
401 | Invalid parameter. |
801 | Capability not supported. |
2900001 | Service stopped. |
2900003 | Bluetooth switch is off. |
2900099 | Operation failed. |
Example
import { BusinessError } from '@ohos.base';
try {
connection.connectAllowedProfiles('68:13:24:79:4C:8C').then(() => {
console.info('connectAllowedProfiles');
}, (err: BusinessError) => {
console.error('connectAllowedProfiles:errCode' + err.code + ', errMessage: ' + err.message);
});
} catch (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}
connection.disconnectAllowedProfiles11+
disconnectAllowedProfiles(deviceId: string, callback: AsyncCallback<void>): void
Disconnects all connected profiles for a remote device.
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 target remote device, for example, XX:XX:XX:XX:XX. |
callback | AsyncCallback<void> | Yes | Callback invoked to return the result. This API uses an asynchronous callback 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.
ID | Error Message |
---|---|
201 | Permission denied. |
202 | Non-system applications are not allowed to use system APIs. |
401 | Invalid parameter. |
801 | Capability not supported. |
2900001 | Service stopped. |
2900003 | Bluetooth switch is off. |
2900099 | Operation failed. |
Example
import { BusinessError } from '@ohos.base';
try {
connection.disconnectAllowedProfiles('68:13:24:79:4C:8C', (err: BusinessError) => {
if (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
return;
}
console.info('disconnectAllowedProfiles, err: ' + JSON.stringify(err));
});
} catch (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}
connection.disconnectAllowedProfiles11+
disconnectAllowedProfiles(deviceId: string): Promise<void>
Disconnects all connected profiles for a remote device. 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 target remote device, for example, XX:XX:XX:XX:XX. |
Return value
Type | Description |
---|---|
Promise<void> | Promise used to return the result. If the operation is successful, true is returned; otherwise, false is returned. |
Error codes
For details about the error codes, see Bluetooth Error Codes.
ID | Error Message |
---|---|
201 | Permission denied. |
202 | Non-system applications are not allowed to use system APIs. |
401 | Invalid parameter. |
801 | Capability not supported. |
2900001 | Service stopped. |
2900003 | Bluetooth switch is off. |
2900099 | Operation failed. |
Example
import { BusinessError } from '@ohos.base';
try {
connection.disconnectAllowedProfiles('68:13:24:79:4C:8C').then(() => {
console.info('disconnectAllowedProfiles');
}, (err: BusinessError) => {
console.error('disconnectAllowedProfiles:errCode' + err.code + ', errMessage: ' + err.message);
});
} catch (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}
connection.getRemoteProductId11+
getRemoteProductId(deviceId: string): string
Obtains the product ID of a remote Bluetooth device.
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 remote device, for example, XX:XX:XX:XX:XX:XX. |
Return value
Type | Description |
---|---|
string | Product ID obtained. |
Error codes
For details about the error codes, see Bluetooth Error Codes.
ID | Error Message |
---|---|
2900001 | Service stopped. |
2900003 | Bluetooth switch is off. |
2900099 | Operation failed. |
Example
import connection from '@ohos.bluetooth.connection';
try {
let remoteDeviceProductId = connection.getRemoteProductId('XX:XX:XX:XX:XX:XX');
} catch (err) {
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}
PinRequiredParam
Represents the pairing request parameters.
System capability: SystemCapability.Communication.Bluetooth.Core
Name | Type | Readable | Writable | Description |
---|---|---|---|---|
pinType | PinType | Yes | No | Type of the device to pair. This is a system API. |
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. |