HarmonyOS 订阅蓝牙开关的 打开状态 的公共事件失败?

订单了wifi和蓝牙 的开关,wifi连接和断开时,可以收到,但是每次都会收到2次。蓝牙开关的 开和关,收不到。

阅读 577
1 个回答

如果在订阅蓝牙开关的打开状态的公共事件时遇到失败,请检查是否已经获取了必要的权限,即ohos.permission.ACCESS\_BLUETOOTH,并且确保的设备具有SystemCapability.Communication.Bluetooth.Core系统能力。如果这些条件都满足但仍然遇到问题,请确认是否有系统错误码返回,如2900099,它表示操作失败。

以下是文档中的示例代码:

@ohos.bluetooth.access (蓝牙access模块):

import { BusinessError } from '@ohos.base';
function onReceiveEvent(access.BluetoothState) {
  console.info('bluetooth state = '+ JSON.stringify(data));
}
try {
  access.on('stateChange', onReceiveEvent);
} catch (err) {
  console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

@ohos.bluetooth.access (蓝牙access模块):
import { BusinessError } from '@ohos.base';
function onReceiveEvent(access.BluetoothState) {
  console.info('bluetooth state = '+ JSON.stringify(data));
}
try {
  access.on('stateChange', onReceiveEvent);
  access.off('stateChange', onReceiveEvent);
} catch (err) {
  console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

并且需要注意:订阅不能连续订阅,需要等第一个成功后才能进行第二个

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进