as

Settings
Sign out
Notifications
Alexa
亚马逊应用商店
AWS
文档
Support
Contact Us
My Cases
新手入门
设计和开发
应用发布
参考
支持

@amazon-devices/kepler-identifiers

@amazon-devices/kepler-identifiers

Kepler Identifiers API可以提供生成、检索和管理标识符的功能,这些标识符的例子包括UUID、设备名称和用户代理,以及跟踪和广告标识符。

开始使用

设置

  1. 将以下库依赖项添加到package.json文件的dependencies部分。

    已复制到剪贴板。

     "@amazon-devices/kepler-identifiers": "~0.1.0"
    
  2. 在您的manifest.toml中,添加访问设备易记型名称所需的以下权限。

    已复制到剪贴板。

     [[needs.privilege]]
     id = "com.amazon.devconf.privilege.identifiers.device-friendly-name"
    

用法

生成UUID v4

此示例演示如何生成UUID v4。该代码显示了一个简单的处理程序函数,该函数封装了generateUuidV4() 方法并实现了基本的错误处理。您可以使用此模式将生成UUID v4的功能集成到应用的事件处理程序或实用工具函数中。

已复制到剪贴板。

import {KeplerIdentifiers as Identifiers} from '@amazon-devices/kepler-identifiers';

const handleGenerateUuidV4 = () => {
  try {
    let result = Identifiers.generateUuidV4();
    return result;
  } catch (error) {
    console.error(error);
  }
};

获取设备的易记名称

getFriendlyDeviceName() 方法返回当前角色的设备的易记型名称。

返回以下项之一:

  • 用户自定义的设备名称,前提是该名称存在且调用者拥有com.amazon.devconf.privilege.identifiers.device-friendly-name运行时权限
  • 以下情况下的设备型号名称:
    • 未设置用户自定义的名称
    • 调用者缺乏所需的权限

已复制到剪贴板。

import {KeplerIdentifiers as Identifiers} from '@amazon-devices/kepler-identifiers';

const handleGetFriendlyDeviceName = async () => {
  try {
    let result = await Identifiers.getFriendlyDeviceName();
    return result;
  } catch (error) {
    console.error(error);
  }
};

申请运行时权限

要获得运行时权限,需要征得用户的同意。您可以使用安全管理器API申请“com.amazon.devconf.privilege.identifiers.device-friendly-name”运行时权限,从而打开用户同意对话框。

已复制到剪贴板。

import {SecurityManager, PrivilegeState} from '@amazon-devices/security-manager-lib';
import { KeplerIdentifiers as Identifiers } from '@amazon-devices/kepler-identifiers';

const DEVICE_NAME_PRIVILEGE = 'com.amazon.devconf.privilege.identifiers.device-friendly-name';
SecurityManager.getPrivilegeState(DEVICE_NAME_PRIVILEGE).then(
      function(state: PrivilegeState) {
        if (state == PrivilegeState.ALLOW) {
          // 用户已同意。
          return Identifiers.getFriendlyDeviceName();
        } else {
          // 征求用户同意
          // 此调用阻塞,每个应用都会将此调用集成到其线程模型或事件循环中
          SecurityManager.requestPrivilege(DEVICE_NAME_PRIVILEGE).then(
            function(state: PrivilegeState) {
              if (state == PrivilegeState.ALLOW) {
                // 用户现在已同意。
                return Identifiers.getFriendlyDeviceName();
              } else {
                // 用户不同意。
              }
            },
            function(error: Object) { KPLOG().error("申请权限失败:{}", error["message"]); }
          )
        }
      },
      function(error: Object) { KPLOG().error("无法获取权限状态:{}", error["message"]); }
    )

模块


Last updated: 2025年11月14日