Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

How to make a Bluetooth connection (classic bluetooth), crash on connect

$
0
0

Hi, I am new to Bluetooth communication and I am wondering how to connect to a paired device that is found, but getting a connection error. The project being made in Xamarin (Android app).

This is my button code that calls the class:

     Button btConnect = FindViewById<Button>(Resource.Id.connect);

     btConnect.Click += (sender, e) =>

     {

            BluetoothManager manager = new BluetoothManager();

            //This calls a function that is displayed bellow

            if (manager.getAllPairedDevices() == true)

            {
                 //Just to check if it accually found it
                 btConnect.Text = "found";

            }

            else

            {

                 btConnect.Text = "Not";

            }

      };

And then this is my bluetooth class:

    BluetoothAdapter btAdapter = BluetoothAdapter.DefaultAdapter;
    private static UUID uuid = UUID.FromString("e6f56502-9fb3-4d34-a7b3-a9d9eee043cd");
    BluetoothSocket mSocket;

    public bool getAllPairedDevices()
    {
        // Default android phone bluetooth
        var devices = btAdapter.BondedDevices;
        if (devices != null && devices.Count > 0)
        {
            // All paired devices
            foreach (BluetoothDevice mDevice in devices)
            {
                openDeviceConnection(mDevice);
            }
            return true;
        }
        else
        {
            return false;
        }
    }

    private void openDeviceConnection(BluetoothDevice btDevice)
    {
        try
        {
            mSocket = btDevice.CreateRfcommSocketToServiceRecord(uuid);
            mSocket.Connect();
        }
        catch (IOException ex)
        {
            mSocket.Close();

            //Then this throws Java.IO.IOException: 'read failed, socket might closed ortimeout, read 
         ret: -1', but if no catch the same error on mSocker.Connect();
            throw ex;     // <---
        }
    }

Then the error gets thrown in catch, but if I remove the catch the error is on the connect(). Gives this error Java.IO.IOException: 'read failed, socket might closed or timeout, read ret: -1'. Tried changing the uuid (read somewhere), but that give Java.IO.IOException: Service discovery failed. So how would I fix the connect?


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>