我想到了。我走在正确的轨道上。
连接后使用:
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 配对。所以我希望这可以帮助任何可能陷入困境的人。