harmony 鸿蒙@ohos.bluetooth.baseProfile (Bluetooth baseProfile Module)

2023-10-30 浏览 (521)

@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 '@ohos.bluetooth.baseProfile';

ConnectionStrategy

Enumerates the profile connection strategies.

System API: This is a system API.

System capability: SystemCapability.Communication.Bluetooth.Core

NameValueDescription
CONNECTION_STRATEGY_UNSUPPORTED0Default connection strategy to use when the device is not paired.
This is a system API.
CONNECTION_STRATEGY_ALLOWED1Connection strategy to use when the device is allowed to accept or initiate pairing.
This is a system API.
CONNECTION_STRATEGY_FORBIDDEN2Connection strategy to use when the device is not allowed to accept or initiate pairing.
This is a system API.

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.

baseProfile.setConnectionStrategy

setConnectionStrategy(deviceId: string, strategy: ConnectionStrategy, callback: AsyncCallback<void>): void

Sets the profile connection strategy for this 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

NameTypeMandatoryDescription
deviceIdstringYesAddress of the device to pair, for example, XX:XX:XX:XX:XX:XX.
strategyConnectionStrategyYesProfile connection strategy to set.
callbackAsyncCallback<void>YesCallback 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.

IDError Message
2900001Service stopped.
2900003Bluetooth switch is off.
2900004Profile is not supported.
2900099Operation failed.

Example

import { BusinessError } from '@ohos.base';
import a2dp from '@ohos.bluetooth.a2dp';
try {
    let a2dpSrc = a2dp.createA2dpSrcProfile();
    a2dpSrc.setConnectionStrategy('XX:XX:XX:XX:XX:XX', 0, (err: BusinessError) => {
        console.info('setConnectionStrategy, err: ' + JSON.stringify(err));
    });
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

baseProfile.setConnectionStrategy

setConnectionStrategy(deviceId: string, strategy: ConnectionStrategy): Promise<void>

Sets the profile connection strategy for this 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

NameTypeMandatoryDescription
deviceIdstringYesAddress of the device to pair, for example, XX:XX:XX:XX:XX:XX.
strategyConnectionStrategyYesProfile connection strategy to set.

Return value

TypeDescription
Promise<void>Promise used to return the result.

Error codes

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

IDError Message
2900001Service stopped.
2900003Bluetooth switch is off.
2900004Profile is not supported.
2900099Operation failed.

Example

import { BusinessError } from '@ohos.base';
import a2dp from '@ohos.bluetooth.a2dp';
try {
    let a2dpSrc = a2dp.createA2dpSrcProfile();
    a2dpSrc.setConnectionStrategy('XX:XX:XX:XX:XX:XX', 1).then(() => {
        console.info('setConnectionStrategy');
    }, (err: BusinessError) => {
        console.error('setConnectionStrategy errCode: ' + err.code + ', errMessage: ' + err.message);
    });
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

baseProfile.getConnectionStrategy

getConnectionStrategy(deviceId: string, callback: AsyncCallback<ConnectionStrategy>): void

Obtains the profile connection strategy. 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

NameTypeMandatoryDescription
deviceIdstringYesAddress of the device to pair, for example, XX:XX:XX:XX:XX:XX.
callbackAsyncCallback<ConnectionStrategy>YesCallback 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.

IDError Message
2900001Service stopped.
2900003Bluetooth switch is off.
2900004Profile is not supported.
2900099Operation failed.

Example

import { BusinessError } from '@ohos.base';
import a2dp from '@ohos.bluetooth.a2dp';
try {
    let a2dpSrc = a2dp.createA2dpSrcProfile();
    a2dpSrc.getConnectionStrategy('XX:XX:XX:XX:XX:XX', 0, (err: BusinessError, data: baseProfile.ConnectionStrategy) => {
        console.info('getConnectionStrategy, err: ' + JSON.stringify(err) + ', data: ' + JSON.stringify(data));
    });
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

baseProfile.getConnectionStrategy

getConnectionStrategy(deviceId: string): Promise<ConnectionStrategy>

Obtains the profile connection strategy. 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

NameTypeMandatoryDescription
deviceIdstringYesAddress of the device to pair, for example, XX:XX:XX:XX:XX:XX.

Return value

TypeDescription
Promise<ConnectionStrategy>Promise used to return the profile connection strategy obtained.

Error codes

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

IDError Message
2900001Service stopped.
2900003Bluetooth switch is off.
2900004Profile is not supported.
2900099Operation failed.

Example

import { BusinessError } from '@ohos.base';
import a2dp from '@ohos.bluetooth.a2dp';
try {
    let a2dpSrc = a2dp.createA2dpSrcProfile();
    a2dpSrc.getConnectionStrategy('XX:XX:XX:XX:XX:XX', 1).then((data: baseProfile.ConnectionStrategy) => {
        console.info('getConnectionStrategy');
    }, (err: BusinessError) => {
        console.error('getConnectionStrategy errCode: ' + err.code + ', errMessage: ' + err.message);
    });
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

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.

Error codes

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

IDError Message
2900001Service stopped.
2900003Bluetooth switch is off.
2900004Profile is not supported.
2900099Operation failed.

Example

import { BusinessError } from '@ohos.base';
import a2dp from '@ohos.bluetooth.a2dp';
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 Bluetooth Error Codes.

IDError Message
2900001Service stopped.
2900003Bluetooth switch is off.
2900004Profile is not supported.
2900099Operation failed.

Example

import { BusinessError } from '@ohos.base';
import a2dp from '@ohos.bluetooth.a2dp';
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.

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 invoked to return the profile connection state change.

Example

import { BusinessError } from '@ohos.base';
import a2dp from '@ohos.bluetooth.a2dp';
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 for the profile connection state change.

Example

import { BusinessError } from '@ohos.base';
import a2dp from '@ohos.bluetooth.a2dp';
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 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

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