蓝牙方面的知识,进着只要是有用的信息就有分

xuyan87101 2009-08-10 11:32:13
蓝牙方面的知识,只要是蓝牙方面的资料都可以,希望是自己做的程序源码分享一下,最近没事想研究一下这方面的知识,wtk上的例子就不要说了,我自己就可以看的O(∩_∩)O~,呵呵……谢谢大家了……

我希望各位兄弟姐妹们能给点实例,不要让我在网上搜索了,呵呵……我已经搜索了好些了……

就把实例发到我邮箱吧,嘿嘿……谢谢……

我的邮箱xuyan87101@126.com

再次的谢谢了……
...全文
141 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
hc0120 2009-08-14
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 xuyan87101 的回复:]
要结贴了,如果想看看上面的资料的话就快点看啊
[/Quote]
结了贴我们也能再看啊。只是可惜了,一共就一个:http://www.j2megame.org/index.php/content/view/337/125.html
和一个:http://blog.csdn.net/hellogv/archive/2008/04/24/2323313.aspx
23楼真 广告....
xuyan87101 2009-08-14
  • 打赏
  • 举报
回复
要结贴了,如果想看看上面的资料的话就快点看啊
呵呵……发好几天了该结了……谢谢各位楼上的O(∩_∩)O~
  • 打赏
  • 举报
回复
现在正在学习,楼主的帖子很不错,跟帖的代码太长了,看不懂
javacaspnet 2009-08-13
  • 打赏
  • 举报
回复
同意楼上
xuyan87101 2009-08-13
  • 打赏
  • 举报
回复
呵呵……现在没事做,了解了解……
xuyan87101 2009-08-12
  • 打赏
  • 举报
回复
呵呵……再来点就结贴了^资料啊呵呵……
chaner2002 2009-08-12
  • 打赏
  • 举报
回复
你怎么又高蓝牙了,我都转行了
xuyan87101 2009-08-11
  • 打赏
  • 举报
回复
这大早上的都去哪喝茶去了???没事了i来讨论一下吧,顶一下……
LeoTang2 2009-08-11
  • 打赏
  • 举报
回复
貌似一个实例,有手机屏幕与模拟器的对比图的一个案例:
------------------------------------------------------------------------------
引用 3 楼 foolstudio2 的回复:
到机械工业出版社下载《J2ME手机高级编程》一书的源代码,好像是5还是6章,
是在NOKIA 5310还是5320 的机器上进行蓝牙通信,可以参考下,免费注册之后就可以下载了。


从书上看的,在N5310上运行的状况似乎和WTK模拟器上是一模一样的,~~~
------------------------------------------------------------------------------
doney_dongxiang 2009-08-11
  • 打赏
  • 举报
回复
我一般 使用蓝牙的时候 直接查看 API文档,也建议 楼主 查看API吧 顺便说一下,WTK下面的一些源代码 也有蓝牙功能的
xuyan87101 2009-08-11
  • 打赏
  • 举报
回复
楼上的各位辛苦了,谢谢啊O(∩_∩)O~
xuyan87101 2009-08-11
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 zl3450341 的回复:]
实例代码

该程序包括3个java文件。一个是MIDlet,另外2个为服务端GUI和客户端GUI。该程序已经在wtk22模拟器和Nokia 6600,Nokia 6670两款手机上测试通过。


http://www.j2megame.org/index.php/content/view/337/125.html

这个上面有一个实例
本来准备贴过来 太长了
[/Quote]
和这位的一样的,呵呵……

有没有做好的两个可以相互通讯的蓝牙程序啊???
就是像发短信那样……
luojun198305 2009-08-11
  • 打赏
  • 举报
回复
楼上的有心了,这么长的代码很辛苦哦,顶下!
  • 打赏
  • 举报
回复
ServerBox.java


import java.io.java/io/DataInputStream.java.html" target="_blank">DataInputStream;
import java.io.java/io/DataOutputStream.java.html" target="_blank">DataOutputStream;
import java.io.java/io/IOException.java.html" target="_blank">IOException;
import java.util.java/util/Vector.java.html" target="_blank">Vector;
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.io.StreamConnectionNotifier;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
/**
* 服务端GUI
* @author Jagie
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class ServerBox extends TextBox implements java/lang/Runnable.java.html" target="_blank">Runnable, CommandListener {
Command com_pub = new Command("开启服务", Command.OK, 0);
Command com_cancel = new Command("终止服务", Command.CANCEL, 0);
Command com_back = new Command("返回", Command.BACK, 1);
LocalDevice localDevice;
StreamConnectionNotifier notifier;
ServiceRecord record;
boolean isClosed;
ClientProcessor processor;
StupidBTMIDlet midlet;
//响应服务的uuid
private static final UUID ECHO_SERVER_UUID = new UUID(
"F0E0D0C0B0A000908070605040302010", false);
public ServerBox(StupidBTMIDlet midlet) {
super(null, "", 500, TextField.ANY);
this.midlet = midlet;
this.addCommand(com_pub);
this.addCommand(com_back);
this.setCommandListener(this);
}
public void run() {
boolean isBTReady = false;
try {
localDevice = LocalDevice.getLocalDevice();
if (!localDevice.setDiscoverable(DiscoveryAgent.GIAC)) {
showInfo("无法设置设备发现模式");
return;
}
// prepare a URL to create a notifier
java/lang/StringBuffer.java.html" target="_blank">StringBuffer url = new java/lang/StringBuffer.java.html" target="_blank">StringBuffer("btspp://");
// indicate this is a server
url.append("localhost").append(:);
// add the UUID to identify this service
url.append(ECHO_SERVER_UUID.toString());
// add the name for our service
url.append(";name=Echo Server");
// request all of the client not to be authorized
// some devices fail on authorize=true
url.append(";authorize=false");
// create notifier now
notifier = (StreamConnectionNotifier) Connector
.open(url.toString());
record = localDevice.getRecord(notifier);
// remember weve reached this point.
isBTReady = true;
} catch (java/lang/Exception.java.html" target="_blank">Exception e) {
e.printStackTrace();

}
// nothing to do if no bluetooth available
if (isBTReady) {
showInfo("初始化成功,等待连接");
this.removeCommand(com_pub);
this.addCommand(com_cancel);
} else {
showInfo("初始化失败,退出");
return;
}
// 生成服务端服务线程对象
processor = new ClientProcessor();
// ok, start accepting connections then
while (!isClosed) {
StreamConnection conn = null;
try {
conn = notifier.acceptAndOpen();
} catch (java/io/IOException.java.html" target="_blank">IOException e) {
// wrong client or interrupted - continue anyway
continue;
}
processor.addConnection(conn);
}
}
public void publish() {
isClosed = false;
this.setString(null);
new java/lang/Thread.java.html" target="_blank">Thread(this).start();
}
public void cancelService() {
isClosed = true;
showInfo("服务终止");
this.removeCommand(com_cancel);
this.addCommand(com_pub);
}
/*
* (non-Javadoc)
*
* @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command,
* javax.microedition.lcdui.Displayable)
*/
public void commandAction(Command arg0, Displayable arg1) {
if (arg0 == com_pub) {
//发布service
publish();
} else if (arg0 == com_cancel) {
cancelService();
} else {
cancelService();
midlet.showMainMenu();
}
}

