[英]Android NFC App not pushing Ndef messages
我正在創建一個使用NFC在設備之間注冊觸摸的Android應用。 我正在使用兩個Nexus 7進行測試。
理想的用例是在一個設備上激活該應用程序,而在另一設備上不激活該應用程序。 主動設備將NdefMessage包含一條記錄的記錄推送到被動設備應用程序來處理。 被動設備將包含一些數據的記錄傳回主動應用。
我在清單中設置了以下意圖過濾器:
<activity android:name=".MainActivity" android:label="@string/title_activity_main">
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<data android:mimeType="application/com.killerapprejji.MainActivity"/>
<data android:mimeType="application/com.*"/>
<data android:mimeType="application/com.killerapprejji.*"/>
<data android:mimeType="application/*"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
在我的MainActivity中,我在onCreate中設置了以下內容來設置NFC適配器:
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
PendingIntent pendingIntent = PendingIntent.getActivity(
this, 0, new Intent(this,
getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
mNfcPendingIntent = pendingIntent;
// Intent filters for exchanging over p2p.
if(mNfcAdapter != null){
IntentFilter ndefDetected = new
IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
try {
ndef.addDataType("application/*");
}
catch (MalformedMimeTypeException e) {
throw new RuntimeException("fail", e);
}
this.intentFiltersArray = new IntentFilter[] {ndef, };
Log.d(this.toString(), "mNfcPendingIntent: ");
mNdefExchangeFilters = new IntentFilter[] { ndefDetected,defendDetected };
}
我的onNewIntent有以下內容:
protected void onNewIntent(Intent intent) {
// NDEF exchange mode
Log.d("onNewIntent", intent.getAction());
if (NfcAdapter.ACTION_NDEF_DISCOVERED == (intent.getAction())) {
NdefMessage[] msgs = getNdefMessages(intent);
for(int i = 0; i < msgs.length; i++){
Log.d("onNewIntent", "found new NdefMessage");
}
}
finish();
}
現在,我運行此調用:
public void setIdleMessage(){
InteractionHistory intHist = InteractionHistory.getInstance();
NdefMessage attackNdefMessage = null;
NdefRecord[] ndefRecords = new NdefRecord[10];
ndefRecords[0] = NdefRecord.createMime("application/com.killerapprejji.NfcHandle", new String("attack,attacker:"
+ intHist.getDisplayName()
+ ",attackerid:" + "1").getBytes());
attackNdefMessage = new NdefMessage(ndefRecords[0]);
// need to come up with a way to end if the above try/catch fails
mNfcAdapter.setNdefPushMessage(attackNdefMessage, this);
}
期望它設置NdefPushMessage。 每當我將兩台設備置於NFC范圍內時,我仍然只能獲得可選的“觸摸到光束”界面。
關於如何獲取這些意圖的任何想法,或者我的NdefMessage是否能按我的期望發送?
“ Touch to Beam”消息是Google定義的一種消息,用於將您希望共享的NDEF消息推送到setNdefPushMessage上,因此在您推送該屏幕之前,不會將任何內容發送到另一台設備。
實際上,您從Android使用的API稱為Android BEAM
換句話說 ,如果一個設備正在推送NDEF消息, 則在您停止推送之前它不會接收到消息 ,因此您必須實現NDefPushCallback來捕獲NDEF消息的成功發送,停止推送第一個設備,然后再推送將能夠從其他設備接收新的意圖(接收到NDEF消息)
我建議您首先使用URI MIME類型或純文本(很簡單)來制作一個測試應用程序,以確保您的“共享邏輯”正常。 確認后,您可以回滾到自己的啞劇類型
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.