11
Android NFC P2P模式開發 Android Application Development of NFC Peer-to-Peer Mode Chun-Kai Wang (王雋凱) IDSL - Dept. of IM - NTUST

Android Application Development of NFC Peer-to-Peer Mode

Embed Size (px)

Citation preview

Page 1: Android Application Development of NFC Peer-to-Peer Mode

Android NFC P2P模式開發Android Application Development of NFC Peer-to-Peer Mode

Chun-Kai Wang (王雋凱)

IDSL - Dept. of IM - NTUST

Page 2: Android Application Development of NFC Peer-to-Peer Mode

NFC Peer-to-Peer Mode

▪ Description from Android Developers:

▪ “P2P mode, allowing the NFC device to exchange data with other NFC peers; this operation mode is used by Android Beam.”

2

Page 3: Android Application Development of NFC Peer-to-Peer Mode

Android Beam▪ NFC P2P模式是指兩個 NFC裝置在接近彼此時完成資料的交換。

▪ 在 Android API 14+後,將兩個 Android 裝置之間透過 P2P傳輸資料稱為 Beam。

3

Page 4: Android Application Development of NFC Peer-to-Peer Mode

How to use Android Beam?▪ 必須開啟手機的 Android Beam 功能。

▪ 要實現 Beam NDEF 訊息的應用程式 (發送端)必須在前台,不能是後台服務,不能處在螢幕鎖定狀態。

▪ 要接收 Beam 訊息的手機 (接收端),其螢幕不能處在螢幕鎖定狀態。

▪ 要在 Android 手機裝置中完成 Beam 功能,其手機系統版本必須是 API 14+。

▪ 兩個手機在進行彼此接近實現 Beam功能時,Android 手機畫面會顯示「Touch to Beam」,此時,使用者應該觸碰需要 Beam NDEF 訊息的裝置。

4

Page 5: Android Application Development of NFC Peer-to-Peer Mode

Beaming NDEF Messages▪ 使用 NfcAdapter物件提供的兩種方法實現 Beam 功能。

▪ setNdefPushMessage()

1. 建立 NdefMessage物件。

2. 呼叫 setNdefPushMessage() 傳入 NdefMessage物件

▪ setNdefPushMessageCallback()

1. 在 Activity 中實作 CreateNdefMessageCallback介面。

2. 呼叫 setNdefPushMessageCallback() 方法。

3. 在回呼函式 (createNdefMessage(NfcEvent))中實現 Beam Data。

5

• enableForegroundNdefPush() & disableForegroundNdefPush()

• 在 API 10 中加入 Android NFC P2P 方法,該方法已經在 API 14 被標示為@Deprecated,被 setNdefPushMessage() 所取代。

• 可使用此方法支援較舊版本的Android NFC 裝置 (API 10 ~ 13)。

Page 6: Android Application Development of NFC Peer-to-Peer Mode

Beaming NDEF Messages (Cont.)

▪ public void setNdefPushMessage (NdefMessage message, Activity activity, Activity... activities)

▪ 透過 Android Beam 發送靜態 NDEF Message。

▪ 在 Activity 呼叫該方法時,可以在生命週期 onDestroy() 之前,一般是在 onCreate() 中呼叫。

▪ 同時使用該方法和 setNdefPushMessageCallback() 方法時,setNdefPushMessageCallback()具有較高優先順序。

▪ 使用該方法時,如果 NdefMessage 為 null,則呼叫該方法的 Activity 的setNdefPushMessage 功能將 disable。

6

Page 7: Android Application Development of NFC Peer-to-Peer Mode

Beaming NDEF Messages (Cont.)

▪ public void setNdefPushMessageCallback (NfcAdapter.CreateNdefMessageCallback callback, Activity activity, Activity... activities)

▪ 透過 Android Beam 發送經由 callback 函式所動態生成的 NDEF Message。

▪ 在 Activity 呼叫該方法時,可以在生命週期 onDestroy() 之前,一般是在 onCreate() 中呼叫。

▪ 同時使用該方法和 setNdefPushMessage() 方法時,該方法具有較高優先順序。

▪ 使用該方法時,如果 callback 為 null,則 Activity 的 NDEF Push 功能將 disable。

7

Page 8: Android Application Development of NFC Peer-to-Peer Mode

Beaming NDEF Messages (Cont.)

▪ setNdefPushMessage() vs. setNdefPushMessageCallback()

▪ 當應用程式 Activity 需要在任何時候都發送相同的 NDEF Message 時,可使用setNdefPushMessage() 方法。

▪ 當應用程式 Activity 希望根據使用者不同的操作行為來進行發送時,可使用setNdefPushMessageCallback() 方法。

▪ 當 Activity 中兩者方法都使用時,NDEF Message 只會在一處方法中被 Push,而setNdefPushMessageCallback() 方法的優先順序高於 setNdefPushMessage() 方法。

▪ 在兩個 Android NFC 裝置接近時,如果發送端目前開啟的應用程式並沒有實現 Android Beam 功能,系統會發送一筆預設的 NDEF Message。

▪ 如果想要阻止 Android 系統發送預設的 NDEF Message,可在 AndroidManifest.xml 中的application 增加以下程式:

8

Page 9: Android Application Development of NFC Peer-to-Peer Mode

Receiving Beam Message▪ 接收 Beam 訊息的方法與接收 Tag 訊息類似,實現步驟如下:

1. 在 Activity 中實作 onNewIntent(Intent) 方法,該方法會呼叫 setIntent(Intent)。在使用 onNewIntent(Intent) 方法後,onResume() 方法會自動呼叫。

2. 在 Activity的 onResume() 方法中,檢測目前訊息是否來自 Beam,如果是,取得並處理該 NDEF 訊息。

3. 呼叫自定義的訊息解析函式,將取得的NDEF訊息解析並取得 Payload,再對 Payload 進行進一步的 UI操作。

9

protected void onNewIntent(Intent intent) {// onResume gets called after this to handle the intentsetIntent(intent);

}

protected void onResume() {super.onResume();// Check to see that the Activity started due to an Android Beamif (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {

processIntent(getIntent());}

}

private void processIntent(Intent intent) {// your code here...

}

Page 10: Android Application Development of NFC Peer-to-Peer Mode

Sample Project – AndroidBeamDemo▪ Android Beam 的操作可以參考

Android 官方的 Sample Project。

▪ Eclipse → File → New → Other → Android → Android Sample Project → Android 4.x → AndroidBeamDemo

10

Page 11: Android Application Development of NFC Peer-to-Peer Mode

Thank You!