匿名用户智慧生活APP
近距离通信服务
无线传输服务
场景联动
设备媒体
低功耗蓝牙连接问题
雷霆击碎黑暗* 从API16版本开始,@ohos.bluetooth.connection模块提供了connectAllowedProfiles接口,支持对HFP、HID、A2DP设备主动发起连接。
* 从API12版本开始,@ohos.bluetooth.connection模块提供了getRemoteProfileUuids接口,可用于查询对端蓝牙设备的profile协议能力,建议仅对已配对的设备调用该方法。
**【解决方案】**
connection.connectAllowedProfiles接口实现自动连接,注意需要先调用connection.pairDevice发起配对,且仅允许在每次发起配对后30s内调用此接口一次。
* 当与HID设备完成配对,调用connectAllowedProfiles接口成功建立连接后,后续每次重新打开蓝牙开关,系统都可以自动与HID设备建立连接。参考如下代码:
import { connection } from '@kit.ConnectivityKit';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct connectAllowedProfiles {
connectAllowedProfiles() {
// 发起蓝牙配对请求,此处的mac需要提前获取
connection.pairDevice('xx.xx.xx.xx', (err: BusinessError) => {
console.info(`pairDevice, device name err:${err}`);
});
// 订阅蓝牙配对状态变化事件
connection.on('bondStateChange', (data: connection.BondStateParam) => {
// 蓝牙已配对
if (data.state === connection.BondState.BOND_STATE_BONDED) {
// 调用getRemoteProfileUuids接口获取对端蓝牙支持的Profile类型
connection.getRemoteProfileUuids(data.deviceId,
(err: BusinessError, dataArray: Array<connection.ProfileUuids>) => {
console.error(`getRemoteProfileUuids errCode: ${(err as BusinessError).code}, errMessage: ${(err as BusinessError).message}, dataArray: ${dataArray}`);
// 当dataArray中支持类型包含A2DP、HFP和HID其中的一种,可调用connectAllowedProfiles接口发起连接。
connection.connectAllowedProfiles(data.deviceId, (err: BusinessError) => {
if (err) {
console.error(`connectAllowedProfiles errCode: ${(err as BusinessError).code}, errMessage: ${(err as BusinessError).message}`);
return;
}
console.info('connectAllowedProfiles');
});
});
}
});
}
build() {
Column() {
Button('connect Allowed Profiles')
.onClick(() => {
// 创建蓝牙配对状态变化监听,发起蓝牙配对,连接蓝牙
this.connectAllowedProfiles();
});
};
}
}* 如果在系统蓝牙界面使用手动点击的方式与HID设备建立连接,需要在已配对的设备配置信息中打开‘输入设备’开关,才能实现稳定回连。
- 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
- 如您发布的内容为转载内容,请注明内容来源。
我要提问题
HarmonyOS 关于蓝牙BLE做为主设备最多连接10台设备(API12+)
HarmonyOS 如何监听手表是否连接或断开蓝牙设备 (API12+)
HarmonyOS P2P连接配置,WifiP2pConfig中的参数详细说明 (API12+)
HarmonyOS 在没有配对的情况下,如何快速的连接目标蓝牙设备(API15+)
HarmonyOS 经典蓝牙连接配对未出现弹窗(API12+)
HarmonyOS 蓝牙配对成功后怎么连接 (API12+)
HarmonyOS 对于已经连接的蓝牙设备,断开连接后如何重连?(API12+)
HarmonyOS 如何获取蓝牙系统已配对设备列表 (API12+)




































苏公安网备 32011402010933号