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

我正在开发一个应用程序,并有以下问题.

在使用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"));
    }
}
上一篇:关于Certificate、Provisioning Profile


下一篇:Failed to install one or more provisioning profiles on the device