Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android Problems Solved #89

Closed
SkySrfr opened this issue Nov 5, 2014 · 8 comments
Closed

Android Problems Solved #89

SkySrfr opened this issue Nov 5, 2014 · 8 comments

Comments

@SkySrfr
Copy link

SkySrfr commented Nov 5, 2014

I had some issues connecting to a bluetooth serial device with my Android 4.1.2 and my Android 4.4.2 phone. Turns out it was related to this: http://stackoverflow.com/questions/18657427/ioexception-read-failed-socket-might-closed-bluetooth-on-android-4-3?rq=1

I fixed it by changing run() in BluetoothSerialService.java to start with this:

    public void run() {
        Log.i(TAG, "BEGIN mConnectThread SocketType:" + mSocketType);
        setName("ConnectThread" + mSocketType);

        // Always cancel discovery because it will slow down a connection
        mAdapter.cancelDiscovery();

        // Make a connection to the BluetoothSocket
        try {
            // This is a blocking call and will only return on a successful connection or an exception
            Log.i(TAG,"Connecting to socket...");
            mmSocket.connect();
        } catch (IOException e) {
          Log.e(TAG, e.toString());
          //  e.printStackTrace();

          try {
            Log.i(TAG,"Trying fallback...");
            mmSocket =(BluetoothSocket) mmDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class}).invoke(mmDevice,1);
            mmSocket.connect();
            Log.i(TAG,"Connected");
          } catch (Exception e2) {
            Log.e(TAG, "Couldn't establish Bluetooth connection!");
            try {
              mmSocket.close();
            } catch (IOException e3) {
              Log.e(TAG, "unable to close() " + mSocketType + " socket during connection failure", e3);
            }
            connectionFailed();
            return;
          }
         // return;
        }

...

This also made it necessary to remove 'final' from mmSocket, but I'm sure there is a better way to execute the same idea.

If you are running this on Android and seeing "read failed, socket might closed or timeout, read ret: -1" in your log, this will most likely solve the problem.

Thanks for all your work on this plugin! Hopefully this can help someone down the line...

@SkySrfr
Copy link
Author

SkySrfr commented Nov 5, 2014

If you rather me do a pull request and change it myself, I can do so. I just figured this was minor enough to suggest to you this way.

@yangyyapp
Copy link

I found that after I connected, and then send the command, which is the first Bluetooth device is not responding, only after disconnecting the Bluetooth device will execute the instruction. My Android phone is version 4.4.2, Bluetooth device is Bluetooth 2.0. I am very depressed!

@viren-vii
Copy link

The solution not working for me! @SkySrfr Please help :)

@SkySrfr
Copy link
Author

SkySrfr commented May 1, 2021

The solution not working for me! @SkySrfr Please help :)

Oh man, this was patched and closed 7 years ago. Assuming you are using the latest version, I would guess you are having a different problem, but I haven't looked at this stuff in years, so I doubt I can be of any help. Maybe file a new ticket?

@viren-vii
Copy link

Oh man, this was patched and closed 7 years ago. Assuming you are using the latest version, I would guess you are having a different problem, but I haven't looked at this stuff in years, so I doubt I can be of any help. Maybe file a new ticket?

Okay! Thanks for replying :DDD

@vaibhiarora03
Copy link

@viren-vii did anything worked for you?

@viren-vii
Copy link

@vaibhiarora03 I worked on this which worked fine with connections of Bluetooth classic devices.

@vaibhiarora03
Copy link

@viren-vii okay. thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants