Can not connect to my bluetooth device

advertisements

I have been working on this for days, but can't fix the problem.

This is what I've got right now ->

Bluetooth handler

    protected BluetoothAdapter bluetoothAdapter;
    protected BluetoothServer btServer;
    protected BluetoothSocket btSocket;
    protected BluetoothDevice pairedBTDevice;
    protected BluetoothListener btListener;
    protected ParcelUuid uuid;

    public BluetoothHandler()
    {
        BluetoothAdapter = null;
    }

    public void Initialize()
    {
        BluetoothAdapter = BluetoothAdapter.DefaultAdapter;

        // Check if it has a bluetooth interface
        if (BluetoothAdapter == null)
        {
            Console.WriteLine("Bluetooth is not available!");
            return;
        }

        // Check if bluetooth interface is enabled
        if (!BluetoothAdapter.IsEnabled)
        {
            BluetoothAdapter.Enable();
        }

        int count = 0;

        // Get all the devices in the bluetooth list
        var listDevices = BluetoothAdapter.BondedDevices;
        if (listDevices.Count > 0)
        {
            foreach (var btDevice in listDevices)
            {
                // Get the specific controller
                if (btDevice.Name == "MOCUTE-032_B52-CA7E")
                {
                    UUID = btDevice.GetUuids().ElementAt(count);
                    pairedBTDevice = btDevice;
                }
                count++;
            }
        }

        // Check if bluetooth is enabled
        // Check if there is a device
        if (BluetoothAdapter.IsEnabled && pairedBTDevice != null)
        {
            // Check if it's paired
            if (pairedBTDevice.BondState == Bond.Bonded)
            {
                // First start the server
                btServer = new BluetoothServer(this);

                Thread.Sleep(1000);

                // Start a new thread
                Thread thread = new Thread(new ThreadStart(Connect));
                thread.Start();
            }
        }
    }

    protected void Connect()
    {
        // Check if there is no socket already
        if (btSocket == null)
        {
            try
            {
                btSocket = pairedBTDevice.CreateRfcommSocketToServiceRecord(UUID.Uuid);
            }
            catch (IOException ex)
            {
                throw ex;
            }
        }

        try
        {
            Console.WriteLine("Attempting to connect...");

            // Create a socket connection
            btSocket.Connect();
        }
        catch
        {
            Console.WriteLine("Connection failed...");
            Console.WriteLine("Attempting to connect...");
            try
            {
                btSocket = pairedBTDevice.CreateInsecureRfcommSocketToServiceRecord(UUID.Uuid);
                btSocket.Connect();
            }
            catch
            {
                Console.WriteLine("Connection failed...");
                return;
            }
        }

        Console.WriteLine("Client socket is connected!");

        Read();
    }

    protected void Read()
    {
        btListener = new BluetoothListener();
        btListener.Read(btSocket);
    }

Bluetooth server

    private BluetoothHandler bluetoothHandler;
    private BluetoothServerSocket serverSocket;
    private Thread thread;

    public BluetoothServer(BluetoothHandler bluetoothHandler)
    {
        this.bluetoothHandler = bluetoothHandler;

        BluetoothServerSocket tmp = null;

        try
        {
            tmp = bluetoothHandler.BluetoothAdapter.ListenUsingRfcommWithServiceRecord("MOCUTE-032_B52-CA7E", bluetoothHandler.UUID.Uuid);
        }
        catch (IOException ex)
        {
            throw ex;
        }

        serverSocket = tmp;

        thread = new Thread(new ThreadStart(Run));
        thread.Start();
    }

    protected void Run()
    {
        System.Console.WriteLine("Server is running...");
        while (true)
        {
            try
            {
                serverSocket.Accept();
            }
            catch (IOException ex)
            {
                System.Console.WriteLine("FAILED! === > " + ex);
            }
        }
    }

Bluetooth listener

    protected Stream mmInStream;

    public void Read(BluetoothSocket socket)
    {
        Stream tmpIn = null;

        try
        {
            tmpIn = socket.InputStream;
        }
        catch (IOException ex)
        {
            throw ex;
        }

        mmInStream = tmpIn;

        Thread thread = new Thread(new ThreadStart(Run));
        thread.Start();
    }

    protected void Run()
    {
        byte[] buffer = new byte[1024];
        int bytes;

        Console.WriteLine("Waiting for events...");

        while (true)
        {
            try
            {
                if (mmInStream.IsDataAvailable())
                {
                    bytes = mmInStream.Read(buffer, 0, buffer.Length);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

I would like to connect with the following controller ->

So, I would like to catch the buttons events from the controller, but I have no idea anymore..

I've got also the known error: Java.IO.IOException: "read failed, socket might closed or timeout, read ret: -1

The UUID of the controller is: 00001124-0000-1000-8000-00805f9b34fb


I have one example to connect to a Bluetooth(2.0) device on my Github you can check the code and see if you setup is correct here is the link https://github.com/AlejandroRuiz/Mono/blob/master/Arduino/Bluetooth/MainActivity.cs if you have any specific question about the code please let me know also you need to be sure what kind of bluetooth is using because the way to connect to a 4.0 BLE is very different that the old 2.0