基本上就是主题所说的。我有一个蓝牙设备,每次我尝试使用 Windows 10 机器与其配对时,它都会在屏幕上显示 PIN。我需要在 .Net8 MAUI 应用程序中显示某种弹出对话框,以便用户输入所述 PIN,以便将其提交到 BLE 窗口堆栈。我当前的代码如下所示:
using Windows.Devices.Bluetooth;
using Windows.Devices.Bluetooth.GenericAttributeProfile;
using Windows.Devices.Enumeration;
public static class BluetoothPairing
{
public async Task<bool> PairAsync(BluetoothLEDevice bluetoothLeDevice, DevicePairingProtectionLevel devicePairingProtectionLevel = DevicePairingProtectionLevel.Default)
{
if (bluetoothLeDevice.DeviceInformation.Pairing.IsPaired) //order
return true;
var canPair = bluetoothLeDevice.DeviceInformation.Pairing.CanPair; //order
if (!canPair)
return false;
try
{
bluetoothLeDevice.DeviceInformation.Pairing.Custom.PairingRequested += PairingRequestedHandler_;
const DevicePairingKinds allowedDevicePairingKinds = DevicePairingKinds.ConfirmOnly
| DevicePairingKinds.DisplayPin
| DevicePairingKinds.ProvidePin
| DevicePairingKinds.ConfirmPinMatch
| DevicePairingKinds.ProvidePasswordCredential; //supported only by Windows10.0.18362.0 and later
var pairingStatus = DevicePairingResultStatus.Failed;
var pairingResult = await bluetoothLeDevice.DeviceInformation.Pairing.Custom.PairAsync(
pairingKindsSupported: allowedDevicePairingKinds,
minProtectionLevel: devicePairingProtectionLevel
);
pairingStatus = pairingResult?.Status ?? DevicePairingResultStatus.Failed;
return pairingStatus is DevicePairingResultStatus.Paired or DevicePairingResultStatus.AlreadyPaired;
}
finally
{
bluetoothLeDevice.DeviceInformation.Pairing.Custom.PairingRequested -= PairingRequestedHandler_;
}
}
}
我的主要问题是我找不到在 PairingRequestedHandler_() 内显示 MAUI 输入对话框的正确方法,因为无论我做什么,UI 都会挂起。我找不到解决这个看似微不足道的问题的方法,我已经束手无策了:
static async void PairingRequestedHandler_(DeviceInformationCustomPairing sender, DevicePairingRequestedEventArgs ea)
{
//var deferral = ea.GetDeferral();
// var tcs = new TaskCompletionSource();
var appCurrent = Application.Current;
if (appCurrent == null)
return;
var mainPage = Application.Current?.MainPage;
if (mainPage == null)
return;
await appCurrent.Dispatcher.DispatchAsync(async () =>
{
//var deferral = ea.GetDeferral();
try
{
var typedPin = await mainPage.DisplayPromptAsync(
title: "Enter Pairing PIN",
message: $"Type the PIN (ea.Pin='{ea.Pin}') displayed on the device to proceed:",
accept: "OK",
cancel: "Cancel"
);
if (string.IsNullOrEmpty(typedPin))
return;
ea.Accept(typedPin);
}
catch (Exception ex) //if a dialog is already open we will definitely get an exception
{
//tcs.TrySetException(ex);
}
finally
{
//deferral.Complete();
// if (!tcs.Task.IsFaulted)
// {
// tcs.TrySetCompleted(); // deferral.Complete();
// }
}
});
// tcs.Task.GetAwaiter().GetResult(); //couldnt find a different way to wait for the async code to finish inside the handler
// await tcs.Task;
}
我的主要问题是我找不到在 PairingRequestedHandler_() 内显示 MAUI 输入对话框的正确方法,因为无论我做什么,UI 都会挂起。
您可以将代码放入
MainThread.BeginInvokeOnMainThread()
:
MainThread.BeginInvokeOnMainThread(() =>
{
//display a MAUI input-dialog
});
有关 MainThread 的更多信息,您可以查看官方文档:在 .NET MAUI UI 线程上创建线程。