Advertisement
Guest User

Untitled

a guest
Apr 26th, 2015
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.43 KB | None | 0 0
  1. package com.example.plugin;
  2.  
  3. import org.apache.cordova.*;
  4. import org.json.JSONArray;
  5. import org.json.JSONException;
  6.  
  7. import android.app.DialogFragment;
  8. import android.app.PendingIntent;
  9. import android.content.Intent;
  10. import android.content.IntentFilter;
  11. import android.nfc.NdefMessage;
  12. import android.nfc.NdefRecord;
  13. import android.nfc.NfcManager;
  14. import android.nfc.Tag;
  15. import android.os.Parcelable;
  16. /*import android.support.v7.app.ActionBarActivity;*/
  17. import android.os.Bundle;
  18. import android.util.Log;
  19. import android.view.Menu;
  20. import android.view.MenuItem;
  21. import android.view.View;
  22. import android.widget.TextView;
  23. import android.nfc.NfcAdapter;
  24. import android.nfc.tech.*;
  25. import android.nfc.tech.MifareClassic;
  26. import android.nfc.tech.NfcA;
  27. /*import android.support.v4.app.FragmentActivity;*/
  28. import android.app.AlertDialog;
  29.  
  30.  
  31. import java.io.IOException;
  32. import java.text.MessageFormat;
  33. import java.util.ArrayList;
  34. import java.util.Iterator;
  35. import java.util.List;
  36.  
  37. public class Hello extends CordovaPlugin {
  38.     private NfcAdapter nfcAdapter;
  39.     public Tag tagFromIntent;
  40.     public MifareClassic mc;
  41.     public PendingIntent pendingIntent;
  42.     TextView textView1;
  43.     public int pocetSektorov,pocetBlokovSector4;
  44.     public static final byte[] kluc = { (byte) 0x67,
  45.             (byte) 0xE7, (byte) 0xDD, (byte) 0x55, (byte) 0xD8,
  46.             (byte) 0x2A };
  47.     public byte[]data16;
  48.     public byte[]data17;
  49.     public int prvyIndex;
  50.  
  51.     @Override
  52.     public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {
  53.  
  54.  
  55.  
  56.    if (action.equals("ahoj")) {
  57.  
  58.  
  59.  
  60.  
  61.             String name = data.getString(0);
  62.             String message = "Ahoj, ";
  63.                int x = 10;
  64.  
  65.  
  66.        callbackContext.success(x);
  67.            
  68.  
  69.             return true;
  70.  
  71.         }
  72.  
  73.         if (action.equals("greett")) {
  74.  
  75.             String name = data.getString(0);
  76.             String message = "Hello, " + name;
  77.             callbackContext.success(message);
  78.  
  79.             return true;
  80.  
  81.         } else {
  82.            
  83.             return false;
  84.  
  85.         }
  86.     }
  87.  
  88.  
  89.  
  90.  
  91. @Override
  92.     protected void onCreate(Bundle savedInstanceState) {
  93.         super.onCreate(savedInstanceState);
  94.         setContentView(R.layout.activity_main);
  95.         pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
  96.  
  97.         resolveIntent(getIntent());
  98.  
  99.  
  100.     }
  101.  
  102.     public void resolveIntent(Intent intent)
  103.     {
  104.         textView1 = (TextView) findViewById(R.id.textView2);
  105.         String action = intent.getAction();
  106.  
  107.         if(NfcAdapter.ACTION_TECH_DISCOVERED.equals(action))
  108.         {
  109.             textView1.setText("mam najdeny");
  110.             TextView textView3 = (TextView) findViewById(R.id.textView3);
  111.             tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
  112.             mc = MifareClassic.get(tagFromIntent);
  113.             byte[] data;
  114.             try // pripojenie na kartu
  115.             {
  116.                 mc.connect();
  117.                 textView1.setText("som pripojeny");
  118.                 boolean auth = false;
  119.                 pocetSektorov = mc.getSectorCount();
  120.                 pocetBlokovSector4 = mc.getBlockCountInSector(4);
  121.                 auth = mc.authenticateSectorWithKeyA(4,kluc);
  122.                 if(auth)
  123.                 {
  124.                     prvyIndex = mc.sectorToBlock(4);
  125.                     data16 = mc.readBlock(16);
  126.                     data17 = mc.readBlock(17);
  127.                     textView1.setText("index prveho bloku ="+prvyIndex+"dlzka je"+data16.length);
  128.                 }
  129.  
  130.  
  131.             }
  132.             catch (IOException e)
  133.             {
  134.                 DialogFragment dialog = new DialogFragment();
  135.                 dialog.show(getFragmentManager(),e.toString());
  136.             }
  137.  
  138.         }
  139.  
  140.     }
  141.  
  142.     @Override
  143.     protected void onResume() {
  144.         super.onResume();
  145.         //dorbit aj resume aj pause
  146.     }
  147.  
  148.     @Override
  149.     public boolean onCreateOptionsMenu(Menu menu) {
  150.         // Inflate the menu; this adds items to the action bar if it is present.
  151.         getMenuInflater().inflate(R.menu.menu_main, menu);
  152.         return true;
  153.     }
  154.  
  155.     @Override
  156.     public void startActivity(Intent intent) {
  157.         super.startActivity(intent);
  158.  
  159.     }
  160.  
  161.  
  162.  
  163.     public void buttonOnClick(View v)
  164.     {
  165.         TextView textView = (TextView) findViewById(R.id.textView);
  166.  
  167.         nfcAdapter = NfcAdapter.getDefaultAdapter(this);
  168.         if(!nfcAdapter.isEnabled())
  169.         {
  170.             textView.setText("zapni si adapter ty vole");
  171.         }
  172.         else if (mc.isConnected())
  173.         {
  174.             textView.setText("je pripojeny spoctom sektorov"+pocetSektorov+"blokov :"+pocetBlokovSector4);
  175.  
  176.         }
  177.     }
  178.  
  179.     @Override
  180.     public boolean onOptionsItemSelected(MenuItem item) {
  181.         // Handle action bar item clicks here. The action bar will
  182.         // automatically handle clicks on the Home/Up button, so long
  183.         // as you specify a parent activity in AndroidManifest.xml.
  184.         int id = item.getItemId();
  185.  
  186.         //noinspection SimplifiableIfStatement
  187.         if (id == R.id.action_settings) {
  188.             return true;
  189.         }
  190.  
  191.         return super.onOptionsItemSelected(item);
  192.     }
  193.  
  194.     @Override
  195.     protected void onNewIntent(Intent intent) {
  196.         super.onNewIntent(intent);
  197.         resolveIntent(intent);
  198.     }
  199.  
  200.  
  201.  
  202.  
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement