Java蓝牙电话_java蓝牙通信

这篇博客介绍了一个使用Java进行蓝牙通信的实例,通过`BlueClient`类实现了一个简单的蓝牙电话应用。内容涵盖了LocalDevice获取、DiscoveryAgent初始化、设备发现、服务搜索以及连接建立等关键步骤,读者可以了解到如何在MIDlet中处理蓝牙通信的基本流程。
摘要由CSDN通过智能技术生成

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值