Skip to content

Commit 4ba140d

Browse files
Fixed(NfcAPI): Check if NfcAdapter is available and not null in NfcActivity.onResume() in addition to onCreate() before calling its methods otherwise would trigger a NullPointerException
1 parent c986fb0 commit 4ba140d

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

app/src/main/java/com/termux/api/apis/NfcAPI.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ public static void onReceive(final Context context, final Intent intent) {
3333

3434
public static class NfcActivity extends AppCompatActivity {
3535

36-
private NfcAdapter adapter;
36+
private Intent mIntent;
37+
private NfcAdapter mAdapter;
3738
static String socket_input;
3839
static String socket_output;
3940
String mode;
@@ -66,6 +67,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
6667
super.onCreate(savedInstanceState);
6768
Intent intent = this.getIntent();
6869
if (intent != null) {
70+
mIntent = intent;
6971
mode = intent.getStringExtra("mode");
7072
if (null == mode)
7173
mode = "noData";
@@ -94,14 +96,21 @@ protected void onResume() {
9496
Logger.logVerbose(LOG_TAG, "onResume");
9597

9698
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+
}
98107
Intent intentNew = new Intent(this, NfcActivity.class).addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
99108
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intentNew, 0);
100109
IntentFilter[] intentFilter = new IntentFilter[]{
101110
new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED),
102111
new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED),
103112
new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED)};
104-
adapter.enableForegroundDispatch(this, pendingIntent, intentFilter, null);
113+
mAdapter.enableForegroundDispatch(this, pendingIntent, intentFilter, null);
105114
}
106115

107116
@Override
@@ -126,7 +135,7 @@ protected void onNewIntent(Intent intent) {
126135
protected void onPause() {
127136
Logger.logDebug(LOG_TAG, "onPause");
128137

129-
adapter.disableForegroundDispatch(this);
138+
mAdapter.disableForegroundDispatch(this);
130139
super.onPause();
131140
}
132141

0 commit comments

Comments
 (0)