当前位置: 首页 > 知识库问答 >
问题:

将蓝牙设备与32英尺长的计算机配对。NET蓝牙库

钱均
2023-03-14

如果您想知道如何使用32英尺。NET库与蓝牙设备通信,请阅读解决方案

我目前正在尝试通过蓝牙在电脑和自建电脑之间进行通信。NET小工具原型。

Gadgeteer原型由主板、电源和蓝牙模块组成。模块处于可发现模式。

电脑上有一个基于32英尺的自定义蓝牙程序。NET Bluetooth正在运行。该程序检测范围内的所有蓝牙设备,并尝试与它们配对。然而,目前这并不是自动完成的,我必须输入设备的配对代码。

如何在不输入配对代码的情况下配对设备?

设备找到了,问题是配对部分。我做了很多实验,但没有找到解决方案...

foreach (BluetoothDeviceInfo device in this.deviceList)
{
    try
    {
        //BluetoothClient client = new BluetoothClient(this.CreateNewEndpoint(localAddress));
        //BluetoothEndPoint ep = this.CreateNewEndpoint(device.DeviceAddress);

        EventHandler<BluetoothWin32AuthenticationEventArgs> handler = new EventHandler<BluetoothWin32AuthenticationEventArgs>(HandleRequests);
        BluetoothWin32Authentication auth = new BluetoothWin32Authentication(handler);

        BluetoothSecurity.PairRequest(device.DeviceAddress, null);
    }
}

这个代码块启动了配对,它可以工作,但Windows要求我输入设备的配对代码。我读过BluetoothWin32Authentication来阻止这个案例,但我没有正确理解。

private void HandleRequests(object that, BluetoothWin32AuthenticationEventArgs e)
{
    e.Confirm = true;
}

