[Andriod官方API指南]连接之蓝牙

Bluetooth —— 蓝牙

The Android platform includes support for the Bluetooth network stack, which allows a device to wirelessly exchange data with other Bluetooth devices. The application framework provides access to the Bluetooth functionality through the Android Bluetooth APIs. These APIs let applications wirelessly connect to other Bluetooth devices, enabling point-to-point and multipoint wireless features.

Android平台包含了对蓝牙网络栈的支持,它允许一个设备与其他蓝牙设备进行无线数据传输。该应用程序框架通过Android Bluetooth APIs提供了一个蓝牙编程接口。这些接口使得应用可以与其他蓝牙设备进行无线连接,完成点对点以及多点的无线特点。

Using the Bluetooth APIs, an Android application can perform the following:

  • Scan for other Bluetooth devices
  • Query the local Bluetooth adapter for paired Bluetooth devices
  • Establish RFCOMM channels
  • Connect to other devices through service discovery
  • Transfer data to and from other devices
  • Manage multiple connections

使用蓝牙接口,一个Android应用可以执行以下功能:

  • 扫描其他蓝牙设备
  • 为配对的蓝牙设备查询本地蓝牙适配器
  • 建立RFCOMM串口
  • 通过服务发现连接到其他设备
  • 和其他设备之间进行数据传输
  • 控制多重连接

The Basics —— 基础


This document describes how to use the Android Bluetooth APIs to accomplish the four major tasks necessary to communicate using Bluetooth: setting up Bluetooth, finding devices that are either paired or available in the local area, connecting devices, and transferring data between devices.

本文档描述了怎样使用Android Bluetooth APIs来完成以下使用蓝牙进行交互的所必要的四个主要任务:建立蓝牙,查找设备(配对的或是在当前区域可用的),连接设备,以及在设备间传输数据。

All of the Bluetooth APIs are available in the android.bluetoothpackage. Here's a summary of the classes and interfaces you will need to create Bluetooth connections:

所有的蓝牙接口在android.bluetooth包内都是可用的。对于你将要需要创建的蓝牙连接,下面展示了一些类和接口的总结:

BluetoothAdapter
Represents the local Bluetooth adapter (Bluetooth radio). TheBluetoothAdapter is the entry-point for all Bluetooth interaction. Using this, you can discover other Bluetooth devices, query a list of bonded (paired) devices, instantiate aBluetoothDevice using a known MAC address, and create aBluetoothServerSocket to listen for communications from other devices.
代表本地蓝牙适配器(蓝牙无线电)。BluetoothAdapter是所有蓝牙交互的入口。使用这个你可以发现其他蓝牙设备,查询已配对的设备列表,使用一个已知的MAC地址来实例化一个BluetoothDevice,以及创建一个BluetoothServerSocket来为监听与其他设备的通信。
BluetoothDevice
Represents a remote Bluetooth device. Use this to request a connection with a remote device through aBluetoothSocket or query information about the device such as its name, address, class, and bonding state.
代表一个远程蓝牙设备,使用这个来请求一个与远程设备的BluetoothSocket连接,或者查询关于设备名称、地址、类和连接状态等设备信息。
BluetoothSocket
Represents the interface for a Bluetooth socket (similar to a TCPSocket). This is the connection point that allows an application to exchange data with another Bluetooth device via InputStream and OutputStream.
代表一个蓝牙socket的接口(和TCP Socket类似)。这是一个连接点,它允许一个应用与其他蓝牙设备通过InputStream和OutputStream交换数据。
BluetoothServerSocket
Represents an open server socket that listens for incoming requests (similar to a TCPServerSocket). In order to connect two Android devices, one device must open a server socket with this class. When a remote Bluetooth device makes a connection request to the this device, theBluetoothServerSocket will return a connectedBluetoothSocket when the connection is accepted.
代表一个开放的服务器socket,它监听接受的请求(与TCP ServerSocket类似)。为了连接两台Android设备,一个设备必须使用这个类开启一个服务器socket。当一个远程蓝牙设备开始一个和该设备的连接请求,BluetoothServerSocket将会返回一个已连接的BluetoothSocket,接受该连接。
BluetoothClass
Describes the general characteristics and capabilities of a Bluetooth device. This is a read-only set of properties that define the device's major and minor device classes and its services. However, this does not reliably describe all Bluetooth profiles and services supported by the device, but is useful as a hint to the device type.
描述一个蓝牙设备的基本特性和性能。这是一个只读的属性集合,它定义了设备的主要和次要的设备类以及它的服务。但是,它没有描述所有的蓝牙配置和设备支持的服务,它只是暗示了设备的类型。
BluetoothProfile
An interface that represents a Bluetooth profile. A Bluetooth profile is a wireless interface specification for Bluetooth-based communication between devices. An example is the Hands-Free profile.  For more discussion of profiles, seeWorking with Profiles
一个表示蓝牙配置文件的接口。一个Bluetooth profile是一个基于蓝牙的通信无线接口定义。一个例子是Hands-Free profile。更多的讨论请见Working with Profiles
BluetoothHeadset
Provides support for Bluetooth headsets to be used with mobile phones. This includes both  Bluetooth Headset and Hands-Free (v1.5) profiles.
提供对移动手机使用的蓝牙耳机的支持。它包含了Headset and Hands-Free (v1.5)配置文件。
BluetoothA2dp
Defines how high quality audio can be streamed from one device to another over a Bluetooth connection. "A2DP" stands for Advanced Audio Distribution Profile.
定义高品质的音频如何通过蓝牙连接从一个设备传输到另一个设备。”A2DP“是Advanced Audio Distribution Profile的缩写。
BluetoothHealth
Represents a Health Device Profile proxy that controls the Bluetooth service.
表示一个Health Device Profile代理,它控制蓝牙服务。
BluetoothHealthCallback
An abstract class that you use to implement BluetoothHealth callbacks. You must extend this class and implement the callback methods to receive updates about changes in the application’s registration state and Bluetooth channel state.
一个抽象类,你可以使用它来实现BluetoothHealth的回调函数。你必须扩展这个类并实现回调函数方法来接收应用程序的注册状态改变以及蓝牙串口状态的更新。
BluetoothHealthAppConfiguration
Represents an application configuration that the Bluetooth Health third-party  application registers to communicate with a remote Bluetooth health device.
表示一个应用程序配置,Bluetooth Health第三方应用程序注册和一个远程Bluetooth Health设备通信。
BluetoothProfile.ServiceListener
An interface that notifies BluetoothProfile IPC clients when they have  been connected to or disconnected from the service (that is, the internal service that runs a particular profile).
一个接口,当BluetoothProfile IPC客户端从服务器上建立连接或断开连接时,它负责通知它们(也就是,运行在特性配置的内部服务)。

Bluetooth Permissions —— 蓝牙权限


In order to use Bluetooth features in your application, you need to declare at least one of two Bluetooth permissions:BLUETOOTH andBLUETOOTH_ADMIN.

为了在你的应用中使用蓝牙特性,你需要至少声明一种蓝牙权限:BLUETOOTH 和BLUETOOTH_ADMIN

You must request the BLUETOOTH permission in order to perform any Bluetooth communication, such as requesting a connection, accepting a connection, and transferring data.

为了执行任何蓝牙通信,例如请求一个连接、接受一个连接以及传输数据,你必须请求BLUETOOTH 权限。

You must request the BLUETOOTH_ADMINpermission in order to initiate device discovery or manipulate Bluetooth settings. Most applications need this permission solely for the ability to discover local Bluetooth devices. The other abilities granted by this permission should not be used, unless the application is a "power manager" that will modify Bluetooth settings upon user request.Note: If you use BLUETOOTH_ADMIN permission, then must also have theBLUETOOTH permission.

为了初始化设备查找或控制蓝牙设置,你必须请求BLUETOOTH_ADMIN权限。大多数应用需要这个权限,仅仅是为了可以发现本地蓝牙设备。这个权限授权的其他功能不应该被使用,除非该应用是一个“强大的控制器”,来通过用户请求修改蓝牙设置。注意:如果你使用BLUETOOTH_ADMIN权限,那么必须拥有BLUETOOTH权限。

Declare the Bluetooth permission(s) in your application manifest file. For example:

在你的应用程序清单文件中声明蓝牙权限。例如:

<manifest ... >  
    <uses-permission android:name="android.permission.BLUETOOTH" />  
    ...
</manifest>

See the <uses-permission>  reference for more information about declaring application permissions.

更多关于声明应用程序权限的信息请见<uses-permission>参考。

Setting Up Bluetooth —— 建立蓝牙


Figure 1: The enabling Bluetooth dialog.

图1:开启蓝牙对话框

 

 

Before your application can communicate over Bluetooth, you need to verify that Bluetooth is supported on the device, and if so, ensure that it is enabled.

在你的应用可以通过蓝牙进行通信之前,你需要验证该设备是否支持蓝牙,如果支持,确保它被打开了。

If Bluetooth is not supported, then you should gracefully disable any Bluetooth features. If Bluetooth is supported, but disabled, then you can request that the user enable Bluetooth without leaving your application. This setup is accomplished in two steps, using the BluetoothAdapter.

如果不支持蓝牙,那么你应该优雅地关掉所有蓝牙特性,如果支持蓝牙,但是没有开启,那么你可以请求用户在不离开你的应用前提下开启蓝牙。这个过程使用BluetoothAdapter用两步完成。

  1. Get the BluetoothAdapter 得到BluetoothAdapter

    The BluetoothAdapter is required for any and all Bluetooth activity. To get theBluetoothAdapter, call the staticgetDefaultAdapter() method. This returns aBluetoothAdapter that represents the device's own Bluetooth adapter (the Bluetooth radio). There's one Bluetooth adapter for the entire system, and your application can interact with it using this object. IfgetDefaultAdapter() returns null, then the device does not support Bluetooth and your story ends here. For example:                                                                                                                                                                                                                                                                                                                                                                                       所有的蓝牙活动都需要请求BluetoothAdapter。为了得到BluetoothAdapter,调用静态的getDefaultAdapter()方法。它返回一个BluetoothAdapter,代表设备本身的蓝牙适配器(蓝牙无线电)。一个完整的系统只有一个蓝牙适配器,而且你的应用可以使用BluetoothAdapter与它进行交互。如果BluetoothAdapter返回为null,那么设备不支持蓝牙而你的故事将在此结束。例如:

    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBluetoothAdapter == null) {    // Device does not support Bluetooth
    }

     

  2. Enable Bluetooth 开启蓝牙

    Next, you need to ensure that Bluetooth is enabled. Call isEnabled() to check whether Bluetooth is currently enable. If this method returns false, then Bluetooth is disabled. To request that Bluetooth be enabled, call startActivityForResult()with theACTION_REQUEST_ENABLE action Intent. This will issue a request to enable Bluetooth through the system settings (without stopping your application). For example:         接下来,你需要保证蓝牙是开启的。调用isEnabled()来检查蓝牙最近是否是开启的。如果这个方法返回false,那么蓝牙没有开启。为了请求开启蓝牙,调用startActivityForResult()并使用ACTION_REQUEST_ENABLE方法Intent。这将发出一个请求来利用系统设置开启蓝牙(不需要停止你的应用)。例如:

    if (!mBluetoothAdapter.isEnabled()) {    
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);    
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }

    A dialog will appear requesting user permission to enable Bluetooth, as shown in Figure 1. If the user responds "Yes," the system will begin to enable Bluetooth and focus will return to your application once the process completes (or fails).

    一个对话框将会出现,请求用户开启蓝牙,像在图1中展示的那样。如果用户相应”是“,系统将会开启蓝牙,而且一旦完成进程(或失败)就返回你的应用。

The REQUEST_ENABLE_BT constant passed to startActivityForResult() is a locally defined integer (which must be greater than 0), that the system passes back to you in youronActivityResult() implementation as therequestCode parameter.

传给startActivityForResult()的REQUEST_ENABLE_BT常量是一个本地定义的整型(必须大于0),系统在你的onActivityResult()实现中返回给你作为requestCode参数。

If enabling Bluetooth succeeds, your activity receives the RESULT_OK result code in theonActivityResult()callback. If Bluetooth was not enabled due to an error (or the user responded "No") then the result code isRESULT_CANCELED.

如果开启蓝牙成功,你的activity在onActivityResult()回调函数中接受一个RESULT_OK结果代码。如果蓝牙因为一个错误(或者用户相应”否“)没有开启,那么结果代码就是RESULT_CANCELED

Optionally, your application can also listen for theACTION_STATE_CHANGED broadcast Intent, which the system will broadcast whenever the Bluetooth state has changed. This broadcast contains the extra fields EXTRA_STATE andEXTRA_PREVIOUS_STATE, containing the new and old Bluetooth states, respectively. Possible values for these extra fields areSTATE_TURNING_ON,STATE_ON,STATE_TURNING_OFF, andSTATE_OFF. Listening for this broadcast can be useful to detect changes made to the Bluetooth state while your app is running.

可选的,你的应用也可以监听ACTION_STATE_CHANGED广播Intent,任何时候蓝牙状态改变了系统将会广播该Intent。这个广播包含了额外的变量EXTRA_STATEEXTRA_PREVIOUS_STATE,分别包含了新的和老的蓝牙状态。这些额外的变量可能值是STATE_TURNING_ON,STATE_ON,STATE_TURNING_OFFSTATE_OFF。监听这个广播对于检测你的应用运行时蓝牙状态的变化是非常有用的。

Tip: Enabling discoverability will automatically enable Bluetooth. If you plan to consistently enable device discoverability before performing Bluetooth activity, you can skip step 2 above. Read aboutenabling discoverability, below.

提示:开启可发现性将会自动开启蓝牙。如果你打算在执行蓝牙activity之前长期开启设备的可发现性,你可以跳过上面的第二步。阅读下面的enabling discoverability

Finding Devices


Using the BluetoothAdapter, you can find remote Bluetooth devices either through device discovery or by querying the list of paired (bonded) devices.

使用BluetoothAdapter,你可以通过设备检测或者查询已配对的设备来找到远程蓝牙设备。

Device discovery is a scanning procedure that searches the local area for Bluetooth enabled devices and then requesting some information about each one (this is sometimes referred to as "discovering," "inquiring" or "scanning"). However, a Bluetooth device within the local area will respond to a discovery request only if it is currently enabled to be discoverable. If a device is discoverable, it will respond to the discovery request by sharing some information, such as the device name, class, and its unique MAC address. Using this information, the device performing discovery can then choose to initiate a connection to the discovered device.

设备检测是一个浏览流程,它查找附近的蓝牙可用设备,然后请求每个设备的相关信息(这有时被称为“检测”“查询”或“浏览”等)。虽然,附近的蓝牙设备仅在它目前是可被检测的状态下时才会回应发现请求。如果一个设备是可被检测的,它将通过分享一些数据来相应检测请求,例如设备名称,类,以及他的唯一的MAC地址。使用这个信息,进行检测的设备可以选择和检测到的的设备初始化一个连接。

Once a connection is made with a remote device for the first time, a pairing request is automatically presented to the user. When a device is paired, the basic information about that device (such as the device name, class, and MAC address) is saved and can be read using the Bluetooth APIs. Using the known MAC address for a remote device, a connection can be initiated with it at any time without performing discovery (assuming the device is within range).

一旦一个连接第一次和一个远程设备进行连接,一个匹配的请求会自动呈现在用户面前。当一个设备配对后,设备的基本信息(例如设备名称、类、MAC地址)将被保存,并且可以使用蓝牙接口进行访问。使用已知的MAC地址,一个连接可以在任何时间被初始化,而不需要执行检测(假设设备在可检测范围内)。

Remember there is a difference between being paired and being connected. To be paired means that two devices are aware of each other's existence, have a shared link-key that can be used for authentication, and are capable of establishing an encrypted connection with each other. To be connected means that the devices currently share an RFCOMM channel and are able to transmit data with each other. The current Android Bluetooth API's require devices to be paired before an RFCOMM connection can be established. (Pairing is automatically performed when you initiate an encrypted connection with the Bluetooth APIs.)

记住,配对和连接之间有一点是不同的。配对以为着两台设备是知道彼此的存在的,它们有一个共享的链接密匙,可以用于授权以及建立一个加密的连接。而连接意味着设备目前共享一个RFCOMM通道,并且可以彼此传输数据。目前的安卓蓝牙接口要求设备在建立一个RFCOMM连接之前先进行配对。(当你使用蓝牙接口初始化一个加密的连接时,配对是自动执行的)。

The following sections describe how to find devices that have been paired, or discover new devices using device discovery.

下面的章节描述了如何查找已配对的设备,或者使用设备检测找到新的设备。

Note: Android-powered devices are not discoverable by default. A user can make the device discoverable for a limited time through the system settings, or an application can request that the user enable discoverability without leaving the application. How to enable discoverability  is discussed below.

注意:安卓设备默认是不可检测的。一个用户可以通过系统设置使设备在一定时间内是可检测的,或者一个应用可以请求用户开启可检测功能而不需要离开应用。下面描述了怎样开启可检测功能。

Querying paired devices —— 查询已配对的设备

Before performing device discovery, its worth querying the set of paired devices to see if the desired device is already known. To do so, callgetBondedDevices(). This will return a Set ofBluetoothDevices representing paired devices. For example, you can query all paired devices and then show the name of each device to the user, using an ArrayAdapter:

 

Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
// If there are paired devices
if (pairedDevices.size() > 0) {
    // Loop through paired devices
    for (BluetoothDevice device : pairedDevices) {
        // Add the name and address to an array adapter to show in a ListView
        mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
    }
}

All that's needed from the BluetoothDevice object in order to initiate a connection is the MAC address. In this example, it's saved as a part of an ArrayAdapter that's shown to the user. The MAC address can later be extracted in order to initiate the connection. You can learn more about creating a connection in the section aboutConnecting Devices.

Discovering devices

To start discovering devices, simply call startDiscovery(). The process is asynchronous and the method will immediately return with a boolean indicating whether discovery has successfully started. The discovery process usually involves an inquiry scan of about 12 seconds, followed by a page scan of each found device to retrieve its Bluetooth name.

Your application must register a BroadcastReceiver for theACTION_FOUND Intent in order to receive information about each device discovered. For each device, the system will broadcast theACTION_FOUND Intent. This Intent carries the extra fieldsEXTRA_DEVICE andEXTRA_CLASS, containing aBluetoothDevice and a BluetoothClass, respectively. For example, here's how you can register to handle the broadcast when devices are discovered:

// Create a BroadcastReceiver for ACTION_FOUND
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        // When discovery finds a device
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            // Add the name and address to an array adapter to show in a ListView
            mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
        }
    }
};
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy

All that's needed from the BluetoothDevice object in order to initiate a connection is the MAC address. In this example, it's saved as a part of an ArrayAdapter that's shown to the user. The MAC address can later be extracted in order to initiate the connection. You can learn more about creating a connection in the section aboutConnecting Devices.

Caution: Performing device discovery is a heavy procedure for the Bluetooth adapter and will consume a lot of its resources. Once you have found a device to connect, be certain that you always stop discovery withcancelDiscovery() before attempting a connection. Also, if you already hold a connection with a device, then performing discovery can significantly reduce the bandwidth available for the connection, so you should not perform discovery while connected.

Enabling discoverability

If you would like to make the local device discoverable to other devices, callstartActivityForResult(Intent, int) with theACTION_REQUEST_DISCOVERABLE action Intent. This will issue a request to enable discoverable mode through the system settings (without stopping your application). By default, the device will become discoverable for 120 seconds. You can define a different duration by adding theEXTRA_DISCOVERABLE_DURATION Intent extra. The maximum duration an app can set is 3600 seconds, and a value of 0 means the device is always discoverable. Any value below 0 or above 3600 is automatically set to 120 secs). For example, this snippet sets the duration to 300:

Intent discoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);
Figure 2: The enabling discoverability dialog.

A dialog will be displayed, requesting user permission to make the device discoverable, as shown in Figure 2. If the user responds "Yes," then the device will become discoverable for the specified amount of time. Your activity will then receive a call to the onActivityResult()) callback, with the result code equal to the duration that the device is discoverable. If the user responded "No" or if an error occurred, the result code will be RESULT_CANCELED.

Note: If Bluetooth has not been enabled on the device, then enabling device discoverability will automatically enable Bluetooth.

The device will silently remain in discoverable mode for the allotted time. If you would like to be notified when the discoverable mode has changed, you can register a BroadcastReceiver for theACTION_SCAN_MODE_CHANGEDIntent. This will contain the extra fieldsEXTRA_SCAN_MODE andEXTRA_PREVIOUS_SCAN_MODE, which tell you the new and old scan mode, respectively. Possible values for each areSCAN_MODE_CONNECTABLE_DISCOVERABLE,SCAN_MODE_CONNECTABLE, or SCAN_MODE_NONE, which indicate that the device is either in discoverable mode, not in discoverable mode but still able to receive connections, or not in discoverable mode and unable to receive connections, respectively.

You do not need to enable device discoverability if you will be initiating the connection to a remote device. Enabling discoverability is only necessary when you want your application to host a server socket that will accept incoming connections, because the remote devices must be able to discover the device before it can initiate the connection.

Connecting Devices —— 连接设备


In order to create a connection between your application on two devices, you must implement both the server-side and client-side mechanisms, because one device must open a server socket and the other one must initiate the connection (using the server device's MAC address to initiate a connection). The server and client are considered connected to each other when they each have a connected BluetoothSocket on the same RFCOMM channel. At this point, each device can obtain input and output streams and data transfer can begin, which is discussed in the section about Managing a Connection. This section describes how to initiate the connection between two devices.

为了在两台设备上创建一个连接,你必须实现服务器端和客户端两头的机制,因为一个设备必须打开一个服务器socket,而另一个设备初始化创建(使用服务器设备的MAC地址来初始化一个连接)。当他们在相同的RFCOMM通道上有一个已连接的 BluetoothSocket 时,服务器和客户被认为是互相连接了。这时,每一个设备可以包含输入和输出流,而且可以开始数据传输,这在Managing a Connection课程中将会讨论。本节课描述了怎样在两台设备之间初始化连接。

The server device and the client device each obtain the required BluetoothSocket in different ways. The server will receive it when an incoming connection is accepted. The client will receive it when it opens an RFCOMM channel to the server.

服务器设备和客户端设备使用不同的方法来得到需要的 BluetoothSocket 。服务器在接受外来的连接的将会接收到它。客户端在向服务器端打开一个RFCOMM通道时会接收到它。

Figure 3: The Bluetooth pairing dialog.
图3:蓝牙配对对话框

One implementation technique is to automatically prepare each device as a server, so that each one has a server socket open and listening for connections. Then either device can initiate a connection with the other and become the client. Alternatively, one device can explicitly "host" the connection and open a server socket on demand and the other device can simply initiate the connection.

一种实现技术使得每一个设备都可以成为一个服务器,因此每一个都有一个打开的服务器socket,并且随时监听连接。然后另一个设备可以初始化连接,并且成为客户端。对应的,一个设备可以显式地“发起”连接,并且在需要时打开一个服务器socket,而另一个设备可以简单地初始化连接即可。

Note: If the two devices have not been previously paired, then the Android framework will automatically show a pairing request notification or dialog to the user during the connection procedure, as shown in Figure 3. So when attempting to connect devices, your application does not need to be concerned about whether or not the devices are paired. Your RFCOMM connection attempt will block until the user has successfully paired, or will fail if the user rejects pairing, or if pairing fails or times out.

注意:如果两台设备之前没有配对过,那么Android框架将会自动显示一个请求配对的通知或对话框,正如图3中显示的那样。因此,当尝试连接设备时,你的应用不需要考虑设备是否配对过。你的RFCOMM连接尝试将会阻塞,知道用户成功配对,或者用户拒绝失败时,或者配对失败,或者超时。

Connecting as a server —— 作为服务器端连接

When you want to connect two devices, one must act as a server by holding an open BluetoothServerSocket. The purpose of the server socket is to listen for incoming connection requests and when one is accepted, provide a connected BluetoothSocket. When theBluetoothSocket is acquired from the BluetoothServerSocket, the BluetoothServerSocket can (and should) be discarded, unless you want to accept more connections.

当你想要连接两台设备时,一个必须通过持有一个打开的 BluetoothServerSocket 来作为服务器端。服务器socket的目的是为了监听外来的连接请求,当一个请求被接受后,提供一个连接的BluetoothSocket。当该BluetoothSocketBluetoothServerSocket请求时,BluetoothServerSocket 可以(而且应当)被舍弃,除非你想要接收更多的连接。

Here's the basic procedure to set up a server socket and accept a connection:

下面是创建一个服务器socket并且接受一个连接的基本过程:

  1. Get a BluetoothServerSocket by calling the listenUsingRfcommWithServiceRecord(String, UUID). 通过调用 listenUsingRfcommWithServiceRecord(String, UUID)得到一个BluetoothServerSocket

    The string is an identifiable name of your service, which the system will automatically write to a new Service Discovery Protocol (SDP) database entry on the device (the name is arbitrary and can simply be your application name). The UUID is also included in the SDP entry and will be the basis for the connection agreement with the client device. That is, when the client attempts to connect with this device, it will carry a UUID that uniquely identifies the service with which it wants to connect. These UUIDs must match in order for the connection to be accepted (in the next step).

    这个String是你的服务的标志名称,系统将会把它写入设备中的一个新的服务发现协议(SDP)数据库条目中(名字是任意的,并且可以只是你应用的名字)。UUID同样被包含在SDP条目中,并且将会成为和客户端设备连接协议的基础。也就是说,当客户端尝试连接这个设备时,它将会携带一个UUID用于唯一指定它想要连接的服务器。这些UUIDs必须匹配以便该连接可以被接受(在下一步中)。

  2. Start listening for connection requests by calling accept(). 通过调用accept()开始监听连接请求。

    This is a blocking call. It will return when either a connection has been accepted or an exception has occurred. A connection is accepted only when a remote device has sent a connection request with a UUID matching the one registered with this listening server socket. When successful, accept() will return a connected BluetoothSocket.

    这一个阻塞调用。在一个连接被接受或一个异常出现时,它将会返回。只有当一个远程设备使用一个UUID发送了一个连接请求,并且该UUID和正在监听的服务器socket注册的UUID相匹配时,一个连接才会被接受。成功后,accept() 将会返回一个已连接的 BluetoothSocket

  3. Unless you want to accept additional connections, call close(). 调用close(),除非你想要接受更多的连接。

    This releases the server socket and all its resources, but does not close the connected BluetoothSocket that's been returned by accept(). Unlike TCP/IP, RFCOMM only allows one connected client per channel at a time, so in most cases it makes sense to call close() on the BluetoothServerSocket immediately after accepting a connected socket.

    这将释放服务器socket和它所有的资源,但是不会关闭 accept()返回的已连接的 BluetoothSocket。不同于TCP/IP,RFCOMM仅仅允许每一个通道上在某一时刻只有一个已连接的客户端,因此在大多数情况下在接受一个已连接的socket后,在BluetoothServerSocket上调用 close() 是非常必要的。

The accept() call should not be executed in the main activity UI thread because it is a blocking call and will prevent any other interaction with the application. It usually makes sense to do all work with a BluetoothServerSocket or BluetoothSocket in a new thread managed by your application. To abort a blocked call such as accept(), call close() on the BluetoothServerSocket (orBluetoothSocket) from another thread and the blocked call will immediately return. Note that all methods on a BluetoothServerSocket or BluetoothSocketare thread-safe.

accept() 不应该再主活动UI线程上执行,因为它是一个阻塞调用,并且将会阻止任何与应用的交互行为。它通常在你的应用管理的一个新的线程中使用一个BluetoothServerSocket 或 BluetoothSocket 来完成所有工作。为了中止一个阻塞调用,例如accept(),从你的其他线程里在BluetoothServerSocket (或 BluetoothSocket) 上调用 close() ,然后阻塞调用就会立即返回。注意在 BluetoothServerSocket 或 BluetoothSocket 上所有的方法都是线程安全的。

Example —— 例子

Here's a simplified thread for the server component that accepts incoming connections:

下面是一个简单的线程,用于服务器端接受外来的连接:

private class AcceptThread extends Thread {
    private final BluetoothServerSocket mmServerSocket;
 
    public AcceptThread() {
        // Use a temporary object that is later assigned to mmServerSocket,
        // because mmServerSocket is final
        BluetoothServerSocket tmp = null;
        try {
            // MY_UUID is the app's UUID string, also used by the client code
            tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
        } catch (IOException e) { }
        mmServerSocket = tmp;
    }
 
    public void run() {
        BluetoothSocket socket = null;
        // Keep listening until exception occurs or a socket is returned
        while (true) {
            try {
                socket = mmServerSocket.accept();
            } catch (IOException e) {
                break;
            }
            // If a connection was accepted
            if (socket != null) {
                // Do work to manage the connection (in a separate thread)
                manageConnectedSocket(socket);
                mmServerSocket.close();
                break;
            }
        }
    }
 
    /** Will cancel the listening socket, and cause the thread to finish */
    public void cancel() {
        try {
            mmServerSocket.close();
        } catch (IOException e) { }
    }
}

In this example, only one incoming connection is desired, so as soon as a connection is accepted and the BluetoothSocket is acquired, the application sends the acquired BluetoothSocket to a separate thread, closes the BluetoothServerSocket and breaks the loop.

在这个例子里,只接受一个外来的连接,因此一旦一个连接被接受了并且需要一个BluetoothSocket ,应用将会发送需要的 BluetoothSocket 给另一个线程,关闭 BluetoothServerSocket 然后打破循环。

Note that when accept()returns the BluetoothSocket, the socket is already connected, so you should not call connect() (as you do from the client-side).

注意当accept()返回一个 BluetoothSocket是,该socket已经被连接了,因此你不应该调用connect()(正如你在客户端里所做的)。

manageConnectedSocket() is a fictional method in the application that will initiate the thread for transferring data, which is discussed in the section about Managing a Connection.

manageConnectedSocket()是一个虚构的方法,它将会初始化线程来传输数据,这在 Managing a Connection 中将会讨论。

You should usually close your BluetoothServerSocket as soon as you are done listening for incoming connections. In this example,close() is called as soon as the BluetoothSocket is acquired. You may also want to provide a public method in your thread that can close the private BluetoothSocket in the event that you need to stop listening on the server socket.

你通常应该关闭你的 BluetoothServerSocket,一旦你已经监听到了外来的连接。在这个例子里, 一旦得到了 BluetoothSocket 就会调用close()。你也可能想要在你的线程中提供一个公共的方法来关闭事件中私有的 BluetoothSocket ,以便你需要停止在服务器socket上监听。

Connecting as a client —— 作为客户端连接

In order to initiate a connection with a remote device (a device holding an open server socket), you must first obtain a BluetoothDevice object that represents the remote device. (Getting a BluetoothDevice is covered in the above section about Finding Devices.) You must then use the BluetoothDevice to acquire a BluetoothSocket and initiate the connection.

为了和一个远程设备(一个持有服务器socket的设备)初始化一个连接,你必须首先得到一个 BluetoothDevice 对象来表示这个远程设备。(上面的课程Finding Devices讲述了如何得到一个 BluetoothDevice )。然后你必须使用BluetoothDevice来得到一个 BluetoothSocket ,然后初始化该连接。

Here's the basic procedure: 下面是基本的过程:

  1. Using the BluetoothDevice, get a BluetoothSocket by calling createRfcommSocketToServiceRecord(UUID).  使用 BluetoothDevice通过调用createRfcommSocketToServiceRecord(UUID)来得到一个 BluetoothSocket 。

    This initializes a BluetoothSocket that will connect to the BluetoothDevice. The UUID passed here must match the UUID used by the server device when it opened its BluetoothServerSocket (withlistenUsingRfcommWithServiceRecord(String, UUID)). Using the same UUID is simply a matter of hard-coding the UUID string into your application and then referencing it from both the server and client code.

    这将初始化一个BluetoothSocket,它连接到该BluetoothDevice。这里传递的UUID必须和服务器设备开启它的 BluetoothServerSocket使用的UUID相匹配。

  2. Initiate the connection by calling connect(). 通过调用connect()初始化一个连接。

    Upon this call, the system will perform an SDP lookup on the remote device in order to match the UUID. If the lookup is successful and the remote device accepts the connection, it will share the RFCOMM channel to use during the connection and connect() will return. This method is a blocking call. If, for any reason, the connection fails or the connect() method times out (after about 12 seconds), then it will throw an exception.

    执行这个调用时,系统将会在远程设备上执行一个SDP查找工作,来匹配UUID。如果查找成功,并且远程设备接受了连接,它将会在连接过程中分享RFCOMM通道,而 connect() 将会返回。这个方法是阻塞的。如果,处于任何原因,该连接失败了或者connect()超时了(大约12秒以后),那么它将会抛出一个异常。

    Because connect() is a blocking call, this connection procedure should always be performed in a thread separate from the main activity thread.

    因为connect()是一个阻塞调用,这个连接过程应该总是在一个单独的线程中执行。

    Note: You should always ensure that the device is not performing device discovery when you callconnect(). If discovery is in progress, then the connection attempt will be significantly slowed and is more likely to fail.

    注意:你应该总是确保在你调用connect()时设备没有执行设备查找工作。如果正在查找设备,那么连接尝试将会很大程度的减缓,并且很有可能会失败。

Example —— 例子

Here is a basic example of a thread that initiates a Bluetooth connection:

下面是初始化一个蓝牙连接的基本例子:

private class ConnectThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final BluetoothDevice mmDevice;
 
    public ConnectThread(BluetoothDevice device) {
        // Use a temporary object that is later assigned to mmSocket,
        // because mmSocket is final
        BluetoothSocket tmp = null;
        mmDevice = device;
 
        // Get a BluetoothSocket to connect with the given BluetoothDevice
        try {
            // MY_UUID is the app's UUID string, also used by the server code
            tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
        } catch (IOException e) { }
        mmSocket = tmp;
    }
 
    public void run() {
        // Cancel discovery because it will slow down the connection
        mBluetoothAdapter.cancelDiscovery();
 
        try {
            // Connect the device through the socket. This will block
            // until it succeeds or throws an exception
            mmSocket.connect();
        } catch (IOException connectException) {
            // Unable to connect; close the socket and get out
            try {
                mmSocket.close();
            } catch (IOException closeException) { }
            return;
        }
 
        // Do work to manage the connection (in a separate thread)
        manageConnectedSocket(mmSocket);
    }
 
    /** Will cancel an in-progress connection, and close the socket */
    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) { }
    }
}

Notice that cancelDiscovery() is called before the connection is made. You should always do this before connecting and it is safe to call without actually checking whether it is running or not (but if you do want to check, call isDiscovering()).

manageConnectedSocket() is a fictional method in the application that will initiate the thread for transferring data, which is discussed in the section about Managing a Connection.

注意到在创建一个连接之前调用了cancelDiscovery()。你应该在连接前总是这样做,而不需要考虑是否真的有在执行查询任务(但是如果你想要检查,调用 isDiscovering())。manageConnectedSocket()是一个虚拟的方法,它将会初始化一个线程用于传输数据,这在Managing a Connection课程中将会讨论。

When you're done with your BluetoothSocket, always call close() to clean up. Doing so will immediately close the connected socket and clean up all internal resources.

当你使用完你的 BluetoothSocket后,总是调用close()来清除资源。这样做将会立即关闭已连接的socket,然后清除所有的内部资源。

Managing a Connection —— 管理一个连接


When you have successfully connected two (or more) devices, each one will have a connected BluetoothSocket. This is where the fun begins because you can share data between devices. Using the BluetoothSocket, the general procedure to transfer arbitrary data is simple:

当你成功连接两个(或更多)设备后,每一个都将有一个已连接的 BluetoothSocket。这就是乐趣开始的地方,因为你可以在不同的设备之间共享数据了!使用 BluetoothSocket,常见的二进制数据传输是很简单的:

  1. Get the InputStream and OutputStream that handle transmissions through the socket, via getInputStream() and getOutputStream(), respectively.   分别通过getInputStream() 和 getOutputStream()来得到 InputStream 和 OutputStream 来控制socket之间的传输。
  2. Read and write data to the streams with read(byte[]) and write(byte[]).   使用 read(byte[]) 和 write(byte[])来向数据流中读取和写入数据。

That's it.  就是这样啦!

There are, of course, implementation details to consider. First and foremost, you should use a dedicated thread for all stream reading and writing. This is important because both read(byte[]) and write(byte[]) methods are blocking calls. read(byte[]) will block until there is something to read from the stream. write(byte[]) does not usually block, but can block for flow control if the remote device is not calling read(byte[]) quickly enough and the intermediate buffers are full. So, your main loop in the thread should be dedicated to reading from the InputStream. A separate public method in the thread can be used to initiate writes to the OutputStream.

当然,实现的细节需要考虑。首先并且最重要的是,你应该为所有输入和输出的数据流使用一个专属的线程。这是十分重要的,因为read(byte[]) 和 write(byte[])方法都是阻塞调用。 read(byte[])将会发生阻塞知道送数据流中读取到了一些东西。 write(byte[])不经常发生阻塞,但是当远程设备没有足够迅速地调用 read(byte[])而中间缓冲区已经负载时可以阻塞。因此,你的线程中的主要循环应该是专门从InputStream中读取数据的。一个单独的公共方法可以被用于初始化向 OutputStream 中写入数据。

