?? lanyasousuo.txt
字號:
用J2ME搜索藍牙設(shè)備并通信
代碼來源于我目前的項目,所以不能給出完整代碼(非常多),只給出關(guān)鍵代碼,對于一般J2ME程序員絕對能看懂!
首先,給出J2ME搜索藍牙設(shè)備的代碼:
public void commandAction(Command command, Displayable displayable)
{
if(command==cmd_Search)
{
try
{
LocalDevice localDevice = LocalDevice.getLocalDevice();
DiscoveryAgent discoveryAgent = localDevice.getDiscoveryAgent();
discoveryAgent.startInquiry(DiscoveryAgent.GIAC,new MyDiscoveryListener(this));
}
catch (Exception e)
{
MyClass.MessageBox("搜索出錯,找不到藍牙設(shè)備!", display);
return;
}
Menu.removeCommand(cmd_Search);
}
}
}
class MyDiscoveryListener implements DiscoveryListener {
//用于保存搜索到的設(shè)備
Vector devices = new Vector();
SearchDevices midlet;
public MyDiscoveryListener(SearchDevices midlet) {
this.midlet = midlet;
}
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
devices.addElement(btDevice);
}
public void inquiryCompleted(int discType) {
for (int i=0; i {
RemoteDevice btDevice = (RemoteDevice)devices.elementAt(i);
try
{
String device_address=btDevice.getBluetoothAddress(); //取得藍牙設(shè)備的全球唯一地址
}
catch (Exception e) {e.printStackTrace(); }
}
}
public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {}
public void serviceSearchCompleted(int transID, int responseCode) {}
}
接下來就是根據(jù)搜索所得的藍牙設(shè)備地址來連接設(shè)備,并與設(shè)備通信:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.bluetooth.ServiceRecord;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
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.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class MIDlet_ConnDevice extends MIDlet
implements Runnable, CommandListener {
private Display display = null;
private Form form = new Form("藍牙 ");
//用于輸入要發(fā)送的消息
private TextField tfData = new TextField("請輸入發(fā)送的消息",
"",255,TextField.ANY);
private Command cmdExit = new Command("退出", Command.EXIT, 0);
private Command cmdSend = new Command("發(fā)送", Command.SCREEN, 1);
private Command cmdConnect = new Command("連接", Command.SCREEN, 1);
//線程運行標志
private boolean isRunning = true;
StreamConnection client = null;
//服務(wù)器服務(wù)記錄
ServiceRecord record=null;
private DataInputStream dis = null;
private DataOutputStream dos = null;
public MIDlet_ConnDevice() {
super();
form.append(tfData);
form.addCommand(cmdExit);
form.addCommand(cmdConnect);
form.setCommandListener(this);
}
protected void startApp() throws MIDletStateChangeException {
display = Display.getDisplay(this);
display.setCurrent(form);
}
protected void pauseApp() {
isRunning = false;
close();
}
protected void destroyApp(boolean arg0)
throws MIDletStateChangeException {
isRunning = false;
close();
}
/**
* 處理命令按鈕事件
*/
public void commandAction(Command cmd, Displayable d) {
if (cmd == cmdExit) {
isRunning = false;
notifyDestroyed();
} else if(cmd == cmdSend) {
//發(fā)送數(shù)據(jù)
new Thread() {
public void run() {
if (dos == null) {
return;
}
//把輸入的字符串變?yōu)閿?shù)字
try {
dos.writeUTF(tfData.getString());
dos.flush();
} catch (IOException e) {
form.append("Send error");
}
}
}.start();
} else if (cmd == cmdConnect) {
//開始服務(wù)器線程
new Thread(this).start();
}
}
/**
* 客戶端接收線程
*/
public void run() {
isRunning = true;
client = null;
String device_address; //搜索所得的設(shè)備地址
String conURL = "btspp://"+device_address+":1";
try {
client = (StreamConnection)Connector.open(conURL);
form.append("Connected!\n");
dis = client.openDataInputStream();
dos = client.openDataOutputStream();
form.removeCommand(cmdConnect);
form.addCommand(cmdSend);
} catch (Exception e) {
form.append("connect error");
close();
return;
}
while(isRunning) {
try {
if(dis.available()>1)
{
byte[] rec_package=new byte[dis.available()]; //強烈建議這里使用byte[]
dis.read(rec_package);
String str=new String(rec_package);
}
}
catch (Exception e) {
form.append("rec error");
break;
}
}
close();
form.removeCommand(cmdSend);
form.addCommand(cmdConnect);
}
/**
* 關(guān)閉連接
*/
public void close() {
try {
if (dis != null) {
dis.close();
dis = null;
}
if (dos != null) {
dos.close();
dos = null;
}
if (client != null) {
client.close();
client = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -