- 浏览: 50312 次
- 性别:
- 来自: 湖南
最新评论
栈初始化
在你做任何事之前,你需要初始化你的栈。记住,栈是一个用来控制你的蓝牙设
备的软件或固件。栈初始化由一系列工作组成,但是它的最主要目的是获得准备
无线通信的蓝牙设备。不同的厂商处理不同的初始化工作,所以我们将用Atinav
JAVA Bluetooth SDK来适用如何初始化一个栈。
import javax.bluetooth.*;
import javax.microedition.io.*;
import com.atinav.BCC;
public class WirelessDevice implements DiscoveryListener {
LocalDevice localDevice = null;
public WirelessDevice (){
//setting the port number using Atinav's BCC
BCC.setPortName("COM1");
//setting the baud rate using Atinav's BCC
BCC.setBaudRate(57600);
//connectable mode using Atinav's BCC
BCC.setConnectable(true);
//Set discoverable mode using Atinav's BCC
BCC.setDiscoverable(DiscoveryAgent.GIAC);
try{
localDevice = LocalDevice.getLoaclDevice();
}
catch (BluetoothStateException exp) {
}
// implementation of methods in DiscoveryListener class
// of javax.bluetooth goes here
// now do some work
}
}
设备管理
LocalDevice类
RemoteDevice类
JAVA Bluetooth标准中让你用来进行设备管理的两个主类,可查询一些你的本地
蓝牙设备和区域内设备的静态信息。
静态方法getLocalDevice(),返回一个供你使用的LocalDevice实例对象
LocalDevice对象上的getBluetoothAddress(),获得你的蓝牙无线装置的唯一地
址
LocalDevice对象上的静态方法setDiscoverable(),设备允许其它的蓝牙设备发
现到该设备
当获得一个LocalDevice对象后,调用LocalDeevice.getDiscoveryAgent()实例化
一个DiscoveryAgent对象。
首先,本地对象实现DiscoveryListener接口;为了我启动发现进程,调用你的Di
scoveryAgent对象的startInquiry()方法,此方法为非阻塞式的,所以你能够去
干其它的事情,一边等待其它的蓝牙设备被发现。
当蓝牙设备被发现后,JVM会调用实现DiscoveryListener接口类的deviceDiscove
red()方法,此方法会传递给你一个代表被查询的设备的RemoteDevice对象
服务发现
DiscoveryAgent类的searchServices()方法允许你搜索RemoteDevice对象上的服
务。当服务查找到,而且你的对象实现了DiscoveryListener接口,JVM会调用ser
vicesDiscovered()方法。此回调方法也会传入属于你查找到的服务的一个Servic
eRecord对象。当创建了ServiceRecord对象后,将其连接至RemoteDevice。
String connectionURL = servRecord[i].getConnectionURL(0, false);
服务注册Service Registration
当一个蓝牙客户端设备能够在一个蓝牙服务器设备用服务发现之前,蓝牙服务器
需要在服务发现数据库中(Service Discovery database, SDDB)中注册它的服务
数据库中,此过程叫做服务注册。这一节将讨论一个蓝牙设备服务注册包括一些
什么,同时我也会给出一个需要完成这项工作的类纲要。
注意:在点对点的应用中,比如一个文件传输或聊天应用,记住任何设备都是客
户端或服务器,为了处理同时为服务发现和服务注册的情况,因此你需要将这些
功能(包括客户端和服务器)合并入你的程序中。下面是一个包含了获得你注册
的服务和存入SDDB中的情景。
1、调用Connector.open()并将Connection的结果放入到一个StreamConnectionNo
tifier对象。一个Connector.open()创建一个ServiceRecord并设定一些属性;
2、用LocalDevice对象和StreamConnectionNotifier来获得系统创建的ServiceRe
cord;
3、在ServiceRecord中增加或修改属性;[可选]
4、用StreamConnectionNotifier调用acceptAndOpen()方法,并等待蓝牙客户端
发现并连接服务;
a、系统在SDDB中创建一个服务记录。
5、等待直到一个客户端连接;
6、当服务器端准备退出,在StreamConnectionNotifier上调用close()
a、系统移除SDDB中的服务记录
StreamConnectNotifier和Connector都来自于J2ME平台的javax.microedition.io
包中。完成上面任务的代码片断显示如下:
// lets name our variables
StreamConnectionNotifier notifier = null;
StreamConnection sconn = null;
LocalDevice localdevice = null;
ServiceRecord servicerecord = null;
// step #1
// the String url will already be defined with the
// correct url parameters
notifier = (StreamConnectionNotifier)Connector.open(url);
// step #2
// we will get the LocalDevice if not already done so
localdevice = LocalDevice.getLocalDevice();
servicerecord = localdevice.getRecord(notifier);
// step #3 is optional
// step #4
// this step will block the current thread until
// a client responds this step will also cause the
// service record to be stored in the SDDB
notifier.acceptAndOpen();
// step #5
// just wait...
// assume the client has connected and you are ready to exit
// step #6
// this causes the service record to be removed
// from the SDDB
notifier.close();
你需要在蓝牙里面做的服务发现如上,下一步是通信
通信
蓝牙是一个通信协议,因此你将如何与它通信呢?JAVA Bluetooth API给你三种方法来发送和接收数据。但为立刻起见,我们实现其中的一个:RFCOMM
注意:RFCOMM是用来通信的串行端口操作模式的协议层,但是这两个东西经常被当作同样来使用。
[未完...]
[Original Text]:http://today.java.net/pub/a/today/2004/07/27/bluetooth.html
发表评论
-
项目开发日志杂记
2009-05-04 13:05 925开发日志 0:32 2008-9-18 1、中文 ... -
笔记本维护故障一则
2007-03-18 23:40 671唉呀,今天真的是羞死 ... -
多Web服务器的80端口访问
2007-03-23 11:42 1417写这篇文章,源自于自己的一个需求。这几天一校园WEB站点因为域 ... -
[转]Windows系统文件详细解说
2007-04-02 23:38 579详细的介绍了WINDOWS系统文件的用途,我想各位保存一份以后 ... -
关于Windows文件共享服务的一些问题
2007-04-02 23:44 2475[问题引出]:我刚安装windows2003时,Compute ... -
MS Project 2003的一个问题
2007-04-03 18:04 1000[问题引出]:刚装完MS Project 2003,一运行就出 ... -
IBM xSeries服务器安装内存一则
2007-04-04 00:55 769部门进购IBM xSeries 225服务器已经达三年之久了, ... -
Windows 2000下的远程桌面工具
2007-04-28 18:10 967在Windows XP之后的系统中都会在“系统”属性中可以设置 ... -
最近在看的书
2007-06-25 03:17 6131、JSP网络开发技术与整合应用 ... -
想看的书---<<开发自己的搜索引擎---Lucene 2.0 + Heritrix>>
2007-06-26 21:47 1687开发自己的搜索引擎---Lucene 2.0 + Heritr ... -
数据挖掘相关
2007-06-27 08:43 711什么是规则?就是一个条件和一个结果的和:If con ... -
不要用浏览器来测试
2007-07-03 11:02 885进行B/S系统编程,大概浏览器就是最直接的测试程序是否正确的方 ... -
Big-Endian And Little-Endian
2007-07-07 11:32 821今天老师给我们复习单片机,出了一个题目,就这个字节存储顺序搞得 ... -
MySQL的中文问题
2007-07-08 21:12 690唉,看到网上这么多的关于MySQL中文编码的问题。今天自己碰到 ... -
[转]RAW FileSystem Recovery
2007-07-11 09:09 959To know ho ... -
关于人工神经网络中的M-P模型的一点疑问
2007-08-08 22:31 888人工神经网络M-P模型构成一个逻辑非模型,从书中抄下来的,如下 ... -
JOONE(Java Object-Oriented Network Engine)使用初探
2007-09-30 16:03 12331 /**/ ... -
OpenGL in VC++
2008-01-19 00:30 957首先看一个简单的例子: 1 #include <wind ... -
VC++中的ON_COMMAND_RANGE宏
2008-01-26 13:51 1715VC++中的ON_COMMAND_RANGE宏 ... -
[转] 软件企业以人为本的16项措施
2008-05-25 09:19 693软件企业以人为本的16项措施 ...
相关推荐
Getting Started with Bluetooth Low Energy.pdf 低功耗蓝牙的开发
Getting started with bluetooth low energy 低功耗蓝牙技术参考,比较通俗易懂
Getting Started with Bluetooth Low Energy由飞思卡尔工程师推荐
With Bluetooth Low Energy (BLE), smart devices are about to become even smarter. This practical guide demonstrates how this exciting wireless technology helps developers build mobile apps that share ...
英文清晰版 Getting Started with LLVM Core Libraries ,学习LLVM必不可少的资料 A practical guide to understanding LLVM with the help of source code references and snippets, insights on how compiler ...
Getting started with Spring Framework (4th Edition) is a hands-on guide to begin developing applications using Spring Framework 5. The examples (consisting of 88 sample projects) that accompany this ...
getting started with sql getting started with sql getting started with sql
Keil Getting Started with uVision2 and the C51 Microcontroller Development Tools.pdf
Getting Started with Processing is not a programming textbook; as the title suggests, it will get you started. It’s for teenagers, hobbyists, grandparents, and everyone in between. This book is also...
ABAQUS的帮助文档Getting started with ABAQUS (基于关键词版),对理解ABAQUS的关键词很有好处。
Programming the Intel Edison: Getting Started with Processing and Python lays out the Edison’s powerful features and teaches the basics of Internet-enabled embedded programming. Discover how to ...
Getting started with memcached
"Getting Started with Grails" brings you up to speed on this modern web framework. Companies as varied as LinkedIn, Wired, Tropicana, and Taco Bell are all using Grails. Are you ready to get started ...
Getting Started with PhantomJS
Getting Started with Arduino电子书
Getting Started with Storm
getting started with java
Getting Started with Mathematica, recommend