Android Developer -- Bluetooth篇 开发实例之二

连接设备

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.

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.

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.

应用程序要在两台设备中创建连接,就必须实现服务端和客户端机制。因为一端的设备必须打开server socket,那么另一端就必须初始化一个连接(通过使用server device‘s MAC地址来初始化连接)。当服务端和客户端彼此连接的时候,他们会在同一个RFCOMM通道上,各自拥有一个连接着的 BluetoothSocket 。每台设备都包含输入输出流,用来数据交换,详细内容详见Managing a Connection章节。本章节讨论的是怎么在两台设备中初始化连接。

服务器装置和客户端装置各获得所需的BluetoothSocket的方式不同。当接收连接被接受时,服务器将接收它。当,它打开一个RFCOMM通道到服务器,客户将接收它。

一种实现技术是每台设备都作为server,这样就可以拥有一个server socket来监听连接。当其他设备初始化连接的时候,就变成了客户端。另外,当设备显性的变成“host”时候,会打开一个server 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 框架,会在连接过程中,自动的显示一个配对请求通知,或者dialog。因此,当尝试连接设备时,您的应用程序不需要关注设备是否配对。你的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 the BluetoothSocket is acquired from the BluetoothServerSocket, the BluetoothServerSocket can (and should) be discarded, unless you want to accept more connections.

当你想要连接两台设备的时候,一端必须工作在server状态下,并且要拥有一个打开的BluetoothServerSocket。目的是为了监听外来的连接请求,一旦请求接受,那么就提供一个 BluetoothSocket.当 BluetoothSocket从BluetoothServerSocket获取的时候,BluetoothServerSocket可以(应该)被舍弃,除非你想要更多的连接。

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

  1. Get a BluetoothServerSocket by calling the listenUsingRfcommWithServiceRecord(String, UUID).

    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).

  2. Start listening for connection requests by calling 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.
  3. Unless you want to accept additional connections, call 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.

这里是显示如何设置一个server socket和接受一个连接的基本过程:

步骤:

第一步:

1.通过调用listenUsingRfcommWithServiceRecord(String, UUID).来获取 BluetoothServerSocket。参数String是你服务的名称,系统会自动的写入一个新的 new Service Discovery Protocol (SDP) database entry(该名字可以是任意的,简单写应用程序名就行。) UUID也包括在SDP entry并将成为客户设备的连接协议的基础。它将携带一个UUID用来唯一标识它要连接的服务。这些UUID必须匹配,为了连接能被接受(下一步)。

第二步:

2.调用 accept().方法,开始监听连接请求。

这是一个阻塞的方法,当连接被接受或者异常发生时,它将返回。远程设备需要携带UUID来连接server socket。如果成功,将返回一个 connected BluetoothSocket.

第三步:

3.除非你想要接受更多的连接,那么要调用close().方法

这个方法会释放server socket和它所占的所有资源,但它不会关闭已经连接的BluetoothSocket。不同于TCP/IP, RFCOMM只允许每个信道在一个时刻内存在一个连接的客户端,所以在大多情况下,在接受一个连接的socket后,立即调用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 (or BluetoothSocket) from another thread and the blocked call will immediately return. Note that all methods on aBluetoothServerSocket or BluetoothSocket are thread-safe.

accept() 方法不应该在主线程中调用,因为它是一个阻塞的方法。所以需要另开线程来处理BluetoothServerSocket or BluetoothSocket

Example

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

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.

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.

注意:在连接设备的时候,需要调用cancelDiscovery()方法。当你连接的时候,你总是要这样做。而且,这个方法是安全的,无论它是否在运行(如果需要确认,调用isDiscovering()方法)。

manageConnectedSocket() 是一个虚构的方法,用来初始化交换数据的线程,详细描述在Managing a Connection.章节。

当你使用BluetoothSocket,完成事情的时候,总是要调用close()来清理。这样做将立即关闭连接的套接字和清理所有内部资源。

时间: 2024-10-04 22:19:56

Android Developer -- Bluetooth篇 开发实例之二的相关文章

Android Developer -- Bluetooth篇 开发实例之一

第一步:声明Bluetooth Permissions <!-- 设置蓝牙访问权限 --> <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 第二步:获取BluetoothAdapter,判断该设备是否支持蓝牙 Bl

