UWP BluetoothDevice.FromIdAsync()返回null

问题描述 投票:0回答:1

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

打印机确实与笔记本电脑配对没有任何问题(打印配对密钥),但是当我尝试从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个回答
0
投票

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

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

否则,工作/故障机器使用什么操作系统版本?而且,您是否偶然尝试过暂时禁用防火墙以进行测试?连接时是否有UAC提示?您是否以管理员身份在发生故障的计算机上尝试过此测试?

© www.soinside.com 2019 - 2024. All rights reserved.