Android NFC设备所有者设置:发送自定义属性.是否有可能? [英] Android NFC device owner provisioning: send custom properties. Is it possible?

查看:253
本文介绍了Android NFC设备所有者设置:发送自定义属性.是否有可能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在开发一个应用程序,并且存在以下问题.

I'm currently developing an app and have the following issue.

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

While using NFC for device owner provisioning, I would like to send a string, which would be used by the new device owner app.

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

I'm aware of the standard MIME properties for device owner provisioning, found here

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

Here's a snippet that can give you a better visual of my issue. Notice the "myCustomValue" property.

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)

,您可以找到模板在可能的情况下,我还想知道如何在配置的应用程序启动后立即检索该字符串值.

In case this is possible, I also would like to know how to retrieve that string value as soon as the provisioned app has started.

推荐答案

下面的代码应该是您想要的.为简便起见,我只设置了程序包名称和两个字符串,该字符串将被发送到您的DeviceAdminReceiver.

The code below should be what you're looking for. For brevity, I only set the package name plus two strings that will be sent to your 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中,以接收管理员附加信息" ...如果您不覆盖 onReceive ,则需要 onProfileProvisioningComplete 在那里被EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE覆盖了.

This next snippet will go in your DeviceAdminReceiver in order to receive the "Admin Extras"... If you don't override onReceive, onProfileProvisioningComplete will need to be overridden with EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE handled in there instead.

@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"));
    }
}

这篇关于Android NFC设备所有者设置:发送自定义属性.是否有可能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