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

Not able to read Data from device via bluetooth in Xamarin.android

$
0
0

Hello,

I'm new in xamarin and I want to send and receive data via bluetooth between two devices.
My connection between the two android device using socket created successfully and I am able to send data on send button click but I'm not getting response
Kindly help me how i can read data:-

My code is given below:

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

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

            ICollection<BluetoothDevice> boundSet = adapter.BondedDevices;
            BluetoothDevice btDevice1 = boundSet.ElementAt(1);
            foreach (var bd in adapter.BondedDevices)
            {
                string name = "Galaxy J7 Prime";
                System.Diagnostics.Debug.WriteLine("Paired devices found: " + bd.Name.ToUpper());
                if (bd.Name.ToUpper().IndexOf(name.ToUpper()) >= 0)
                {

                    System.Diagnostics.Debug.WriteLine("Found " + bd.Name + ". Try to connect with it!");
                    device = bd;
                    break;
                }
            }

            ParcelUuid[] uuids = null;
            if (btDevice1.FetchUuidsWithSdp())
            {
                uuids = btDevice1.GetUuids();
            }
            if ((uuids != null) && (uuids.Length > 0))
            {
                foreach (var uuid in uuids)
                {
                    try
                    {
                        btSocket = btDevice1.CreateRfcommSocketToServiceRecord(uuid.Uuid);
                        await btSocket.ConnectAsync();
                        if (btSocket.IsConnected)
                        {                                
                            txtStatus.Text = "Connection established";
                            mStream = btSocket.InputStream;
                            mReader = new InputStreamReader(mStream);
                            reader = new BufferedReader(mReader);                                

                            break;
                        }
                    }
                    catch (System.Exception ex)
                    {
                        txtStatus.Text = ex.Message;
                        Toast.MakeText(this, ex.Message, ToastLength.Short);
                    }
                }

                 outStream = btSocket.OutputStream;
                 inStream = btSocket.InputStream;

              byte[] outBuf = new byte[5] { 0xAC, 0x00, 0x00, 0x74, 0xAA };
              byte[] inBuf = new byte[1];
             outStream.Write(outBuf, 0, outBuf.Length);
              run();

And my Run function for receive is given below

public void run()
{
int BUFFER_SIZE = 1024;
byte[] buffer = new byte[BUFFER_SIZE];
int bytes = 0;
int b = BUFFER_SIZE;

        while (true)
        {
            try
            {
                Thread.Sleep(5000);
                bytes = inStream.Read(buffer, bytes, BUFFER_SIZE - bytes);
            }
            catch (Java.Lang.Exception ex)
            {
                txtStatus.Text = ex.Message;
            }
        }
    }

At below line my application hangs at a point and nothing happens on screen
bytes = inStream.Read(buffer, bytes, BUFFER_SIZE - bytes);

Kindly don't share bluetooth chat example


Viewing all articles
Browse latest Browse all 204402

Trending Articles



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