Android Bluetooth accept() / connect() with already paired devices
Asked Answered
H

3

6

I am having trouble connecting two Android devices via Bluetooth, which happens only when they have been paired before. I am running one as the server and the other as the client.

Here is the sequence of things on the server side:

  1. Check various Bluetooth statuses (adapter available, is enabled, etc).
  2. call listenUsingRfcommWithServiceRecord() with a pre-defined UUID that I chose.
  3. request to make device discoverable
  4. since being discoverable happens asynchronously, I call accept() and wait for an incoming connection.

On the client side:

  1. Check various Bluetooth statuses (adapter available, is enabled, etc).
  2. for each device in getBondedDevices(), I compare getName() with the server's name. If there is a match, skip to step 6.
  3. Start BT discovery
  4. For each discovered device (note that paired devices from 2a do not show up here), compare the device name with the server's name. If there is a match, go to step 6.
  5. Cancel discovery
  6. On the device that was found from step 2, call createRfcommSocketToServiceRecord() with the same pre-defined UUID that was used on the server side.
  7. Call connect() and wait for it to return a connected socket.

The above process works perfectly fine for me when the client and the server have never been paired before. However, after Android registered them in the device list, they will inevitably timeout at the connect()/accept() stage.

I have been searching for a solution for a couple days now and have tried quite a few things including this one: Connecting to a already paired Bluetooth device

The reflection method does not work for me either. It seems that connect() would return immediately but when I tried to getOutputStream() I get an exception. On the accept() side it does not even register that someone tried to connect. I seriously need some help or pointer on getting the devices to establish a connection once they have been paired before.

Here is some info about the devices:

  • I am testing the server and client on two LG G2X phones.
  • Both of them run on Android 2.3.3, which corresponds to API level 10.
  • Again, the above works after I unpair the devices manually in the settings.

Thank you ahead of time. I am about 2-week-old in Android and Bluetooth, so if you see any missing steps or best practices, please point them out as well.

Hughs answered 14/2, 2012 at 19:20 Comment(0)
H
7

In the client when I attempt to make a connection to a bonded device, I simply called it on the BluetoothDevice I found in BluetoothAdapter.getBondedDevices(). This does NOT work.

In order to properly establish the Bluetooth connection, I had to do something similar to the pseudocode below:

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

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

I arrived this answer by following exactly the Bluetooth chat example: Bluetooth Chat Service. Why it doesn't work on the device from getBondedDevices() is beyond me. Maybe someone with more intimate knowledge of Android can answer that.

Hughs answered 15/2, 2012 at 6:24 Comment(3)
getRemoteDevice is not a static method ,how you are calling itOdor
You always (almost?) want to use BluetoothAdapter.getDefaultAdapter() as your BluetoothAdapter. The above is only pseudocode, so you cannot simply copy and paste.Hughs
Great post, this actually makes sense because the device is a reference and will be stale. The adapter will always be up to date.Alvardo
G
2

Check out this example: http://developer.android.com/resources/samples/BluetoothChat/index.html.

this could explain how the bluetooth device connected and trans informations.

Gladiolus answered 7/3, 2012 at 22:46 Comment(0)
H
-1
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();
                }
            }
   }
Hagar answered 16/9, 2017 at 9:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.