1 /* *****************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
21 package org.iotivity.ca;
23 import java.util.ArrayList;
25 import android.app.Activity;
26 import android.content.BroadcastReceiver;
27 import android.content.Context;
28 import android.content.Intent;
29 import android.content.IntentFilter;
30 import android.app.PendingIntent;
31 import android.content.IntentFilter.MalformedMimeTypeException;
32 import android.net.NetworkInfo;
33 import android.nfc.NdefMessage;
34 import android.nfc.NdefRecord;
35 import android.nfc.NfcAdapter;
36 import android.nfc.NfcEvent;
37 import android.os.Parcelable;
38 import android.util.Log;
42 public class CaNfcInterface implements NfcAdapter.CreateNdefMessageCallback {
43 private final static String MYTAG = CaNfcInterface.class.getSimpleName();
44 private static Context mContext;
45 private Activity mActivity;
46 private NfcAdapter mAdapter;
47 private NdefMessage mMessage;
48 private boolean misInvokeBeam;
50 private CaNfcInterface(Context context, Activity activity) {
51 Log.d(MYTAG, "NFC registerNfcReceiver");
55 mAdapter = NfcAdapter.getDefaultAdapter(mContext);
58 Log.e(MYTAG, "Failed to get the Adapter");
63 private native static void caNativeNfcPacketReceived(byte[] receivedData);
64 private native static NdefMessage caNativeNfcCreateNdefMessage(byte[] sendData);
65 private native static boolean caNativeNfcInvokeBeam();
68 public NdefMessage createNdefMessage(NfcEvent event) {
69 Log.d(MYTAG, "NFC createNdefMessage");
71 // Returns the already created message
75 public void processSendRquest(byte[] sendData) {
77 Log.d(MYTAG, "NFC processSendRquest IN");
79 mMessage = caNativeNfcCreateNdefMessage(sendData);
80 misInvokeBeam = caNativeNfcInvokeBeam();
84 Log.e(MYTAG, "NFC Beam error");
88 public void caNfcInitialize() {
89 Log.d(MYTAG, "caNfcInitialize");
91 if ((null == mActivity) || (null == mContext) || (null == mAdapter)) {
92 Log.e(MYTAG, "caNfcInitialize failed, invalid parameters");
96 PendingIntent pendingIntent = PendingIntent.getActivity(mActivity, 0,
97 new Intent(mActivity, mActivity.getClass())
98 .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
99 | Intent.FLAG_ACTIVITY_CLEAR_TOP), 0);
101 IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
104 ndef.addDataType("*/*"); /* Handles all MIME based dispatches.
105 You should specify only the ones that you need. */
106 } catch (MalformedMimeTypeException e) {
107 throw new RuntimeException("fail", e);
110 IntentFilter[] intentFiltersArray = new IntentFilter[] {ndef, };
112 mAdapter.enableForegroundDispatch(mActivity, pendingIntent, intentFiltersArray, null);
113 Log.d(MYTAG, " enableForegroundDispatch ");
115 mContext.registerReceiver(mReceiver, new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED));
116 Log.d(MYTAG, "NFC caNfcInitialize OUT");
119 private static BroadcastReceiver mReceiver = new BroadcastReceiver() {
121 public void onReceive(Context context, Intent intent) {
122 Log.d(MYTAG, "onReceive broadcast intent updated - disable callback");
124 if (intent.getAction().equals(NfcAdapter.ACTION_NDEF_DISCOVERED))
126 processIntent(intent);
130 private void processIntent(Intent intent) {
132 Log.d(MYTAG, "processIntent");
133 Parcelable[] rawMsgs = intent
134 .getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
135 NdefMessage msg = (NdefMessage) rawMsgs[0];
136 Log.d(MYTAG, msg.getRecords()[0].toMimeType().toString());
137 caNativeNfcPacketReceived(msg.getRecords()[0].getPayload());