Reputation: 29844
I already successfully paired my Android (6.0.1) phone (Sony Xperia Z3) with my laptop (running Ubuntu 14.04). I can send files back and forth. The Bluetooth connection menu shows that the connection switch switches to ON if files are sent.
I established a persistent connection using:
sudo rfcomm connect rfcomm0 [MAC ADDRESS] [CHANNEL]
I want to send data from my phone to my laptop via Bluetooth. If I run this code the switch goes on as well, but immediately closes the connection down (switch goes back to OFF).
Logcat shows the following warning after calling init()
:
W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
and upon calling the write()
method this exception:
E/error: error init:java.io.IOException: read failed, socket might closed or timeout, read ret: -1
When connecting using rfcomm
some channels fail and refuse the connection. My guess is that I am using the wrong channel.
rfcomm
?Upvotes: 2
Views: 814
Reputation: 29844
Somehow I could not get it working with the method createRfcommSocketToServiceRecord
.
What I did then was removing:
ParcelUuid[] uuids = device.getUuids();
BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuids[0].getUuid());
and substitute those lines with:
int channel = 1; // substitute with channel you are listening on
Method m = device.getClass().getMethod("createRfcommSocket",new Class[] { int.class });
BluetoothSocket socket = (BluetoothSocket) m.invoke(device, channel);
Then I issued sudo rfcomm listen rfcomm0
which then shows the channel it is listening on the Linux terminal and I could finally connect!
To answer my own questions:
How can I know which channel to use when calling rfcomm?
The Linux terminal shows the channel when issuing sudo rfcomm listen rfcomm0
How can I specify this channel in my Android app?
The method I am accessing using reflection has this parameter now (createRfcommSocket
)
How can I know which UUID to use?
In this solution none.
In the example code the first UUID is used: why?
Does not matter for the found solution.
Upvotes: 2