⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 a bluetooth sniffer.txt

📁 蓝牙嗅探器 使用于手机等移动设备的处理上 用于基础开发移动设备的蓝牙功能
💻 TXT
字号:
a 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&#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);
} 
}
/**
* @return Returns the btReady.
*/
protected boolean isBtReady() {
return btReady;
}
/**
* @param btReady The btReady to set.
*/
protected void setBtReady(boolean btReady) {
this.btReady = btReady;
}
}
###############################################################
Symbian 3rd 开发蓝牙OBEX 详细设计与代码2007-09-27 17:45 
3、1、4 Connection 
通过上述两个过程我们获得了远程设备的地址和端口仅仅联接就可以了。
TObexBluetoothProtocolInfo protocolInfo;
protocolInfo.iTransport.Copy(KStrRFCOMM);
protocolInfo.iAddr.SetBTAddr(aRemote); 
protocolInfo.iAddr.SetPort(aPort);
_LIT(KDevAddrAndPort, "DevAddr: %S, DevPort: %d");
TBuf<75> devAddrAndPort;
TBuf<12> remoteDevAddr;
aRemote.GetReadable(remoteDevAddr);
devAddrAndPort.Format(KDevAddrAndPort, &remoteDevAddr, aPort);
LOG(ELevel1, devAddrAndPort);
if (iClient)
{
delete iClient;
iClient = NULL;
}
iClient = CObexClient::NewL(protocolInfo);
iClient->Connect(iStatus);
SetActive();
就象在第一部分析的那样当传输层的联接建立后这时候会通知应用程序建立OBEX联接,这里需要使用CObexClient iClient对象。
3、1、5 发送对象
当联接建立后Client 就可以要求PUT对象
if (iState != EWaitingToSend)
{
User::Leave(KErrDisconnected);
}
else if (IsActive()) 
{
User::Leave(KErrInUse);
}
iClient->Put(*iCurrObject, iStatus);
SetActive();
3、1、6 断开联接
if (iState == EWaitingToGetDevice)
{
return;
}
if (iState == EWaitingToSend)
{
// Disconnecting...
iState = EDisconnecting;
iClient->Disconnect(iStatus);
iCurrObject->Reset();
SetActive();
}
else 
{
User::Leave(KErrInUse);
}
3、1、7服务端(Server)过程
Listen for a Connection and Security Manager Registeration--à启动OBEX 服务--àSDP database Registration--à --àAccept a connection--à Receive Data--àClose Connection
3、1、8 Listen for a Connection and Security Manager Registeration
设备在服务注册之前必须监听连接,用于决定RFCOMM的端口。
代码如下:
// Local variable to channel to listen to.
TInt channel;
RSocketServ socketServer;
// Connect to SocetServer
User::LeaveIfError( socketServer.Connect() );
CleanupClosePushL( socketServer );
RSocket socket;
// Open the Socket connection
User::LeaveIfError( socket.Open(socketServer, KStrRFCOMM) ); 
CleanupClosePushL( socket );
// Retrieve to one channel that is available.
User::LeaveIfError( socket.GetOpt( KRFCOMMGetAvailableServerChannel,KSolBtRFCOMM, channel ) );
// Set the Socket's Port.
TBTSockAddr sockaddr;
sockaddr.SetPort( channel );
所有的蓝牙服务端都需要进行安全注册,从而可协商和远程主机进行通信时使用的授权能力。安全注册的过程需要使用RBTMan 对象来获得会话,同时使用RBTSecuritySettings 来进行注册和注销设置。另一个办法就是在获得RFCOMM端口时就指定安全策略在设置监听端口的同时指定。在这里使用后者,没有比较过两者的优越性,只是后者更方便简洁。代码如下:
// Set the security according to.
TBTServiceSecurity serviceSecurity;
serviceSecurity.SetUid ( KUidBTFileSend );
serviceSecurity.SetAuthentication ( aAuthentication );
serviceSecurity.SetEncryption ( aEncryption );
serviceSecurity.SetAuthorisation ( aAuthorisation );
serviceSecurity.SetDenied( aDenied );
// Attach the security settings.
sockaddr.SetSecurity(serviceSecurity);
// Bind and start listening the port with security stetted,
User::LeaveIfError(socket.Bind(sockaddr));
User::LeaveIfError(socket.Listen(KSimultainousSocketsOpen));
// now close the socket and the socket server
CleanupStack::PopAndDestroy(); // socket
CleanupStack::PopAndDestroy(); // socketServer
//LOG(ELevel1, _L("CBTObexServer::SetSecurityWithChannelL()"));
return channel;
3、1、9启动OBEX 服务
// start the OBEX server
TObexBluetoothProtocolInfo obexProtocolInfo;
obexProtocolInfo.iTransport.Copy(KStrRFCOMM);
obexProtocolInfo.iAddr.SetPort( channel );
iObexServer = CObexServer::NewL( obexProtocolInfo );
iObexServer->Start( this );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -