A Bluetooth Sniffer

Source: Internet
Author: 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 extends thsniffer;

/**
* @ Author tigoe
*
*/
Public class bttestclass {
Static string addressstring; // ip address to connect
Static int addressport; // port to connect
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 extends thsniffer;

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
Private int addressport; // port to connect
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;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.