Unable to connect Bluetooth Device

jaspal6015jaspal6015 ✭✭Member ✭✭

Hope you are fine and i need your help to resolve my below issue
my connection is not going to connect with device and showing me error
"read failed, socket might closed or timeout, read ret: -1"

My code is listed below:-

try
{
adapter = BluetoothAdapter.DefaultAdapter;
if (adapter == null)
throw new Exception("No Bluetooth adapter found.");

        if (!adapter.IsEnabled)
            throw new Exception("Bluetooth adapter is not enabled.");

        ICollection<BluetoothDevice> boundSet = adapter.BondedDevices;
        BluetoothDevice btDevice1 = boundSet.ElementAt(1);
        btSocket = btDevice1.CreateRfcommSocketToServiceRecord(UUID.FromString("00001101-0000-1000-8000-00805F9B34FB"));
        adapter.CancelDiscovery();
            btSocket.Connect();
            txtStatus.Text = "Galaxy J7 Prime Connected";
            btnConnect.Enabled = false;
    }
    catch (Exception ex)
    {
        txtStatus.Text = ex.Message;
        Toast.MakeText(this, ex.Message, ToastLength.Short);
    }

Kindly help me to resolve out.My connection is not established because of this I am mot able to move ahead..

Posts

  • AlessandroCaliaroAlessandroCaliaro ✭✭✭✭✭ ITMember ✭✭✭✭✭
  • QHXamarinQHXamarin Member
    edited June 2018

    Thank you for you example. However, it didn't work and returned the same error: "read failed, socket might closed or timeout, read ret: -1" when I tried connecting the app on my HTC phone with my computer.

    The funny thing is, when I use my own Xamarin.Forms app to connect, it returns the same error, but when I use normal Xamarin.Android to connect, the connection will be created just fine. Both 2 apps use the same code base to connect the same HTC phone to my computer.

    To OP: Are you using Xamarin.Forms?

  • QHXamarinQHXamarin Member

    Update: I've been testing the Bluetooth since yesterday using 2 different Xamarin versions and Visual Studio versions:

    • Xamarin 4.9.0.753, Xamarin Android 8.2.0.16 on Visual Studio 2015: Bluetooth works after you request permissions inside the app, connection established without any errors.
    • Xamarin 4.10.10.1, Xamarin Android 8.3.3.2 on Visual Studio 2017: Bluetooth works even without requesting permission inside the app (I requested permissions anyway just to be sure), attempt to connect throw this error "read failed, socket might closed or timeout, read ret: -1"

    Both apps requested 4 permissions: "ACCESS_FINE_LOCATION", "ACCESS_COARSE_LOCATION", "BLUETOOTH", "BLUETOOTH_ADMIN" in both Manifest file and inside the app using code.
    Both apps connect to the same bluetooth server on my computer using the same UUID to create RFCOMMSOCKET that my computer broadcasts.

    On Xamarin 4.10.10.1, I tried connecting using Reflection and Method.Invoke like below:
    Java.Lang.Reflect.Method method = Device.Class.GetMethod("createRfcommSocket", new Java.Lang.Class[] { Java.Lang.Integer.Type });
    Socket = (BluetoothSocket)method.Invoke(Device, 1);

    The connection was reported as established on my android device, but my bluetooth server on my computer did not recognize it.

  • JoaoFortesJoaoFortes ✭✭ ESMember ✭✭

    Hi, same problem here after update Xamarin and Xamarin Android.
    Please, do you found any solution?
    Thanks!

Sign In or Register to comment.