-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainActivity.java
More file actions
116 lines (94 loc) · 4.11 KB
/
Copy pathMainActivity.java
File metadata and controls
116 lines (94 loc) · 4.11 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
package com.lumpology.nfcterminal;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.nfc.NfcEvent;
import android.nfc.NfcManager;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.os.Bundle;
import android.os.Parcelable;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Toast;
import android.os.AsyncTask;
import com.lumpology.nfcterminal.UdpClient;
import java.util.ArrayList;
public class MainActivity extends Activity {
private TextView nfcInfoTextView;
private EditText DNS_BANK_MERCH_PRICE;
private EditText DNS_NETWORK;
private EditText passwordEditText;
private NfcAdapter nfcAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nfcInfoTextView = findViewById(R.id.nfcInfoTextView);
DNS_BANK_MERCH_PRICE = findViewById(R.id.DNS_BANK_MERCH_PRICE);
passwordEditText = findViewById(R.id.passwordEditText);
DNS_NETWORK = findViewById(R.id.DNS_NETWORK);
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (nfcAdapter == null) {
Toast.makeText(this, "NFC is not available on this device.", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onResume() {
super.onResume();
enableNfcForegroundDispatch();
}
@Override
protected void onPause() {
super.onPause();
disableNfcForegroundDispatch();
}
private void enableNfcForegroundDispatch() {
Intent intent = new Intent(this, getClass()).addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
nfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null);
}
private void disableNfcForegroundDispatch() {
nfcAdapter.disableForegroundDispatch(this);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
// Handle the NFC tag data here
processNfcTag(intent);
}
}
private void processNfcTag(Intent intent) {
// Extract NFC tag data and update the UI
// Example: Read NDEF messages from the tag
Parcelable[] rawMessages = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
ArrayList<String> extracted_data = new ArrayList<String>();
String tagData = "";
if (rawMessages != null && rawMessages.length > 0) {
StringBuilder tagDataBuilder = new StringBuilder();
for (Parcelable rawMessage : rawMessages) {
NdefMessage message = (NdefMessage) rawMessage;
NdefRecord[] records = message.getRecords();
for (NdefRecord record : records) {
String payload = new String(record.getPayload());
tagDataBuilder.append(payload);
}
}
tagData = tagDataBuilder.toString();
extracted_data.add(tagData);
nfcInfoTextView.setText(tagData);
} else {
nfcInfoTextView.setText("No Data Messages Found On The DNS Card.");
}
String converted_extracetd_data = DNS_NETWORK.getText().toString()+"\nExtracted Payload: {\nCard Data: {"+nfcInfoTextView.getText().toString()+"}\nPrice: {"+ DNS_BANK_MERCH_PRICE.getText().toString()+"}\nVendor: {"+ passwordEditText.getText().toString()+"}\n}";
//Toast.makeText(this, "Gots:"+converted_extracetd_data, Toast.LENGTH_SHORT).show();
//UdpClientCaller.callUdpClientInBackground(converted_extracetd_data);
//this triggers the UDP sender and accepts a string array to be sent to the server
tagData = UdpClient.card_sender(converted_extracetd_data);
Toast.makeText(this, tagData, Toast.LENGTH_SHORT).show();
nfcInfoTextView.setText(tagData);
}
}