簡體   English   中英

Android NFC設備所有者配置:發送自定義屬性。可能嗎?

[英]Android NFC device owner provisioning: send custom properties. Is it possible?

我正在開發一個應用程序,並有以下問題。

在使用NFC進行設備所有者配置時,我想發送一個字符串,該字符串將由新設備所有者應用程序使用。

我知道設備所有者配置的標准MIME屬性,可在此處找到

這是一個片段,可以讓您更好地了解我的問題。 注意“myCustomValue”屬性。

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) {                         

}

這個片段在里面

public NdefMessage createNdefMessage(NfcEvent event)

你可以在這里找到一個模板

如果可能,我也想知道如何在配置的應用程序啟動后立即檢索該字符串值。

下面的代碼應該是您正在尋找的。 為簡潔起見,我只設置包名稱加上兩個將發送到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);
    }
}

接下來的這個片段將進入你的DeviceAdminReceiver,以收到“管理工具” ......如果你沒有覆蓋onReceive,onProfileProvisioningComplete需要與在那里,而不是處理EXTRA_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"));
    }
}

onProfileProvisioningComplete將被覆蓋。

@Override
public void onProfileProvisioningComplete(final Context context, final Intent intent)
{
    final PersistableBundle extras = intent.getParcelableExtra(DevicePolicyManager.EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE);
    Log.d(TAG, "onProfileProvisioningComplete Extras:" + extras.getString("Key1") + " / " + extras.getString("Key2"));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM