android bluetooth can't connect

Kent Andersen

I've been having this problem for a while and haven't been able to figure it out.

I have a android application that puts all paired devices in a listview. When you click one of the list items, it will initiate a request to connect to that bluetooth device.

I can get the list of devices with their addresses no problem. The problem is that once I try to connect I get an IOException on socket.connect();

The error message is as follows: "connect read failed, socket might closed or timeout, read ret: -1"

Here is my code. ANY suggestions would be appreciated. I'm pretty stuck on this.

fyi: the "onEvent" methods is a library that simplifies callbacks...that part works. When the user clicks on a list items this method is called "public void onEvent(EventMessage.DeviceSelected event)"

public class EcoDashActivity extends BaseActivity {

public static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");


private BluetoothAdapter mBluetoothAdapter;
private int REQUEST_ENABLE_BT = 100;
private ArrayList<BluetoothDevice> mDevicesList;
private BluetoothDeviceDialog mDialog;
private ProgressDialog progressBar;
private int progressBarStatus = 0;
private Handler progressBarHandler = new Handler();


@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);

    mDevicesList = new ArrayList<BluetoothDevice>();

    // Register the BroadcastReceiver
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    registerReceiver(mReceiver, filter);

    setupBluetooth();
}

private void setupBluetooth() {
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBluetoothAdapter == null) {
        // Device does not support Bluetooth
        Toast.makeText(this, "Device does not support Bluetooth", Toast.LENGTH_SHORT).show();
    }

    if (!mBluetoothAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    } else {
        searchForPairedDevices();
        mDialog = new BluetoothDeviceDialog(this, mDevicesList);
        mDialog.show(getFragmentManager(), "");
    }

}

private void searchForPairedDevices() {

    Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
    // If there are paired devices
    if (pairedDevices.size() > 0) {
        // Loop through paired devices
        for (BluetoothDevice device : pairedDevices) {
            // Add the name and address to an array adapter to show in a ListView
            mDevices.add(device.getName() + "\n" + device.getAddress());
            mDevicesList.add(device);
        }
    }
}


private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        // When discovery finds a device
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            // Add the name and address to an array adapter to show in a ListView
            mDevicesList.add(device);
        }
    }
};


@Override
protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(mReceiver);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_ENABLE_BT) {
        if (resultCode == RESULT_OK) {
            Toast.makeText(this, "BT turned on!", Toast.LENGTH_SHORT).show();
            searchForPairedDevices();

            mDialog = new BluetoothDeviceDialog(this, mDevicesList);
            mDialog.show(getFragmentManager(), "");
        }
    }

    super.onActivityResult(requestCode, resultCode, data);
}


public void onEvent(EventMessage.DeviceSelected event) {

    mDialog.dismiss();

    BluetoothDevice device = event.getDevice();

    ConnectThread connectThread = new ConnectThread(device);
    connectThread.start();
}


public class ConnectThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final BluetoothDevice mmDevice;

    public ConnectThread(BluetoothDevice device) {
        // Use a temporary object that is later assigned to mmSocket,
        // because mmSocket is final
        BluetoothSocket tmp = null;
        mmDevice = device;

        // Get a BluetoothSocket to connect with the given BluetoothDevice
        try {
            // MY_UUID is the app's UUID string, also used by the server code
            tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
        } catch (IOException e) { }
        mmSocket = tmp;
    }

    public void run() {
        setName("ConnectThread");
        // Cancel discovery because it will slow down the connection
        mBluetoothAdapter.cancelDiscovery();

        try {
            // Connect the device through the socket. This will block
            // until it succeeds or throws an exception
            Log.d("kent", "trying to connect to device");
            mmSocket.connect();
            Log.d("kent", "Connected!");
        } catch (IOException connectException) {
            // Unable to connect; close the socket and get out
            try {
                Log.d("kent", "failed to connect");

                mmSocket.close();
            } catch (IOException closeException) { }
            return;
        }

        Log.d("kent", "Connected!");
    }

    /** Will cancel an in-progress connection, and close the socket */
    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) { }
    }
}

Here is my logcat. Pretty short.

07-22 10:37:05.129: DEBUG/kent(17512): trying to connect to device
07-22 10:37:05.129: WARN/BluetoothAdapter(17512): getBluetoothService() called with no BluetoothManagerCallback
07-22 10:37:05.129: DEBUG/BluetoothSocket(17512): connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[98]}
07-22 10:37:40.757: DEBUG/dalvikvm(17512): GC_CONCURRENT freed 6157K, 9% free 62793K/68972K, paused 7ms+7ms, total 72ms
07-22 10:38:06.975: DEBUG/kent(17512): failed to connect
07-22 10:38:06.975: DEBUG/kent(17512): read failed, socket might closed or timeout, read ret: -1

