openharmony 鸿蒙 js-apis-bluetooth-baseProfile

2025-06-12 浏览 (1)

@ohos.bluetooth.baseProfile (Bluetooth baseProfile Module)

The baseProfile module provides APIs for using basic Bluetooth profiles.

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

import { baseProfile } from '@kit.ConnectivityKit';

ProfileConnectionState

type ProfileConnectionState = constant.ProfileConnectionState

Defines the profile connection status of the Bluetooth device.

System capability: SystemCapability.Communication.Bluetooth.Core

TypeDescription
constant.ProfileConnectionStateProfile connection status of the Bluetooth device.

StateChangeParam

Represents the profile state change parameters.

System capability: SystemCapability.Communication.Bluetooth.Core

NameTypeReadableWritableDescription
deviceIdstringYesNoAddress of the Bluetooth device.
stateProfileConnectionStateYesNoProfile connection state of the device.
cause12+DisconnectCauseYesNoCause of the disconnection.

DisconnectCause12+

Enumerates the possible causes of a Bluetooth disconnection.

System capability: SystemCapability.Communication.Bluetooth.Core

NameValueDescription
USER_DISCONNECT0The user proactively disconnects the connection.
CONNECT_FROM_KEYBOARD1The connection should be initiated from a keyboard.
CONNECT_FROM_MOUSE2The connection should be initiated from a mouse device.
CONNECT_FROM_CAR3The connection should be initiated from a head unit side.
TOO_MANY_CONNECTED_DEVICES4The number of connections exceeds the limit.
CONNECT_FAIL_INTERNAL5Internal error.

BaseProfile.getConnectedDevices

getConnectedDevices(): Array<string>

Obtains the connected devices.

Required permissions: ohos.permission.ACCESS_BLUETOOTH

System capability: SystemCapability.Communication.Bluetooth.Core

Return value

TypeDescription
Array<string>Addresses of the connected devices. For security purposes, the device addresses obtained are random MAC addresses.
The random MAC address remains unchanged after a device is paired successfully.
The random address changes if the paired device is unpaired and scanned again or the Bluetooth service is turned off.

Error codes

For details about the error codes, see Universal Error Codes and Bluetooth Error Codes.

IDError Message
201Permission denied.
801Capability not supported.
2900001Service stopped.
2900003Bluetooth disabled.
2900004Profile not supported.
2900099Operation failed.

Example

import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
import { a2dp } from '@kit.ConnectivityKit';
try {
    let a2dpSrc = a2dp.createA2dpSrcProfile();
    let retArray = a2dpSrc.getConnectedDevices();
} catch (err) {
    console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message);
}

BaseProfile.getConnectionState

getConnectionState(deviceId: string): ProfileConnectionState

Obtains the profile connection state of a device.

Required permissions: ohos.permission.ACCESS_BLUETOOTH

System capability: SystemCapability.Communication.Bluetooth.Core

Parameters

NameTypeMandatoryDescription
deviceIdstringYesAddress of the remote device.

Return value

TypeDescription
ProfileConnectionStateProfile connection state obtained.

Error codes

For details about the error codes, see Universal Error Codes and Bluetooth Error Codes.

IDError Message
201Permission denied.
401Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.
801Capability not supported.
2900001Service stopped.
2900003Bluetooth disabled.
2900004Profile not supported.
2900099Operation failed.

Example

import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
import { a2dp } from '@kit.ConnectivityKit';
try {
    let a2dpSrc = a2dp.createA2dpSrcProfile();
    let ret = a2dpSrc.getConnectionState('XX:XX:XX:XX:XX:XX');
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

BaseProfile.on('connectionStateChange')

on(type: 'connectionStateChange', callback: Callback<StateChangeParam>): void

Subscribes to profile connection state changes. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.ACCESS_BLUETOOTH

System capability: SystemCapability.Communication.Bluetooth.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is connectionStateChange, which indicates a profile connection state change event.
callbackCallback<StateChangeParam>YesCallback used to return the profile connection state change.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
201Permission denied.
401Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.
801Capability not supported.

Example

import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
import { a2dp } from '@kit.ConnectivityKit';
function onReceiveEvent(data: baseProfile.StateChangeParam) {
    console.info('a2dp state = '+ JSON.stringify(data));
}
try {
    let a2dpSrc = a2dp.createA2dpSrcProfile();
    a2dpSrc.on('connectionStateChange', onReceiveEvent);
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

BaseProfile.off('connectionStateChange')

off(type: 'connectionStateChange', callback?: Callback<StateChangeParam>): void

Unsubscribes from profile connection state changes.

Required permissions: ohos.permission.ACCESS_BLUETOOTH

System capability: SystemCapability.Communication.Bluetooth.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is connectionStateChange, which indicates a profile connection state change event.
callbackCallback<StateChangeParam>NoCallback to unregister.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
201Permission denied.
401Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.
801Capability not supported.

Example

import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
import { a2dp } from '@kit.ConnectivityKit';
function onReceiveEvent(data: baseProfile.StateChangeParam) {
    console.info('a2dp state = '+ JSON.stringify(data));
}
try {
    let a2dpSrc = a2dp.createA2dpSrcProfile();
    a2dpSrc.on('connectionStateChange', onReceiveEvent);
    a2dpSrc.off('connectionStateChange', onReceiveEvent);
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Connectivity Kit (Short-Range Communication Service)

harmony 鸿蒙Bluetooth

harmony 鸿蒙Wifi

harmony 鸿蒙Bluetooth Error Codes

harmony 鸿蒙NFC Error Codes

harmony 鸿蒙SecureElement Error Codes

harmony 鸿蒙Wi-Fi Error Codes

harmony 鸿蒙@ohos.bluetooth.a2dp (Bluetooth A2DP Module) (System API)

harmony 鸿蒙@ohos.bluetooth.a2dp (Bluetooth A2DP Module)

harmony 鸿蒙@ohos.bluetooth.access (Bluetooth Access Module) (System API)

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/12219k