我正在尝试从通用Windows C#应用程序配对蓝牙 - 串行转换器,无需用户交互。
开发是在Windows 10 Pro下的Visual Studio 2015中,但应用程序适用于任何带有蓝牙适配器的基于Windows 10的设备。
出于安全原因,BT串行转换器不可发现并受引脚保护,因此我无法执行任何枚举过程来检测和配对。
我的应用程序只知道BT地址(MAC)和PIN的设备(它也知道友好的蓝牙名称,但我从未使用它。)
以前我可以使用Windows Mobile SDK和C ++在Windows Mobile 6 Pocket PC下执行此任务,但遗憾的是代码无法移植到通用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中 - &gt;功能,我检查了所有字段以丢弃任何权限缺失。
目前,如果BT-serial转换器可见,此代码只能执行配对操作。 如果BT设备不可见,则BluetoothDevice.FromBluetoothAddressAsync会抛出异常(找不到资源:0x80070002),而不是创建BluetoothDevice对象。
就好像Windows“需要”知道BT设备的某些东西才能执行操作,我怀疑当我调用FromBluetoothAddressAsync时,OS会在内部列出系统中的所有设备(这包括检测到范围内的BT设备)寻找和项目给定地址。
我一直在寻找其他方法对隐藏的bt设备执行基于mac的配对,但没有成功(可能是某种类型的“预配对”?我没有找到任何东西)
感谢。