That last line is in the "Catch" section of a try/catch...I'm just logging the error message.

Please note, there is about a 20 second gap between "trying to connect to device" and "failed to connect"

Vrashabh Irde

The jelly bean bluetooth stack is markedly different from the other versions.

This might help: http://wiresareobsolete.com/wordpress/2010/11/android-bluetooth-rfcomm/

In gist: The UUID is a value that must point to a published service on your embedded device, it is not just randomly generated. The RFCOMM SPP connection you want to access has a specific UUID that it publishes to identify that service, and when you create a socket it must match the same UUID.

If you are targeting 4.0.3 device and above , use fetchUuidsWithSdp() and getUuids() to find all the published services and their associated UUID values. For backward compatibility read the article

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Android - Could not connect to bluetooth device on Lollipop

From Dev

Android App Can't Connect to Server

From Dev

How to connect the Bluetooth device in Android

From Dev

Can't connect to a node js server in Android

From Dev

android studio can't connect to debug devices

From Dev

Spika-Android: "Can't connect to server"

From Dev

Android app can't connect to hostname but can connect to IP on server

From Dev

Android Volley ignoring params can't connect

From Dev

Bluetooth devices can't connect after ranging and monitoring for iBeacons until Bluetooth is reset

From Dev

Android Wear can't connect my device via bluetooth

From Dev

Android program to connect Bluetooth devices

From Dev

Android bluetooth connect to paired device

From Dev

Why can't Android Wear connect target for debugging over Bluetooth?

From Dev

Android 6.0 won't connect to BLE device anymore after turning bluetooth off and back on again

From Dev

How to programmatically pair and connect a HID bluetooth device(Bluetooth Keyboard) on Android

From Dev

Android app crashes on Bluetooth connect?

From Dev

Android Studio : Can't connect to mobile device

From Dev

android bluetooth can't connect

From Dev

How to connect the Bluetooth device in Android

From Dev

Can't pair Android with Windows 7 via Bluetooth

From Dev

Can't connect to a node js server in Android

From Dev

android studio can't connect to debug devices

From Dev

Why won't Sony Bluetooth Headphones Connect?

From Dev

Android bluetooth connect to paired device

From Dev

MacBook Pro can't connect to Bluetooth speaker after upgrade to latest macOS

From Dev

In android, How can I run a background service that in case of a specific bluetooth device connect it activates the microphone?

From Dev

Why can't I view and connect to a Bluetooth LE device? (Android)

From Dev

Android - Bluetooth LE can't find any device

From Dev

Can't connect Android devices to MacBook Pro

Related Related

  1. 1

    Android - Could not connect to bluetooth device on Lollipop

  2. 2

    Android App Can't Connect to Server

  3. 3

    How to connect the Bluetooth device in Android

  4. 4

    Can't connect to a node js server in Android

  5. 5

    android studio can't connect to debug devices

  6. 6

    Spika-Android: "Can't connect to server"

  7. 7

    Android app can't connect to hostname but can connect to IP on server

  8. 8

    Android Volley ignoring params can't connect

  9. 9

    Bluetooth devices can't connect after ranging and monitoring for iBeacons until Bluetooth is reset

  10. 10

    Android Wear can't connect my device via bluetooth

  11. 11

    Android program to connect Bluetooth devices

  12. 12

    Android bluetooth connect to paired device

  13. 13

    Why can't Android Wear connect target for debugging over Bluetooth?

  14. 14

    Android 6.0 won't connect to BLE device anymore after turning bluetooth off and back on again

  15. 15

    How to programmatically pair and connect a HID bluetooth device(Bluetooth Keyboard) on Android

  16. 16

    Android app crashes on Bluetooth connect?

  17. 17

    Android Studio : Can't connect to mobile device

  18. 18

    android bluetooth can't connect

  19. 19

    How to connect the Bluetooth device in Android

  20. 20

    Can't pair Android with Windows 7 via Bluetooth

  21. 21

    Can't connect to a node js server in Android

  22. 22

    android studio can't connect to debug devices

  23. 23

    Why won't Sony Bluetooth Headphones Connect?

  24. 24

    Android bluetooth connect to paired device

  25. 25

    MacBook Pro can't connect to Bluetooth speaker after upgrade to latest macOS

  26. 26

    In android, How can I run a background service that in case of a specific bluetooth device connect it activates the microphone?

  27. 27

    Why can't I view and connect to a Bluetooth LE device? (Android)

  28. 28

    Android - Bluetooth LE can't find any device

  29. 29

    Can't connect Android devices to MacBook Pro

HotTag

Archive