Android Developer -- Bluetooth篇 开发实例之四 为什么无线信号(RSSI)是负值?

原文:http://www.cnblogs.com/lele/articles/2832885.html 为什么无线信号(RSSI)是负值 答:其实归根到底为什么接收的无线信号是负值,这样子是不是容易理解多了.因为无线信号多为mW级别,所以对它进行了极化,转化为dBm而已,不表示信号是负的.1mW就是0dBm,小于1mW就是负数的dBm数. 弄清信号强度的定义就行了: RSSI(接收信号强度)Received Signal Strength IndicatorRss=10logP,只需将接受到的

Android Developer -- Bluetooth篇 开发实例之六 蓝牙RSSI计算距离

计算公式: d = 10^((abs(RSSI) - A) / (10 * n)) 其中: d - 计算所得距离 RSSI - 接收信号强度(负值) A - 发射端和接收端相隔1米时的信号强度 n - 环境衰减因子 计算公式的代码实现 - (float)calcDistByRSSI:(int)rssi { int iRssi = abs(rssi); float power = (iRssi-59)/(10*2.0); return pow(10, power); } 传入RSSI值,返回距离(

Bluetooth篇 开发实例之九 和蓝牙模块通信

首先,我们要去连接蓝牙模块,那么,我们只要写客户端的程序就好了,蓝牙模块就相当于服务端. 连接就需要UUID. #蓝牙串口服务SerialPortServiceClass_UUID = ‘{00001101-0000-1000-8000-00805F9B34FB}’ 第一步: 首先要连接设备.这个参考Android Developer的例子来写:Android Developer -- Bluetooth篇 开发实例之二 连接设备 private class ConnectThread exte

Bluetooth篇 开发实例之八 匹配

自己写的App匹配蓝牙设备,不需要通过系统设置去连接. 匹配和通信是两回事. 用过Android系统设置(Setting)的人都知道蓝牙搜索之后可以建立配对和解除配对,但是这两项功能的函数没有在SDK中给出.但是可以通过反射来获取. Method[] hideMethod2 =BluetoothDevice.class.getMethods(); int k = 0; for (; k < hideMethod2.length; k++) { Log.e("BluetoothDevice

Bluetooth篇 开发实例之七 匹配&amp;UUID

匹配和通信是两回事. 1.用过Android系统设置(Setting)的人都知道蓝牙搜索之后可以建立配对和解除配对,但是这两项功能的函数没有在SDK中给出.但是可以通过反射来获取. 知道这两个API的宿主(BluetoothDevice): /** * 与设备配对 参考源码:platform/packages/apps/Settings.git * /Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java */

Android Developer -- Bluetooth篇 概述

Bluetooth 安卓平台支持蓝牙网络协议栈,它允许设备与其他蓝牙设备进行无线交换数据.应用程序框架通过安卓蓝牙APIs提供访问蓝牙功能.这些APIs使应用程序通过无线连接到其他蓝牙设备,使点对点和多点的无线功能. 使用蓝牙APIs,安卓应用程序可以执行以下功能: 扫描其他蓝牙设备 查询本地蓝牙适配器,用于配对蓝牙设备 建立RFCOMM通道 通过发现服务service discovery连接到其他设备 交换数据和其他设备 管理多个连接 The Basics 这个文档描述了如何使用Android

Bluetooth篇 开发实例之十一 官网的Bluetooth Chat sample的bug

当没有匹配的设备和没有找到可用设备的时候. // If there are paired devices, add each one to the ArrayAdapter if (pairedDevices.size() > 0) { findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE); for (BluetoothDevice device : pairedDevices) { mPairedDevicesA

Android系统Google Maps开发实例浅析

Google Map(谷歌地图)是Google公司提供的电子地图服务.包括了三种视图:矢量地图.卫星图片.地形地图.对于Android系统来说,可以利用Google提供的地图服务来开发自己的一些应用.Google Map的服务体现在两个方面:地图API和位置API.使用Android Maps API(地图API)和Android Location API(定位API)可以轻松实现实用而且强大的功能. 我的位置:“我的位置”在地图上显示你的当前位置(通常在 1000 米范围之内).即使没有 GP