📄 btclient.java
字号:
package btClient;
import java.io.DataOutputStream;
import java.io.DataInputStream;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;
import net.benhui.btgallery.bluelet.BLUElet;
public class btClient extends MIDlet implements CommandListener {
public static btClient instance;
public static Display display;
public Form mainForm;
public String localAdd;
public Alert alert;
// Main form
public static final Command seCommand = new Command ("Search", Command.SCREEN, 1);
public static final Command fbCommand = new Command ("File Browser", Command.SCREEN, 2);
public static final Command exitCommand = new Command ("Exit", Command.EXIT, 0);
public StringItem status = new StringItem ("Status: ", "No server found.");
// List page
public String[] files = new String[100];
public List fileList;
public static final Command dlCommand = new Command ("Download", Command.SCREEN, 1);
public static final Command upCommand = new Command ("Update", Command.SCREEN, 2);
public static final Command backCommand = new Command("Back", Command.BACK, 0);
// BLUElet component
// To perform device discovery, device selection and service discovery
public BLUElet bluelet = null;
public ServiceRecord r;
public String url;
StreamConnection con;
public void startApp() throws javax.microedition.midlet.MIDletStateChangeException {
display = Display.getDisplay (this);
bluelet.startApp();
display.setCurrent (mainForm);
}
public void pauseApp() {
// Do Bluelet pause app
bluelet.pauseApp();
}
public void destroyApp(boolean unconditional) {
// Do Bluelet destroy app
bluelet.destroyApp( unconditional );
}
public static void quitApp() {
instance.destroyApp(true);
instance.notifyDestroyed();
instance = null;
}
public btClient() {
instance = this;
bluelet = new BLUElet( this, this );
// Get bluetooth device info
try {
localAdd = LocalDevice.getLocalDevice().getBluetoothAddress();
} catch (Exception e) {
printMsg(e.toString());
}
// Draw form
mainForm = new Form ("btClient v1.0");
mainForm.addCommand (seCommand);
mainForm.addCommand (fbCommand);
mainForm.addCommand (exitCommand);
mainForm.append (status);
mainForm.setCommandListener (this);
fileList = new List ("File Browser", Choice.IMPLICIT);
fileList.append("superStar.mid",null);
fileList.addCommand(dlCommand);
fileList.addCommand(upCommand);
fileList.addCommand(backCommand);
fileList.setCommandListener (this);
}
public void commandAction (Command c, Displayable d) {
// Exit Command
if (c == exitCommand) {
notifyDestroyed();
}
// Search Command
else if (c == seCommand) {
bluelet.startInquiry( DiscoveryAgent.GIAC, new UUID[]{ new UUID("32df102b20d64f0b8b1f45d824eff75f", false) });
display.setCurrent( bluelet.getUI() );
}
// File Browser Command
else if (c == fbCommand) {
display.setCurrent (fileList);
}
// Update List Command
else if (c == upCommand) {
getList();
}
// Download Command
else if (c == dlCommand) {
getFile(fileList.getString(fileList.getSelectedIndex()));
}
//Back Command
else if (c == backCommand) {
display.setCurrent (mainForm);
}
else if( c == List.SELECT_COMMAND ){
getFile(fileList.getString(fileList.getSelectedIndex()));
}
// BLUElet Selected Command
else if ( c.equals( BLUElet.SELECTED ) ) {
printMsg("Performing service discovery.....");
}
// BLUElet Back Command
else if ( c.equals( BLUElet.BACK ) ) {
display.setCurrent( mainForm );
}
// BLUElet Completed Command
else if ( c.equals( BLUElet.COMPLETED ) ) {
r = bluelet.getFirstDiscoveredService();
url = r.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false );
status.setText("Server found.");
display.setCurrent( mainForm );
}
}
public void getFile(String f){
try {
con = (StreamConnection) Connector.open(url);
DataOutputStream out = con.openDataOutputStream();
out.writeUTF("sendFile" ); // Request file
out.flush();
out.writeUTF(f); // filename to download
out.flush();
out.writeUTF(localAdd); // client bt address
out.flush();
out.close();
con.close();
printMsg("Downloading file ...");
} catch (Exception e) {
printMsg(e.toString());
}
}
public void getList() {
String[] tmpList = new String[100];
String tmpFile;
try {
con = (StreamConnection) Connector.open(url);
DataOutputStream out = con.openDataOutputStream();
out.writeUTF("sendList" );
out.flush();
out.close();
DataInputStream in = con.openDataInputStream();
tmpFile=in.readUTF();
in.close();
con.close();
// Break String back to String array
int c = 0;
char chr = ','; // String separator
while (true) {
if (tmpFile.indexOf(chr) != tmpFile.lastIndexOf(chr)) {
tmpList[c] = tmpFile.substring(0,tmpFile.indexOf(","));
tmpFile = tmpFile.substring(tmpFile.indexOf(",") + 1);
c++;
}
else {
tmpList[c] = tmpFile.substring(0,tmpFile.indexOf(","));
c++;
tmpList[c] = tmpFile.substring(tmpFile.indexOf(",") + 1);
c++;
break;
}
}
// Clear fileList
fileList.deleteAll();
// Update file list
for (int i=0; i<c ;i++)
fileList.append(tmpList[i],null);
printMsg("List Updated.");
} catch (Exception e) {
printMsg(e.toString());
}
}
public void printMsg(String s){
alert = new Alert("btClient");
alert.setTimeout(5000);
alert.setType(AlertType.INFO);
alert.setString(s);
Display.getDisplay(this).setCurrent(alert);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -