j2me实现蓝牙通信

客户端:

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();
}
}
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值