Help connecting via Bluetooth

Hi:
Can someone help me understand what else I need to do here to connect to a device so I can start reading data via Bluetooth?
When I run my code, it errors out with this error at the "btSocket.Connect();" line :

Java.IO.IOException: read failed, socket might closed or timeout, read ret: -1

Here is what I have:

       async private void CheckBTButton_Click(object sender, EventArgs e)
        {
            EditText InputText = FindViewById<EditText>(Resource.Id.txtOBDInput);
            TextView PairedList = FindViewById<TextView>(Resource.Id.txtBluetoothDevices);


            List<string> devicelists = new List<string> { };
            StringBuilder sb = new StringBuilder();
            BluetoothDevice device = null;
            foreach (var bd in bluetoothAdapter.BondedDevices)
            {
                devicelists.Add(bd.Name);
                System.Console.WriteLine("Logging paired device: " + bd.Name);
                sb.Append(bd.Name);
                if (bd.Name == InputText.Text)
                {
                    device = bd;
                }

            }
            PairedList.Text = sb.ToString();



            if (device == null)
            {
                Android.Widget.Toast.MakeText(this, "Device not found.", Android.Widget.ToastLength.Short).Show();
            }
            else
            {
                Android.Widget.Toast.MakeText(this, "Device bond state: " + device.BondState.ToString() + ". Device Address: " + device.Address.ToString() + ". Device bluetooth class: " + device.BluetoothClass.ToString()
                    + ". Device UUID: " + device.GetUuids()[0].Uuid, Android.Widget.ToastLength.Short).Show();

                DEVICE_UUID = device.GetUuids()[0].Uuid;
            }

            //Finally, use the device’s CreateRfCommSocketToServiceRecord method, which will return a BluetoothSocket that can be used for connection and communication.
            //Note that the UUID specified below is the standard UUID for SPP:

            btSocket = device.CreateRfcommSocketToServiceRecord(DEVICE_UUID);
            if (btSocket != null)
            {
                Task<string> SubmitTask = new Task<string>(ConnectBluetooth);
                SubmitTask.Start();

                string Result = await SubmitTask;

                if (Result == "0")
                {
                    Android.Widget.Toast.MakeText(this, "Connection Successful!", Android.Widget.ToastLength.Short).Show();
                }
                else
                {
                    Android.Widget.Toast.MakeText(this, "Connection Failed.", Android.Widget.ToastLength.Short).Show();
                }
            }
            }
        private string ConnectBluetooth()
        {
                btSocket.Connect();                
            return "0";
        }

Best Answer

  • asusmaniasusmani ✭✭
    Accepted Answer

    Actually, When I was testing, I was connecting my LG tablet to my iPhone.
    When I tried an actual Bluetooth device, the connection worked fine.

    The code I had worked as expected.
    Thanks!

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai
    edited September 17

    @asusmani
    Please change your code of ConnectBluetooth like following code, you should connect bluetooth in a thread.

      private string ConnectBluetooth()
        {
            Task.Run(() => { btSocket.Connect(); });
    
            return "0";
        }
    

    Here is running GIF.

  • asusmaniasusmani Member ✭✭
    Accepted Answer

    Actually, When I was testing, I was connecting my LG tablet to my iPhone.
    When I tried an actual Bluetooth device, the connection worked fine.

    The code I had worked as expected.
    Thanks!

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    Ok, thanks for your sharing. Please mark your reply as answer, it will help others who have similar issue.

Sign In or Register to comment.