Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.plugin;
- import org.apache.cordova.*;
- import org.json.JSONArray;
- import org.json.JSONException;
- import android.app.DialogFragment;
- import android.app.PendingIntent;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.nfc.NdefMessage;
- import android.nfc.NdefRecord;
- import android.nfc.NfcManager;
- import android.nfc.Tag;
- import android.os.Parcelable;
- /*import android.support.v7.app.ActionBarActivity;*/
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.TextView;
- import android.nfc.NfcAdapter;
- import android.nfc.tech.*;
- import android.nfc.tech.MifareClassic;
- import android.nfc.tech.NfcA;
- /*import android.support.v4.app.FragmentActivity;*/
- import android.app.AlertDialog;
- import java.io.IOException;
- import java.text.MessageFormat;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- public class Hello extends CordovaPlugin {
- private NfcAdapter nfcAdapter;
- public Tag tagFromIntent;
- public MifareClassic mc;
- public PendingIntent pendingIntent;
- TextView textView1;
- public int pocetSektorov,pocetBlokovSector4;
- public static final byte[] kluc = { (byte) 0x67,
- (byte) 0xE7, (byte) 0xDD, (byte) 0x55, (byte) 0xD8,
- (byte) 0x2A };
- public byte[]data16;
- public byte[]data17;
- public int prvyIndex;
- @Override
- public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {
- if (action.equals("ahoj")) {
- String name = data.getString(0);
- String message = "Ahoj, ";
- int x = 10;
- callbackContext.success(x);
- return true;
- }
- if (action.equals("greett")) {
- String name = data.getString(0);
- String message = "Hello, " + name;
- callbackContext.success(message);
- return true;
- } else {
- return false;
- }
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
- resolveIntent(getIntent());
- }
- public void resolveIntent(Intent intent)
- {
- textView1 = (TextView) findViewById(R.id.textView2);
- String action = intent.getAction();
- if(NfcAdapter.ACTION_TECH_DISCOVERED.equals(action))
- {
- textView1.setText("mam najdeny");
- TextView textView3 = (TextView) findViewById(R.id.textView3);
- tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
- mc = MifareClassic.get(tagFromIntent);
- byte[] data;
- try // pripojenie na kartu
- {
- mc.connect();
- textView1.setText("som pripojeny");
- boolean auth = false;
- pocetSektorov = mc.getSectorCount();
- pocetBlokovSector4 = mc.getBlockCountInSector(4);
- auth = mc.authenticateSectorWithKeyA(4,kluc);
- if(auth)
- {
- prvyIndex = mc.sectorToBlock(4);
- data16 = mc.readBlock(16);
- data17 = mc.readBlock(17);
- textView1.setText("index prveho bloku ="+prvyIndex+"dlzka je"+data16.length);
- }
- }
- catch (IOException e)
- {
- DialogFragment dialog = new DialogFragment();
- dialog.show(getFragmentManager(),e.toString());
- }
- }
- }
- @Override
- protected void onResume() {
- super.onResume();
- //dorbit aj resume aj pause
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public void startActivity(Intent intent) {
- super.startActivity(intent);
- }
- public void buttonOnClick(View v)
- {
- TextView textView = (TextView) findViewById(R.id.textView);
- nfcAdapter = NfcAdapter.getDefaultAdapter(this);
- if(!nfcAdapter.isEnabled())
- {
- textView.setText("zapni si adapter ty vole");
- }
- else if (mc.isConnected())
- {
- textView.setText("je pripojeny spoctom sektorov"+pocetSektorov+"blokov :"+pocetBlokovSector4);
- }
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- @Override
- protected void onNewIntent(Intent intent) {
- super.onNewIntent(intent);
- resolveIntent(intent);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement