无法连接我的蓝牙设备

时间:2016-04-03 14:39:25

标签: c# android xamarin bluetooth xamarin.android

我已经在这方面工作了好几天,但无法解决问题。

这就是我现在所拥有的 - >

蓝牙处理程序

    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);
    }

蓝牙服务器

    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);
            }
        }
    }

蓝牙监听器

    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;
            }
        }
    }

我想连接以下控制器 - >

enter image description here

所以,我想从控制器中捕捉按钮事件,但我不知道了......

我还有已知的错误:Java.IO.IOException: "read failed, socket might closed or timeout, read ret: -1

控制器的UUID为:00001124-0000-1000-8000-00805f9b34fb

2 个答案:

答案 0 :(得分:0)

我有一个示例连接到我的Github上的蓝牙(2.0)设备,您可以检查代码并查看您的设置是否正确这里是链接https://github.com/AlejandroRuiz/Mono/blob/master/Arduino/Bluetooth/MainActivity.cs如果您对代码有任何具体问题请让我知道你还需要确定使用什么样的蓝牙,因为连接到4.0 BLE的方式与旧的2.0非常不同

答案 1 :(得分:0)

问题解决了。我不需要蓝牙插座。我刚刚使用了覆盖方法" KeyDown"和" KeyUp"。现在效果很好:))

如果你需要一个套接字并且你有一个像IOException这样的异常:读取失败,套接字可能关闭,那么你应该在这里阅读我的修复:

IOException:读取失败,套接字可能已关闭 - Android 4.3上的蓝牙