14

I am trying to connect devices through my app installed in Nexus 5.I want to make an app like rainbow contacts in android. In my app I aim to connect to another device through Bluetooth and transfer set of contacts or files. I followed this question, but the workaround mentioned there is not working for me Here is my full code. This is the code snippet from my application where I have tried to get the socket and make connections.

I am able to get through pairing device dialog ,but when i trying to paired then error is raised

//to create socket
if (secure) {
            bluetoothSocket = device.createRfcommSocketToServiceRecord(uuid);
        } else {
            bluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(uuid);
        }
//connection establishment
try {

                bluetoothSocket.connect();
                success = true;
                break;
            } catch (IOException e) {
                //try the fallback
                try {
                    Class<?> clazz = tmp.getRemoteDevice().getClass();
                    Class<?>[] paramTypes = new Class<?>[] {Integer.TYPE};
                    Method m = clazz.getMethod("createRfcommSocket", paramTypes);
                    Object[] params = new Object[] {Integer.valueOf(1)};
                    bluetoothSocket  = (BluetoothSocket) m.invoke(tmp.getRemoteDevice(), params);
                    Thread.sleep(500);
                    bluetoothSocket.connect();
                    success = true;
                    break;
                } catch (FallbackException e1) {
                    Log.w("BT", "Could not initialize FallbackBluetoothSocket classes.", e);
                } catch (InterruptedException e1) {
                    Log.w("BT", e1.getMessage(), e1);
                } catch (IOException e1) {
                    Log.w("BT", "Fallback failed. Cancelling.", e1);
                }
            }

Error I am getting

09-06 13:44:57.247  27860-27860/com.example.gauravdubey.myapplication I/BT﹕ Attempting to connect to Protocol: 00001101-0000-1000-8000-00805f9b34fb
09-06 13:44:57.247  27860-27860/com.example.gauravdubey.myapplication W/BluetoothAdapter﹕ getBluetoothService() called with no BluetoothManagerCallback
09-06 13:44:57.247  27860-27860/com.example.gauravdubey.myapplication D/BluetoothSocket﹕ connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[56]}
09-06 13:44:58.667  27860-27860/com.example.gauravdubey.myapplication W/BluetoothAdapter﹕ getBluetoothService() called with no BluetoothManagerCallback
09-06 13:44:58.667  27860-27860/com.example.gauravdubey.myapplication D/BluetoothSocket﹕ connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[59]}
09-06 13:45:03.267  27860-27860/com.example.gauravdubey.myapplication W/BT﹕ Fallback failed. Cancelling.
    java.io.IOException: read failed, socket might closed or timeout, read ret: -1
            at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:505)
            at android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:482)
            at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:324)
            at com.example.gauravdubey.myapplication.BluetoothConnector$FallbackBluetoothSocket.connect(BluetoothConnector.java:198)
            at com.example.gauravdubey.myapplication.BluetoothConnector.connect(BluetoothConnector.java:62)
            at com.example.gauravdubey.myapplication.ConnectThread.run(ConnectThread.java:101)
            at com.example.gauravdubey.myapplication.MainActivity$1.onItemClick(MainActivity.java:288)
            at android.widget.AdapterView.performItemClick(AdapterView.java:299)
            at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
            at android.widget.AbsListView$PerformClick.run(AbsListView.java:2911)
            at android.widget.AbsListView$3.run(AbsListView.java:3645)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
09-06 13:45:03.267  27860-27860/com.example.gauravdubey.myapplication V/connectThread﹕ Could not connect to device: B0:D0:9C:8B:A4:47
09-06 13:45:03.267  27860-27860/com.example.gauravdubey.myapplication I/Choreographer﹕ Skipped 361 frames!  The application may be doing too much work on its main thread.

So what i am doing wrong ? any help would be appreciated

Community
  • 1
  • 1
Java_begins
  • 1,711
  • 4
  • 18
  • 26
  • 3
    "java.io.IOException: read failed, socket might closed or timeout, read ret: -1" Is that illiterate and incorrect error message really what was printed? Hard to believe, and it needs attention if true. – user207421 Sep 07 '14 at 09:42
  • @EJP Yes the error message was same I tried everything possible. If you want I Can give you complete logs. – Java_begins Sep 07 '14 at 14:19
  • @EJP [Here](http://pastecode.org/index.php/view/33528662) is Complete error log. I am able to get pair screen but whenever i am trying to pair devices it shows me error. – Java_begins Sep 07 '14 at 15:27
  • 4
    A note to the Android developers: "read failed, socket might closed or timeout" is a truly terrible error message. (1) It's illiterate if quoted here accurately (2) a timeout is completely different from a peer close is completely different from a local close: they should not be conflated (3) a timeout should be reported by `SocketTimeoutException` (4) a local close should be reported by `SocketException: socket is closed` (5) a peer close should be reported by `read()` returning -1 *etc.* – user207421 Dec 18 '14 at 22:31
  • I wrote up the grammar error here: https://code.google.com/p/android/issues/detail?id=159898 – ThomasW Mar 13 '15 at 09:53
  • The source file that the message appears in is line 512 of https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/bluetooth/BluetoothSocket.java – ThomasW Mar 13 '15 at 09:59

4 Answers4

4

In the constructor of BluetoothConnector.java, a list of uuidCandidates is passed.

public BluetoothConnector(BluetoothDevice device, boolean secure, BluetoothAdapter adapter,
                              List<UUID> uuidCandidates) 
          {
        this.device = device;
        this.secure = secure;
        this.adapter = adapter;
        this.uuidCandidates = uuidCandidates;

        if (this.uuidCandidates == null || this.uuidCandidates.isEmpty()) {
            this.uuidCandidates = new ArrayList<UUID>();
            this.uuidCandidates.add(UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66"));
        }
    }

Have you passed it null?

If yes,then try calling device.fetchUuidsWithSdp() for the Bluetooth device you want to connect and receive BluetoothDevice.ACTION_UUID intent in your receiver.As this will fetch a list of uuids supported for the device.

twister_void
  • 1,377
  • 3
  • 13
  • 31
  • @Gaurav_Dubey Thanks it works for now , I am able to make connection – Java_begins Sep 16 '14 at 16:50
  • 3
    Hi, I have same error but I don't understand how to use your code. If I pass null value on uuidCandidates, what I have to do later? Could you write a complete example? Thanks in advance – Paride Letizia Feb 03 '15 at 15:16
0

Within the try ("//try the fallback") use this:

socket =(BluetoothSocket) device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}).invoke(device,1);
socket.connect();

Another tip may be to use a different UUID, it may solve the problem.


I don't know if you tryed it yet, but similarly to what has been done in the bluetooth chat example, just before creating your ConnectThread, call this function:

public synchronized void connect(BluetoothDevice device) {

    // Cancel any thread attempting to make a connection
    if (mState == STATE_CONNECTING) {
        if (mConnectThread != null) {
            mConnectThread.cancel();
            mConnectThread = null;
        }
    }

    // Cancel any thread currently running a connection
    if (mConnectedThread != null) {
        mConnectedThread.cancel();
        mConnectedThread = null;
    }

    // Start the thread to connect with the given device
    mConnectThread = new ConnectThread(device);
    mConnectThread.start();
    setState(STATE_CONNECTING);
}
kalup
  • 61
  • 1
  • 6
  • I tired you code withing the try block and also trying with another UUID . Doesn't help – Java_begins Sep 07 '14 at 15:30
  • Have you tried 3rd method? I would use it within MainActivity.onItemClick, befor launching ConnectThread. Moreover, try BluetoothChatExample on those devices and look if it works – kalup Sep 07 '14 at 20:09
-1

Dirty cache. Find where on the device to clear app cache, then reboot the thing. You may have a device flow control interrupter (firewall, app wall, security software) that intercepts the port and re-issues it-- this is the problem. Depending upon where it instantiates at startup, it's now different than what your app wants. Therefore, rebuild the table by snarfing the cache and rebooting to build the cache/redirect tables back. I know this sounds wickedly ephemeral, but it worked for me.

WU9I
  • 1
-3
button.setOnClickListener(new View.OnClickListener() { 
    @Override
    public void onClick(View view) { 
        device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(Device);
        device.createBond();
    }
});
Sergei Kozelko
  • 691
  • 4
  • 17
M. Ahmad
  • 1
  • 1
  • 1
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Sergei Kozelko Mar 28 '23 at 04:52