The context is the following, I have an Android App that launch an activity when a specific device connects to it, and send a code message. The device is not another android app, in such case the problem would be much easier. The device is an IoT device, the HC05. So this device is in master mode and it tries to connect with the android app that in this case is slave. In brief, the android app must be the slave as a server so it can receive connections, and the other device is in master mode, also, the android app has to detect the device in a broadcast receiver, so I can't use directly the BluetoothServerSocket
with the accept()
method as I can't run a thread.
The farthest I get is to implement this feature connecting and disconnecting the bluetooth device twice. So, the first time my app launch the broadcast receiver and detect the BluetoothDevice
, and it blocks in serverSocket.accept()
. Then, the second time when I disconnect and connect again the bluetooth device, my app success returning a connected socket which I can transmit data... Yeah!
Here is the code that I have
public class BluetoothConnectionReceiver extends BroadcastReceiver {
private ConnectedThread mConnectedThread;
private BluetoothSocket btSocket = null;
public BluetoothConnectionReceiver(){}
@Override
public void onReceive(Context context, Intent intent) {
//First time I connect the bluetooth device, this method is launched
String action = intent.getAction();
switch (action){
case BluetoothDevice.ACTION_ACL_CONNECTED:
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
BluetoothSocket socket = null;
try{
BluetoothServerSocket serverSocket = adapter.listenUsingInsecureRfcommWithServiceRecord("MyApp", MY_UUID_SECURE);
socket = serverSocket.accept(); //The first launch blocks
//The second time, when I disconnect and connect again the device, the code continues properly and I can correctly transmit the data
serverSocket.close();
try{
mConnectedThread = new ConnectedThread(socket);
mConnectedThread.start();
}catch (IOException ex){
ex.printStackTrace();
}
} catch (IOException ex){
ex.printStackTrace();
}
public class ConnectedThread extends Thread {
private InputStream mmInStream;
private OutputStream mmOutStream;
private BluetoothSocket mSocket;
public ConnectedThread(BluetoothSocket socket) throws IOException{
InputStream tmpIn = null;
OutputStream tmpOut = null;
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
mmInStream = tmpIn;
mmOutStream = tmpOut;
mSocket = socket;
}
// Other methods to receive and trasnmit data with the connected socket-
}
The thing is that I need to do this with only one connection from the device, without connecting and disconnecting the device. Something like creating a connected socket from the device (as the BluetoothServerSocket
does with the accept()
method) but with the BluetoothDevice
that the broadcast receiver gives me.
As an addition, I had no problem doing independently starting the connection with the app with the device set in slave mode
BluetoothSocket socket = device.createInsecureRfcommSocketToServiceRecord(BTMODULEUUID);
socket.connect();
But this doesn't work for me when I use de object device
that the BroadcastReceiver gave me. It throws IOException: socket CONNECT read failed socket might closed or timeout
Maybe because the device is in master mode.
I hope someone could help me with this context that I can't find it any ware.