A Bluetooth Sniffer

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;
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值