2017-04-23 93 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 
相关问题