一个蓝牙嗅探器的源码

package bluetoothSniffer; public class BTTestClass { static String addressString; // IP address to connect to static int addressPort; // port to connect to static BTSnifferThread mySniffer; // thread to start running public static void main(String argv[]){ // we need two arguments: if (argv.length < 2) { System.out.println("invocation: BTTestClass addressString, portNumber"); System.exit(0); } else { addressString = new String(argv[0]); addressPort = Integer.parseInt(argv[1]); mySniffer = new BTSnifferThread(addressString, addressPort); mySniffer.start(); } while (mySniffer.isAlive()) { // twiddle your thumbs. } System.exit(0); } } package bluetoothSniffer; import java.net.*; import java.io.*; class BTSnifferThread extends Thread { Socket s; // connection to Lantronix device private BufferedReader networkIn; // text in from socket private PrintWriter networkOut; // text out to socket String someText; // for reading string in from socket private String addressString; // address to connect to private int addressPort; // port to connect to private boolean btReady; // if the BT dongle can take a command or not BufferedReader localIn; // input from keyboard BTSnifferThread(String _addressString, int _addressPort) { // put parameters in local variables, open local input stream: addressString = _addressString; addressPort = _addressPort; localIn = new BufferedReader(new InputStreamReader(System.in)); } public void run() { try { // open socket to Lantronix device: s = new Socket(addressString, addressPort); System.out.println("Connection accepted"); // set up input and output streams from socket: networkIn = new BufferedReader( new InputStreamReader(s.getInputStream())); networkOut = new PrintWriter(s.getOutputStream()); // clear output buffer: networkOut.flush(); } catch(IOException e) {System.out.println(e);} // send an initial info query, to see that the BT dongle is alive: this.getBTInfo(); // repeat until socket is closed: while(!s.isClosed()) { try { // read in a line at a time: someText = networkIn.readLine(); System.out.println(someText); // if BT dongle says "OK", then // it&#39;s ready to accept another command: if (someText.equals("OK")) { this.setBtReady(true); } //if BT dongle is doing nothing, scan for new devices: if (this.isBtReady()) { this.startBTInquiry(); } } catch(IOException e){ System.out.println(e); } } } public void kill() { // close socket if it&#39;s still open: if (!s.isClosed()) { try { s.close(); } catch(IOException e) {System.out.println(e);} } } public void getBTInfo () { // if the socket&#39;s open, send the BT dongle info string: if (!s.isClosed()) { networkOut.print("AT+BTINFO?\r\n"); networkOut.flush(); setBtReady(false); } } public void startBTInquiry() { // if the socket&#39;s open, send the BT dongle inquiry string: if (!s.isClosed()) { networkOut.print("AT+BTINQ?\r\n"); networkOut.flush(); setBtReady(false); } } protected boolean isBtReady() { return btReady; } protected void setBtReady(boolean btReady) { this.btReady = btReady; } }
posted @ 2011-08-18 11:15  Mr.Jim  阅读(346)  评论(0编辑  收藏  举报