博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一个蓝牙嗅探器的源码
阅读量:5305 次
发布时间:2019-06-14

本文共 3084 字,大约阅读时间需要 10 分钟。

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

转载于:https://www.cnblogs.com/mrjim/archive/2011/08/18/4475243.html

你可能感兴趣的文章
thinkphp5的控制器调用自身模块和调用其他模块的方法
查看>>
钻牛角尖の根据时间计算周次
查看>>
bzoj 1029: [JSOI2007]建筑抢修
查看>>
【英语】IT English (随时更新...)
查看>>
php采集
查看>>
8.25 ccpc 比赛总结
查看>>
一台java服务器可以跑多少个线程?
查看>>
Java开发体系
查看>>
LeetCode22 Generate Parentheses
查看>>
面试题----合并两个有序数组
查看>>
VeloView源码编译错误记录——VS manifest
查看>>
161.101 - 2018 Summer Semester: Assignment
查看>>
UML - Basic Notations
查看>>
Decorator Pattern
查看>>
栈———链表实现
查看>>
一个新的开始,fightting!
查看>>
idc交叉引用
查看>>
函数的重载
查看>>
批量将.txt编码格式转化为utf8
查看>>
redis 安装启动及设置密码<windows>
查看>>