Java – Android NFC device owner configuration: send custom attributes Is it possible?
I am developing an application and have the following problems
When using NFC for device owner configuration, I want to send a string that will be used by the new device owner application
I know the standard mime attribute configured by the device owner and found here
This is a clip that can give you a better understanding of my problem Note the "mycustomvalue" attribute
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 clip is inside
public NdefMessage createNdefMessage(NfcEvent event)
You can find a template here
If possible, I also want to know how to retrieve the string value immediately after the configured application starts
Solution
The following code should be what you are looking for For brevity, I only set the package name plus two strings to be sent to 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); } }
The next fragment will enter your deviceadminreceiver to receive "admin extras"... If you do not overwrite onReceive, you need to overwrite onprofileprovisioningcomplete and process extras in it_ 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")); } }