这是事件处理程序的代码(http://32feet.codeplex.com/wikipage?title=BluetoothWin32Authentication)

如果您只是想让配对在SSP设备连接时继续进行,那么处理回调和设置e。确认=真就足够了——但这有点不安全...

我很困惑目标是应用程序和gadgeteer模块可以在没有任何用户干扰的情况下向两个方向发送数据。

我真的不能在没有用户交互的情况下自动配对设备吗?

如果两个设备已经配对,它们可以在没有用户交互的情况下交换数据,这是真的吗?

共有1个答案

山越
2023-03-14

扫描

这意味着检测到范围内的设备。我的代码:

// mac is mac address of local bluetooth device
BluetoothEndPoint localEndpoint = new BluetoothEndPoint(mac, BluetoothService.SerialPort);
// client is used to manage connections
BluetoothClient localClient = new BluetoothClient(localEndpoint);
// component is used to manage device discovery
BluetoothComponent localComponent = new BluetoothComponent(localClient);
// async methods, can be done synchronously too
localComponent.DiscoverDevicesAsync(255, true, true, true, true, null);
localComponent.DiscoverDevicesProgress += new EventHandler<DiscoverDevicesEventArgs>(component_DiscoverDevicesProgress);
localComponent.DiscoverDevicesComplete += new EventHandler<DiscoverDevicesEventArgs>(component_DiscoverDevicesComplete);

private void component_DiscoverDevicesProgress(object sender, DiscoverDevicesEventArgs e)
{
    // log and save all found devices
    for (int i = 0; i < e.Devices.Length; i++)
    {           
        if (e.Devices[i].Remembered)
        {
            Print(e.Devices[i].DeviceName + " (" + e.Devices[i].DeviceAddress + "): Device is known");
        }
        else
        {
            Print(e.Devices[i].DeviceName + " (" + e.Devices[i].DeviceAddress + "): Device is unknown");
        }
        this.deviceList.Add(e.Devices[i]);         
    }
}

private void component_DiscoverDevicesComplete(object sender, DiscoverDevicesEventArgs e)
{
    // log some stuff
}

配对

这意味着设备与本地蓝牙设备耦合。这需要通过输入双方的代码来完成一次。可以通过代码完成,这样用户甚至不会注意到添加了设备。我的代码:

// get a list of all paired devices
BluetoothDeviceInfo[] paired = localClient.DiscoverDevices(255, false, true, false, false);
// check every discovered device if it is already paired 
foreach (BluetoothDeviceInfo device in this.deviceList)
{
    bool isPaired = false;
    for (int i = 0; i < paired.Length; i++)
    {
        if (device.Equals(paired[i]))
        {
            isPaired = true;
            break;
        }
    }

    // if the device is not paired, pair it!
    if (!isPaired)
    {
        // replace DEVICE_PIN here, synchronous method, but fast
        isPaired = BluetoothSecurity.PairRequest(device.DeviceAddress, DEVICE_PIN);
        if (isPaired)
        {
            // now it is paired
        }
        else
        {
            // pairing failed
        }
    }
}

连接

这意味着建立连接和交换数据。同样是一些代码:

// check if device is paired
if (device.Authenticated)
{
    // set pin of device to connect with
    localClient.SetPin(DEVICE_PIN);
    // async connection method
    localClient.BeginConnect(device.DeviceAddress, BluetoothService.SerialPort, new AsyncCallback(Connect), device);
}

// callback
private void Connect(IAsyncResult result)
{
    if (result.IsCompleted)
    {
        // client is connected now :)
    }
}

如果你保持扫描、配对、连接的顺序,一切都应该正常。要发送或接收数据,请使用BluetoothClient的方法。它提供了一个可以操作的网络流。

接收连接

如果你想让另一台设备与你的设备连接,你需要监听传入的连接请求。这仅在设备之前已配对的情况下有效。我的代码:

BluetoothListener l = new BluetoothListener(LOCAL_MAC, BluetoothService.SerialPort);
l.Start(10);
l.BeginAcceptBluetoothClient(new AsyncCallback(AcceptConnection), l);

void AcceptConnection(IAsyncResult result){
    if (result.IsCompleted){
        BluetoothClient remoteDevice = ((BluetoothListener)result.AsyncState).EndAcceptBluetoothClient(result);    
    }    
}

用有效的BluetoothAddress(例如,通过使用BluetoothAddress.Parse()替换本地MAC) )。连接设备后,它们可以通过底层流交换消息。如果连接不工作,则可能存在身份验证问题,因此请尝试在侦听器中设置本地设备pin(l.SetPin(local\u MAC,MY\u PASSWORD)

 类似资料:
  • 我刚刚开始了一个项目,需要将Windows 10平板电脑与另一个蓝牙设备配对。 我决定从一个简单的windows窗体应用程序开始,以熟悉这个过程。我加了32英尺。NET NuGet包,并很快在搜索设备和填充列表框方面取得了成功。 然后,我添加了一个事件处理程序,以便可以选择检测到的设备并尝试与之配对。 在装有廉价蓝牙2.0适配器的Windows7台式电脑上,我的手机上会出现一个弹出窗口,要求我输入

  • 连接和断开蓝牙设备在Windows Phone/Desktop 8.1上产生了各种结果。我一直在使用命名空间和我尝试连接具有不同蓝牙版本/类的几个设备。 版本1.2(1级和2级) 每当我尝试连接到2.0或2.1设备时,都会出现问题。第一次尝试连接到每个设备时,一切都会顺利,连接也会打开。当我随后关闭连接并重新连接设备时,问题就开始了。在重新连接期间,连接将永远不会打开,调试器将抛出一个系统。例外:

  • deviceName String deviceId String

  • 我正在开发一款连接蓝牙物联网设备的Flitter应用程序。我正在使用Flatter_blue图书馆。该库允许扫描附近的蓝牙设备。基于该扫描,您可以“连接”到设备。没有与设备配对的概念。 根据我以前在手机上使用蓝牙的经验(连接到我的汽车和蓝牙扬声器时),我必须在Android操作系统上配对设备。 我很好奇,从高层次上讲,配对设备和连接设备之间有什么区别。此外,更具体地说,在操作系统内配对设备与扫描并

  • 我正在Visual Studio 2015中用C语言为运行Windows IoT Core的Raspberry PI 2设备开发。 对于我的应用程序,我需要配对和取消配对蓝牙设备 我可以获得配对/未配对/所有蓝牙设备的列表吗<类似于内置管理网站的蓝牙页面(http://[设备IP]:8080/Bluetooth.htm) 我找到了一个例子(https://github.com/Microsoft/

  • 问题内容: 我正在创建一个应通过蓝牙连接到特定设备的应用程序。 无论设备是否已配对,我都希望我的应用程序与此设备连接。 现在我有这个 但是此功能仅连接到配对的设备。如果我的设备尚未配对,我想将其配对。不知道该怎么做。 有人可以给我任何建议吗? 问题答案: 首次请求权限。 然后使您的设备可发现: 然后创建一个 BroadcastReceiver 来监听系统中的动作: 并通过注册以下 Boardcas