/**
* 内部类,服务端服务线程。
* @author Jagie
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
private class ClientProcessor implements java/lang/Runnable.java.html" target="_blank">Runnable {
private java/lang/Thread.java.html" target="_blank">Thread processorThread;
private java/util/Vector.java.html" target="_blank">Vector queue = new java/util/Vector.java.html" target="_blank">Vector();
private boolean isOk = true;
ClientProcessor() {
processorThread = new java/lang/Thread.java.html" target="_blank">Thread(this);
processorThread.start();
}
public void run() {
while (!isClosed) {
synchronized (this) {
if (queue.size() == 0) {
try {
//阻塞,直到有新客户连接
wait();
} catch (java/lang/InterruptedException.java.html" target="_blank">InterruptedException e) {
}
}
}

//处理连接队列
StreamConnection conn;
synchronized (this) {
if (isClosed) {
return;
}
conn = (StreamConnection) queue.firstElement();
queue.removeElementAt(0);
processConnection(conn);
}
}
}

/**
* 往连接队列添加新连接,同时唤醒处理线程
* @param conn
*/
void addConnection(StreamConnection conn) {
synchronized (this) {
queue.addElement(conn);
notify();
}
}
}
/**
* 从StreamConnection读取输入
* @param conn
* @return
*/
private java/lang/String.java.html" target="_blank">String readInputString(StreamConnection conn) {
java/lang/String.java.html" target="_blank">String inputString = null;
try {
java/io/DataInputStream.java.html" target="_blank">DataInputStream dis = conn.openDataInputStream();
inputString = dis.readUTF();
dis.close();
} catch (java/lang/Exception.java.html" target="_blank">Exception e) {
e.printStackTrace();
}
return inputString;
}

/**
* debug
* @param s
*/
private void showInfo(java/lang/String.java.html" target="_blank">String s) {
java/lang/StringBuffer.java.html" target="_blank">StringBuffer sb = new java/lang/StringBuffer.java.html" target="_blank">StringBuffer(this.getString());
if (sb.length() > 0) {
sb.append("\n");
}
sb.append(s);
this.setString(sb.toString());
}

/**
* 处理客户端连接
* @param conn
*/
private void processConnection(StreamConnection conn) {
// 读取输入
java/lang/String.java.html" target="_blank">String inputString = readInputString(conn);
//生成响应
java/lang/String.java.html" target="_blank">String outputString = inputString.toUpperCase();
//输出响应
sendOutputData(outputString, conn);
try {
conn.close();
} catch (java/io/IOException.java.html" target="_blank">IOException e) {
} // ignore
showInfo("客户端输入:" + inputString + ",已成功响应!");
}

/**
* 输出响应
* @param outputData
* @param conn
*/
private void sendOutputData(java/lang/String.java.html" target="_blank">String outputData, StreamConnection conn) {
try {
java/io/DataOutputStream.java.html" target="_blank">DataOutputStream dos = conn.openDataOutputStream();
dos.writeUTF(outputData);
dos.close();
} catch (java/io/IOException.java.html" target="_blank">IOException e) {
}
}
}
  • 打赏
  • 举报
回复
ClientBox.java


import java.io.java/io/DataInputStream.java.html" target="_blank">DataInputStream;
import java.io.java/io/DataOutputStream.java.html" target="_blank">DataOutputStream;
import java.io.java/io/IOException.java.html" target="_blank">IOException;
import java.util.java/util/Vector.java.html" target="_blank">Vector;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
//jsr082 API
import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;
/**
* 客户端GUI
* @author Jagie
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class ClientBox extends Form implements java/lang/Runnable.java.html" target="_blank">Runnable, CommandListener,
DiscoveryListener {

//字串输入框
TextField input = new TextField(null, "", 50, TextField.ANY);
//loger
StringItem result = new StringItem("结果:", "");
private DiscoveryAgent discoveryAgent;

private UUID[] uuidSet;
//响应服务的UUID
private static final UUID ECHO_SERVER_UUID = new UUID(
"F0E0D0C0B0A000908070605040302010", false);
//设备集合
java/util/Vector.java.html" target="_blank">Vector devices = new java/util/Vector.java.html" target="_blank">Vector();
//服务集合
java/util/Vector.java.html" target="_blank">Vector records = new java/util/Vector.java.html" target="_blank">Vector();

//服务搜索的事务id集合
int[] transIDs;
StupidBTMIDlet midlet;
public ClientBox(StupidBTMIDlet midlet) {
super("");
this.midlet=midlet;

this.append(result);

this.addCommand(new Command("取消",Command.CANCEL,1));
this.setCommandListener(this);

new java/lang/Thread.java.html" target="_blank">Thread(this).start();
}

public void commandAction(Command arg0, Displayable arg1) {
if(arg0.getCommandType()==Command.CANCEL){
midlet.showMainMenu();
}else{
//匿名内部Thread,访问远程服务。
java/lang/Thread.java.html" target="_blank">Thread fetchThread=new java/lang/Thread.java.html" target="_blank">Thread(){
public void run(){
for(int i=0;i<records.size();i++){
ServiceRecord sr=(ServiceRecord)records.elementAt(i);
if(accessService(sr)){
//访问到一个可用的服务即可
break;
}
}
}
};
fetchThread.start();
}

}


private boolean accessService(ServiceRecord sr){
boolean result=false;
try {
java/lang/String.java.html" target="_blank">String url = sr.getConnectionURL(
ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
StreamConnection conn = (StreamConnection) Connector.open(url);

java/io/DataOutputStream.java.html" target="_blank">DataOutputStream dos=conn.openDataOutputStream();
dos.writeUTF(input.getString());
dos.close();
java/io/DataInputStream.java.html" target="_blank">DataInputStream dis=conn.openDataInputStream();
java/lang/String.java.html" target="_blank">String echo=dis.readUTF();
dis.close();
showInfo("反馈结果是:"+echo);
result=true;

} catch (java/io/IOException.java.html" target="_blank">IOException e) {

}
return result;
}
public synchronized void run() {
//发现设备和服务的过程中,给用户以Gauge
Gauge g=new Gauge(null,false,Gauge.INDEFINITE,Gauge.CONTINUOUS_RUNNING);
this.append(g);
showInfo("蓝牙初始化...");
boolean isBTReady = false;
try {
LocalDevice localDevice = LocalDevice.getLocalDevice();
discoveryAgent = localDevice.getDiscoveryAgent();
isBTReady = true;
} catch (java/lang/Exception.java.html" target="_blank">Exception e) {
e.printStackTrace();
}
if (!isBTReady) {
showInfo("蓝牙不可用");
//删除Gauge
this.delete(1);
return;
}
uuidSet = new UUID[2];
//标志我们的响应服务的UUID集合
uuidSet[0] = new UUID(0x1101);
uuidSet[1] = ECHO_SERVER_UUID;

try {
discoveryAgent.startInquiry(DiscoveryAgent.GIAC, this);
} catch (BluetoothStateException e) {
}
try {
//阻塞,由inquiryCompleted()回调方法唤醒
wait();
} catch (java/lang/InterruptedException.java.html" target="_blank">InterruptedException e1) {

e1.printStackTrace();
}
showInfo("设备搜索完毕,共找到"+devices.size()+"个设备,开始搜索服务");
transIDs = new int[devices.size()];
for (int i = 0; i < devices.size(); i++) {
RemoteDevice rd = (RemoteDevice) devices.elementAt(i);
try {
//记录每一次服务搜索的事务id
transIDs[i] = discoveryAgent.searchServices(null, uuidSet,
rd, this);
} catch (BluetoothStateException e) {
continue;
}
}

try {
//阻塞,由serviceSearchCompleted()回调方法在所有设备都搜索完的情况下唤醒
wait();
} catch (java/lang/InterruptedException.java.html" target="_blank">InterruptedException e1) {
e1.printStackTrace();
}

showInfo("服务搜索完毕,共找到"+records.size()+"个服务,准备发送请求");
if(records.size()>0){
this.append(input);
this.addCommand(new Command("发送",Command.OK,0));
}

//删除Gauge
this.delete(1);

}

/**
* debug
* @param s
*/

private void showInfo(java/lang/String.java.html" target="_blank">String s){
java/lang/StringBuffer.java.html" target="_blank">StringBuffer sb=new java/lang/StringBuffer.java.html" target="_blank">StringBuffer(result.getText());
if(sb.length()>0){
sb.append("\n");
}
sb.append(s);
result.setText(sb.toString());
}

