【问题标题】:UWP BluetoothDevice.FromIdAsync() returns nullUWP BluetoothDevice.FromIdAsync() 返回 null
【发布时间】:2020-09-28 17:43:00
【问题描述】:

我有一台客户的笔记本电脑无法连接到蓝牙打印机。这是一个经典,适用于我的机器(和其他几个)情况。我有一种感觉,它是一种安全许可、防病毒等,我只是想证明它或找到安全设置以使其工作或重写我的代码来处理这种情况。 ——你知道解决问题。

打印机确实与笔记本电脑配对没有任何问题(打印配对密钥),但是当我尝试从 Id 中找到它时,它总是返回 null:

var bluetoothDevice = await BluetoothDevice.FromIdAsync(device.Id) //bluetoothDevice = null

我尝试了BluetoothDeviceBluetoothLeDevice API,没有运气。

我使用了 Microsoft 的示例 GATT services,得到了相同的结果。无法连接到笔记本电脑上的设备,在我的开发机器上工作正常。

我添加了 DeviceAccessInformation API 来获取当前状态并返回:

Windows.Devices.Enumeration.DeviceAccessStatus.DeniedByUser 
//on my dev machine I get DeviceAccessStatus.Allowed

我在 DeviceAccessInformation API 上找不到任何可以告诉我为什么/如何返回 DeniedByUser 的信息。

一如既往,感谢您抽出宝贵时间。

Windows 版本:1909 (18363.78)

适用代码:

var devices = await DeviceInformation.FindAllAsync(BluetoothDevice.GetDeviceSelector());

foreach (var device in devices)
{

    var accessStatus = DeviceAccessInformation.CreateFromId(device.Id).CurrentStatus;

    textLog.AppendLine($"---DeviceId: {device.Id} -- {accessStatus}");

    using (var bluetoothDevice = await BluetoothDevice.FromIdAsync(device.Id))
    {
        if (bluetoothDevice == null)
        {
            textLog.AppendLine($"---DeviceId: {device.Id} Not Found");
            textLog.AppendLine($"---Device Information: {JsonConvert.SerializeObject(device)}");
            txtLog.Text = textLog.ToString();
            continue;
        }
    }
}

远程笔记本电脑日志输出:

connecting to ac:3f:a4:dd:69:a7
---DeviceId: Bluetooth#Bluetoothac:67:5d:1d:d5:25-ac:3f:a4:dd:69:a7 -- DeniedByUser
---DeviceId: Bluetooth#Bluetoothac:67:5d:1d:d5:25-ac:3f:a4:dd:69:a7 Not Found
---Device Information: {"EnclosureLocation":null,"Id":"Bluetooth#Bluetoothac:67:5d:1d:d5:25-ac:3f:a4:dd:69:a7","IsDefault":false,"IsEnabled":false,"Name":"dev-zebra1","Properties":{},"Kind":5,"Pairing":{"CanPair":false,"IsPaired":false,"Custom":{},"ProtectionLevel":1}}
---Printer [ac:3f:a4:dd:69:a7] not found

我的开发机器日志输出:

connecting to ac:3f:a4:dd:69:a7
---DeviceId: Bluetooth#Bluetooth00:23:15:d4:a2:e1-ac:3f:a4:dd:69:a7 -- Allowed
---Device Information: {"EnclosureLocation":null,"Id":"Bluetooth#Bluetooth00:23:15:d4:a2:e1-ac:3f:a4:dd:69:a7","IsDefault":false,"IsEnabled":false,"Name":"dev-zebra1","Properties":{},"Kind":5,"Pairing":{"CanPair":false,"IsPaired":false,"Custom":{},"ProtectionLevel":1}}

--- 更新 ---

我已经根据 David 提供的链接设置了功能。我厌倦了将所有功能类型放在那里,但笔记本电脑上没有任何变化。我添加了网络摄像头功能,我看到切换显示在应用权限下。但我从来没有看到蓝牙开关。

<Capabilities>
    <Capability Name="internetClient" />
    <DeviceCapability Name="bluetooth"/>
    <DeviceCapability Name="webcam"/>
    <DeviceCapability Name="bluetooth.rfcomm">
    <Device Id="any">
       <Function Type ="name:serialPort"/>
    </Device>
   </DeviceCapability>
</Capabilities>

【问题讨论】:

    标签: windows uwp bluetooth bluetooth-lowenergy


    【解决方案1】:

    您是否检查了 Package.appxmanifest 中的蓝牙功能选项? https://docs.microsoft.com/en-us/uwp/schemas/appxpackage/how-to-specify-device-capabilities-for-bluetooth

    在清单中启用此功能应该可以解决问题。请注意,用户仍然可以选择在设置中禁止应用访问,但如果选择了该功能并且用户授予应用访问蓝牙的权限,这应该可以解决。

    否则,工作/故障机器使用哪些操作系统版本?而且,偶然地,您是否尝试过暂时禁用防火墙来测试这一点?连接时有UAC提示吗?您是否以管理员身份在故障机器上尝试过此操作,只是作为测试?

    【讨论】:

    • 我只是在应用清单的“功能”选项卡上检查了蓝牙功能。今晚我会考虑定制它。 - 操作系统版本相同:1909 build 18363.778 - 很确定我在禁用防火墙的情况下对其进行了测试 - 我会仔细检查 - 笔记本电脑或我的开发机器上没有 UAC 提示 - 我相信我在笔记本电脑上作为本地管理员运行我也尝试以管理员身份运行测试应用,但我会再次运行该测试。
    • 我无法关闭笔记本电脑上的防火墙,因为它受组策略控制。
    【解决方案2】:

    我想通了!

    开始菜单 -> 设置 -> 隐私设置: 其他设备并切换设备:

    我能够通过进入注册表并将 HKEY_LOCAL_MACHINE\Software\Policies\Microsoft\Windows\AppPrivacy\LetAppsAccessTrustedDevices DWORD 设置为 0 来启用切换。重新启动机器,检查隐私设置以启用设备和打印机开始工作。

    More Info on the GPO / Registry settings

    【讨论】:

      猜你喜欢
      • 2018-11-03
      • 2017-05-10
      • 1970-01-01
      • 1970-01-01
      • 2019-02-11
      • 2018-02-16
      • 2021-03-13
      • 2018-06-16
      • 1970-01-01
      相关资源
      最近更新 更多