加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – Android NFC设备所有者配置:发送自定义属性.可能吗?

发布时间:2020-12-15 04:34:48 所属栏目:Java 来源:网络整理
导读:我正在开发一个应用程序,并有以下问题. 在使用NFC进行设备所有者配置时,我想发送一个字符串,该字符串将由新设备所有者应用程序使用. 我知道设备所有者配置的标准MIME属性,找到了here 这是一个片段,可以让您更好地了解我的问题.注意“myCustomValue”属性. Pr
我正在开发一个应用程序,并有以下问题.

在使用NFC进行设备所有者配置时,我想发送一个字符串,该字符串将由新设备所有者应用程序使用.

我知道设备所有者配置的标准MIME属性,找到了here

这是一个片段,可以让您更好地了解我的问题.注意“myCustomValue”属性.

Properties properties = new Properties();
properties.put("myCustomValue",value);
properties.put(DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME,"com.example.some.app");
try {                    
    properties.store(stream,"NFC Provisioning");            
    ndefMessage = new NdefMessage(new NdefRecord[{NdefRecord.createMime(DevicePolicyManager.MIME_TYPE_PROVISIONING_NFC,stream.toByteArray())});
} catch (IOException e) {                         

}

这个片段在里面

public NdefMessage createNdefMessage(NfcEvent event)

你可以找到一个模板here

如果可能,我也想知道如何在配置的应用程序启动后立即检索该字符串值.

解决方法

下面的代码应该是您正在寻找的.为简洁起见,我只设置包名称加上两个将发送到DeviceAdminReceiver的字符串.

@Override
public NdefMessage createNdefMessage(NfcEvent event) {
    try {
        Properties p = new Properties();

        p.setProperty(
                DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME,"com.example.some.app");

        Properties extras = new Properties();
        extras.setProperty("Key1","TestString1");
        extras.setProperty("Key2","TestString2");
        StringWriter sw = new StringWriter();
        try{
            extras.store(sw,"admin extras bundle");
            p.put(DevicePolicyManager.EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE,sw.toString());
            Log.d(TAG,"Admin extras bundle=" + p.get(
                    DevicePolicyManager.EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE));
        } catch (IOException e) {
            Log.e(TAG,"Unable to build admin extras bundle");
        }

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        OutputStream out = new ObjectOutputStream(bos);
        p.store(out,"");
        final byte[] bytes = bos.toByteArray();

        NdefMessage msg = new NdefMessage(NdefRecord.createMime(
                DevicePolicyManager.MIME_TYPE_PROVISIONING_NFC,bytes));
        return msg;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

下一个片段将进入您的DeviceAdminReceiver,以便接收“Admin Extras”…如果您不覆盖onReceive,则需要覆盖onProfileProvisioningComplete并在其中处理EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE.

@Override
public void onReceive(Context context,Intent intent) {
    Log.d(TAG,"onReceive " + intent.getAction());
    if (ACTION_PROFILE_PROVISIONING_COMPLETE.equals(intent.getAction())) {
        PersistableBundle extras = intent.getParcelableExtra(
                EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE);
        Log.d(TAG,"onReceive Extras:" + extras.getString("Key1") + " / "  + extras.getString("Key2"));
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读