خرید بک لینک

Vote count: 0

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!");
            retu;
        }

        // 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(Coect));
                thread.Start();
            }
        }
    }

    protected void Coect()
    {
        // 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 coect...");

            // Create a socket coection
            btSocket.Coect();
        }
        catch
        {
            Console.WriteLine("Coection failed...");
            Console.WriteLine("Attempting to coect...");
            try
            {
                btSocket = pairedBTDevice.CreateInsecureRfcommSocketToServiceRecord(UUID.Uuid);
                btSocket.Coect();
            }
            catch
            {
                Console.WriteLine("Coection failed...");
                retu;
            }
        }

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

        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 ruing...");
        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 coect with the following controller ->

enter image description here

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

asked 1 min ago

برچسب: نویسنده: استخدام کار تاريخ: يکشنبه 15 فروردين 1395 ساعت: 21:52

صفحه بندی