2 * wpadebug - wpa_supplicant and Wi-Fi debugging app for Android
3 * Copyright (c) 2013, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 package w1.fi.wpadebug;
11 import java.io.BufferedReader;
12 import java.io.InputStreamReader;
13 import java.io.IOException;
15 import android.app.Activity;
16 import android.app.AlertDialog;
17 import android.os.Bundle;
18 import android.os.Parcelable;
19 import android.view.MenuItem;
20 import android.content.Intent;
21 import android.content.DialogInterface;
22 import android.widget.TextView;
23 import android.widget.Toast;
24 import android.text.method.ScrollingMovementMethod;
25 import android.util.Log;
26 import android.nfc.NdefMessage;
27 import android.nfc.NdefRecord;
28 import android.nfc.NfcAdapter;
30 public class WpaNfcActivity extends Activity
32 private static final String TAG = "wpadebug";
34 String byteArrayHex(byte[] a) {
35 StringBuilder sb = new StringBuilder();
37 sb.append(String.format("%02x", b));
41 private void show_alert(String title, String message)
43 AlertDialog.Builder alert = new AlertDialog.Builder(this);
44 alert.setTitle(title);
45 alert.setMessage(message);
46 alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
47 public void onClick(DialogInterface dialog, int id)
52 alert.create().show();
55 private String wpaCmd(String cmd)
58 Log.d(TAG, "Executing wpaCmd: " + cmd);
59 Process proc = Runtime.getRuntime().exec(new String[]{"/system/bin/mksh-su", "-c", "wpa_cli " + cmd});
60 BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
61 StringBuffer output = new StringBuffer();
63 char[] buffer = new char[1024];
64 while ((read = reader.read(buffer)) > 0)
65 output.append(buffer, 0, read);
68 Log.d(TAG, "External process completed - exitValue " +
70 return output.toString();
71 } catch (IOException e) {
72 show_alert("Could not run external program",
73 "Execution of an external program failed. " +
74 "Maybe mksh-su was not installed.");
76 } catch (InterruptedException e) {
77 throw new RuntimeException(e);
81 public boolean report_tag_read(byte[] payload)
83 String res = wpaCmd("WPS_NFC_TAG_READ " + byteArrayHex(payload));
86 if (!res.contains("OK")) {
87 Toast.makeText(this, "Failed to report WSC tag read to " +
88 "wpa_supplicant", Toast.LENGTH_LONG).show();
90 Toast.makeText(this, "Reported WSC tag read to wpa_supplicant",
91 Toast.LENGTH_LONG).show();
98 public void onCreate(Bundle savedInstanceState)
100 super.onCreate(savedInstanceState);
102 Intent intent = getIntent();
103 String action = intent.getAction();
104 Log.d(TAG, "onCreate: action=" + action);
106 if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
107 Log.d(TAG, "NDEF discovered");
108 Parcelable[] raw = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
110 Log.d(TAG, "NDEF message count: " + raw.length);
111 NdefMessage[] msgs = new NdefMessage[raw.length];
112 for (int i = 0; i < raw.length; i++) {
113 msgs[i] = (NdefMessage) raw[i];
114 NdefRecord rec = msgs[i].getRecords()[0];
115 Log.d(TAG, "MIME type: " + rec.toMimeType());
116 byte[] a = rec.getPayload();
117 Log.d(TAG, "NDEF record: " + byteArrayHex(a));
118 if (rec.getTnf() == NdefRecord.TNF_MIME_MEDIA &&
119 rec.toMimeType().equals("application/vnd/wfa.wsc")) {
120 Log.d(TAG, "WSC tag read");
123 if (!report_tag_read(a))