我试图从通用Windows C#应用程序配对蓝牙串行转换器,无需用户交互。
开发工作在Windows 10 Pro的Visual Studio 2015中,但应用程序旨在运行在任何具有蓝牙适配器的基于Windows 10的设备上。
出于安全原因,BT串行转换器是不可发现的,并受到一个引脚的保护,所以我不能执行任何枚举过程来检测和配对它。
我的应用程序只知道BT地址(MAC)和PIN的设备(也知道友好的蓝牙名称,但我从来没有使用它)。
以前,我可以在Windows Mobile 6 Pocket PC下使用Windows Mobile SDK和C ++执行此任务,但不幸的是代码不能移植到通用Windows平台。
玩通用Windows的MS样本( https://github.com/Microsoft/Windows-universal-samples )我实现了编写一个代码来实现设备配对,从string创build源样本中的一些对象是从枚举过程派生的。
但是只有当设备可见时才能使用。 这是代码:
using System; using Windows.UI.Xaml.Controls; using Windows.Networking; using Windows.Devices.Enumeration; using Windows.Devices.Bluetooth; // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 namespace UWPBTTest { /// <summary> /// An empty page that can be used on its own or navigated to within a Frame. /// </summary> public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); DoPairing(); } async public void DoPairing() { UInt64 targetMAC = 32217180653; //target MAC in decimal, this number corresponds to my device (00:07:80:4b:29:ed) BluetoothDevice btDev = await BluetoothDevice.FromBluetoothAddressAsync(targetMAC); //We create target BT device object, here app throws 0x80070002 exception DeviceInformation infDev = btDev.DeviceInformation; //We need this aux object to perform pairing DevicePairingKinds ceremoniesSelected = DevicePairingKinds.ConfirmOnly | DevicePairingKinds.ProvidePin; //Only confirm pairing, we'll provide PIN from app DevicePairingProtectionLevel protectionLevel = Windows.Devices.Enumeration.DevicePairingProtectionLevel.Encryption; //Encrypted connection DeviceInformationCustomPairing customPairing = infDev.Pairing.Custom; //Our app takes control of pairing, not OS customPairing.PairingRequested += PairingRequestedHandler; //Our pairing request handler DevicePairingResult result = await customPairing.PairAsync(ceremoniesSelected, protectionLevel); //launc pairing customPairing.PairingRequested -= PairingRequestedHandler; if ((result.Status == DevicePairingResultStatus.Paired) || (result.Status == DevicePairingResultStatus.AlreadyPaired)) { //success, now we are able to open a socket } else { //pairing failed } } //Adapted from https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/DeviceEnumerationAndPairing , scenario 9 private async void PairingRequestedHandler( DeviceInformationCustomPairing sender, DevicePairingRequestedEventArgs args) { switch (args.PairingKind) { case DevicePairingKinds.ConfirmOnly: // Windows itself will pop the confirmation dialog as part of "consent" if this is running on Desktop or Mobile // If this is an App for 'Windows IoT Core' where there is no Windows Consent UX, you may want to provide your own confirmation. args.Accept(); break; case DevicePairingKinds.ProvidePin: // As function must be asyn, we simulate a delay of 1 second on GetPinAsync var collectPinDeferral = args.GetDeferral(); string pin = "1234"; //BT converter pin, currently is "1234" for testing purposes args.Accept(pin); collectPinDeferral.Complete(); break; } } } }
这个原型应用程序使用空白表单,因为所有数据都是硬编码的。 同样在Package.appxmanifest – > Capabilities中,我检查了所有字段以放弃任何权限缺乏。
目前该代码只能在BT串口转换器可见的情况下执行配对操作。 如果BT设备不可见,则BluetoothDevice.FromBluetoothAddressAsync将引发exception(资源未find:0x80070002),而不是创buildBluetoothDevice对象。
就好像Windows需要知道BT设备的某些东西来执行操作一样,我怀疑当我调用FromBluetoothAddressAsync时,OS会在内部列出系统中的所有设备(这包括在范围内检测到的BT设备)并寻找具有给定地址的项目。
我一直在寻找其他方法来执行基于mac的配对隐藏bt设备,但没有成功(也许某种types的“预配对”?我没有find任何东西)
谢谢。