4

ユーザーの操作なしで、ユニバーサル Windows C# アプリから Bluetooth - シリアル コンバーターにペアリングしようとしています。

開発は Windows 10 Pro の Visual Studio 2015 で行われますが、アプリは Bluetooth アダプターを備えた Windows 10 ベースのデバイスで実行することを目的としています。

セキュリティ上の理由から、BT シリアル コンバーターは検出できず、ピンで保護されているため、検出してペアリングするための列挙手順を実行できません。

私のアプリケーションは、BT アドレス (MAC) と PIN のデバイスしか認識していません (Bluetooth のフレンドリ名も認識していますが、使用したことはありません)。

以前は、Windows Mobile SDK と C++ を使用して、Windows Mobile 6 Pocket PC でこのタスクを実行できましたが、残念ながらコードは Universal Windows Platform に移植できません。

ユニバーサル 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 オブジェクトを作成する代わりに、BluetoothDevice.FromBluetoothAddressAsync が例外 (リソースが見つかりません: 0x80070002) をスローします。

あたかもWindowsが操作を実行するためにBTデバイスの何かを知る必要があるかのようです.FromBluetoothAddressAsyncを呼び出すと、OSがシステム内のすべてのデバイスを内部的にリストします(これには範囲内で検出されたBTデバイスが含まれます)。 .

非表示の bt デバイスに対して Mac ベースのペアリングを実行する他の方法を探していましたが、成功しませんでした (おそらく、ある種の「事前ペアリング」? 何も見つかりませんでした)。

ありがとう。

4

0 に答える 0