android / enterprise-samples

Multiple samples showing the best practices in enterprise on Android.
Apache License 2.0
387 stars 241 forks source link

How to pass Enrolment Token using NFC sample #32

Closed zubairehman closed 4 years ago

zubairehman commented 4 years ago

Hi,

I have been using NFC sample to enroll an Android device, I followed all the steps mentioned here: https://developers.google.com/android/work/play/emm-api/prov-devices#nfc_method

I have been passing WiFi info, locale, the DPC package and it seems all of them are working and my device gets connected with the WiFI but the enrolment token is not working. After taking a while, it says updating the device and take me to enter/scan the QR code screen to continue.

here is my code, where I am adding enrolment token:

private void gatherAdminExtras(HashMap<String, String> values) {
        Properties props = new Properties();
        Set<String> keys = new HashSet<>(values.keySet());
        for (String key : keys) {
            if (key.startsWith("android.app.extra")) {
                continue;
            }
            props.put(key, values.get(key));
            values.remove(key);
        }
        StringWriter sw = new StringWriter();
        try{
            props.store(sw, "{\"com.google.android.apps.work.clouddpc.EXTRA_ENROLLMENT_TOKEN\": \"{Enrolment_Token}\"}");
            values.put(DevicePolicyManager.EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE,
                    sw.toString());
            Log.d(TAG, "Admin extras bundle=" + values.get(
                    DevicePolicyManager.EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE));
        } catch (IOException e) {
            Log.e(TAG, "Unable to build admin extras bundle");
        }
    }

Help appreciated, thanks.