I am trying to operate the function of flash light which can be on/off through NFC tag.
After writing tag, I tried to set up if a certain information would be read on broadcastReceiver, Flash light should be on/off. However, the receiver never responded. I don't know Why..
So, what I really want to know is following below as
"My application can read the information first where is written in the tag through My application. "
as you saw below codes, I tried to operate the function.
This code For write NFC tag
private void enableTagWriteMode() {
mWriteMode = true;
//IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
//this.registerReceiver(Flash.class, tagDetected);
//IntentFilter[] mWriteTagFilters = new IntentFilter[] { tagDetected };
mNfcAdapter.enableForegroundDispatch(this, mNfcPendingIntent, null, null);
}
private void disableTagWriteMode() {
mWriteMode = false;
mNfcAdapter.disableForegroundDispatch(this);
}
protected void onNewIntent(Intent intent) {
// Tag writing mode
String action = intent.getAction();
System.out.println("aa: " + action);
//android.nfc.action.NDEF_DISCOVERED
if (mWriteMode) {
Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
String flash = "Flash";
byte[] textBytes = flash.getBytes();
NdefRecord textRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
"text/plain".getBytes(), new byte[] {}, textBytes);
NdefMessage message= new NdefMessage(new NdefRecord[] { textRecord });
boolean write = writeTag(message, detectedTag);
System.out.println(""+write);
if (write) {
Toast.makeText(this, "Success: Wrote placeid to nfc tag", Toast.LENGTH_LONG)
.show();
}
}
}
This code, For Read NFC code
public class Flash extends BroadcastReceiver {
private boolean isLighOn = false;
private Camera camera;
@Override
public void onReceive(Context context, Intent intent) {
if ( NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
System.out.println("call?");
//LayoutInflater mInflater = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
PackageManager pm = context.getPackageManager();
if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Log.e("err", "Device has no camera!");
return;
}
camera = Camera.open();
final Parameters p = camera.getParameters();
if (isLighOn) {
Log.i("info", "torch is turn off!");
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
isLighOn = false;
}
else {
Log.i("info", "torch is turn on!");
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
isLighOn = true;
}
}
}
}
This is Filter in manifest.xml
<receiver android:name =".Flash">
<intent-filter android:priority="10000">
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<data android:mimeType="text/plain" />
</intent-filter>
</receiver>