Skip to content
This repository has been archived by the owner on May 19, 2023. It is now read-only.

Commit

Permalink
Issues #32, #2 and #4: We have NFC tags reading for the two types of …
Browse files Browse the repository at this point in the history
…tags we have so far.

It works on nexus tablet and galaxy 3 phone.
I didn't create a library as #4 request, but a simple activity. I will do if we need to complexify the code.
Expected tag format follows the one used on QR codes, but as explained in issue #33 wireless configuration doesn't work.
The format is yaml with the following parameters:
 * URL (ROS master URI)
 * CURL (control URI, no idea what is this used for)
 * WIFI (SSID)
 * WIFIENC (WIFI encoding)
 * WIFIPW (WIFI password)
Maybe is time to update this, following issue #33.
  • Loading branch information
corot committed Oct 4, 2013
1 parent d295020 commit 25041ca
Show file tree
Hide file tree
Showing 9 changed files with 346 additions and 94 deletions.
17 changes: 17 additions & 0 deletions robot_remocon/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.NFC" />

<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
<provider
Expand All @@ -32,6 +33,22 @@
</activity>
<activity android:name="com.github.rosjava.android_remocons.robot_remocon.RobotMasterChooser" />
<activity android:name="org.ros.android.MasterChooser" />
<activity android:name="com.github.rosjava.android_remocons.robot_remocon.nfc.ForegroundDispatch"
android:label="NFC tag scan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

<!-- Add a technology filter -->
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED" />
</intent-filter>

<meta-data android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/filter_nfc_tech"
/>
</activity>

<service android:name="AppLauncher" />
<service android:name="org.ros.android.NodeMainExecutorService" >
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,6 @@

package com.github.rosjava.android_remocons.robot_remocon;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import com.github.rosjava.zeroconf_jmdns_suite.jmdns.DiscoveredService;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
Expand Down Expand Up @@ -74,16 +64,24 @@
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.IntentIntegrator;
import com.google.zxing.IntentResult;

import com.github.rosjava.android_apps.application_management.RobotDescription;
import com.github.rosjava.android_apps.application_management.RobotId;
import com.github.rosjava.android_apps.application_management.RobotsContentProvider;
import com.github.rosjava.android_remocons.robot_remocon.zeroconf.MasterSearcher;
import com.github.rosjava.zeroconf_jmdns_suite.jmdns.DiscoveredService;
import com.google.zxing.IntentIntegrator;
import com.google.zxing.IntentResult;

import org.yaml.snakeyaml.Yaml;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

