Example usage for android.app.admin DevicePolicyManager MIME_TYPE_PROVISIONING_NFC

List of usage examples for android.app.admin DevicePolicyManager MIME_TYPE_PROVISIONING_NFC

Introduction

In this page you can find the example usage for android.app.admin DevicePolicyManager MIME_TYPE_PROVISIONING_NFC.

Prototype

String MIME_TYPE_PROVISIONING_NFC

To view the source code for android.app.admin DevicePolicyManager MIME_TYPE_PROVISIONING_NFC.

Click Source Link

Document

This MIME type is used for starting the device owner provisioning.

Usage

From source file:com.example.android.nfcprovisioning.NfcProvisioningFragment.java

@Override
public NdefMessage createNdefMessage(NfcEvent event) {
    if (mProvisioningValues == null) {
        return null;
    }/*  www.ja v a  2  s .c  o  m*/
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    Properties properties = new Properties();
    // Store all the values into the Properties object
    for (Map.Entry<String, String> e : mProvisioningValues.entrySet()) {
        if (!TextUtils.isEmpty(e.getValue())) {
            String value;
            if (e.getKey().equals(DevicePolicyManager.EXTRA_PROVISIONING_WIFI_SSID)) {
                // Make sure to surround SSID with double quotes
                value = e.getValue();
                if (!value.startsWith("\"") || !value.endsWith("\"")) {
                    value = "\"" + value + "\"";
                }
            } else {
                value = e.getValue();
            }
            properties.put(e.getKey(), value);
        }
    }
    // Make sure to put local time in the properties. This is necessary on some devices to
    // reliably download the device owner APK from an HTTPS connection.
    if (!properties.contains(DevicePolicyManager.EXTRA_PROVISIONING_LOCAL_TIME)) {
        properties.put(DevicePolicyManager.EXTRA_PROVISIONING_LOCAL_TIME,
                String.valueOf(System.currentTimeMillis()));
    }
    try {
        properties.store(stream, getString(R.string.nfc_comment));
        NdefRecord record = NdefRecord.createMime(DevicePolicyManager.MIME_TYPE_PROVISIONING_NFC,
                stream.toByteArray());
        return new NdefMessage(new NdefRecord[] { record });
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}