Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

Reading from Bluetooth freezes the app

$
0
0

Recently I have been building an app that allows Bluetooth communication. I have been following this documentation and I got the connection working. Since it is a blocking call the popup message disapears when another person clicks connect. But now I cant find the error when receiving. The sending doesnt post an error. But when checking if receive returns a value, both apps run unresponsive without any error or crash. I call the code with buttons like this (with dependency service):

`private void SendData(object sender, EventArgs e)
{
    string test = "Testing";
    byte[] temp = Encoding.ASCII.GetBytes(test);
    DependencyService.Get<BluetoothManager>().write(temp);
}

private void CheckButton(object sender, EventArgs e)
{
    if (DependencyService.Get<BluetoothManager>().read() != null)
    {
        CheckTxt.Text = "Ok";
    }
}`

The CheckButton is used to only check if something was returned.

And then this is my entire Bluetooth class:

` using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Bluetooth;
using Xamarin.Forms;
using ShareTheMusic.Droid;
using Java.Util;
using System.IO;

[assembly: Dependency(typeof(BluetoothManagerService))]
namespace ShareTheMusic.Droid
{
    public class BluetoothManagerService : BluetoothManager
    {
        BluetoothAdapter btAdapter = BluetoothAdapter.DefaultAdapter;
        BluetoothServerSocket mServerSocket = null;
        BluetoothSocket mSocket = null;
        BluetoothDevice mDevice = null;
        UUID uuid = UUID.FromString("30324d3a-3050-4e87-a159-f8fa6c433786");

        Stream mmInStream;
        Stream mmOutStream;
        byte[] mmBuffer = null;

        public string checkBluetooth(bool permition)
        {
            if (btAdapter == null)
            {
                return "NoBluetooth";
            }
            else
            {
                if (btAdapter.IsEnabled == false && permition != true)
                {
                    return "TurnOnBluetooth";
                }
                else if(permition == true)
                {
                    btAdapter.Enable();
                    return "AlreadyOn";
                }
                else
                {
                    return "AlreadyOn";
                }
            }
        }

        public string[] findBTdevices()
        {
            string[] deviceNames = null;
            deviceNames = (from qr in BluetoothAdapter.DefaultAdapter.BondedDevices select 
                           qr.Name).ToArray();
            return deviceNames;
        }

        public string discoverDevices()
        {
            return "not implemented yet";
        }

        public void acceptThread()
        {
            BluetoothServerSocket temp = null;

            try
            {
                temp = btAdapter.ListenUsingRfcommWithServiceRecord("ShareTheMusic", uuid);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Socket's listen() method failed", ex);
            }
            mServerSocket = temp;
        }


        public void runServerSide()
        {
            acceptThread();
            BluetoothSocket socket = null;

            while (true)
            {
                try
                {
                    socket = mServerSocket.Accept();
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine("Socket's accept() method failed", ex);
                }

                if (socket != null)
                {
                    manageConnectedSocket(socket);
                    mServerSocket.Close();
                    break;
                }
            }
        }

        public void ConnectThread(BluetoothDevice device)
        {
            BluetoothSocket temp = null;
            mDevice = device;

            try
            {
                temp = device.CreateRfcommSocketToServiceRecord(uuid);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Socket's create() method failed", ex);
            }
            mSocket = temp;
        }

        public void runClientSide(string deviceName)
        {
            BluetoothDevice tmp = (from qr in BluetoothAdapter.DefaultAdapter.BondedDevices where 
                                        qr.Name == deviceName select qr).FirstOrDefault();
            ConnectThread(tmp);

            try
            {
                mSocket.Connect();
            }
            catch (Exception ex)
            {
                try
                {
                    mSocket.Close();
                }
                catch (Exception)
                {
                    System.Diagnostics.Debug.WriteLine("Could not close the client socket", ex);
                }
            }
            manageConnectedSocket(mSocket);
        }

        public void cancel()
        {
            try
            {
                mSocket.Close();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Could not close the connect socket", ex);
            }
        }

    /*---------------------*/

        public void manageConnectedSocket(BluetoothSocket socket)
        {
            mSocket = socket;
            Stream tmpIn = null;
            Stream tmpOut = null;

            try
            {
                tmpIn = socket.InputStream;
            }
            catch (IOException ex)
            {
                System.Diagnostics.Debug.WriteLine("Error occurred when creating input stream", ex);
            }
            try
            {
                tmpOut = socket.OutputStream;
            }
            catch (IOException ex)
            {
                System.Diagnostics.Debug.WriteLine("Error occurred when creating output stream", ex);
            }

            mmInStream = tmpIn;
            mmOutStream = tmpOut;
        }

        public List<byte[]> read()
        {
            mmBuffer = new byte[1024];
            int numBytes;
            List<byte[]> intBytes = new List<byte[]>();

            while (true)
            {
                try
                {
                    numBytes = mmInStream.Read(mmBuffer);
                    intBytes.Add(BitConverter.GetBytes(numBytes));
                }
                catch (IOException ex)
                {
                    System.Diagnostics.Debug.WriteLine("Input stream was disconnected", ex);
                    break;
                }
            }
            return intBytes;
        }

        public void write(byte[] bytes)
        {
            try
            {
                mmOutStream.Write(bytes);
            }
            catch (IOException ex)
            {
                System.Diagnostics.Debug.WriteLine("Error occurred when sending data", ex);
            }
        }
    }
}`

Viewing all articles
Browse latest Browse all 91519

Trending Articles



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