如何使用C#手动绑定到WinForm中的BlueTooth低能耗设备?

这个问题主要通过以下方式解答: Windows UWP在发现后连接到BLE设备

我正在编写一个自定义服务并测试,目前,在Windows 10上使用C#.NET WinForm连接到蓝牙低功耗(BLE)设备。 我正在使用Framework 4.6.1。 我们使用TI SmartRF06评估板和TI CC2650 BLE子卡。 另一位开发人员正在处理董事会的固件。

目前使用类似于上面参考答案的方法,我能够连接到已经绑定的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码?

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

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

任何帮助表示赞赏。 如果您需要更多代码,请查看我在顶部提供的链接,因为这是我的开始。 我很乐意提供我所有的实际代码,如果有一个序列,我正在做的不合适。

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

使用后连接:

 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(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(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配对。 所以我希望这可以帮助任何可能卡住的人。