📄 bluedump.java
字号:
//------------------------------------------------------------------
// Copyright (c) 2004 Jyperion SARL
// France
//
// This software is provided "AS IS," without a warranty of any kind.
// ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
// INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
//
//------------------------------------------------------------------
package example.bluetooth.dump;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.bluetooth.*;
/**
* The BlueDump MIDlet.
* This MIDlet will listen/spy on every services to dump the sent data to the display
* It could be very useful for debuggging
*
* @author Guillaume COMPAGNON
* @version 1.0
*/
public class BlueDump extends MIDlet implements CommandListener {
Display display;
static final byte DISCONNECT = 0x01;
static final byte CONNECTING = 0x02;
static final byte CONNECT = 0x03;
private int state = DISCONNECT;
//private BlueListener listener = null;
// the command labels
private static final String CMD_DISCONNECT = "Disconnect";
private static final String CMD_CONNECT = "Connect";
private static final String CMD_EXIT = "Quit";
private static final String CMD_CLEARSCREEN = "Clear";
private static final String CMD_OK = "OK";
private static final String CMD_CANCEL = "Cancel";
private static final String CMD_REFRESH = "Refresh";
// Displayables
BlueDumpGUI gui;
private Alert exitAlert;
javax.microedition.lcdui.List deviceList, serviceList;
// Commands buttons
private Command disconnectCommand;
private Command connectCommand;
private Command exitCommand;
// Worker Threads in order to make the background tasks
private Thread discoveryThread;
private Thread spyThread;
/**
* the unique instance of the Midlet as a Singleton pattern
*/
public static BlueDump singleton;
/** found bluetoothdevices */
public Hashtable bluetoothDevices = new Hashtable(4);
/** found bluetooth services */
public Hashtable bluetoothServices = new Hashtable(16);
/** spied connection */
public ServiceRecord record;
/**
* Debug Tag
*/
public static boolean DEBUG = true;
/**
* Constructor for the BlueDump
*/
public BlueDump()
{
// instanciation of the singleton
singleton = this;
// Get a handle on the display object
display = Display.getDisplay(this);
disconnectCommand = new Command(CMD_DISCONNECT, Command.STOP, 8);
connectCommand = new Command(CMD_CONNECT, Command.SCREEN, 8);
exitCommand = new Command(CMD_EXIT, Command.EXIT, 99);
// Create the BlueDump GUI
gui = new BlueDumpGUI();
gui.addCommand(new Command(CMD_CLEARSCREEN, Command.SCREEN, 2));
gui.addCommand(connectCommand);
gui.addCommand(exitCommand);
gui.setCommandListener(this);
exitAlert = new Alert("error");
// Create the Device List
deviceList = new List("Select Device", List.EXCLUSIVE);
deviceList.addCommand(new Command(CMD_OK, Command.OK, 1));
deviceList.addCommand(new Command(CMD_REFRESH, Command.SCREEN, 2));
deviceList.addCommand(new Command(CMD_CANCEL, Command.CANCEL, 3));
deviceList.addCommand(exitCommand);
deviceList.setCommandListener(this);
// Create the Services List
serviceList = new List("Select Service", List.EXCLUSIVE);
serviceList.addCommand(new Command(CMD_OK, Command.OK, 1));
serviceList.addCommand(new Command(CMD_REFRESH, Command.SCREEN, 2));
serviceList.addCommand(new Command(CMD_CANCEL, Command.CANCEL, 3));
serviceList.addCommand(exitCommand);
serviceList.setCommandListener(this);
}
public int getState() {
return state;
}
/** display a line to the screen */
public static void display( String message, final Integer color){
BlueDump.singleton.gui.messages.addElement(message);
BlueDump.singleton.gui.messagesColors.addElement(color);
BlueDump.singleton.gui.repaint();
}
/** display a line to the screen */
public static void display( String message){
display(message, BlueDumpGUI.BLACK);
}
/** display a popup windows, and exit */
public static void fireExitAlert(){
BlueDump.singleton.display.setCurrent(BlueDump.singleton.exitAlert);
}
/**
* The startup method
* from the MIDP API
*/
public void startApp() throws MIDletStateChangeException {
display.setCurrent(gui);
// welcome messages
display("Welcome to BlueDump");
display("Spy BT connections");
// Start a worker thread to make a device discovery
discoveryThread = new Thread(new BlueDumpDiscoveryThread());
discoveryThread.start();
// prepare the worker thread for the spying of the connection
spyThread = new Thread (new BlueDumpSpyThread());
}
/**
* Method called when the application is put in background
* from the MIDP API
*/
public void pauseApp() {
}
/**
* Method called when the exit is coming
* from the MIDP API
*/
public void destroyApp(boolean unconditional)
throws MIDletStateChangeException
{
// use the MIDP utility to make a clean exit
notifyDestroyed();
}
/**
* CallBack method for every events from the application
*/
public void commandAction(Command c, Displayable s) {
if( DEBUG ) System.out.println("CommandAction: " + c.getLabel());
// Exit
if (c.getLabel().equals(CMD_EXIT) && c == exitCommand) {
try {
destroyApp(false);
}
catch (MIDletStateChangeException ex) {
if( DEBUG ) ex.printStackTrace();
}
}
// Clear the GUI
else if (c.getLabel().equals(CMD_CLEARSCREEN)) {
gui.clearScreen(true);
}
// Connect
else if (c.getLabel().equals(CMD_CONNECT)) {
if( s == gui ){
// Remove old entries
for (int i=deviceList.size(); i > 0; i--) {
deviceList.delete(i-1);
}
Enumeration devices = bluetoothDevices.keys();
while( devices.hasMoreElements() )
deviceList.append((String)devices.nextElement(),null);
display.setCurrent(this.deviceList);
}
}
// Disconnect
else if (c.getLabel().equals(CMD_DISCONNECT)) {
if (state != DISCONNECT) {
state = DISCONNECT;
// stop the listener Thread
//this.spyThread.stop();
// change Disconnect command with the connect command
gui.removeCommand(disconnectCommand);
gui.addCommand(connectCommand);
gui.repaint();
}
}
// OK
else if (c.getLabel().equals(CMD_OK)) {
if( s == deviceList ){ // choose the service to listen
if (state != CONNECTING) {
state = CONNECTING;
}
// Remove old entries
for (int i=serviceList.size(); i > 0; i--) {
serviceList.delete(i-1);
}
Enumeration services = bluetoothServices.keys();
while( services.hasMoreElements() )
serviceList.append((String)services.nextElement(),null);
display.setCurrent(this.serviceList);
} else if( s == serviceList){
String service = serviceList.getString(serviceList.getSelectedIndex());
record = (ServiceRecord)bluetoothServices.get(service);
// change Connect command with the disconnect command
gui.removeCommand(connectCommand);
gui.addCommand(disconnectCommand);
gui.repaint();
// launch the Spy Thread
if(! spyThread.isAlive())
spyThread.start();
display.setCurrent(gui);
}
}
// CANCEL
else if (c.getLabel().equals(CMD_CANCEL)) {
display.setCurrent(gui);
}
// Refresh Device List or the Service List
else if (c.getLabel().equals(CMD_REFRESH)) {
if( ! discoveryThread.isAlive() )
discoveryThread.start();
}
}
public void setStateConnected(){
state = CONNECT;
}
public void setStateDisconnected(){
state = DISCONNECT;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -