我正在尝试从通用 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)我实现了编写一个实现配对设备的代码,从字符串创建源示例中的一些对象来自枚举过程.
但它仅在设备可见时才有效。这是代码:
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 会抛出异常(找不到资源:0x80070002),而不是创建 BluetoothDevice 对象。
好像Windows“需要”了解BT设备才能执行操作,我怀疑当我调用FromBluetoothAddressAsync时,操作系统在内部列出了系统中的所有设备(这包括范围内检测到的BT设备)寻找和具有给定地址的项目.
我一直在寻找其他方法来针对隐藏的 bt 设备执行基于 mac 的配对,但没有成功(可能是某种类型的“预配对”?我什么也没找到)
谢谢。