2017-04-23 123 views
6

我升級到Windows 10,1703版本(版本15063)(Creators Update)正式版本。當我在WPF桌面應用程序中運行以下代碼時,BluetoothLEDevice.FromBluetoothAddressAsync永不返回。FromBluetoothAddressAsync永不返回Windows 10創作者WPF應用程序中的更新

此代碼在我的Windows 10更新(即之前的1607版本14393)之前正常工作。如果在新的10場1703

BluetoothLEAdvertisementWatcher BleWatcher = null; 

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    BleWatcher = new BluetoothLEAdvertisementWatcher 
    { 
      ScanningMode = BluetoothLEScanningMode.Active 
    }; 
    BleWatcher.Received += Watcher_Received; 
    BleWatcher.Start(); 
} 

private async void Watcher_Received(BluetoothLEAdvertisementWatcher sender, 
            BluetoothLEAdvertisementReceivedEventArgs args) 
{ 
     var device = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress); 
     // never continues beyond this point above with my BTLE devices that previously worked 
} 

運行作爲UWP此代碼也能正常工作我跟着指示這裏https://stackoverflow.com/a/37335251/3187714設置我的WPF桌面應用程序使用UWP的API。

的問題更是雪上加霜,因爲當用戶開始升級贏得10 1703,因爲我現有的exe不再工作我現有的WPF應用程序將被打破。

是別人的(非UWP)桌面exe文件與Windows 10 1703更新遇到此問題?

經過進一步的實驗中,我確實發現,如果我加入了可選的BluetoothAddressType.Public第二個參數來調用FromBluetoothAddressAsync,該函數返回,但設備返回爲null。

var device = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress, BluetoothAddressType.Public); 
+0

是的,我有同樣的問題。這實際上很煩人,因爲我們項目的要求明確排除了UWP的使用,但強制使用BLE。使用微軟的API是一個痛苦的屁股... – Frank

+0

權限?多數民衆贊成通常與這樣的東西問題。有時候它並不明顯,這可能是UWP windows藍牙許可的不同,它將UWP應用與藍牙進行比較。 –

回答

3

它看起來像有在周圍的藍牙API的安全功能在15063的錯誤是造成這(我看到同樣的事情)。看看這個線程:

https://social.msdn.microsoft.com/Forums/en-US/58da3fdb-a0e1-4161-8af3-778b6839f4e1/bluetooth-bluetoothledevicefromidasync-does-not-complete-on-10015063?forum=wdk#ef927009-676c-47bb-8201-8a80d2323a7f

TL;博士對於C++應用程序,它們提供了CoInitializeSecurity函數調用。對於其他人,看起來他們建議在註冊表中創建一個AppId ,因爲P/Invoke並不好玩。

古怪的是,事情對我來說,通過高尚的UWP的那一刻,它採用C++綁定到UWP功能通過node.js中訪問工作正常只有通過C#,我遇到了問題,並且根據我是在UWP還是WPF /控制檯/桌面應用程序中,事情在不同點上都會失敗。

如論壇帖子中所述,將AppId添加到註冊表後,再次爲我工作。

0

有趣的是,它確實使用cppwinrt在桌面應用程序工作:

Advertisement::BluetoothLEAdvertisementWatcher watcher; 

void Start() { 
    watcher.ScanningMode(Advertisement::BluetoothLEScanningMode::Active); 
    Windows::Foundation::TimeSpan timeout = std::chrono::seconds(2); 
    watcher.SignalStrengthFilter().OutOfRangeTimeout(timeout); 
    watcher.SignalStrengthFilter().OutOfRangeThresholdInDBm(-90); 

    watcher.Received([&](Advertisement::BluetoothLEAdvertisementWatcher watcher, Advertisement::BluetoothLEAdvertisementReceivedEventArgs eventArgs) { 
     connect(eventArgs.BluetoothAddress()); 
    }); 
    watcher.Start(); 
} 

Windows::Foundation::IAsyncAction connect(uint64_t uuid) { 
    co_await resume_background(); 
    BluetoothLEDevice device = co_await BluetoothLEDevice::FromBluetoothAddressAsync(uuid); 
    GenericAttributeProfile::GattDeviceServicesResult gatt = co_await device.GetGattServicesForUuidAsync(myServiceUUID); 
    // works fine! 
} 

然而,GATT特性通知不爲我之後的工作。 I have filed a bug report與cppwinrt項目,但開發人員似乎並不特別傾向於研究它。

我不知道該API的做法與桌面C#版本不同。

0

如果你想在VB中使用CoInitializeSecurity解決方案,這裏是一個解決方案。請記住要修復/修復所有BLE設備!

請注意,這必須是在您的代碼中執行的拳頭事情。 請注意,這不是安全的。

Public Enum RpcAuthnLevel 
    Default1 = 0 
    None = 1 
    Connect = 2 
    Call1 = 3 
    Pkt = 4 
    PktIntegrity = 5 
    PktPrivacy = 6 
End Enum 

Public Enum RpcImpLevel 
    Default1 = 0 
    Anonymous = 1 
    Identify = 2 
    Impersonate = 3 
    Delegate1 = 4 
End Enum 

Public Enum EoAuthnCap 
    None = &H0 
    MutualAuth = &H1 
    StaticCloaking = &H20 
    DynamicCloaking = &H40 
    AnyAuthority = &H80 
    MakeFullSIC = &H100 
    Default1 = &H800 
    SecureRefs = &H2 
    AccessControl = &H4 
    AppID = &H8 
    Dynamic = &H10 
    RequireFullSIC = &H200 
    AutoImpersonate = &H400 
    NoCustomMarshal = &H2000 
    DisableAAA = &H1000 
End Enum 

Declare Function CoInitializeSecurity Lib "ole32.dll" (pVoid As IntPtr, cAuthSvc As Integer, asAuthSvc As IntPtr, pReserved1 As IntPtr, dwAuthnLevel As Integer, dwImpLevel As Integer, pAuthList As IntPtr, dwCapabilities As Integer, pReserved3 As IntPtr) As Integer 

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    CoInitializeSecurity(IntPtr.Zero, -1, IntPtr.Zero, IntPtr.Zero, RpcAuthnLevel.Default1, RpcImpLevel.Identify, IntPtr.Zero, EoAuthnCap.None, IntPtr.Zero) 
    'Other code 
End Sub 
相關問題