Bluetooth - Receive, read data from RFID device DOTR900

Hi,

I am more than 2 weeks trying connect and receive data from RFID device.
Already read all forum in Xamarin, is possible that I did not know how to use the commands, i´m new in develop for android with Visual studio.
Now I connected in the device successfully.

In this momment, I created a thread to check the sockect until some data is receive, but this data never arrives.
I would like to check if Bluetooh is receiving the code from the reader.

Tks in advance.

Image of device

using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using Android.Bluetooth;
using Java.Util;
using System;
using System.IO;
using System.Collections.Generic;
using System.Threading;
using Java.IO;
using System.Text;

namespace BT3
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {
        private BluetoothAdapter mBluetoothAdapter = null;
        private BluetoothDevice mBluetoothDevice = null;
        public  BluetoothSocket mBluetoothSocket = null;
        public  BluetoothServerSocket mBluetoothSocketServer = null;
        public BluetoothSocket mBluetoothSocketAtual = null;
        public InputStreamReader minputstreamreader = null;
        public OutputStream moutputStream = null;
        public Stream mstreamIN = null;
        public Stream mstreamOUT = null;
        public InputStream minputStream = null;
        public FileOutputStream mFileOutputStream = null;

        public IDisposable mIDisposable = null;
        public StreamReader mStreamReader = null;
        public BufferedReader mbufferedReader = null; 
        public BufferedInputStream mBufferinputstream = null;
        public IBluetoothProfileServiceListener mIBluetoothProfileServiceListener =null;

        char[] buffer2 = new char[1024];
        byte[] buffer = new byte[1024];
        private UUID mUuid;
        public string data = null;



        protected override void OnCreate(Bundle savedInstanceState)
        {

            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);

            this.RunOnUiThread(ConnectBT);


            Thread thread = new Thread(() =>
            {             

                while (true)
                {
                    if (mBluetoothSocket != null)
                    {

                        while (true)
                        {
                           // if(mblu CanRead)
                            {
                                if (mBluetoothSocket.InputStream != null)
                                {
                                    try
                                    {
                                        int bytesRead = mBluetoothSocket.InputStream.Read(buffer, 0, buffer.Length);
                                        if(bytesRead != 0)
                                        {
                                            System.Console.WriteLine("");
                                        }

                                    }
                                    catch (Exception e)
                                    { 

                                    }

                                }
                            }                            

                        }                        

                    }
                }

            });
            thread.IsBackground = true;
            thread.Start();


        }
        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }

        public void Click()
        {
            Button btn1 = FindViewById<Button>(Resource.Id.btnconnect);
            btn1.Click += Btn1_Click;

            Button btn2 = FindViewById<Button>(Resource.Id.btndisconnect);
            btn2.Click += Btn2_Click;

            Button btn3 = FindViewById<Button>(Resource.Id.btnonoff);
            btn3.Click += Btn3_Click;       

        }

        private void Btn3_Click(object sender, EventArgs e)
        {
            mBluetoothAdapter = BluetoothAdapter.DefaultAdapter;

            if (mBluetoothAdapter.IsEnabled == false)
            {
                mBluetoothAdapter.Enable();
            }
            else
            {
                mBluetoothAdapter.Disable();
            }

        }

        private void Btn2_Click(object sender, EventArgs e)
        {

        }



        private async void Btn1_Click(object sender, System.EventArgs e)
        {
            mBluetoothAdapter = BluetoothAdapter.DefaultAdapter;


            foreach (BluetoothDevice btpaired in mBluetoothAdapter.BondedDevices)
            {
                if (btpaired.Name.Contains("HQ_UHF_READER")) // its device is my RFID READER DOTR900
                //if (btpaired.Name.Contains("A-16688")) // sound box
                    {
                //    ParcelUuid[] uuids = btpaired.GetUuids();

                //    foreach (var uuuu in uuids)
                    {
                //        System.Console.WriteLine("UUIDs RFID: " + uuuu.Uuid);
                //        System.Console.WriteLine("UUIDs RFID AUTOMATIC: " + btpaired.GetUuids()[0].Uuid);
                    }
                    mBluetoothAdapter.CancelDiscovery();
                    mBluetoothDevice = mBluetoothAdapter.GetRemoteDevice(btpaired.Address);
                    //mUuid = UUID.FromString(btpaired.GetUuids()[0].Uuid.ToString()); //works, but in the moment I dont need it
                    mUuid = UUID.FromString("00001101-0000-1000-8000-00805f9b34fb");
                    mBluetoothSocket = mBluetoothDevice.CreateRfcommSocketToServiceRecord(mUuid);
                    System.Console.WriteLine("PRE CONNECTION  : " + mBluetoothSocket.IsConnected); // Check if socket connection is enable, is always false ( expected)




                    if (mBluetoothSocket != null)
                    {

                        try { await mBluetoothSocket.ConnectAsync(); } //Connect();
                        catch (System.Exception erro) { System.Console.WriteLine("RESULT : " + erro.Message); }

                    }

                    System.Console.WriteLine("POS CONNECTION  : " + mBluetoothSocket.IsConnected); // Check if socket connection is enable, when connect shows true ( expected)


                    mstreamIN = mBluetoothSocket.InputStream;
                    mstreamOUT = mBluetoothSocket.OutputStream;


                }


            }



        }

       private void ConnectBT()
        {
            Click();

        }
        public int receiveData(BluetoothSocket socket)
        {
        byte[] buffer = new byte[4];
        ByteArrayInputStream input = new ByteArrayInputStream(buffer);
        System.IO.Stream inputStream = socket.InputStream;
        inputStream.Read(buffer);
        return input.Read();
    }

}
}

Sign In or Register to comment.