Guest User

Untitled

a guest
Mar 20th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.56 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity implements Listener,
  2. NFCReadFragment.OnDataPass {
  3.  
  4. private Button mBtRead;
  5. private NFCReadFragment mNfcReadFragment;
  6. public static final String TAG = MainActivity.class.getSimpleName();
  7. private boolean isDialogDisplayed = false;
  8. private boolean isWrite = false;
  9. private NfcAdapter mNfcAdapter;
  10.  
  11. PendingIntent pendingIntent;
  12. IntentFilter writeTagFilters[];
  13.  
  14. Context context;
  15. LinearLayout llMain;
  16.  
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_read);
  21.  
  22. initViews();
  23. initNFC();
  24. initViews();
  25. }
  26.  
  27. private void initViews() {
  28. mBtRead = (Button) findViewById(R.id.button);
  29. mBtRead.setOnClickListener(view -> showReadFragment());
  30. }
  31.  
  32. private void initNFC(){
  33. mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
  34. }
  35.  
  36. private void showReadFragment() {
  37.  
  38. mNfcReadFragment = (NFCReadFragment) getFragmentManager().findFragmentByTag(NFCReadFragment.TAG);
  39.  
  40. if (mNfcReadFragment == null) {
  41. mNfcReadFragment = NFCReadFragment.newInstance();
  42. }
  43. mNfcReadFragment.show(getFragmentManager(),NFCReadFragment.TAG);
  44.  
  45. }
  46.  
  47. @Override
  48. public void onDialogDisplayed() {
  49.  
  50. isDialogDisplayed = true;
  51. }
  52.  
  53. @Override
  54. public void onDialogDismissed() {
  55.  
  56. isDialogDisplayed = false;
  57. isWrite = false;
  58. }
  59.  
  60. @Override
  61. protected void onResume() {
  62. super.onResume();
  63. IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
  64. IntentFilter ndefDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
  65. IntentFilter techDetected = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
  66. IntentFilter[] nfcIntentFilter = new IntentFilter[]{techDetected,tagDetected,ndefDetected};
  67.  
  68. PendingIntent pendingIntent = PendingIntent.getActivity(
  69. this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
  70. if(mNfcAdapter!= null)
  71. mNfcAdapter.enableForegroundDispatch(this, pendingIntent, nfcIntentFilter, null);
  72.  
  73. }
  74.  
  75. @Override
  76. protected void onPause() {
  77. super.onPause();
  78. if(mNfcAdapter!= null)
  79. mNfcAdapter.disableForegroundDispatch(this);
  80. }
  81.  
  82. @Override
  83. protected void onNewIntent(Intent intent) {
  84. Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
  85.  
  86. Log.d(TAG, "onNewIntent: "+intent.getAction());
  87.  
  88. if(tag != null) {
  89. Toast.makeText(this, getString(R.string.message_tag_detected), Toast.LENGTH_SHORT).show();
  90. Ndef ndef = Ndef.get(tag);
  91.  
  92. if (isDialogDisplayed) {
  93. mNfcReadFragment = (NFCReadFragment)getFragmentManager().findFragmentByTag(NFCReadFragment.TAG);
  94. mNfcReadFragment.onNfcDetected(ndef);
  95. }
  96. }
  97. }
  98.  
  99. public void transfToData(byte[][] buf){
  100. Intent intent = new Intent(MainActivity.this, Tabs.class);
  101. intent.putExtra("record_ndef", buf);
  102.  
  103. startActivity(intent);
  104. }
  105.  
  106. private byte[][] readFromNFC(Ndef ndef) {
  107.  
  108. try {
  109. ndef.connect();
  110. NdefMessage ndefMessage = ndef.getNdefMessage();
  111. byte buf[][] = new byte[ndefMessage.getRecords().length][];
  112.  
  113. //ID[i] = GetId();
  114. //i++;
  115. String f;
  116. for (int i = 0; i < ndefMessage.getRecords().length; i++) {
  117. buf[i] = ndefMessage.getRecords()[i].getPayload();
  118. f = new String(buf[i]);
  119. }
  120.  
  121. ndef.close();
  122.  
  123. return buf;
  124.  
  125. } catch (IOException | FormatException e) {
  126. e.printStackTrace();
  127. return null;
  128. }
  129.  
  130. }
  131.  
  132. private String GetId(){
  133. Tag myTag = (Tag) getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);
  134. String ID = bytesToHexString(myTag.getId());
  135. return ID;
  136. }
  137.  
  138. private String bytesToHexString(byte[] src){
  139. StringBuilder stringBuilder = new StringBuilder("0x");
  140. if(src == null || src.length <= 0){
  141. return null;
  142. }
  143.  
  144. char[] buffer = new char[2];
  145. for(int i = 0; i < src.length; i++){
  146. buffer[0] = Character.forDigit((src[i] >>> 4) & 0x0F, 16);
  147. buffer[1] = Character.forDigit(src[i] & 0x0F, 16);
  148. System.out.println(buffer);
  149. stringBuilder.append(buffer);
  150. }
  151.  
  152. return stringBuilder.toString();
  153. }
  154.  
  155. @Override
  156. public void onDataPass(byte[][] data) {
  157. Intent intent = new Intent(this, Tabs.class);
  158. intent.putExtra("record_ndef", data);
  159. startActivity(intent);
  160. }
  161.  
  162. public class NFCWriteFragment extends DialogFragment {
  163.  
  164. public static final String TAG = NFCWriteFragment.class.getSimpleName();
  165.  
  166. public static NFCWriteFragment newInstance() {
  167.  
  168. return new NFCWriteFragment();
  169. }
  170.  
  171. public interface OnWriteDataPass {
  172. void onWriteDataPass(byte[] data);
  173. }
  174.  
  175. OnWriteDataPass writeDataPass;
  176.  
  177. public void passWriteData(byte[] data){
  178. writeDataPass.onWriteDataPass(data);
  179. }
  180.  
  181. private TextView mTvMessage;
  182. private ProgressBar mProgress;
  183. private Listener mListener;
  184.  
  185. @Nullable
  186. @Override
  187. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  188. View view = inflater.inflate(R.layout.fragment_write,container,false);
  189. initViews(view);
  190. return view;
  191. }
  192.  
  193. private void initViews(View view) {
  194.  
  195. mTvMessage = (TextView) view.findViewById(R.id.tv_message);
  196. mProgress = (ProgressBar) view.findViewById(R.id.progress);
  197. }
  198.  
  199. @Override
  200. public void onAttach(Context context) {
  201. super.onAttach(context);
  202. mListener = (Tabs)context;
  203. mListener.onDialogDisplayed();
  204. }
  205.  
  206. @Override
  207. public void onDetach() {
  208. super.onDetach();
  209. mListener.onDialogDismissed();
  210. }
  211.  
  212. public void onNfcDetected(Ndef ndef, byte[] messageToWrite){
  213.  
  214. mProgress.setVisibility(View.VISIBLE);
  215. byte[] buf = writeToNfc(ndef,messageToWrite);
  216. if(buf != null){
  217. passWriteData(buf);
  218. }
  219. //getActivity().getFragmentManager().beginTransaction().remove(this).commit();
  220. }
  221.  
  222. private byte[] writeToNfc(Ndef ndef, byte[] message){
  223.  
  224. mTvMessage.setText(getString(R.string.message_write_progress));
  225. if (ndef != null) {
  226. String as = new String(message);
  227. NdefRecord mimeRecord = NdefRecord.createMime("text/plain", message);
  228. NdefRecord applicationRecord = NdefRecord.createApplicationRecord("com.learn2crack.nfc");
  229.  
  230. try {
  231.  
  232. ndef.connect();
  233. ndef.writeNdefMessage(new NdefMessage(applicationRecord , mimeRecord));
  234. ndef.close();
  235. //Write Successful
  236. mTvMessage.setText(getString(R.string.message_write_success));
  237. return message;
  238.  
  239. } catch (IOException | FormatException e) {
  240. e.printStackTrace();
  241. mTvMessage.setText(getString(R.string.message_write_error));
  242. return null;
  243.  
  244. } finally {
  245. mProgress.setVisibility(View.GONE);
  246. }
  247.  
  248. }
  249. return null;
  250. }
  251. }
Add Comment
Please, Sign In to add comment