您的位置:首页 > 其它

j2me实现蓝牙通信

2011-04-24 18:40 10 查看
客户端:

import java.io.*;
import java.util.*;
import javax.bluetooth.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.io.*;
import javax.microedition.midlet.MIDletStateChangeException;

public class BlueClient extends MIDlet implements Runnable,DiscoveryListener,CommandListener{
private DiscoveryAgent discoveryAgent=null;
private static final UUID Server_ID=new UUID("F0E0D0C0B0A000908070605040302010",false);
private UUID [] uuidSet;
private String rURL=null;
private ServiceRecord sr=null;
private StreamConnection conn;
private DataOutputStream dos=null;
private DataInputStream dis=null;
private Thread c=null;

private Display display=null;
private Form fm=new Form("BlueMe");
private TextField tf=new TextField("","",255,TextField.ANY);
private Command send=new Command("Send",Command.SCREEN,1);
private Command exit=new Command("Exit",Command.EXIT,1);
private Vector v=new Vector();
private List l=new List("Devices",Choice.IMPLICIT);

public BlueClient() {
uuidSet=new UUID[1];
uuidSet[0]=Server_ID;
try{
LocalDevice localDevice=LocalDevice.getLocalDevice();
discoveryAgent=localDevice.getDiscoveryAgent();
discoveryAgent.startInquiry(DiscoveryAgent.GIAC, this);
}catch(Exception ex){
ex.printStackTrace();
}
}

protected void startApp() throws MIDletStateChangeException {
display=Display.getDisplay(this);
fm.append(tf);
fm.addCommand(exit);
fm.addCommand(send);
l.addCommand(exit);
fm.setCommandListener(this);
l.setCommandListener(this);

}

protected void pauseApp() {
}

protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}

public void commandAction(Command c,Displayable d){
if(c==send){
sendMessage();
}
else if(c==List.SELECT_COMMAND){
int i=l.getSelectedIndex();
try{
discoveryAgent.searchServices(null, uuidSet, (RemoteDevice)v.elementAt(i), this);
}catch(Exception ex){
ex.printStackTrace();
}
}
else if(c==exit){
this.notifyDestroyed();
}
}

public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
try{
l.append(btDevice.getFriendlyName(false), null);
v.addElement(btDevice);
}catch(Exception ex){
ex.printStackTrace();
}
}

public void inquiryCompleted(int discType) {
display.setCurrent(l);
}

public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
if(servRecord.length!=0)
sr=servRecord[0];
}

public void serviceSearchCompleted(int transID, int respCode) {
if(sr!=null){
rURL=sr.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
c=new Thread(this);
c.start();
}
else{
System.out.println("Error");
}
display.setCurrent(fm);
}

public void run(){
try{
conn=(StreamConnection)Connector.open(rURL);
dis=conn.openDataInputStream();
dos=conn.openDataOutputStream();
new Thread(new Receive()).start();
}catch(Exception ex){
ex.printStackTrace();
}
}

public void sendMessage(){
try{
dos.writeUTF(tf.getString());

    dos.flush();
tf.setString("");
}catch(Exception ex){
ex.printStackTrace();
}
}

class Receive implements Runnable{
public void run(){
while(true){
try{
String msg=dis.readUTF()+"/n";
fm.append(msg);
}catch(EOFException ex){
}
catch(Exception ex){
ex.printStackTrace();
}
}
}
}
}

服务端:

import java.io.*;

import javax.bluetooth.*;
import javax.microedition.io.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDletStateChangeException;

public class BlueServer extends MIDlet implements Runnable,CommandListener{
private LocalDevice localdevice=null;
private StreamConnectionNotifier notifier=null;
private Thread serv=null;
private StreamConnection conn;
private DataInputStream dis=null;
private DataOutputStream dos=null;
private static final UUID Server_ID=new UUID("F0E0D0C0B0A000908070605040302010",false);

private Display display=null;
private Form fm=new Form("BlueMe");
private TextField tf=new TextField("","",255,TextField.ANY);
private Command send=new Command("Send",Command.SCREEN,1);
private Command exit=new Command("Exit",Command.EXIT,1);

public BlueServer() {
try{
localdevice=LocalDevice.getLocalDevice();
}catch(Exception ex){
ex.printStackTrace();
}
serv=new Thread(this);
serv.start();
}

protected void startApp() throws MIDletStateChangeException {
display=Display.getDisplay(this);
fm.append(tf);
fm.addCommand(send);
fm.addCommand(exit);
fm.setCommandListener(this);
display.setCurrent(fm);
}

protected void pauseApp() {
}

protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}

public void commandAction(Command c,Displayable d){
if(c==send){
sendMessage();
}
else if(c==exit){
this.notifyDestroyed();
}
}

public void run(){

try{
notifier=(StreamConnectionNotifier)Connector.open("btspp://localhost:"+Server_ID.toString()+
";name=CheckOne Server;authorize=false");
conn=notifier.acceptAndOpen();
dis=conn.openDataInputStream();
dos=conn.openDataOutputStream();
new Thread(new Receive()).start();   
}catch(Exception ex){
ex.printStackTrace();
}
}

public void sendMessage(){
try{
dos.writeUTF(tf.getString());
tf.setString("");
}catch(Exception ex){
ex.printStackTrace();
}
}

class Receive implements Runnable{
public void run(){
while(true){
try{
String msg=dis.readUTF()+"/n";
fm.append(msg);
}catch(EOFException ex){
}
catch(Exception ex){
ex.printStackTrace();
}
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息