@@ -33,7 +33,8 @@ public static void onReceive(final Context context, final Intent intent) {
33
33
34
34
public static class NfcActivity extends AppCompatActivity {
35
35
36
- private NfcAdapter adapter ;
36
+ private Intent mIntent ;
37
+ private NfcAdapter mAdapter ;
37
38
static String socket_input ;
38
39
static String socket_output ;
39
40
String mode ;
@@ -66,6 +67,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
66
67
super .onCreate (savedInstanceState );
67
68
Intent intent = this .getIntent ();
68
69
if (intent != null ) {
70
+ mIntent = intent ;
69
71
mode = intent .getStringExtra ("mode" );
70
72
if (null == mode )
71
73
mode = "noData" ;
@@ -94,14 +96,21 @@ protected void onResume() {
94
96
Logger .logVerbose (LOG_TAG , "onResume" );
95
97
96
98
super .onResume ();
97
- adapter = NfcAdapter .getDefaultAdapter (this );
99
+
100
+ mAdapter = NfcAdapter .getDefaultAdapter (this );
101
+ if (mAdapter == null || !mAdapter .isEnabled ()) {
102
+ if (mIntent != null )
103
+ errorNfc (this , mIntent ,"" );
104
+ finish ();
105
+ return ;
106
+ }
98
107
Intent intentNew = new Intent (this , NfcActivity .class ).addFlags (Intent .FLAG_RECEIVER_REPLACE_PENDING );
99
108
PendingIntent pendingIntent = PendingIntent .getActivity (this , 0 , intentNew , 0 );
100
109
IntentFilter [] intentFilter = new IntentFilter []{
101
110
new IntentFilter (NfcAdapter .ACTION_NDEF_DISCOVERED ),
102
111
new IntentFilter (NfcAdapter .ACTION_TAG_DISCOVERED ),
103
112
new IntentFilter (NfcAdapter .ACTION_TECH_DISCOVERED )};
104
- adapter .enableForegroundDispatch (this , pendingIntent , intentFilter , null );
113
+ mAdapter .enableForegroundDispatch (this , pendingIntent , intentFilter , null );
105
114
}
106
115
107
116
@ Override
@@ -126,7 +135,7 @@ protected void onNewIntent(Intent intent) {
126
135
protected void onPause () {
127
136
Logger .logDebug (LOG_TAG , "onPause" );
128
137
129
- adapter .disableForegroundDispatch (this );
138
+ mAdapter .disableForegroundDispatch (this );
130
139
super .onPause ();
131
140
}
132
141
0 commit comments