NFCManager.java
5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package com.huaheng.mes.nfc;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentFilter.MalformedMimeTypeException;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.IsoDep;
import android.nfc.tech.MifareClassic;
import android.nfc.tech.Ndef;
import android.nfc.tech.NfcA;
import android.nfc.tech.NfcB;
import android.nfc.tech.NfcF;
import android.nfc.tech.NfcV;
import android.os.Build;
import android.util.Log;
import java.io.IOException;
public class NFCManager {
private static NFCManager instance;
private NfcAdapter nfcAdapter;
private PendingIntent mPendingIntent;
private IntentFilter[] mFilters;
private String[][] mTechLists;
// 是否是NFC请求
private boolean isNfcRequest = false;
private NFCListener mNFCListener;
private Activity mActivity;
private IsoDep na;
private NFCManager() {
}
public static NFCManager getInstance() {
if (instance == null)
instance = new NFCManager();
return instance;
}
@SuppressLint("NewApi")
public void init(Context context) {
if (Build.VERSION.SDK_INT < 10) {
return;
}
try {
nfcAdapter = NfcAdapter.getDefaultAdapter(context);
} catch (Exception e1) {
e1.printStackTrace();
nfcAdapter = null;
return;
}
// 判断2
if (nfcAdapter == null) {
// 如果手机不支持NFC,或者NFC没有打开就直接返回
Log.d(this.getClass().getName(), "手机不支持NFC功能!");
return;
}
// 三种Activity NDEF_DISCOVERED ,TECH_DISCOVERED,TAG_DISCOVERED
// 指明的先后顺序非常重要, 当Android设备检测到有NFC Tag靠近时,会根据Action申明的顺序给对应的Activity
// 发送含NFC消息的 Intent.
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
IntentFilter tech = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
IntentFilter tag = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
try {
ndef.addDataType("*/*");
} catch (MalformedMimeTypeException e) {
throw new RuntimeException("fail", e);
}
mFilters = new IntentFilter[] { ndef, tech, tag };
mTechLists = new String[][] { new String[] { Ndef.class.getName(), MifareClassic.class.getName(),
NfcA.class.getName(),NfcB.class.getName(),NfcV.class.getName(),NfcF.class.getName() } };
if (!nfcAdapter.isEnabled()) {
Log.d(this.getClass().getName(), "手机NFC功能没有打开!");
enableDialog(context);
return;
}
}
private void enableDialog(final Context context) {
AlertDialog.Builder ab = new AlertDialog.Builder(context);
ab.setTitle("提醒");
ab.setMessage("手机NFC开关未打开,是否现在去打开?");
ab.setNeutralButton("否", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
ab.setNegativeButton("是", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
context.startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
dialog.dismiss();
}
});
ab.create().show();
}
@SuppressLint("NewApi")
public void onPause(Activity activity) {
if (nfcAdapter != null) {
nfcAdapter.disableForegroundDispatch(activity);
}
mActivity = null;
}
@SuppressLint("NewApi")
public void onResume(Activity activity) {
if (nfcAdapter != null) {
if (mActivity == null) {
mActivity = activity;
mPendingIntent = PendingIntent.getActivity(mActivity, 0,
new Intent(mActivity, mActivity.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
nfcAdapter.enableForegroundDispatch(activity, mPendingIntent, mFilters, mTechLists);
} else {
if (!mActivity.getClass().equals(activity.getClass())) {
mActivity = activity;
mPendingIntent = PendingIntent.getActivity(mActivity, 0,
new Intent(mActivity, mActivity.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
nfcAdapter.enableForegroundDispatch(activity, mPendingIntent, mFilters, mTechLists);
}
}
}
}
public boolean isEnabled() {
return nfcAdapter != null && nfcAdapter.isEnabled();
}
/**
* 处理cup NFC卡
* @param intent
* @throws IOException
*/
@SuppressLint("NewApi")
public void procNFCIntent(Intent intent) throws IOException {
if (isEnabled()) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
try {
na = IsoDep.get(tag);
// na.setTimeout(5000);
na.connect();
}catch (Exception e) {
e.printStackTrace();
mNFCListener.onError("标准NFC卡,卡ID:"+HEX.bytesToHex(tag.getId()));
}
}else{
if (mNFCListener != null) {
mNFCListener.onError("NFC不可用");
}
}
}
public void clearNFCParams() {
isNfcRequest = false;
}
public boolean isNFCReq() {
return isNfcRequest;
}
public void setNFCListener(NFCListener ls) {
mNFCListener = ls;
}
public interface NFCListener {
public void onReciveDataOffline(byte[] id);
public void onError(String error);
}
}