Android蓝牙接受()/ connect()与已配对的设备

时间:2012-02-14 19:20:19

标签: android bluetooth bluetooth-lowenergy android-sdk-2.3 pairing

我无法通过蓝牙连接两台Android设备,只有在之前配对时才会发生。我正在运行一个作为服务器,另一个作为客户端。

以下是服务器端的一系列事项:

  1. 检查各种蓝牙状态(适配器可用,已启用等)。
  2. 使用我选择的预定义UUID调用listenUsingRfcommWithServiceRecord()。
  3. 要求设备可被发现
  4. 由于可被发现异步发生,我调用accept()并等待传入​​连接。
  5. 在客户端:

    1. 检查各种蓝牙状态(适配器可用,已启用等)。
    2. 对于getBondedDevices()中的每个设备,我将getName()与服务器名称进行比较。如果匹配,请跳至步骤6.
    3. 开始BT发现
    4. 对于每个已发现的设备(请注意,2a中的配对设备未显示在此处),请将设备名称与服务器名称进行比较。如果匹配,请转到步骤6.
    5. 取消发现
    6. 在从步骤2找到的设备上,使用服务器端使用的相同的预定义UUID调用createRfcommSocketToServiceRecord()。
    7. 调用connect()并等待它返回已连接的套接字。
    8. 当客户端和服务器之前从未配对时,上述过程对我来说非常好。但是,Android在设备列表中注册后,它们将不可避免地在connect()/ accept()阶段超时。

      我一直在寻找一个解决方案,现在已经尝试了很多东西,包括这个: Connecting to a already paired Bluetooth device

      反射方法对我也不起作用。似乎connect()会立即返回,但是当我尝试getOutputStream()时,我得到一个异常。在accept()方面,它甚至没有注册有人试图连接。我非常需要一些帮助或指针来让设备在配对之前建立连接。

      以下是有关设备的一些信息:

      • 我正在两台LG G2X手机上测试服务器和客户端。
      • 它们都运行在Android 2.3.3上,对应于API级别10。
      • 同样,在我在设置中手动取消配对后,上述工作仍然有效。

      提前谢谢你。我在Android和蓝牙方面大约有两周的时间,所以如果您发现任何缺失的步骤或最佳做法,请同时指出它们。

3 个答案:

答案 0 :(得分:7)

在客户端尝试与绑定设备建立连接时,我只是在BluetoothAdapter.getBondedDevices()中找到的BluetoothDevice上调用它。这不起作用。

为了正确建立蓝牙连接,我必须做类似于下面的伪代码的事情:

BluetoothDevice bonded = a device from BluetoothAdapter.getBondedDevices();
BluetoothDevice actual = BluetoothAdapter.getRemoteDevice(bonded.getAddress());

BluetoothSocket socket = actual.createRfcommSocketToServiceRecord(SOME_UUID);
socket.connect();

我完全按照蓝牙聊天示例Bluetooth Chat Service来到了这个答案。为什么它不能在getBondedDevices()设备上运行,这超出了我的范围。也许对Android有更深入了解的人可以回答这个问题。

答案 1 :(得分:2)

查看此示例:http://developer.android.com/resources/samples/BluetoothChat/index.html

这可以解释蓝牙设备如何连接和转换信息。

答案 2 :(得分:0)

private static BluetoothSocket mSocket;
BluetoothDevice selectDevice = null;
void connectDevice(){
    if(mSocket == null) {
        //Log.d(TAG, "Socket is null");
        UUID SPP_UUID = UUID
                .fromString("8ce255c0-200a-11e0-ac64-0800200c9a66");
        Set<BluetoothDevice> bondedDevices = BluetoothAdapter
                .getDefaultAdapter().getBondedDevices();
        //Log.d(TAG, "Size: " + bondedDevices.size());
        /**
         * Select your divice form paired devices
         */
        for (BluetoothDevice bluetoothDevice : bondedDevices) {
            selectDevice = bluetoothDevice;
            //Log.d(TAG, bluetoothDevice.getName()+" "+bluetoothDevice.getAddress());
        }

        if(selectDevice.getBondState() == selectDevice.BOND_BONDED) {
            //Log.d(TAG, selectDevice.getName());
            try {
                mSocket = selectDevice.createInsecureRfcommSocketToServiceRecord(SPP_UUID);
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                //Log.d(TAG, "socket not created");
                e1.printStackTrace();
            }
            try {
                mSocket.connect();
            } catch (IOException e) {
                try {
                    mSocket.close();
                    //Log.d(TAG, "Cannot connect");
                } catch (IOException e1) {
                    //Log.d(TAG, "Socket not closed");
                    e1.printStackTrace();
                }
            }
   }
相关问题