0

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mauricio Pastorini
  • 762
  • 2
  • 11
  • 24
  • Yes I have control, is connected to an arduino. I actually tried to set the HC 05 to try multiple times to connect to the app, and that would possibly solve the problem, but I couldnt set that feature in hc05 – Mauricio Pastorini Jan 19 '18 at 18:17
  • to what is HC05 connected (Arduino / Raspberry, etc.)? Do you have control over IoT's connection state e.g. set discovery on and so? – Jakub Licznerski Jan 19 '18 at 18:17
  • Can you use [Bluetooth SDK](https://www.bluetooth.com/develop-with-bluetooth/developer-resources-tools/developer-kits)? It is high-lewel sdk to control your BT module. What do you use now? Do you have specs for it ? – Jakub Licznerski Jan 19 '18 at 18:21
  • Sorry about the questions, but with which purpose specifically? I am currently developing the android app in android studio with the android objects like BluetoothDevice and so on. And in the arduino with the native AT commands for configurating the HC05 module. – Mauricio Pastorini Jan 19 '18 at 18:28
  • 1
    I think you will have to pair devices first before connecting directly from Android. Is [this](http://www.linotux.ch/arduino/HC-0305_serial_module_AT_commamd_set_201104_revised.pdf) list of commands available for your HC05? – Jakub Licznerski Jan 19 '18 at 18:40
  • Yes, I paired the first time setting the HC05 in slave mode and connecting from the android app. The thing is that now I want to start the communication from the device without the android app open. So, I need to set hc05 as master and the app act as server for the device. That is the reason why I use broadcast receiver and want to mix it with BluetoothServerSocket. – Mauricio Pastorini Jan 19 '18 at 18:45
  • I don't quite get the idea, you already connected devices in configuration: hc05 - slave, android - master ? You now want to do the opposite and get error? This would indicate that the hc05 is not listening to the connection I bet that you have to set appropriate mode by AT. Also you could try taking the hc05 MAC address and create a remote BluetoothDevice with it, as described [here](https://stackoverflow.com/questions/16902500/connect-to-device-with-bluetooth-address-on-string) maybe the discovery has problems... – Jakub Licznerski Jan 19 '18 at 19:11

0 Answers0