0
votes

I am using bluetooth chat sample and its perfectly fine with pairing the devices and sending text from one device to another but sometimes it disconnects and I would like to reconnect with the last connected device again .How do I achieve this .I have tried auto connect bluetooth from play store but it connects headset and from outside the app not from within.

How do I achieve this from within the App?

Thanks in Advance.

E/BluetoothChatService(10175): accept() failed
E/BluetoothChatService(10175): java.io.IOException: Operation Canceled
E/BluetoothChatService(10175):  at android.bluetooth.BluetoothSocket.acceptNative(Native Method)
E/BluetoothChatService(10175):  at android.bluetooth.BluetoothSocket.accept(BluetoothSocket.java:311)
E/BluetoothChatService(10175):  at android.bluetooth.BluetoothServerSocket.accept(BluetoothServerSocket.java:107)
E/BluetoothChatService(10175):  at android.bluetooth.BluetoothServerSocket.accept(BluetoothServerSocket.java:93)
E/BluetoothChatService(10175):  at com.example.android.BluetoothChat.BluetoothChatService$AcceptThread.run(BluetoothChatService.java:276)




 E/BluetoothChatService(10175): disconnected
 E/BluetoothChatService(10175): java.io.IOException: Software caused connection abort
 E/BluetoothChatService(10175):     at android.bluetooth.BluetoothSocket.readNative(Native Method)
 E/BluetoothChatService(10175):     at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:333)
 E/BluetoothChatService(10175):     at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96)
 E/BluetoothChatService(10175):     at java.io.InputStream.read(InputStream.java:163)
 E/BluetoothChatService(10175):     at com.example.android.BluetoothChat.BluetoothChatService$ConnectedThread.run(BluetoothChatService.java:436)



 E/AndroidRuntime(10175): FATAL EXCEPTION: Thread-1274
 E/AndroidRuntime(10175): java.lang.NullPointerException
 E/AndroidRuntime(10175):   at com.example.android.BluetoothChat.BluetoothChatService.connectionLost(BluetoothChatService.java:242)
 E/AndroidRuntime(10175):   at com.example.android.BluetoothChat.BluetoothChatService.access$6(BluetoothChatService.java:221)
 E/AndroidRuntime(10175):   at com.example.android.BluetoothChat.BluetoothChatService$ConnectedThread.run(BluetoothChatService.java:443)E/BluetoothChat(10175): - ON PAUSE -
1
I haven't done exactly as your requirement but, you can achieve this by storing your connected device address into shared preferences and then use a broadcast receiver to detect if that address is in the discovery list. If there is then you can connect with it. Follow this link for broasdcast receiver developer.android.com/guide/topics/connectivity/bluetooth.html - Milan
Thank You Milanix for looking into my query.I would like only data to to be sent not audio/headset which is done in auto connect bluetooth app in play store .So How can I achieve that PLease can you provide more clarity on how to achieve the above requirement ,probably a link or working example.Thank You again for your time. - James

1 Answers

3
votes

Wrote this using gEdit since, I don't have eclipse atm. So if there is some coding error please don't hesitate to edit this. For ease of explanation I haven't used SharedPreferences here, if you want to use don't hesitate it.

//Assuming that you have device address and is connected
private String partnerDevAdd="00:11:22:AA:BB:CC";
private boolean isConnected=true;

IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);

registerReceiver(mReceiver, filter);

// Create a BroadcastReceiver for bluetooth related checks
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        //We don't want to reconnect to already connected device
        if(isConnected==false){
            // 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);

                // Check if the found device is one we had comm with
                if(device.getAddress().equals(partnerDevAdd)==true)
                    connectToExisting(device);
            }
        }

        if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            // Check if the connected device is one we had comm with
            if(device.getAddress().equals(partnerDevAdd)==true)
                isConnected=true;
        }else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            // Check if the connected device is one we had comm with
            if(device.getAddress().equals(partnerDevAdd)==true)
                isConnected=false;
        }
    }
};

private void connectToExisting(BluetoothDevice device){
    new ConnectThread(device);
}

ConnectThread is available here http://developer.android.com/guide/topics/connectivity/bluetooth.html#ConnectingAsAClient