I had problems connecting two Android devices via Bluetooth, which only happens when they were paired before. I start one server and the other as a client.
Here is the sequence of things on the server side:
- Check the various statuses of Bluetooth (adapter available, enabled, etc.).
- call listenUsingRfcommWithServiceRecord () with the predefined UUID that I selected.
- device discovery request
- since detection happens asynchronously, I call accept () and wait for your incoming connection.
On the client side:
- Check the various statuses of Bluetooth (adapter available, enabled, etc.).
- for each device in getBondedDevices (), I compare getName () with the server name. If there is a match, go to step 6.
- Start BT discovery
- For each device found (note that paired devices from 2a do not appear here), compare the device name with the server name. If there is a match, go to step 6.
- Cancel Detection
- On the device that was found in step 2, call createRfcommSocketToServiceRecord () with the same predefined UUID that was used on the server side.
- Call connect () and wait for it to return the connected socket.
The above process works fine for me when the client and server have never been paired before. However, after Android registers them in the list of devices, they will inevitably go to the connect () / accept () stage.
I searched for a solution for a couple of days and tried a lot of things, including this one: Connecting to an already connected Bluetooth device
The reflection method does not work for me either. It seems that connect () will return immediately, but when I tried getOutputStream (), I get an exception. On the accept () side, it does not even register that someone was trying to connect. I really need help or a pointer to ensure that the devices establish a connection as soon as they are paired before.
The following is device information:
- I am testing the server and client on two LG G2X phones.
- Both of them work on Android 2.3.3, which corresponds to API level 10.
- Again, the above works after I manually disconnected the devices in the settings.
Thanks in advance. I am about two weeks old on Android and Bluetooth, so if you see any missing steps or best practices, indicate them as well.
oc
source share