0

i've a problem with the connection between the module HC-05 of Arduino and my android app on AndroidStudio. When i try to connect, the log show me that after the socket creation, it doesn't make the connection. Why? This is the part of code where i make the connection:

        if(nome_device.equals("BT05") || nome_device.equals("BT06")){

        BluetoothDevice dev = mBlueadapter.getRemoteDevice(MAC_address);
        ParcelUuid list[] = dev.getUuids();
        System.out.println("ciao");
        System.out.println(dev);

        BluetoothSocket btSocket = null;
        int count = 0;

        do {
            try {
                btSocket = dev.createRfcommSocketToServiceRecord(uuid); //creo un socket per comunicare
              //  System.out.println(dev);
               // System.out.println(btSocket);
                btSocket.connect(); //avvio la connessione
                System.out.println(btSocket.isConnected());
            } catch (IOException e) {
                e.printStackTrace();
            }

            count++;
        } while(!btSocket.isConnected() && count < 3);

        try {
            OutputStream outputStream = btSocket.getOutputStream();
            outputStream.write(48);
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            InputStream inputStream = btSocket.getInputStream();
            inputStream.skip(inputStream.available());  //pulisce il buffer

            for(int i=0; i<26; i++){

                byte b = (byte) inputStream.read();
                System.out.println((char) b);

            }
        } catch (IOException e) {
            e.printStackTrace();
        }


        try {
                btSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }


    }
    else
        Toast.makeText(this, "The selected device is not compatible with this app! ", Toast.LENGTH_SHORT).show();

And this is the log after i click on the name of the module:

2021-03-15 21:22:43.978 8585-8585/com.example.skatex W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
2021-03-15 21:22:43.983 8585-8585/com.example.skatex D/BluetoothUtils: isSocketAllowedBySecurityPolicy start : device null
2021-03-15 21:22:43.984 8585-8585/com.example.skatex W/System.err: java.io.IOException: bt socket closed, read return: -1
2021-03-15 21:22:43.985 8585-8585/com.example.skatex W/System.err:     at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:721)
2021-03-15 21:22:43.985 8585-8585/com.example.skatex W/System.err:     at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:59)
2021-03-15 21:22:43.986 8585-8585/com.example.skatex W/System.err:     at com.example.skatex.Bluetooth.connection(Bluetooth.java:200)
2021-03-15 21:22:43.986 8585-8585/com.example.skatex W/System.err:     at com.example.skatex.Bluetooth.access$200(Bluetooth.java:32)
2021-03-15 21:22:43.986 8585-8585/com.example.skatex W/System.err:     at com.example.skatex.Bluetooth$2.onItemClick(Bluetooth.java:93)
2021-03-15 21:22:43.987 8585-8585/com.example.skatex W/System.err:     at android.widget.AdapterView.performItemClick(AdapterView.java:374)
2021-03-15 21:22:43.987 8585-8585/com.example.skatex W/System.err:     at android.widget.AbsListView.performItemClick(AbsListView.java:1736)
2021-03-15 21:22:43.987 8585-8585/com.example.skatex W/System.err:     at android.widget.AbsListView$PerformClick.run(AbsListView.java:4207)
2021-03-15 21:22:43.988 8585-8585/com.example.skatex W/System.err:     at android.widget.AbsListView$7.run(AbsListView.java:6692)
2021-03-15 21:22:43.988 8585-8585/com.example.skatex W/System.err:     at android.os.Handler.handleCallback(Handler.java:883)
2021-03-15 21:22:43.989 8585-8585/com.example.skatex W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:100)
2021-03-15 21:22:43.989 8585-8585/com.example.skatex W/System.err:     at android.os.Looper.loop(Looper.java:237)
2021-03-15 21:22:43.989 8585-8585/com.example.skatex W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:8107)
2021-03-15 21:22:43.990 8585-8585/com.example.skatex W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
2021-03-15 21:22:43.990 8585-8585/com.example.skatex W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
2021-03-15 21:22:43.990 8585-8585/com.example.skatex W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
2021-03-15 21:22:43.992 8585-8585/com.example.skatex D/BluetoothSocket: close() this: android.bluetooth.BluetoothSocket@3a48555, channel: -1, mSocketIS: android.net.LocalSocketImpl$SocketInputStream@26adb6a, mSocketOS: android.net.LocalSocketImpl$SocketOutputStream@455e85bmSocket: android.net.LocalSocket@6c1d8f8 impl:android.net.LocalSocketImpl@87521d1 fd:java.io.FileDescriptor@2d34436, mSocketState: INIT
2021-03-15 21:22:43.994 8585-8585/com.example.skatex I/Choreographer: Skipped 1163 frames!  The application may be doing too much work on its main thread.

If someone can help me i appreciate a lot.

1 Answers1

0
isSocketAllowedBySecurityPolicy start : device null

this means you didn't get the remote device.

The application may be doing too much work on its main thread

This is another important message, you are running a long task on the main thread.

Take a look on this google example on how to manage the bluetooth connection

Pico
  • 79
  • 11