/**
* @author [email protected]
* @author [email protected] (Kazuto Murase)
Expand All @@ -94,6 +92,9 @@ public class RobotMasterChooser extends Activity {
private static final int ADD_DELETION_DIALOG_ID = 1;
private static final int ADD_SEARCH_ROBOT_DIALOG_ID = 2;

private static final int QR_CODE_SCAN_REQUEST_CODE = 101;
private static final int NFC_TAG_SCAN_REQUEST_CODE = 102;

private List<RobotDescription> robots;
private boolean[] selections;
private MasterSearcher masterSearcher;
Expand Down Expand Up @@ -282,25 +283,49 @@ protected void onCreate(Bundle savedInstanceState) {

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(
requestCode, resultCode, intent);
if (scanResult != null && scanResult.getContents() != null) {
Yaml yaml = new Yaml();
Map<String, Object> data = (Map<String, Object>) yaml
.load(scanResult.getContents().toString());
Log.d("RobotRemocon", "RobotMasterChooser OBJECT: " + data.toString());
try {
addMaster(new RobotId(data), false);
} catch (Exception e) {
Toast.makeText(this,
"Invalid robot description: " + e.getMessage(),
Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "Scan failed", Toast.LENGTH_SHORT).show();
}
// Sub-activity to gather robot connection data completed: can be QR code or NFC tag scan
// TODO: cannot unify both calls?

}
if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show();
return;
}

String scanned_data = null;

if (requestCode == QR_CODE_SCAN_REQUEST_CODE) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(
requestCode, resultCode, intent);
if (scanResult != null && scanResult.getContents() != null) {
scanned_data = scanResult.getContents().toString();
}
}
else if (requestCode == NFC_TAG_SCAN_REQUEST_CODE && resultCode == RESULT_OK) {
if (intent.hasExtra("tag_data")) {
scanned_data = intent.getExtras().getString("tag_data");
}
}
else {
Log.w("RobotRemocon", "Unknown activity request code: " + requestCode);
return;
}

if (scanned_data == null) {
Toast.makeText(this, "Scan failed", Toast.LENGTH_SHORT).show();
}
else {
try {
Yaml yaml = new Yaml();
Map<String, Object> data = (Map<String, Object>) yaml.load(scanned_data);
Log.d("RobotRemocon", "RobotMasterChooser OBJECT: " + data.toString());
addMaster(new RobotId(data), false);
} catch (Exception e) {
Toast.makeText(this,
"Invalid robot description: " + e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
}

@Override
protected Dialog onCreateDialog(int id) {
Expand Down Expand Up @@ -328,13 +353,20 @@ public void onClick(View v) {
removeDialog(ADD_URI_DIALOG_ID);
}
});
button = (Button) dialog.findViewById(R.id.scan_robot_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
scanRobotClicked(v);
}
});
button = (Button) dialog.findViewById(R.id.qr_code_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
scanQRCodeClicked(v);
}
});
button = (Button) dialog.findViewById(R.id.nfc_tag_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
scanNFCTagClicked(v);
}
});
button = (Button) dialog.findViewById(R.id.search_master_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
Expand Down Expand Up @@ -542,12 +574,20 @@ public void refreshClicked(View view) {
refresh();
}

public void scanRobotClicked(View view) {
dismissDialog(ADD_URI_DIALOG_ID);
IntentIntegrator.initiateScan(this, IntentIntegrator.DEFAULT_TITLE,
IntentIntegrator.DEFAULT_MESSAGE, IntentIntegrator.DEFAULT_YES,
IntentIntegrator.DEFAULT_NO, IntentIntegrator.QR_CODE_TYPES);
}
public void scanQRCodeClicked(View view) {
dismissDialog(ADD_URI_DIALOG_ID);
IntentIntegrator.initiateScan(this, IntentIntegrator.DEFAULT_TITLE,
IntentIntegrator.DEFAULT_MESSAGE, IntentIntegrator.DEFAULT_YES,
IntentIntegrator.DEFAULT_NO, IntentIntegrator.QR_CODE_TYPES);
}

public void scanNFCTagClicked(View view) {
dismissDialog(ADD_URI_DIALOG_ID);
Intent i = new Intent(this,
com.github.rosjava.android_remocons.robot_remocon.nfc.ForegroundDispatch.class);
// Set the request code so we can identify the callback via this code
startActivityForResult(i, NFC_TAG_SCAN_REQUEST_CODE);
}

public void searchRobotClicked(View view) {
removeDialog(ADD_URI_DIALOG_ID);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/

package com.github.rosjava.android_remocons.robot_remocon.nfc;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentFilter.MalformedMimeTypeException;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.tech.*;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.widget.TextView;

import com.github.rosjava.android_remocons.robot_remocon.R;

/**
* An example of how to use the NFC foreground dispatch APIs. This will intercept any MIME data
* based NDEF dispatch as well as all dispatched for NfcF tags.
*/
public class ForegroundDispatch extends Activity {
private NfcAdapter mAdapter;
private PendingIntent mPendingIntent;
private IntentFilter[] mFilters;
private String[][] mTechLists;
private TextView mText;
private int mCount = 0;
private String tag_data = null;

@Override
public void onCreate(Bundle savedState) {
super.onCreate(savedState);

setContentView(R.layout.nfc_tag_scan);
mText = (TextView) findViewById(R.id.text);
mText.setText("Scan a NFC tag");

mAdapter = NfcAdapter.getDefaultAdapter(this);

// Create a generic PendingIntent that will be deliver to this activity. The NFC stack
// will fill in the intent with the details of the discovered tag before delivering to
// this activity.
mPendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

// Setup an intent filter for all MIME based dispatches
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
try {
ndef.addDataType("text/plain");
} catch (MalformedMimeTypeException e) {
throw new RuntimeException("fail", e);
}
mFilters = new IntentFilter[] {
ndef,
};

// Setup a tech list for all NfcF tags
mTechLists = new String[][] { new String[] { NfcF.class.getName() },
new String[] { NfcA.class.getName(), MifareClassic.class.getName() },
new String[] { NfcA.class.getName(), MifareUltralight.class.getName() } };
}

@Override
public void onResume() {
super.onResume();
if (mAdapter != null) mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters,
mTechLists);
}

@Override
public void onNewIntent(Intent intent) {
Log.i("Foreground dispatch", "Discovered tag with intent: " + intent);
mText.setText("Discovered tag " + ++mCount + " with intent: " + intent);

if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if ((rawMsgs != null) && (rawMsgs.length > 0)) {
NdefMessage msg = (NdefMessage) rawMsgs[0];
NdefRecord[] records = msg.getRecords();
if ((records != null) && (records.length > 0)) {
String payload = new String(records[0].getPayload());
tag_data = payload.substring(3); // Remove data type (1 byte) and language code (2 bytes)
mText.append("\n\n\n" + tag_data);
finish(); // TODO add ok/cancel buttons instead
}
}

// if (payload2[0] != NdefRecord.TNF_MIME_MEDIA)
}

mText.setText("Unrecognized NFC tag format");
}

@Override
public void onPause() {
super.onPause();
if (mAdapter != null) mAdapter.disableForegroundDispatch(this);
}

@Override
public void finish() {
// Prepare data in tent
Intent data = new Intent();
if (tag_data != null) {
// Activity finished ok, return the data
data.putExtra("tag_data", tag_data);
setResult(RESULT_OK, data);
}
else {
setResult(RESULT_CANCELED, data);
}

super.finish();
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 25041ca

Please sign in to comment.