Android Open Source - Android-NFC-WiFi-Controller Control Activity






From Project

Back to project page Android-NFC-WiFi-Controller.

License

The source code is released under:

MIT License

If you think the Android project Android-NFC-WiFi-Controller listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package sanchezsobrino.multimedia.anwc;
//  ww w  . j  a  va2 s. c o m
import java.io.UnsupportedEncodingException;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ControlActivity extends Activity {
  private ListView controlListView;
  private String[] controlArray;
  private boolean isWriteReady = false;
  private NfcAdapter mNfcAdapter;
  private CodeEvent currentEvent;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_control);

    mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
    if (mNfcAdapter == null) {
      Toast.makeText(this, "NFC no est? disponible en este dispositivo", Toast.LENGTH_SHORT).show();
      finish();
    } else if (!mNfcAdapter.isEnabled()) {
      Toast.makeText(this, "NFC desactivado. Por favor, act?velo", Toast.LENGTH_SHORT).show();
      finish();
    }

    controlArray = new String[6];
    controlArray[0] = "Arriba";
    controlArray[1] = "Abajo";
    controlArray[2] = "Izquierda";
    controlArray[3] = "Derecha";
    controlArray[4] = "F5";
    controlArray[5] = "Esc";

    controlListView = (ListView) findViewById(R.id.controlListView);
    controlListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, controlArray));

    controlListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (position) {
        case 0:
          currentEvent = CodeEvent.ARRIBA;
          setTagWriteReady(true);
          break;
        case 1:
          currentEvent = CodeEvent.ABAJO;
          setTagWriteReady(true);
          break;
        case 2:
          currentEvent = CodeEvent.IZQUIERDA;
          setTagWriteReady(true);
          break;
        case 3:
          currentEvent = CodeEvent.DERECHA;
          setTagWriteReady(true);
          break;
          
        case 4:
          currentEvent = CodeEvent.START_PRESENTATION;
          setTagWriteReady(true);
          break;
        case 5:
          currentEvent = CodeEvent.STOP_PRESENTATION;
          setTagWriteReady(true);
          break;
        }
      }
    });
  }
  
  public void onResume() {
    super.onResume();
    if (isWriteReady && NfcAdapter.ACTION_TAG_DISCOVERED.equals(getIntent().getAction())) {
      try {
        writeTag(getIntent());
      } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
      }
    }
  }

  public void setTagWriteReady(boolean flag) {
    isWriteReady = flag;

    if (isWriteReady) {
      IntentFilter[] filters = new IntentFilter[] { new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED) };
      mNfcAdapter.enableForegroundDispatch(this, NfcUtils.getPendingIntent(this), filters, null);
    } else {
      mNfcAdapter.disableForegroundDispatch(this);
    }
  }

  public void onNewIntent(Intent intent) {
    setIntent(intent);
  }

  private static final String MIME_TYPE = "application/sanchezsobrino.multimedia.anwc.tag";
  
  private void writeTag(Intent intent) throws UnsupportedEncodingException {
    if (isWriteReady && NfcAdapter.ACTION_TAG_DISCOVERED.equals(getIntent().getAction())) {
      Tag detectedTag = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);
      byte[] payload = new String(String.valueOf(CodeEvent.fromCodeEvent(currentEvent))).getBytes();
      
      if (detectedTag != null && NfcUtils.writeTag(NfcUtils.createMessage(MIME_TYPE, payload), detectedTag)) {
        Toast.makeText(this, "Escrito: '" + CodeEvent.fromInt(Integer.parseInt(new String(payload, "UTF-8"))).toString() + "'", Toast.LENGTH_LONG).show();
        setTagWriteReady(false);
      } else {
        Toast.makeText(this, "La escritura fall, intentalo de nuevo", Toast.LENGTH_LONG).show();
      }
    }
  }
}




Java Source Code List

com.dm.zbar.android.scanner.CameraPreview.java
com.dm.zbar.android.scanner.ZBarConstants.java
com.dm.zbar.android.scanner.ZBarScannerActivity.java
sanchezsobrino.multimedia.anwc.CodeEvent.java
sanchezsobrino.multimedia.anwc.ConnectedActivity.java
sanchezsobrino.multimedia.anwc.ControlActivity.java
sanchezsobrino.multimedia.anwc.MainActivity.java
sanchezsobrino.multimedia.anwc.NfcUtils.java
sanchezsobrino.multimedia.anwc.business.AboutMenuItemListener.java
sanchezsobrino.multimedia.anwc.business.ChoosePortDialogListener.java
sanchezsobrino.multimedia.anwc.business.CodeEvent.java
sanchezsobrino.multimedia.anwc.business.CustomQRCode.java
sanchezsobrino.multimedia.anwc.business.ExitMenuItemListener.java
sanchezsobrino.multimedia.anwc.business.InstructionsMenuItemListener.java
sanchezsobrino.multimedia.anwc.business.KeySimulator.java
sanchezsobrino.multimedia.anwc.business.LocalizedString.java
sanchezsobrino.multimedia.anwc.business.TCPServer.java
sanchezsobrino.multimedia.anwc.persistence.LocalizedStringManager.java
sanchezsobrino.multimedia.anwc.presentation.ChoosePortDialog.java
sanchezsobrino.multimedia.anwc.presentation.LogPane.java
sanchezsobrino.multimedia.anwc.presentation.MainWindow.java