当前位置:文档之家› 蓝牙解决方案

蓝牙解决方案

Device Management设备管理

The Java Bluetooth APIs contain the classes LocalDevice and RemoteDevice, which provide the

device-management capabilities defined in the Generic Access Profile. LocalDevice depends on

the javax.bluetooth.DeviceClass class to retrieve the device's type and the kinds of services it

offers. The RemoteDevice class represents a remote device (a device within a range of reach) and

provides methods to retrieve information about the device, including its Bluetooth address and

name.

java蓝牙api包含了本地设备和远程设备,这两个类型提供了设备管理能力,这个能力再

GAP定义了,本地设备需要javax.bluetooth.DeviceClass类去限定设备的种类和提供的服

务种类;远程设备类代表一个远程设备(这个设备在一定范围内,译者说明(下同):蓝牙最多

支持传送10米)和提供方法去获得相关设备的信息,包括它自己的蓝牙设备地址和名字(说

明:类似PC机的IP和计算机名字).The following code snippet retrieves that information for the local device: ...

// retrieve the local Bluetooth device object

下面代码片段给出了关于本地设备的相关信息

//获得本地设备对象相关对象:LocalDevice local = LocalDevice.getLocalDevice();//本地设备实现对象// retrieve the Bluetooth address of the local device

下面获得蓝牙设备地址String address = local.getBluetoothAddress();

// retrieve the name of the local Bluetooth device

//下面获得蓝牙设备名字String name = local.getFriendlyName();

...

You can get the same information about a remote device: ...

你也可以获得远程设备同样的相关信息// retrieve the device that is at the other end of

// the Bluetooth Serial Port Profile connection,

// L2CAP connection, or OBEX over RFCOMM connection

//获得另一终端设备RemoteDevice remote =

RemoteDevice.getRemoteDevice(

javax.microedition.io.Connection c);//本地设备实现对象// retrieve the Bluetooth address of the remote device

//获得远程蓝牙设备的地址String remoteAddress = remote.getBluetoothAddress();//获得地址方法// retrieve the name of the remote Bluetooth device

//获得远程设备的名字String remoteName = local.getFriendlyName(true);//获得名字的方法...The RemoteDevice class also provides methods to authenticate, authorize, or encrypt data

transferred between local and remote devices.

远程设备类也可以在本地设备与远程设备之间提供方法去验证,授权和加密数据

Device Discovery设备发现

Because wireless devices are mobile they need a mechanism that allows them to find other devices

and gain access to their capabilities. The core Bluetooth API's DiscoveryAgent class and

DiscoveryListener interface provide the necessary discovery services.

因为无线移动设备是移动的他们需要一个机制允许它们发现其它的设备和获得实现它们能

力权限,蓝牙核心api的DiscoveryAgent类和DiscoveryListener抽象类提供了必要的发

现设备的必要服务.

A Bluetooth device can use a DiscoveryAgent object to obtain a list of accessible devices, in any

of three ways:

一个蓝牙设备能够使用一个DiscoveryAgent的对象去获得一个有访问权限设备的类表,在

下面三种情况:The DiscoveryAgent.startInquiry method places the device into an inquiry mode. To take

advantage of this mode, the application must specify an event listener that will respond to inquiry-

related events. DiscoveryListener.deviceDiscovered is called each time an inquiry finds a device.

When the inquiry is completed or canceled, DiscoveryListener.inquiryCompleted is invoked.

DiscoveryAgent.startInquiry方法把设备置于查询模式下.为了获得这种模式的优点,应

用式必须专门置一个事件侦察器以便可以应答相关的事件,

DiscoveryListener.deviceDiscovered方法被称作每次查询发现一个一个设备.当查询完

成或者取消, DiscoveryListener.inquiryCompleted将会被调用.If the device doesn't wish to wait for devices to be discovered, it can use the

DiscoveryAgent.retrieveDevices method to retrieve an existing list. Depending on the parameter

passed, this method will return either a list of devices that were found in a previous inquiry, or a

list of pre-known devices that the local device has told the Bluetooth Control Center it will contact

often.

如果设备不希望去等待被发现,他将可能使用DiscoveryAgent.retrieveDevices方法去获

得一个存在的列表,依赖于永久通过的,这个方法将返回一个在上次(或者过去)查询被发现

设备的列表,或者一个先前已经知道的设备列表,这个本地设备已经通告了蓝牙控制中心(它

将频繁的连接)These three code snippets demonstrate the various approaches: ...

// retrieve the discovery agent

DiscoveryAgent agent = local.getDiscoveryAgent();

// place the device in inquiry mode

boolean complete = agent.startInquiry();

...

这些代码片段实例将给出各种各样的方法案例

//获得发现设备

//把设备置于查询模式下

...// retrieve the discovery agent

DiscoveryAgent agent = local.getDiscoveryAgent();// return an array of pre-known devices

RemoteDevice[] devices =

agent.retrieveDevices(DiscoveryAgent.PREKNOWN);

...

//获得发现的设备

//获得一个先前已经知道的设备阵列

...

Service Discovery服务

Once the local device has discovered at least one remote device, it can begin to search for

available services - Bluetooth applications it can use to accomplish useful tasks. Because service

discovery is much like device discovery, DiscoveryAgent also provides methods to discover

services on a Bluetooth server device, and to initiate service-discovery transactions. Note that the

API provides mechanisms to search for services on remote devices, but not for services on the

local device.

一旦本地设备一经发现至少一个远程设备,它就开始搜索可以提供的服务-蓝牙技术可以来

完成这些有用的任务,因为服务发现非常类似设备发现, DiscoveryAgent也提供方法去发

现蓝牙服务器设备上的服务,以及初始化服务发现的过度,注意api提供的机制去发现远程

设备上的服务,但是不会服务于本地设备.

Service Registration服务注册

Before a service can be discovered, it must first be registered - advertised on a Bluetooth server

device. The server is responsible for:

在一个服务被发现时,它必须首先被注册到一个蓝牙服务器设备上,这个服务器负责于:Creating a service record that describes the service offered

Adding the service record to the server's Service Discovery DataBase (SDDB), so it's visible and

available to potential clients

Registering the Bluetooth security measures associated with the service (enforced for connections

with clients)

Accepting connections from clients

Updating the service record in the SDDB whenever the service's attributes change

Removing or disabling the service record in the SDDB when the service is no longer available

创造一个提供服务的一个服务记录;

添加服务记录到服务器的服务发现数据库中因此它是可见的和提供给潜在的客户端的

注册蓝牙安全措施联系到服务(实施与客户端连接的服务)

接受来自客户端的联系

不管何时服务属性改变立即在SDDB中升级服务记录

当服务不再提供时,移除或者废掉服务记录的数据库;The following annotated code fragment gives you a flavor of the effort involved in registering a

service using the Java APIs for Bluetooth:

相关主题