如何使用 C# 在 WinForm 中手动绑定到蓝牙低功耗设备?

2024-04-30

这个问题的回答大多是:Windows UWP 发现后连接到 BLE 设备 https://stackoverflow.com/questions/35420940/windows-uwp-connect-to-ble-device-after-discovery/41413736#41413736

我正在编写自定义服务并进行测试,目前使用 Windows 10 上的 C#.NET WinForm 连接到蓝牙低功耗 (BLE) 设备。我使用的是框架4.6.1。我们正在使用一个TI SmartRF06 评估板 http://www.ti.com/tool/smartrf06ebk with a TI CC2650 BLE http://www.ti.com/product/cc2650女儿卡。另一位开发人员正在处理主板的固件。

目前使用的方法与参考答案类似above https://stackoverflow.com/questions/35420940/windows-uwp-connect-to-ble-device-after-discovery/41413736#41413736我能够连接到已绑定的 BLE 设备。该设备是手动绑定的,Windows 确实要求我输入 PIN。由于设备没有 PIN,只需输入“0”即可允许设备连接。连接后,以这种方式,我可以访问所有 GATT 服务并执行我需要执行的操作。因此,我可以轻松找到并获得广告 BLE 设备。

问题是如何连接到尚未配对的 BLE 设备?我在网上找到了许多 BLE 代码的示例,但没有具体说明代码中的配对是如何完成的。不确定我是否需要它来配对,但 Windows 似乎只在配对设备上显示我的 GATT 服务。

当我使用未配对的设备执行此操作时:

private void BleWatcherOnReceived(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)
{       
    var dev = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress);
    // dev.DeviceInformation.Pairing.CanPair is true
    // dpr.Status is Failed
    DevicePairingResult dpr = await dev.DeviceInformation.Pairing.PairAsync(DevicePairingProtectionLevel.None);
    var service = await GattDeviceService.FromIdAsync(dev.DeviceInformation.Id);
}

当设备没有手动配对时,dpr的结果总是失败。这导致GattDeviceServices是空的。但我能够获取 BLE 设备的广告和属性。

还有这种连接方法,但我不知道如何使用它:

var prslt = await device.DeviceInformation.Pairing.Custom.PairAsync(DevicePairingKinds.ProvidePin, DevicePairingProtectionLevel.None,IDevicePairingSettings);

IdeviceParingSettings是一个接口。不确定要使用什么类。我想这是我可以设置我可能需要的“O”PIN 码的地方?

有没有人有幸在 Windows 中使用 C# 与 BLE 设备配对,而 BLE 设备没有安全性。基本上应该是敞开的。我觉得我错过了一些简单的东西,或者这根本不可能(我看到一些帖子声称是这种情况。其中大多数已经有很多年了)。

我确实尝试了上述帖子中描述的方法,结果没有任何差异。

任何帮助表示赞赏。如果您需要更多代码,请查看我在顶部提供的链接,因为这就是我开始的地方。如果可能有一个我做错了的顺序,我将很乐意提供我的所有实际代码。


我想到了。我走在正确的轨道上。

连接后使用:

var dev = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress);

您需要进行自定义配对:

var prslt = await device.DeviceInformation.Pairing.Custom.PairAsync(DevicePairingKinds.ProvidePin, DevicePairingProtectionLevel.None);

但这只会给你一个错误。您还必须创建一个device.DeviceInformation.Pairing.Custom.PairingRequested事件处理程序。

所以我创建了这个处理程序:

private void handlerPairingReq(DeviceInformationCustomPairing CP, DevicePairingRequestedEventArgs DPR)
        {
            //so we get here for custom pairing request.
            //this is the magic place where your pin goes.
            //my device actually does not require a pin but
            //windows requires at least a "0".  So this solved 
            //it.  This does not pull up the Windows UI either.
            DPR.Accept("0");


}

在 PairAsync 调用之前将其连接起来,例如:

device.DeviceInformation.Pairing.Custom.PairingRequested += handlerPairingRequested;

执行我的连接的 BlueToothAdvertisementWatcher 代码的示例代码:

    private BluetoothLEAdvertisementWatcher BTWatch = new BluetoothLEAdvertisementWatcher();

    private void Inits() 
        {
           BTWatch.Received += new TypedEventHandler<BluetoothLEAdvertisementWatcher, BluetoothLEAdvertisementReceivedEventArgs>(BtAddRx);
           BTWatch.Start();
        }

    private async void BtAddRx(BluetoothLEAdvertisementWatcher bw, BluetoothLEAdvertisementReceivedEventArgs args)
        {
            GattCommunicationStatus srslt;
            GattReadResult rslt;
            bw.Stop();//Stop this while inside.

            device = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress);
                if (device.DeviceInformation.Pairing.IsPaired == false)
                {   

                    /* Optional Below - Some examples say use FromIdAsync
                    to get the device. I don't think that it matters.   */            
                    var did = device.DeviceInformation.Id; //I reuse did to reload later.
                    device.Dispose();
                    device = null;
                    device = await BluetoothLEDevice.FromIdAsync(did);
                    /* end optional */
                    var handlerPairingRequested = new TypedEventHandler<DeviceInformationCustomPairing, DevicePairingRequestedEventArgs>(handlerPairingReq);
                    device.DeviceInformation.Pairing.Custom.PairingRequested += handlerPairingRequested;
                    log("Pairing to device now...."); 

                    var prslt = await device.DeviceInformation.Pairing.Custom.PairAsync(DevicePairingKinds.ProvidePin, DevicePairingProtectionLevel.None);                  
                    log("Custom PAIR complete status: " + prslt.Status.ToString() + " Connection Status: " + device.ConnectionStatus.ToString());

                    device.DeviceInformation.Pairing.Custom.PairingRequested -= handlerPairingRequested; //Don't need it anymore once paired.


                    if (prslt.Status != DevicePairingResultStatus.Paired)
                    { //This should not happen. If so we exit to try again.
                        log("prslt exiting.  prslt.status=" + prslt.Status.ToString());// so the status may have updated.  lets drop out of here and get the device again.  should be paired the 2nd time around?
                        bw.Start();//restart this watcher.
                        return;
                    }
                    else
                    {
                        // The pairing takes some time to complete. If you don't wait you may have issues. 5 seconds seems to do the trick.

                        System.Threading.Thread.Sleep(5000); //try 5 second lay.
                        device.Dispose();
                       //Reload device so that the GATT services are there. This is why we wait.                     
                       device = await BluetoothLEDevice.FromIdAsync(did);

                    }
 var services = device.GattServices;
//then more code to finish it up.
}

如果您想断开连接,只需使用:

await device.DeviceInformation.Pairing.UnpairAsync();

抱歉,代码混乱。如果有人发现有用或有疑问请告诉我。我在任何地方都找不到此代码的任何 WinForm 示例。实际上,我找不到任何代码来展示如何在没有 UI 的情况下与 PIN 配对。所以我希望这可以帮助任何可能陷入困境的人。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 C# 在 WinForm 中手动绑定到蓝牙低功耗设备? 的相关文章

随机推荐