NOTE: Only one SPOT can be detected in this, I would like to change it later.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package simpleexec;
import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
*
* @author pdeng
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int portNumber = spotCOMPort();
System.out.println(portNumber);
String deviceMACAddress = getDeviceMACAddress();
System.out.println(deviceMACAddress);
}
private static String getDeviceMACAddress() {
try {
String rawString = "";
String line;
Process p = Runtime.getRuntime().exec("C:\\Program Files\\Sun\\SunSPOT\\sdk\\bin\\spotfinder.exe -vv");
BufferedReader input =
new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
rawString = rawString + line;
}
input.close();
String rawAddress = String.valueOf(rawString.subSequence(rawString.indexOf("00144F01") + 8, rawString.indexOf("00144F01") + 16));
String MACAddress = "0014.4F01." + rawAddress.substring(0, 4) + "." + rawAddress.substring(4, 8);
return MACAddress;
} catch (Exception err) {
err.printStackTrace();
return "-1";
}
}
private static int spotCOMPort() {
try {
String rawString = "";
String line;
Process p = Runtime.getRuntime().exec("C:\\Program Files\\Sun\\SunSPOT\\sdk\\bin\\spotfinder.exe -vv");
BufferedReader input =
new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
rawString = rawString + line;
//System.out.println(line);
}
input.close();
String comPort = String.valueOf(rawString.subSequence(rawString.indexOf("(COM") + 4, rawString.indexOf(")parsed")));
return Integer.parseInt(comPort);
} catch (Exception err) {
err.printStackTrace();
return -1;
}
}
}
没有评论:
发表评论