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.