user2744785
user2744785

Reputation: 71

Connect to Android Bluetooth socket

Hi I am trying to create an Android app which will connect to a Blue SMiRF Bluetooth dongle which I want to send data to. I have read over the developer pages and looked at multiple different examples however I am currently having trouble creating a connection to the socket. The Bluetooth portion of the code is pretty much from an example that I was able to find. When trying to connect to the Bluetooth dongle the app gets a force close because there is some error I am not handling correctly. However I have also tried to use the app just to connect to another PC and the connection wont get established correctly for some reason even though I am already paired with the device through the Bluetooth settings before I even run the app. I have posted some of the more important code below for where I think my issue may be. Any help will be very appreciated, please let me know if I should post any additional code.

protected void connect(BluetoothDevice device) {
    //BluetoothSocket socket = null;
    try {
        //Create a Socket connection: need the server's UUID number of registered

        socket = device.createRfcommSocketToServiceRecord(UUID.fromString("a60f35f0-b93a-11de-8a39-08002009c666"));



        socket.connect();
        Log.d("EF-BTBee", ">>Client connectted");

        InputStream inputStream = socket.getInputStream();                                                      
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write(new byte[] { (byte) 0xa0, 0, 7, 16, 0, 4, 0 });


        new Thread() {
            public void run() {
                    while(true)
                    {   
                    try {
                        Log.d("EF-BTBee", ">>Send data thread!");
                        OutputStream outputStream = socket.getOutputStream();
                        outputStream.write(new byte[] { (byte) 0xa2, 0, 7, 16, 0, 4, 0 });
                    } catch (IOException e) {
                        Log.e("EF-BTBee", "", e);
                    }
                    }
            };
        }.start();

    } catch (IOException e) {
        Log.e("EF-BTBee", "", e);
    } finally {
        if (socket != null) {
            try {
                Log.d("EF-BTBee", ">>Client Close");
                socket.close(); 
                finish();
                return ;
            } catch (IOException e) {
                Log.e("EF-BTBee", "", e);
            }
        }
    }
}`

I have also tried using

Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});

        socket = (BluetoothSocket) m.invoke(device, 1);

instead of just the "socket =" line from above and still had no success.

Upvotes: 6

Views: 46823

Answers (3)

Gilad Haimov
Gilad Haimov

Reputation: 5857

Assuming (check!) the UUID you are using is indeed the server UUID, your best bet would be to attempt insecure mode of Bluetooth communication. Some devices simply do not support secure mode.

To do that duplicate your flow, replacing createRfcommSocketToServiceRecord with createInsecureRfcommSocketToServiceRecord and "createRfcommSocket" with "createInsecureRfcommSocket".

Or better still: use BTWiz which encapsulate this flow inside. BTWiz also provides a simple async API for Bluetooth IO.

Finally - socket creation & connect()ion must never be performed on UI thread. Your code is not conclusive here but I suspect you will need to fix this as well.

Gilad Haimov

www.mobileedge.co.il

Upvotes: 3

bourax webmaster
bourax webmaster

Reputation: 733

if the device is already paired , then you can use

if(device.getBondState()==device.BOND_BONDED){
    Log.d(TAG,device.getName());
    //BluetoothSocket mSocket=null;
    try {
        mSocket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        Log.d(TAG,"socket not created");
        e1.printStackTrace();
    }
    try{
        mSocket.connect();
    }
    catch(IOException e){
        try {
            mSocket.close();
            Log.d(TAG,"Cannot connect");
        } catch (IOException e1) {
            Log.d(TAG,"Socket not closed");
            e1.printStackTrace();
        }
    }

for the MY_UUID use

private static final UUID MY_UUID = UUID.fromString("0000110E-0000-1000-8000-00805F9B34FB");

Upvotes: 3

user2744785
user2744785

Reputation: 71

Have finally found a work around. Don't know why this has to be done to work properly.

IOException: read failed, socket might closed - Bluetooth on Android 4.3

Upvotes: 1

Related Questions