giech / arduinotaskerbluetooth

Android Tasker plugin for sending serial messages over Bluetooth. Example Arduino code for turning a switch on/off
20 stars 9 forks source link

Socket closing before data sent #5

Open JakeBuck opened 5 years ago

JakeBuck commented 5 years ago

I came across an issue when running an ESP-WROOM-32 as a bluetooth serial bridge where the socket would close without transmitting data. I could sometimes get the data to send if I triggered the plugin multiple times very quickly, so I figured there was a timing issue. Downloaded your source, added

    try
    {
        Thread.sleep(100);
    }
    catch(InterruptedException ex)
    {
        Thread.currentThread().interrupt();
    }

Before

    closeSocket(socket);

in SettingReceiver.java and built it, works perfectly now. Not sure as to actual cause but thought you might want to know.

giech commented 5 years ago

Hi there, thanks for reporting this! I don't like the proposed solution as it doesn't solve the issue itself, and may introduce other timing issues. Can you please verify what Android version you are running on? In addition, can you try adding a flush command in closeSocket before the close itself? It seems that in earlier versions of Android flushing didn't really work with BluetoothSockets (https://stackoverflow.com/a/24969079), so this issue may be responsible for the behavior you are seeing.

JakeBuck commented 5 years ago

I'm on Android 8.1.

I'm not well-versed with Android programming yet but adding "socket.getOutputStream().flush();" before "socket.close();" in "private void closeSocket(BluetoothSocket socket)" did not work. Not sure if that's what you were asking for, though. It already runs a flush when it tries to send the message and then it should flush again when the socket is closed if I'm interpreting things properly.