samedi 25 juin 2016

Android: Bluetooth fails to connect ocasionally

I have encountered a problem when connecting my Android tablet (5.0.1) to a Bluetooth chip module. The problem is the tablet does not connect, yet the Bluetooth module says it is connected.

My code is:

private UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
public synchronized void run() {
    try {
        mySocket = btDevice.createInsecureRfcommSocketToServiceRecord(uuid);

    } catch (IOException createSocketException) {
        // Problem with creating a socket
        Log.e(EXCP_LOG, "exception", createSocketException);
    }
    // Cancel discovery on Bluetooth Adapter to prevent slow connection
    btAdapter.cancelDiscovery();


    try {
        Log.d("BLUETOOTH_DEBUG", "Attempting to connect");
        mySocket.connect();
        isConnected = true;

    } catch (IOException connectException) {
        Log.d("BLUETOOTH_DEBUG", "Connect thread exception");
        connectException.printStackTrace();

    }
    mHandler.obtainMessage(CONNECTED).sendToTarget(); 
}

And the logcat print out is:

W/BluetoothSocket: readAll() looping, read partial size: 11, expect size: 16
W/System.err: java.io.IOException: Connection failure, wrong signal size: 7
W/System.err:     at android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:503)
W/System.err:     at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:327)
W/System.err:     at bluetoothpackage.ConnectToBluetooth.run(ConnectToBluetooth.java:72)
W/System.err:     at java.lang.Thread.run(Thread.java:818)

where ConnectToBluetooth.java is the code above and line 72 is the mySocket.connect() command.

any help on this would be greatly appreciated

Aucun commentaire:

Enregistrer un commentaire