/**
* 回调方法
*/
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
if (devices.indexOf(btDevice) == -1) {
devices.addElement(btDevice);
}
}
/**
* 回调方法,唤醒初始化线程
*/
public void inquiryCompleted(int discType) {
synchronized (this) {
notify();
}
}
/**
* 回调方法
*/
public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
for (int i = 0; i < servRecord.length; i++) {
records.addElement(servRecord[i]);
}
}

/**
* 回调方法,唤醒初始化线程
*/
public void serviceSearchCompleted(int transID, int respCode) {

for (int i = 0; i < transIDs.length; i++) {
if (transIDs[i] == transID) {
transIDs[i] = -1;
break;
}
}

//如果所有的设备都已经搜索服务完毕,则唤醒初始化线程。
boolean finished = true;
for (int i = 0; i < transIDs.length; i++) {
if (transIDs[i] != -1) {
finished = false;
break;
}
}
if (finished) {
synchronized (this) {
notify();
}
}
}
}
  • 打赏
  • 举报
回复
该程序包括3个java文件。一个是MIDlet,另外2个为服务端GUI和客户端GUI。该程序已经在wtk22模拟器和Nokia 6600,Nokia 6670两款手机上测试通过。


StupidBTMIDlet.java


import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
/**
* @author Jagie
*
* MIDlet
*/
public class StupidBTMIDlet extends MIDlet implements CommandListener {
List list;
ServerBox sb;
ClientBox cb;
/*
* (non-Javadoc)
*
* @see javax.microedition.midlet.MIDlet#startApp()
*/
protected void startApp() throws MIDletStateChangeException {
list = new List("傻瓜蓝牙入门", List.IMPLICIT);
list.append("Client", null);
list.append("Server", null);
list.setCommandListener(this);
Display.getDisplay(this).setCurrent(list);
}

/**
* debug方法
* @param s 要显示的字串
*/
public void showString(java/lang/String.java.html" target="_blank">String s) {
Displayable dp = Display.getDisplay(this).getCurrent();
Alert al = new Alert(null, s, null, AlertType.INFO);
al.setTimeout(2000);
Display.getDisplay(this).setCurrent(al, dp);
}

/**
* 显示主菜单
*
*/
public void showMainMenu() {
Display.getDisplay(this).setCurrent(list);
}

protected void pauseApp() {
// TODO Auto-generated method stub
}
public void commandAction(Command com, Displayable disp) {
if (com == List.SELECT_COMMAND) {
List list = (List) disp;
int index = list.getSelectedIndex();
if (index == 1) {
if (sb == null) {
sb = new ServerBox(this);
}
sb.setString(null);
Display.getDisplay(this).setCurrent(sb);
} else {
//每次都生成新的客户端实例
cb = null;
java/lang/System.java.html" target="_blank">System.gc();
cb = new ClientBox(this);
Display.getDisplay(this).setCurrent(cb);
}
}
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
}
欧巴Godwin 2009-08-11
  • 打赏
  • 举报
回复
http://blog.csdn.net/hellogv/archive/2008/04/24/2323313.aspx
使用J2ME搜索蓝牙设备,并与搜到的设备通信

已经搞定了手机通过蓝牙与单片机通信
xuyan87101 2009-08-11
  • 打赏
  • 举报
回复
sadfasfafafafa
xuyan87101 2009-08-10
  • 打赏
  • 举报
回复
恩,知道,那个实例已经看过了,呵呵……谢谢啊……

有没有自己做的而且还是不错的???呵呵……比如蓝牙通讯蓝牙控制设备之类的程序???谢谢啊忽忽O(∩_∩)O~

期待中……

大家加油……O(∩_∩)O~

zl3450341 2009-08-10
  • 打赏
  • 举报
回复
实例代码

该程序包括3个java文件。一个是MIDlet,另外2个为服务端GUI和客户端GUI。该程序已经在wtk22模拟器和Nokia 6600,Nokia 6670两款手机上测试通过。


http://www.j2megame.org/index.php/content/view/337/125.html

这个上面有一个实例
本来准备贴过来 太长了
加载更多回复(3)

13,100

社区成员

发帖
与我相关
我的任务
社区描述
Java J2ME
社区管理员
  • J2ME社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