0

Hi I have to develop an app so I have a device (the server) with 3 clients. I make all the validation, turn on the bluetooth, find devices and all work great. But when I'm going to connect a device I don't know what happen.

I'm using the next code, when I click a device I want to connect it. I only have my app in the mother device.

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    // TODO Auto-generated method stub
    try {
        if(btADapter.isDiscovering()){
            btADapter.cancelDiscovery();
        }
        if(listAdapter.getItem(position).contains("Paired")){
            BluetoothDevice selectedDevice = devices.get(position);
            ConnectThread connect = new ConnectThread(selectedDevice);
            connect.start();
        }
        else{
            BluetoothDevice selectedDevice = devices.get(position);
            ConnectThread connect = new ConnectThread(selectedDevice);
            connect.start();
            //pairDevice(devices.get(position));
            //Toast.makeText(getApplicationContext(), "device is not paired", Toast.LENGTH_LONG).show();
        }

    } catch (Exception e) {
        System.out.println(e);
    }
}

Here I have a question, what happen if its not paired? if I try to connect its going to pair them automatically?

My UUID is: "00001101-0000-1000-8000-00805F9B34FB" Then my connect code:

private 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() {
        // Cancel discovery because it will slow down the connection
        btADapter.cancelDiscovery();

        try {
            // Connect the device through the socket. This will block
            // until it succeeds or throws an exception
            mmSocket.connect();
        } catch (IOException connectException) {
            System.out.println(connectException);
            // Unable to connect; close the socket and get out
            try {
                mmSocket.close();
            } catch (IOException closeException) {
            }
            return;
        }

        // Do work to manage the connection (in a separate thread)

        mHandler.obtainMessage(SUCCESS_CONNECT, mmSocket).sendToTarget();
    }

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

When I do mmSocket.connect(); the app crash and returns: java.io.IOException: Service discovery failed and I don't know what to do.

Please I need help. Thanks.

EDIT

After looking for more answers, i found in this question (Android Bluetooth Connection - Service Discovery Failed) the answer of Sandeep Maram and connects perfectly. This is the code:

BluetoothSocket socket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
Method m = device.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class});
socket = (BluetoothSocket) m.invoke(device, 1);
bluetoothAdapter.cancelDiscovery();
socket.connect();
Community
  • 1
  • 1
user3240604
  • 407
  • 1
  • 8
  • 22

2 Answers2

0

When you will try to connect it will send a particular integer code to the device you are trying connecting to,it will have to accept the incoming connection and allowing you(mother device) to share files amongs them,once your devices are paired it will no longer ask you to pair it again and when you select the particular device that you are paired with earlier it will start a connection and you can share data on the go,i will recommend you to use Synchornise as you are doing multi threading so that only one thread is able to read and write a time,there are no deadlock or starvation occuring in your device Best!

Arslan Sohail
  • 1,583
  • 1
  • 12
  • 18
  • yes, thats the second part. The problem is thati cant connect only one device. Can you have an example with all that you said? – user3240604 Feb 03 '15 at 11:44
  • http://luugiathuy.com/2011/02/android-java-bluetooth/ this is the best project that will get you started it is using bluecove.jar library. – Arslan Sohail Feb 04 '15 at 03:35
0

when you call

btADapter.cancelDiscovery();

it may take a little time to stop discovery.you can try to put delay between

btADapter.cancelDiscovery();

and

mmSocket.connect();

Let me know if it helps.

Nirav Tukadiya
  • 3,367
  • 1
  • 18
  • 36