A Bluetooth Sniffer

來源:互聯網
上載者:User

A Bluetooth Sniffer

From:http://www.tigoe.net/pcomp/code/archives/000311.shtml

Bluetooth Sniffer
In this example, a Lantronix WiPort serial-to-ethernet device was connected to an Initiium Promi SD-102 Blutooth serial dongle. No firmware code or microprocessor was needed, since the Promi device communicates serially to the WiPort, and the java program below communicates to the WiPort via TCP.

The code below opens a socket to the WiPort, then sends AT-style commands to the Promi device to ask it to scan for Bluetooth devices in its vicinity.

The test class only has a main method. All the real work is done in the BTSnifferThread class.

This code is very raw, and hasn't been properly tested. It's intended for demo purposes only.

BTTestClass:

/*
* Created on Nov 1, 2004
*
  */
package bluetoothSniffer;

/**
* @author tigoe
*
*/
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);
    }

}

BTSnifferThread class:

/**
* @author tigoe
*
*/

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'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's still open:
        if (!s.isClosed()) {
            try {
                s.close();
            } catch(IOException e) {System.out.println(e);}
        }
    }
    public void getBTInfo () {
        // if the socket'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's open, send the BT dongle inquiry string:
        if (!s.isClosed()) {
            networkOut.print("AT+BTINQ?/r/n");
            networkOut.flush();
            setBtReady(false);
        } 
    }
    /**
    * @return Returns the btReady.
    */
    protected boolean isBtReady() {
        return btReady;
    }
    /**
    * @param btReady The btReady to set.
    */
    protected void setBtReady(boolean btReady) {
        this.btReady = btReady;
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.