OpenHarmony设备如何获取类似HMS的设备唯一标识(OAID)?

OpenHarmony设备如何获取类似HMS的设备唯一标识(OAID)? 我们的广告 SDK 需要匿名设备 ID,但 OpenHarmony 没有 OAID 服务。怎么办?

7 回复

【解决方案】

  1. 配置相关权限: 在应用的module.json5文件中添加ohos.permission.APP_TRACKING_CONSENT权限配置。确保该权限被正确声明,以便应用能够请求获取OAID的权限。该权限为user_grant权限,当申请的权限为user_grant权限时,reason,abilities标签必填,具体申请方式请参见声明权限

  2. 跨应用关联访问权限: 可以通过在应用内提供明确的指引或说明,引导用户前往设置-隐私安全-跨应用关联,手动开启允许“跨应用关联访问权限”,告知用户开启该权限的必要性和用途,以提高用户的授权意愿。比如通过代码跳转设置-隐私二级页面。

  3. 用户手动授权: 调用requestPermissionsFromUser接口弹窗,提示并引导用户允许对应权限。

import { BusinessError } from '@kit.BasicServicesKit';
import { abilityAccessCtrl, common, Want } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { identifier } from '@kit.AdsKit';

@Entry
@Component
struct Index {
  context = this.getUIContext().getHostContext() as common.UIAbilityContext;
  @State oaid: string = '';

  jumpToSetting() {
    let want: Want = {
      bundleName: 'com.huawei.hmos.settings',
      abilityName: 'com.huawei.hmos.settings.MainAbility',
      uri: 'privacy_settings',
      parameters: {
        // 传对应应用的包名
        pushParams: 'com.example.myapplication'
      }
    };
    this.context.startAbility(want);
  }

  requestOAIDTrackingConsentPermissions(context: common.Context): void {
    // 进入页面时,向用户请求授权广告跨应用关联访问权限
    const atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager();
    try {
      atManager.requestPermissionsFromUser(context, ['ohos.permission.APP_TRACKING_CONSENT']).then((data) => {
        if (data.authResults[0] === 0) {
          hilog.info(0x0000, 'testTag', '%{public}s', 'succeeded in requesting permission');
          identifier.getOAID((err: BusinessError, data: string) => {
            if (err.code) {
              hilog.error(0x0000, 'testTag', '%{public}s', `get oaid failed, error: ${err.code} ${err.message}`);
            } else {
              this.oaid = data;
              hilog.info(0x0000, 'testTag', '%{public}s', `succeeded in getting oaid by callback , oaid: ${this.oaid}`);
            }
          });
        } else {
          hilog.error(0x0000, 'testTag', '%{public}s', 'user rejected');
        }
      }).catch((err: BusinessError) => {
        hilog.error(0x0000, 'testTag', '%{public}s', `request permission failed, error: ${err.code} ${err.message}`);
      });
    } catch (err) {
      hilog.error(0x0000, 'testTag', '%{public}s', `catch err->${err.code}, ${err.message}`);
    }
  }

  build() {
    Column({ space: 20 }) {
      Button('跳转设置')
        .onClick(() => {
          this.jumpToSetting();
        });
      Button('获取oaid')
        .onClick(() => {
          this.requestOAIDTrackingConsentPermissions(this.context);
        });
      Text(this.oaid);
    }
    .height('100%')
    .width('100%');
  }
}

identifier.getOAID

本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

使用获取开放匿名设备标识符接口,需向用户申请授权(默认开启权限):ohos.permission.APP_TRACKING_CONSENT。

支持设备PhonePC/2in1TabletTV

getOAID(): Promise<string>

获取开放匿名设备标识符(OAID)。使用Promise异步回调。

需要权限: ohos.permission.APP_TRACKING_CONSENT

系统能力: SystemCapability.Advertising.OAID

返回值:

类型 说明
Promise<string> Promise对象,返回开放匿名设备标识符(OAID)。
1.如应用已配置ohos.permission.APP_TRACKING_CONSENT权限,且“跨应用关联访问权限”为“允许”,则返回OAID。
2.如应用已配置ohos.permission.APP_TRACKING_CONSENT权限,且“跨应用关联访问权限”为“禁止”,则返回00000000-0000-0000-0000-000000000000。
3.如应用未配置ohos.permission.APP_TRACKING_CONSENT权限,则返回00000000-0000-0000-0000-000000000000。

说明

设置项“跨应用关联访问权限”在HarmonyOS NEXT Developer Beta5及更早版本名称为“应用跟踪访问权限”。

import { identifier } from '@kit.AdsKit';
identifier.getOAID().then((data: string) => {
  const oaid: string = data;
});

导入ohos.identifier.oaid (开放匿名设备标识服务)

import { identifier } from ‘@kit.AdsKit’;

identifier.getOAID().then((data: string) => { const oaid: string = data; });

OpenHarmony设备可通过@ohos.deviceInfo模块获取设备唯一标识。使用deviceInfo.getDeviceId()接口可获取设备UDID,该标识符在设备生命周期内保持不变且具有唯一性。需在module.json5中声明ohos.permission.DISTRIBUTED_DATASYNC权限。

在OpenHarmony中,目前没有直接对标HMS OAID的官方服务。但可以通过以下方式获取匿名设备标识:

  1. 使用系统提供的DeviceId

    • 通过@ohos.deviceInfo模块获取设备UDID
    • 注意:需要申请ohos.permission.READ_SYSTEM_INFO权限
  2. 生成自定义匿名ID

    • 结合设备硬件信息生成哈希值
    • 建议使用:设备型号 + 序列号 + 其他不可变硬件信息
    • 使用SHA-256等加密算法确保匿名性
  3. 考虑隐私合规

    • 用户可重置标识
    • 提供用户禁用选项
    • 遵循最小化数据收集原则
  4. 存储方案

    • 使用Preferences持久化存储生成的ID
    • 首次生成后保持不变,除非用户重置

示例代码框架:

import deviceInfo from '@ohos.deviceInfo';
import cryptoFramework from '@ohos.cryptoFramework';

// 生成设备标识哈希
async function generateDeviceId() {
  const info = deviceInfo.getDeviceInfo();
  const input = info.model + info.serialNumber;
  // 使用加密框架生成哈希值
  // ... 具体实现
}

注意:不同设备厂商的OpenHarmony实现可能有差异,建议在实际设备上进行充分测试。

回到顶部