- 浏览: 204236 次
- 性别:
- 来自: 河源
文章分类
- 全部博客 (102)
- Hibernate IBatis (0)
- Spring (0)
- Struts2.x Struts1.x (0)
- Oracle (3)
- sql server (2)
- JavaME (15)
- MySql (0)
- Android (9)
- lwuit (10)
- .net (9)
- Ajax (3)
- UML (0)
- JavaSE——java基础 (4)
- Java web (3)
- 设计模式 (2)
- JSF (0)
- 服务器之:Tomcat (1)
- 计算机网络 (14)
- 操作系统 (5)
- 工具类 (7)
- Linux (0)
- 其他 (3)
- 经验总结 (3)
- 搜索引擎 (1)
- html (1)
- python (1)
- c (2)
- perl (1)
- Lisp (1)
最新评论
-
十口文:
谢谢你的分享!!
操作系统 教程 -
liu_shui8:
请问这是什么考试的试题?
谢谢!!!
操作系统试题及参考答案 -
JAVA言诚eye218:
这个在8.6中已经不能用了
myeclipse8.6正式版下载地址+注册码 -
liky1234567:
虽然是英语,还是耐心看完了
Simple Bluetooth Communication in J2ME -
xiao_feng68:
有个疑问想问博主:问什么直接使用组件事件得到的media是都是 ...
zk fileupload
最简单的蓝牙操作步骤,一步一步教你如何操作,它的流程很清晰,而且也很容易理解,你可以试着做一下。。。
Simple Bluetooth Communication
In this article, I will try to explain the simple Bluetooth communication standards and show how you can create a simple wrapper class around Bluetooth technology. This article is for those peoples who want to write a J2ME Bluetooth application by understanding its API and protocols.
Wireless Technologies
The most famous wireless technologies are infraree, Bluetooth, WiFi, and Zigbee. Infrared is the technology that you can see in TV remote controls or air conditioner remotes where the communication should be pointed to the target device. WiFi technology is used for strong and wide area communication where wireless communication can be made. Zigbee is the most recent technology; it's cheaper than all the other wireless media. Bluetooth technology is the most used temporary communication technology, especially inside mobile devices, palm tops, pocket PCs, and so forth. It can be used to exchange objects, packets, or a simple stream.
Bluetooth Communication Types
There are three types of communication protocols defined inside Bluetooth technology:
OBEX: The "Object Exchange" communication protocol is used to exchange physical data such as files, images, and so on in binary format.
L2CAP: The "Logical Link Control and Adaptation Protocol" used to send packets between host and client.
RFCOMM: The "Radio Frequency COMMunication" is very easy and uncomplicated; it is used to stream simple data.
Java Bluetooth API
Sun Java has introduced the Bluetooth JSR82 API package. The JSR82 API has capability to provide all three kinds of communications: either Obex, L2CAP, or RFCOMM. This article will focus on the simplest protocol, RFCOMM, and send only string data between the devices.
Client and Server
The technique to communicate any device will follow the good old-fashioned rule of Client and Server. You will open the Server and then wait for the client to connect; after that, the server and client both can communicate with each other easily. In Bluetooth, you have to do the same technique; the application must allow the user to select it as either the server or client.
Code and Explanation
1. Server
Every Bluetooth device contains the local Bluetooth object that helps communicate between devices. In JSR82, the LocalDevice.getLocalDevice(); function returns the object of the local Bluetooth device. The local device object should call the setDiscoverable(DiscoveryAgent.GIAC); function, in which the mode is set as GIAC. In simple words, by doing this you give permission to the current device to find other devices.
To open the Bluetooth connection, you have to build a Bluetooth URL string that will be called inside the Connector.open(URL) function; this function will return the StreamConnectionNotifier Object. The URL actually is the way to initialize the communication protocol for Bluetooth, just like on an Internet Explorer search box. You just type http://www.address.com, where http:// is the protocol and the rest is the address of the target place. In Bluetooth, you will do something like this:
URL = "btspp://localhost:" + UUID + ";name=rfcommtest;authorize=true";
Here, you have btspp:// just like the http:// protocol. The rest has an uniquely identified ID so that it will have its unique address.
After the StreamConnectionNotifier has been initialized, it has to call the final acceptAndOpen(); function that simply opens the communication and returns the StreamConnection object. But, unless no client connection is found, it will block the other processes and wait.
Now, you can use two functions by StreamConnectionb' object: openOutputStream() or openInputStream(). Both are used as a way to send and receive data.
m_strUrl= "btspp://localhost:" + RFCOMM_UUID + ";
name=rfcommtest;authorize=true";
// m_StrmConn = BTFACADE.waitForClient(SERVICE_NBR);
try
{
m_LclDevice = LocalDevice.getLocalDevice();
m_LclDevice.setDiscoverable(DiscoveryAgent.GIAC);
m_StrmNotf = (StreamConnectionNotifier)Connector.open(m_strUrl);
//Now it will start waiting for the client connection
m_StrmConn = m_StrmNotf.acceptAndOpen();
m_bInitServer = true;
m_Output = m_StrmConn.openOutputStream();
m_Input = m_StrmConn.openInputStream();
}
catch (BluetoothStateException e)
{
System.err.println( "BluetoothStateException: " + e.getMessage() );
}
catch (IOException ex)
{
ex.printStackTrace();
}
catch(Exception e)
{
System.err.println( "Exception: " + e.getMessage() );
}
2. Client
To create the client, the UPI has to follow some rules to obtain your goal, which is to implement the DiscoveryListener interface. It has four, pure-virtual functions:
void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod)
void servicesDiscovered(int transID, ServiceRecord[] records)
void serviceSearchCompleted(int transID, int respCode)
void inquiryCompleted(int discType)
At first, you have to search the available Bluetooth devices around you. You have to get the local device information and, through it, retrieve the DiscoveryAgent object to start enquiry about available devices
public void SearchAvailDevices()
{
try
{
//First, get the local device and obtain the discovery agent.
m_LclDevice = LocalDevice.getLocalDevice();
m_DscrAgent= m_LclDevice.getDiscoveryAgent();
m_DscrAgent.startInquiry(DiscoveryAgent.GIAC,this);
}
catch (BluetoothStateException ex)
{
System.out.println("Problem in searching the Bluetooth devices");
ex.printStackTrace();
}
}
For the client, these four methods of DiscoveryListener must to be override in the class. According to their name, they do work; for example, deviceDiscovered suddenly gets triggered when any Bluetooth device is found. After that, it is your responsibility to find the services of devices such as OBEX, RFCOMM, or L2CAP.
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod)
{
try
{
// Device information
System.out.println("Major Device Class and information : " +
cod.getMajorDeviceClass() +
" Minor Device Class: " +
cod.getMinorDeviceClass());
System.out.println("Bluetooth Address of the device: " +
btDevice.getBluetoothAddress());
System.out.println("Friendly Name: " +
btDevice.getFriendlyName(true));
// Now its our responsibility to search its services
UUID uuidSet[] = new UUID[1];
uuidSet[0] = RFCOMM_UUID;
int searchID = m_DscrAgent.searchServices(null,uuidSet,
btDevice,this);
}
catch (Exception e)
{
System.out.println("Device Discovered Error: " + e);
}
}
Here, m_DscrAgent is the DiscoveryAgent object that searches the available services of the first device you found.
public void servicesDiscovered(int transID, ServiceRecord[] records)
{
for (int i = 0; i < records.length; i++)
{
m_strUrl = records[i].getConnectionURL(ServiceRecord.
AUTHENTICATE_ENCRYPT, false);
System.out.println(m_strUrl);
//we have found our service protocol
if(m_strUrl.startsWith("btspp"))
{
m_bServerFound = true;
m_bInitClient=true;
break;
}
}
The ServicesDiscovered function above is triggered when services of that device are found. Here, you stop the loop on the first protocol that you found as Bluetooth.
After the Services Search function is complete, serviceSearchCompleted(int transID, int respCode) is triggered. From it, you can do further initialization such as when you found the service that you were looking for. Now, it's time to open input/output variables.
public void serviceSearchCompleted(int transID, int respCode)
{
if(m_bServerFound)
{
try
//lets the communication start by setting the URL and sending
//the client response
{
m_StrmConn = (StreamConnection) Connector.open(m_strUrl);
m_Output = m_StrmConn.openOutputStream();
m_Input = m_StrmConn.openInputStream();
m_Output.write(CLIENT_RESPONSE.length());
m_Output.write(CLIENT_RESPONSE.getBytes());
System.out.println("serviceSearchCompleted");
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
Here, you open the connection with the server Connector.open(m_strUrl); and then you open the Input/Output stream for further communication with the server. To send any data to the current server, you have to send the buffer length or data length first, and then the total bytes of the string. This way, the server or client will know what length the upcoming text will have.
m_Output.write(CLIENT_RESPONSE.length());
m_Output.write(CLIENT_RESPONSE.getBytes());
m_Output is used to send data to the connected person; CLIENT_RESPONSE is just string data. On the server side, the acceptAndOpen() function lets the process close from waiting of any client and start communication.
Send and Receive Data
When the client and server both start working, the communication is held by the two wrapper functions: SendMessage() and ReceiveMessage().
Send and Receive Data
When the client and server both start working, the communication is held by the two wrapper functions: SendMessage() and ReceiveMessage().
public void SendMessages(String v_strData)
{
if((m_bInitClient) || (m_bInitServer) )
{
try
{
m_Output.write(v_strData.length());
m_Output.write(v_strData.getBytes());
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
public String RecieveMessages()
{
byte[] data = null;
try
{
int length = m_Input.read();
data = new byte[length];
length = 0;
while (length != data.length)
{
int ch = m_Input.read(data, length, data.length - length);
if (ch == -1)
{
throw new IOException("Can't read data");
}
length += ch;
}
}
catch (IOException e)
{
System.err.println(e);
}
return new String(data);
}
Any client or server can use these functions easily without any problem. The Receive message function simply decodes the data that was received and returns it in a human-readable format.
How the Wrapper Works
The ClientServer class is a very flexible class that can be used as a client or server dynamically. If it needs to be a server, then simply initialize the object as follow:
ClientServer Obj = new ClientServer(true);
Here, true means that this object is a server; making it a client just passes the value as false. It is better to initialize the Server inside a thread so that it will not block at the time of waiting for a client.
The rest is to use SendMessage() and RecieveMessage() for communication as a client or a server. Before closing down the midlet, always remember to call Obj.CloseAll(); so that everything will remain smooth.
public void destroyApp(boolean unconditional)
{
m_bRunThread=false;
m_BlueObj.CloseAll();
}
public void run()
{
while(m_bRunThread)
{
try
{
if(m_BlueObj==null)
{
//it should be called once when object is null
m_BlueObj=new ClientServer(m_bIsServer);
}
String str = m_BlueObj.RecieveMessages();
System.out.println(str);
if(m_bIsServer)
m_BlueObj.SendMessages("Hi there, it's Mr. Server");
else
m_BlueObj.SendMessages("Hi there, it's Mr. Client");
Thread.sleep(100);
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
} //end while
}
Summary
English is not my native language but I have tried to explain the usage of Bluetooth with a very simple way using the wrapper class; it can be extended by implementing new ideas in it. I hope I have shared the knowledge between learners, as a learner.
Farhan Hameed Khan, from Pakistan
发表评论
-
深入分析JAD和 MANIFEST.MF文件
2010-04-28 01:43 1780从前面的分析 中,我 ... -
J2ME字符串split方法
2010-04-27 20:07 1413/** * Split string into mu ... -
自己收集JavaME的相关资料
2010-04-15 02:05 10391、《WTK进阶使用教程》网址:http:// ... -
JavaME 到底可以干些什么
2010-04-10 22:11 1358最近在做一个JavaME的项目,发现JavaME也不 ... -
BlueTooth探索系列(一)---JSR082 API框架剪影
2009-12-16 20:25 1710可以自由转载, 转载请保留下面的作者信息:作者 cleverp ... -
J2ME 串口编程
2009-12-09 16:34 2150Javax.comm是Sun公司提供的,用于开发平台独立的通讯 ... -
JSR82 API 介绍
2009-12-08 23:24 1918蓝牙是一种低成本、 ... -
j2ME多线程网络编程
2009-12-08 22:07 2028网络编程中的多 ... -
在Java ME中通过蓝牙发现设备并传送文件
2009-12-08 22:00 1329在Java ME设备上 ... -
使用 JSR-82 API 实现 OBEX 图像传输
2009-12-08 21:35 1917蓝牙协议 ... -
蓝牙应用
2009-12-08 21:11 1137网易科技讯 4月13 ... -
基于JSR82的蓝牙应用开发手记
2009-12-08 21:08 2191目目标:基于JSR82的蓝牙应用,实现手机和PC之 ... -
CLDC API简要介绍
2009-12-06 09:16 735* 介绍 ... -
j2me框架大全
2009-12-05 01:06 4868J2ME Polish J2ME Po ...
相关推荐
J2ME中的蓝牙通信主要基于JSR-82(Java API for Bluetooth Wireless Technology)。蓝牙通信涉及到的主要类有`BluetoothAdapter`,`DiscoveryAgent`,`RemoteDevice`,`ServiceRecord`等。 1. 设备发现:使用`...
Java Bluetooth 开发在J2ME平台上是一个相对专业的领域,它主要涉及到移动设备和其它蓝牙设备之间的无线通信。J2ME,即Java Micro Edition,是Java平台的一个子集,主要用于嵌入式系统,如手机和掌上设备。蓝牙技术...
在本篇教程中,我们将深入探讨如何在J2ME(Java 2 Platform, Micro Edition)环境中操作精灵(Sprites)。J2ME是专为资源有限的设备设计的Java平台版本,如移动电话、个人数字助理(PDAs)、机顶盒等。随着MIDP 2.0...
Java APIs for Bluetooth (JSR 82) Location API (JSR 179) MID Profile 2.0 (JSR-118) Mobile Media API 1.1 (JSR-135) Mobile 3D Graphics API (JSR184) PDA Pim 1.0 (JSR 75) PDA FileConnection (JSR 75) ...
标题"用J2ME与ASP建立数据库连接.rar_http in j2me_j2me_j2me 3d_j2me asp"暗示了本主题是关于如何在J2ME应用中通过HTTP协议与ASP服务器进行通信,从而实现数据库的远程访问。这在移动应用中尤其有用,因为它允许...
Java 2 Micro Edition (J2ME) 是一种用于开发小型设备和嵌入式系统的 Java 平台,如手机、智能手表、电视等。PIM,全称Personal Information Management(个人信息管理),是 J2ME 中的一个重要部分,它提供了一系列...
标题中的“SMS.rar_blackberry_java sms_send SMS in j2me_sms blackberry_手机 短”揭示了这个项目的核心内容,这是一个使用Java ME(J2ME)开发的短信(SMS)发送和接收功能,适用于Blackberry 8800手机。...
J2ME,全称Java 2 Micro Edition,是Java平台的一个子集,主要用于嵌入式设备和移动设备的开发,如手机、智能电表、家庭自动化设备等。它提供了跨平台的开发环境,使得开发者可以编写一次代码,到处运行。J2ME由两大...
J2ME的应用程序,可自动搜索附近的蓝牙设备。 记录每个发现设备的时间和设备名称。 因此,可以看到人们给他们的蓝牙设备起的名字。 维基中的更多信息!
《J2ME飞机游戏开发详解》 J2ME(Java 2 Micro Edition)是Java平台的一个子集,专为移动设备、嵌入式系统等资源有限的环境设计。本篇文章将深入探讨如何利用J2ME技术开发一款简单的飞机游戏,以此帮助初学者理解...
压缩包中的“PC_Connectivity_over_Bluetooth_in_Java_Apps_v1_0_en.pdf”很可能是一份详细的教程或指南,详细解释了如何在J2ME应用中实现PC与手机之间的蓝牙连接。它可能会涵盖蓝牙配置、设备配对、连接建立和数据...
**J2ME API 2.0 - J2ME使用手册 - J2ME帮助文档** Java 2 Micro Edition(J2ME)是Java平台的一个子集,专为资源有限的设备如移动电话、智能手表和家用电器等设计。J2ME API 2.0 提供了在这些小型设备上开发应用...
**J2ME中文版教程——全面解读移动设备编程** J2ME(Java 2 Micro Edition)是Java平台的一个重要组成部分,专为嵌入式设备、移动电话和其他资源有限的设备设计。这个J2ME中文版教程是针对初学者和有一定经验的...
在J2ME(Java 2 Micro Edition)平台上开发游戏时,地图的设计与绘制是至关重要的一个环节。J2ME作为一种轻量级的Java平台,广泛应用于移动设备,如早期的智能手机和平板电脑,用于实现各种应用程序,特别是游戏。本...
J2ME小游戏J2ME小游戏J2ME小游戏J2ME小游戏J2ME小游戏J2ME小游戏
j2me 开发框架介绍 j2me 是一种用于开发无线应用程序的平台,它提供了一个灵活、强大和开放的开发环境。随着 j2me 的普及,出现了许多开源框架,旨在简化开发过程,提高开发效率。下面将介绍这些框架,並分析它们的...
在Linux操作系统中,进程间通信(IPC,Inter-Process Communication)是系统中多个并发执行的进程之间交换数据的重要机制。本文将深入探讨Linux内核中的几种主要进程间通信方式,并结合J2ME(Java 2 Micro Edition)...
**J2ME教材:J2ME&Gaming中文版** J2ME,全称为Java 2 Micro Edition,是Java平台的一个子集,主要用于嵌入式设备和移动设备,如手机、智能手表等。这个“J2ME&Gaming中文版”教材主要针对的是Java在游戏开发领域的...
J2ME小游戏J2ME小游戏J2ME小游戏J2ME小游戏J2ME小游戏J2ME小游戏J2ME小游戏
Java 2 Micro Edition(J2ME)是一种针对嵌入式设备和移动设备的Java平台,主要用于开发手机游戏、应用程序和服务。"j2me游戏.rar"很可能是包含了一系列使用J2ME技术开发的游戏资源包。在本文中,我们将深入探讨J2ME...