Device Owner on Android 5.0 (and others) whitout rooted devices, device provisioning by NFC

alex_au picture alex_au · Nov 13, 2014 · Viewed 11.5k times · Source

I need to know how to set my application as Device owner in Android 5.0, 4.4 and 4.3(?). I've yet tried the method for rooted devices (described in there), successfully. I saw that works great in android 5.0 and 4.4.2 emulator and in CyanoGen AOSP 4.4.4 (all rooted devices). But I must need to try this on other non rooted devices, in Android 5.0 Developer API you can read this

"To deploy and activate a device owner, you must perform an NFC data transfer from a programming app to the device while the device is in its unprovisioned state."

but I don't understand what it means, or better, what I've to do. Can someone help me, or explain me the step to do?

PS. I know what NFC is and how it works but I can't understand how to use for this issue.

Answer

Spynet picture Spynet · Nov 19, 2014

Create a NFC trigger application and install that on a device (other than the one on which you want to make your app as device owner) having NFC.

Following is the code for NFC trigger

public class MainActivity extends Activity implements CreateNdefMessageCallback {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
        nfcAdapter.setNdefPushMessageCallback(this, this);
    }

    @Override
    public NdefMessage createNdefMessage(NfcEvent event) {
        try {
            Properties p = new Properties();

            p.setProperty(
                    DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME,
                    "apk package name");
            p.setProperty(
                    DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_LOCATION,
                    "app download url");
            p.setProperty(
                    DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_CHECKSUM,
                    "apk checksum");
            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);
        }
    }
}

For checksum run following command

cat your_device_owner_app_name.apk | openssl dgst -binary -sha1 | openssl base64 | tr '+/' '-_' | tr -d '='​

  • Paste the generated checksum in NFC trigger code.
  • Compile and run NFC trigger app on device.

Now upload your application apk which you want to make as device owner on google drive or dropbox.

Take a fresh device or factory reset the device on which you want to set your application as device owner.

Reboot the device and on first screen bring your device containing NFC trigger application and touch for beam transfer.

Your application will be downloaded and will get installed as device owner.