Example —— 例子

Here's an example of how this might look:

它看起来可能像下面这个例子,:

 
private class ConnectedThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final InputStream mmInStream;
    private final OutputStream mmOutStream;
 
    public ConnectedThread(BluetoothSocket socket) {
        mmSocket = socket;
        InputStream tmpIn = null;
        OutputStream tmpOut = null;
 
        // Get the input and output streams, using temp objects because
        // member streams are final
        try {
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();
        } catch (IOException e) { }
 
        mmInStream = tmpIn;
        mmOutStream = tmpOut;
    }
 
    public void run() {
        byte[] buffer = new byte[1024];  // buffer store for the stream
        int bytes; // bytes returned from read()
 
        // Keep listening to the InputStream until an exception occurs
        while (true) {
            try {
                // Read from the InputStream
                bytes = mmInStream.read(buffer);
                // Send the obtained bytes to the UI activity
                mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
                        .sendToTarget();
            } catch (IOException e) {
                break;
            }
        }
    }
 
    /* Call this from the main activity to send data to the remote device */
    public void write(byte[] bytes) {
        try {
            mmOutStream.write(bytes);
        } catch (IOException e) { }
    }
 
    /* Call this from the main activity to shutdown the connection */
    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) { }
    }
}

The constructor acquires the necessary streams and once executed, the thread will wait for data to come through the InputStream. When read(byte[]) returns with bytes from the stream, the data is sent to the main activity using a member Handler from the parent class. Then it goes back and waits for more bytes from the stream.

构造器需要必须的数据流,并且一旦执行,线程将会等待InputStream 中来的数据。当 read(byte[]) 返回一些数值时,数据将会使用一个父类的一个成员变量句柄发送给主活动。然后它返回并继续等待更多的数据。

Sending outgoing data is as simple as calling the thread's write() method from the main activity and passing in the bytes to be sent. This method then simply calls write(byte[]) to send the data to the remote device.

发送数据只需要在主活动中调用线程的 write()方法,并将需要发送数据传递给它即可。这个方法然后调用 write(byte[])来向远程设备发送数据。

The thread's cancel() method is important so that the connection can be terminated at any time by closing the BluetoothSocket. This should always be called when you're done using the Bluetooth connection.

线程的cancel() 方法是重要的,以便连接可以在任何时间被中断(通过关闭BluetoothSocket)。这个方法应该在你完成蓝牙连接后总是被调用。

For a  demonstration of using the Bluetooth APIs, see the Bluetooth Chat sample app.

使用蓝牙APIs的更多例子,详见Bluetooth Chat sample app

Working with Profiles —— 使用配置文件


Starting in Android 3.0, the Bluetooth API includes support for working with Bluetooth profiles. A Bluetooth profile is a wireless interface specification for Bluetooth-based communication between devices. An example is the Hands-Free profile. For a mobile phone to connect to a wireless headset, both devices must support the Hands-Free profile.

从Android 3.0开始,蓝牙API包含了对蓝牙配置文件的支持。一个蓝牙配置文件是一个对设备之间依赖蓝牙交互的无线接口定义。一个例子是Hands-Free 配置文件。对于一个和蓝牙耳机相连接的移动电话,两个设备都必须支持Hands-Free 配置文件。

You can implement the interface BluetoothProfile to write your own classes to support a particular Bluetooth profile. The Android Bluetooth API provides implementations for the following Bluetooth profiles:

你可以实现BluetoothProfile接口来定义你自己的类,以便支持特定的蓝牙配置文件。Android蓝牙API提供了以下蓝牙配置文件的实现:

  • Headset. The Headset profile provides support for Bluetooth headsets to be used with mobile phones. Android provides the BluetoothHeadset class, which is a proxy for controlling the Bluetooth Headset Service via interprocess communication (IPC). This includes both  Bluetooth Headset and Hands-Free (v1.5) profiles. The BluetoothHeadset class in cludes support for AT commands. For more discussion of this topic, see Vendor-specific AT commands
     耳机:耳机配置文件提供了对于移动设备使用的蓝牙耳机的支持。Android提供了 BluetoothHeadset 类,它是一个通过进程间的交互(IPC)来控制蓝牙耳机服务的代理。它包含了 Bluetooth Headset 和  Hands-Free (v1.5) 配置文件。 BluetoothHeadset类包含了对AT命令的支持。更多的信息,详见 Vendor-specific AT commands 。
  • A2DP. The Advanced Audio Distribution Profile (A2DP) profile defines how high quality audio can be streamed from one device to another over a Bluetooth connection. Android provides the BluetoothA2dp class, which is a proxy for controlling the Bluetooth A2DP  Service via IPC.
       A2DP:Advanced Audio Distribution Profile (A2DP)配置文件定义了高质量的音频可以如何通过蓝牙连接从一台设备上传输到另一台设备上。Android提供了 BluetoothA2dp 类,它是一个通过IPC管理蓝牙A2DP服务的代理。
  • Health Device. Android 4.0 (API level 14) introduces support for the Bluetooth Health Device Profile (HDP). This lets you create applications that use Bluetooth to communicate with health devices that support Bluetooth, such as heart-rate monitors, blood meters, thermometers, scales, and so on. For a list of supported devices and their corresponding device data specialization codes, refer to Bluetooth Assigned Numbers at www.bluetooth.org. Note that these values are also referenced in the ISO/IEEE 11073-20601 [7] specification as MDC_DEV_SPEC_PROFILE_* in the Nomenclature Codes Annex. For more discussion of HDP, see Health Device Profile.
     保健设备:Android 4.0(API level 14)介绍了对于蓝牙保健设备配置文件(HDP)。它允许你使用蓝牙和支持蓝牙的保健设备进行交互来创建应用,例如心跳监控器,血压,温度计,直尺等。参见Bluetooth Assigned Numbers atwww.bluetooth.org得到支持设备的列表和它们对应的设备数据定义代码。注意,这些值也可以在 ISO/IEEE 11073-20601 [7] 定义中查找。更多关于HDP的讨论,详见 Health Device Profile

Here are the basic steps for working with a profile:

下面是操作一个配置文件的基本步骤:

  1. Get the default adapter, as described in    Setting Up Bluetooth.   得到默认的适配器,正如 Setting Up Bluetooth描述的那样。
  2. Use getProfileProxy() to establish a connection to the profile proxy object associated with the profile. In the example below, the profile proxy object is an instance of BluetoothHeadset. 使用 getProfileProxy() 来建立一个和配置代理对象的连接。这下面的实例代码中,配置代理对象是一个 BluetoothHeadset的实例。
  3. Set up a  BluetoothProfile.ServiceListener. This listener notifies BluetoothProfile IPC clients when they have been connected to or disconnected from the service.  建立一个 BluetoothProfile.ServiceListener。这个监听器通知 BluetoothProfile IPC客户端,当它们已经和服务器连接或取消连接时。
  4. In onServiceConnected(), get a handle to the profile proxy object. 在 onServiceConnected()里,得到一个配置代理对象的句柄。
  5. Once you have the profile proxy object, you can use it to monitor the state of the connection and perform other operations that are relevant to that profile.   一旦你有了配置代理对象,你可以使用它来监视连接状态,并且执行和配置文件相关的其他操作。

For example, this code snippet shows how to connect to a BluetoothHeadset proxy object so that you can control the Headset profile:

例如,下面的代码片段展示了如何和一个BluetoothHeadset代理进行连接,以便你可以控制 Headset profile:

BluetoothHeadset mBluetoothHeadset;
 
// Get the default adapter
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
 
// Establish connection to the proxy.
mBluetoothAdapter.getProfileProxy(context, mProfileListener, BluetoothProfile.HEADSET);
 
private BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() {
    public void onServiceConnected(int profile, BluetoothProfile proxy) {
        if (profile == BluetoothProfile.HEADSET) {
            mBluetoothHeadset = (BluetoothHeadset) proxy;
        }
    }
    public void onServiceDisconnected(int profile) {
        if (profile == BluetoothProfile.HEADSET) {
            mBluetoothHeadset = null;
        }
    }
};
 
// ... call functions on mBluetoothHeadset
 
// Close proxy connection after use.
mBluetoothAdapter.closeProfileProxy(mBluetoothHeadset);

Vendor-specific AT commands —— 厂商特定AT命令

Starting in Android 3.0, applications can register to receive system broadcasts of pre-defined vendor-specific AT commands sent by headsets (such as a Plantronics +XEVENT command). For example, an application could receive broadcasts that indicate a connected device's battery level and could notify the user or take other action as needed. Create a broadcast receiver for the ACTION_VENDOR_SPECIFIC_HEADSET_EVENT intent to handle vendor-specific AT commands for the headset.

从Android 3.0开始,应用可以注册来接收系统广播的、耳机发送的、预定义的、厂商特定的AT命令(例如一个Plantronics +XEVENT 命令)。例如,一个应用可以接收广播,来表明一个已连接的设备的电池等级,并且可以告知用于或者采取其他需要的动作。为 ACTION_VENDOR_SPECIFIC_HEADSET_EVENT Intent 创建一个广播接收器来为耳机控制厂商特定的AT命令。

Health Device Profile —— 保健设备配置文件

Android 4.0 (API level 14) introduces support for the Bluetooth Health Device Profile (HDP). This lets you create applications that use Bluetooth to communicate with health devices that support Bluetooth, such as heart-rate monitors, blood meters, thermometers, and scales. The Bluetooth Health API includes the classes BluetoothHealth,BluetoothHealthCallback, andBluetoothHealthAppConfiguration, which are described in The Basics.

Android 4.0(API level 14) 介绍了对蓝牙保健设备配置文件(HDP)的支持。它允许你使用蓝牙和支持蓝牙的保健设备进行交互来创建应用,例如心跳监控器,血压,温度计,直尺等。蓝牙保健设备API包含了BluetoothHealth,BluetoothHealthCallback 和 BluetoothHealthAppConfiguration 类,The Basics.描述了它们。

In using the Bluetooth Health API, it's helpful to understand these key HDP concepts:

为了使用Bluetooth Health API,理解下面这些关键的HDP概念是有用的:

 

Concept Description
Source A role defined in HDP. A source is a  health device that transmits medical data (weight scale, glucose meter, thermometer, etc.) to a smart device such as an Android phone or tablet.
Sink A role defined in HDP. In HDP, a sink is the smart device that receives the medical data. In an Android HDP application, the sink is represented by aBluetoothHealthAppConfigurationobject.
Registration Refers to registering a sink for a particular health device.
Connection Refers to opening a channel between a health device and a smart device such as an Android phone or tablet.

Creating an HDP Application —— 创建一个HDP应用

Here are the basic steps involved in creating an Android HDP application:

下面是创建一个Android HDP应用的基本步骤:

  1. Get a reference to the BluetoothHealth proxy object.    得到BluetoothHealth代理对象的引用。

    Similar to regular headset and A2DP profile devices, you must call getProfileProxy()with a BluetoothProfile.ServiceListener and the HEALTH profile type to establish a connection with the profile proxy object.

    和通常的耳机和A2DP配置文件设备类似,你必须使用BluetoothProfile.ServiceListenerHEALTH 配置文件类型来调用getProfileProxy(),以建立一个和配置代理对象的连接。

  2. Create a BluetoothHealthCallback and register an application configuration  (BluetoothHealthAppConfiguration) that acts as a health sink.    创建一个BluetoothHealthCallback ,然后注册一个应用配置(BluetoothHealthAppConfiguration)来作为一个health sink。
  3. Establish a connection to a health device.  Some devices will initiate the connection.  It is unnecessary to carry out this step for those devices. 建立一个和保健设备的连接,一些设备将会初始化连接。对于这些设备来说,这一步是不需要的。
  4. When connected successfully to a health device, read/write to the health device using the file descriptor.    当成功建立和保健设备的连接后,使用 file descriptor来向保健设备读取/写入数据。

    The received data needs to be interpreted using a health manager which implements the IEEE 11073-xxxxx specifications.    接收到的数据需要使用保健管理器(实现了 IEEE 11073-xxxxx定义)来解读。

  5. When done, close the health channel and unregister the application.  The channel also closes when there is extended inactivity.    完成以后,关闭保健通道,然后注销应用。通道也会在静止时关闭。

For a complete code sample that illustrates these steps, see Bluetooth HDP (Health Device Profile).

完整的实例这些步骤的代码,请见Bluetooth HDP (Health Device Profile)

 

posted on 2013-02-10 15:49  王大王  阅读(539)  评论(0编辑  收藏  举报

导航