簡體   English   中英

如何使用C#手動綁定到WinForm中的藍牙低能耗設備?

[英]How to Bind manually to a BlueTooth Low Energy Device in a WinForm using C#?

這個問題通常由以下問題回答: Windows UWP發現后連接到BLE設備

我目前正在編寫自定義服務和測試,使用Windows 10上的C#.NET WinForm連接到低功耗藍牙(BLE)設備。 我正在使用Framework 4.6.1。 我們正在使用帶有TI CC2650 BLE子卡的TI SmartRF06評估板 另一位開發人員正在處理主板的固件。

當前我使用的方法類似於上面的參考答案,因此我能夠連接到已經綁定的BLE設備。 該設備是手動綁定的,Windows確實要求我輸入PIN。 由於設備沒有PIN,只需輸入“ 0”就可以連接設備。 一旦建立連接,我就可以使用所有GATT服務並做我需要做的事情。 因此,我對找到並持有Advertising BLE設備沒有任何問題。

問題是如何連接尚未配對的BLE設備? 我遍歷網絡,發現了許多BLE代碼示例,但沒有具體說明如何完成代碼配對。 不確定我什至不需要配對,但Windows似乎僅在配對設備上顯示我的GATT服務。

當我使用未配對的設備執行此操作時:

private void BleWatcherOnReceived(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)
{       
    var dev = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress);
    // dev.DeviceInformation.Pairing.CanPair is true
    // dpr.Status is Failed
    DevicePairingResult dpr = await dev.DeviceInformation.Pairing.PairAsync(DevicePairingProtectionLevel.None);
    var service = await GattDeviceService.FromIdAsync(dev.DeviceInformation.Id);
}

當未手動配對設備時,dpr的結果總是失敗。 這導致GattDeviceServices為空。 但是我能夠獲得BLE設備的廣告和屬性。

還有這種類型的連接方法,但我不知道如何使用它:

var prslt = await device.DeviceInformation.Pairing.Custom.PairAsync(DevicePairingKinds.ProvidePin, DevicePairingProtectionLevel.None,IDevicePairingSettings);

IdeviceParingSettings是一個接口。 不確定要使用哪個類。 我在想這是我可能需要設置“ O”的PIN的地方?

在Windows中使用BLE設備沒有安全性的情況下,是否有人能夠與Windows中的BLE設備配對。 基本上,它應該是開放的。 我覺得我缺少一些簡單的東西,或者根本不可能(我看到一些帖子聲稱是這種情況。其中大多數都已經有很多年了)。

我確實嘗試了上述文章中描述的方法,但結果沒有任何差異。

任何幫助表示贊賞。 如果您需要更多代碼,請查看我在頂部提供的鏈接,因為這是我開始的地方。 如果有可能我做錯了一個序列,我將很樂意提供我所有的實際代碼。

我想到了。 我走在正確的軌道上。

使用以下連接后:

var dev = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress);

您需要執行自定義配對:

var prslt = await device.DeviceInformation.Pairing.Custom.PairAsync(DevicePairingKinds.ProvidePin, DevicePairingProtectionLevel.None);

但這只會給您一個錯誤。 您還必須創建device.DeviceInformation.Pairing.Custom.PairingRequested事件處理程序。

所以我創建了這個處理程序:

private void handlerPairingReq(DeviceInformationCustomPairing CP, DevicePairingRequestedEventArgs DPR)
        {
            //so we get here for custom pairing request.
            //this is the magic place where your pin goes.
            //my device actually does not require a pin but
            //windows requires at least a "0".  So this solved 
            //it.  This does not pull up the Windows UI either.
            DPR.Accept("0");


}

在PairAsync調用之前將其連接起來,例如:

device.DeviceInformation.Pairing.Custom.PairingRequested += handlerPairingRequested;

連接我的BlueToothAdvertisementWatcher代碼的示例代碼:

    private BluetoothLEAdvertisementWatcher BTWatch = new BluetoothLEAdvertisementWatcher();

    private void Inits() 
        {
           BTWatch.Received += new TypedEventHandler<BluetoothLEAdvertisementWatcher, BluetoothLEAdvertisementReceivedEventArgs>(BtAddRx);
           BTWatch.Start();
        }

    private async void BtAddRx(BluetoothLEAdvertisementWatcher bw, BluetoothLEAdvertisementReceivedEventArgs args)
        {
            GattCommunicationStatus srslt;
            GattReadResult rslt;
            bw.Stop();//Stop this while inside.

            device = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress);
                if (device.DeviceInformation.Pairing.IsPaired == false)
                {   

                    /* Optional Below - Some examples say use FromIdAsync
                    to get the device. I don't think that it matters.   */            
                    var did = device.DeviceInformation.Id; //I reuse did to reload later.
                    device.Dispose();
                    device = null;
                    device = await BluetoothLEDevice.FromIdAsync(did);
                    /* end optional */
                    var handlerPairingRequested = new TypedEventHandler<DeviceInformationCustomPairing, DevicePairingRequestedEventArgs>(handlerPairingReq);
                    device.DeviceInformation.Pairing.Custom.PairingRequested += handlerPairingRequested;
                    log("Pairing to device now...."); 

                    var prslt = await device.DeviceInformation.Pairing.Custom.PairAsync(DevicePairingKinds.ProvidePin, DevicePairingProtectionLevel.None);                  
                    log("Custom PAIR complete status: " + prslt.Status.ToString() + " Connection Status: " + device.ConnectionStatus.ToString());

                    device.DeviceInformation.Pairing.Custom.PairingRequested -= handlerPairingRequested; //Don't need it anymore once paired.


                    if (prslt.Status != DevicePairingResultStatus.Paired)
                    { //This should not happen. If so we exit to try again.
                        log("prslt exiting.  prslt.status=" + prslt.Status.ToString());// so the status may have updated.  lets drop out of here and get the device again.  should be paired the 2nd time around?
                        bw.Start();//restart this watcher.
                        return;
                    }
                    else
                    {
                        // The pairing takes some time to complete. If you don't wait you may have issues. 5 seconds seems to do the trick.

                        System.Threading.Thread.Sleep(5000); //try 5 second lay.
                        device.Dispose();
                       //Reload device so that the GATT services are there. This is why we wait.                     
                       device = await BluetoothLEDevice.FromIdAsync(did);

                    }
 var services = device.GattServices;
//then more code to finish it up.
}

如果您想斷開連接,請使用:

await device.DeviceInformation.Pairing.UnpairAsync();

抱歉,代碼混亂。 如果發現有人有用或有疑問,請告訴我。 我在任何地方都找不到此代碼的任何WinForm示例。 實際上,在沒有UI的情況下,我找不到任何代碼來顯示如何與PIN配對。 因此,我希望這對任何可能陷入困境的人有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM