我在将Arduino与PC与设备的连接方面遇到问题。C#中的蓝牙类

时间:2019-06-11 14:23:41

标签: c# .net bluetooth arduino

我在将Arduino蓝牙模块连接到PC(模块名称HC-06)时遇到问题。我已经在.net中尝试过port.devices类,但是我需要一个UWP类将其移植到hololens,但是我无法进入Devices.Bluetooth框架,因为我不知道如何从设备中获取Uuid。连接服务或正确的方法以获取DeviceInformation。

每次我从端口= NullReferenceException等待aBluetoothDevice.FromIdAsync(bdeviceInfo.Id);有时它可以工作,但是当我使用port.Hostname时,会出现相同的异常。 HC-06设备也被列出两次,为什么?有人尝试过吗?我将这个东西与Port类btw一起使用很容易。我对蓝牙协议的了解不那么深,所以也许我错过了一些东西。我也尝试从设备获取rfcomm服务,该操作也失败,但是我认为这是一个问题,原因是bdeviceInfo.Id的ID错误。有时它说ArgumentException。这可能是由于这两个列出的设备的随机列表。我希望有人可以帮助我,或者告诉我更多有关这堂课的信息。谢谢!

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Networking.Sockets;

namespace connectAppDice
{
    using System.Collections;
    using System.Collections.Generic;
    using System;
    //using System.IO.Ports;
    //#if ENABLE_WINMD_SUPPORT && WINDOWS_UWPP
    using System.Threading.Tasks;
    using Windows.Devices.SerialCommunication;
    using Windows.Devices.Bluetooth;
    using Windows.Storage.Streams;
    using Windows.Devices.Enumeration;
    using Windows.Foundation;
    using Windows.Devices.Bluetooth.Rfcomm;
    using Windows.Storage;

    public class connectAndReadDiceBluetooth
    {
        private IInputStream input;
        private Task readThread;
        private static Guid guid = new Guid("e0cbf06c-cd8b-4647-bb8a-263b43f0f974");

        void ReceiveData()
        {
            while (readThread.Status == TaskStatus.Created) {

            }
        }

        private void OnApplicationQuit()
        {
            //readThread.Abort();
            //port.Close();
        }

        public static async Task listPorts()
        {
            StreamSocket _socket;
            string bDeviceSelect;
            DeviceInformationCollection bdevices;
            DeviceInformation bdeviceInfo;
            BluetoothDevice port;
            DataWriter dataWriter;
            bDeviceSelect = BluetoothDevice.GetDeviceSelector();
            bdevices = await DeviceInformation.FindAllAsync(bDeviceSelect);

        //create buffer to write
        Console.Write(bdevices.Count + "\n");
            if (bdevices.Count > 0) {
                bdeviceInfo = bdevices[1];

                for (int i = 0; i < bdevices.Count; i++)
                {
                    Console.Write(bdevices[i].Name + " is NOT the preferred Device! \n");
                }

                for (int i = 0; i < bdevices.Count; i++) {
                    if (bdevices[i].Name == "HC-06") {
                        bdeviceInfo = bdevices[i];
                        Console.Write(bdevices[i].Name + " is the preferred Device! \n");
                        Console.Write("The second dice is " + bdevices[i].Name + "\n");
                        break;
                    }
                }

                try {
                    port = await BluetoothDevice.FromIdAsync(bdeviceInfo.Id);
                    Console.Write(port.Name + "\n");
                    Console.Write(port.HostName + "\n");
                    //Guid uuid = BluetoothUuidHelper.FromShortId((uint)bdeviceInfo.Id);
                    //var service = await port.GetRfcommServicesForIdAsync(RfcommServiceId.FromUuid(guid));
                    //_socket = new StreamSocket();
                    /*await _socket.ConnectAsync(port.HostName, "{00001101-0000-1000-8000-00805f9b34fb}",
                        SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);*/

                    /*await _socket.ConnectAsync(bdeviceInfo., "{00001101-0000-1000-8000-00805F9B34FB}",
                        SocketProtectionLevel.PlainSocket);*/

                    Console.Write("Ready");
                } catch (Exception ex) {
                    Console.Write("Exception happend \n");
                    System.Diagnostics.Debug.WriteLine(ex.ToString());
                }

                Console.Write("DeviceInfo \n");
                Console.Write(bdeviceInfo.Name + "\n");
            } else {
                Console.Write("Sorry, no device found. \n");
            }
        }

    }
    //#endif

}

0 个答案:

没有答案