简体   繁体   English

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

[英]Android NFC device owner provisioning: send custom properties. Is it possible?

I'm currently developing an app and have the following issue. 我正在开发一个应用程序,并有以下问题。

While using NFC for device owner provisioning, I would like to send a string, which would be used by the new device owner app. 在使用NFC进行设备所有者配置时,我想发送一个字符串,该字符串将由新设备所有者应用程序使用。

I'm aware of the standard MIME properties for device owner provisioning, found here 我知道设备所有者配置的标准MIME属性,可在此处找到

Here's a snippet that can give you a better visual of my issue. 这是一个片段,可以让您更好地了解我的问题。 Notice the "myCustomValue" property. 注意“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) {                         

}

This snippet lies inside 这个片段在里面

public NdefMessage createNdefMessage(NfcEvent event)

and you can find a template here 你可以在这里找到一个模板

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. 如果可能,我也想知道如何在配置的应用程序启动后立即检索该字符串值。

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. 为简洁起见,我只设置包名称加上两个将发送到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);
    }
}

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. 接下来的这个片段将进入你的DeviceAdminReceiver,以收到“管理工具” ......如果你没有覆盖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"));
    }
}

onProfileProvisioningComplete will be overwritten like this. onProfileProvisioningComplete将被覆盖。

@Override
public void onProfileProvisioningComplete(final Context context, final Intent intent)
{
    final PersistableBundle extras = intent.getParcelableExtra(DevicePolicyManager.EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE);
    Log.d(TAG, "onProfileProvisioningComplete Extras:" + extras.getString("Key1") + " / " + extras.getString("Key2"));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM