UWP BluetoothDevice.FromIdAsync() returns 空
UWP BluetoothDevice.FromIdAsync() returns null
我有一个客户的笔记本电脑无法连接到蓝牙打印机。这是经典的,适用于我的机器(和其他几台)的情况。我有一种安全许可、防病毒等的感觉,我只是想证明它或找到安全设置以使其正常工作或重写我的代码来处理这种情况。 ——你知道解决问题。
打印机与笔记本电脑配对没有任何问题(打印配对密钥),但是当我尝试从 Id 中找到它时,它总是 returns 空值:
var bluetoothDevice = await BluetoothDevice.FromIdAsync(device.Id) //bluetoothDevice = null
BluetoothDevice and BluetoothLeDeviceAPI两个我都试过了,都不行。
我使用了 Microsoft 的示例 GATT services,我得到了相同的结果。无法连接到笔记本电脑上的设备,在我的开发机器上工作正常。
我添加了 DeviceAccessInformation API 来获取当前状态,它 returns:
Windows.Devices.Enumeration.DeviceAccessStatus.DeniedByUser
//on my dev machine I get DeviceAccessStatus.Allowed
我在 DeviceAccessInformation API 上找不到任何告诉我 why/how 它正在返回 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 提供的 link 设置了功能。我厌倦了将所有函数类型放在那里,但笔记本电脑上没有任何改变。我添加了网络摄像头功能,我看到切换显示在应用程序权限下。但是我从来没有看到蓝牙开关。
<Capabilities>
<Capability Name="internetClient" />
<DeviceCapability Name="bluetooth"/>
<DeviceCapability Name="webcam"/>
<DeviceCapability Name="bluetooth.rfcomm">
<Device Id="any">
<Function Type ="name:serialPort"/>
</Device>
</DeviceCapability>
</Capabilities>
您是否检查了 Package.appxmanifest 中的蓝牙功能选项?
https://docs.microsoft.com/en-us/uwp/schemas/appxpackage/how-to-specify-device-capabilities-for-bluetooth
在清单中启用此功能应该可以解决问题。请注意,用户仍然可以选择在设置中禁止应用程序访问,但如果选择了该功能并且用户授予应用程序访问蓝牙的权限,这应该会解决。
否则,working/failing 机器使用什么 OS 版本?而且,您是否偶然尝试暂时禁用防火墙来对此进行测试?连接时有UAC提示吗?作为测试,您是否以管理员身份在故障机器上尝试过此操作?
我想通了!
开始菜单 -> 设置 -> 隐私设置:
其他设备并切换设备:
我能够通过进入注册表并将 HKEY_LOCAL_MACHINE\Software\Policies\Microsoft\Windows\AppPrivacy\LetAppsAccessTrustedDevices DWORD 设置为 0 来启用切换。重新启动机器,检查隐私设置以启用设备和打印机开始工作了。
我有一个客户的笔记本电脑无法连接到蓝牙打印机。这是经典的,适用于我的机器(和其他几台)的情况。我有一种安全许可、防病毒等的感觉,我只是想证明它或找到安全设置以使其正常工作或重写我的代码来处理这种情况。 ——你知道解决问题。
打印机与笔记本电脑配对没有任何问题(打印配对密钥),但是当我尝试从 Id 中找到它时,它总是 returns 空值:
var bluetoothDevice = await BluetoothDevice.FromIdAsync(device.Id) //bluetoothDevice = null
BluetoothDevice and BluetoothLeDeviceAPI两个我都试过了,都不行。
我使用了 Microsoft 的示例 GATT services,我得到了相同的结果。无法连接到笔记本电脑上的设备,在我的开发机器上工作正常。
我添加了 DeviceAccessInformation API 来获取当前状态,它 returns:
Windows.Devices.Enumeration.DeviceAccessStatus.DeniedByUser
//on my dev machine I get DeviceAccessStatus.Allowed
我在 DeviceAccessInformation API 上找不到任何告诉我 why/how 它正在返回 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 提供的 link 设置了功能。我厌倦了将所有函数类型放在那里,但笔记本电脑上没有任何改变。我添加了网络摄像头功能,我看到切换显示在应用程序权限下。但是我从来没有看到蓝牙开关。
<Capabilities>
<Capability Name="internetClient" />
<DeviceCapability Name="bluetooth"/>
<DeviceCapability Name="webcam"/>
<DeviceCapability Name="bluetooth.rfcomm">
<Device Id="any">
<Function Type ="name:serialPort"/>
</Device>
</DeviceCapability>
</Capabilities>
您是否检查了 Package.appxmanifest 中的蓝牙功能选项? https://docs.microsoft.com/en-us/uwp/schemas/appxpackage/how-to-specify-device-capabilities-for-bluetooth
在清单中启用此功能应该可以解决问题。请注意,用户仍然可以选择在设置中禁止应用程序访问,但如果选择了该功能并且用户授予应用程序访问蓝牙的权限,这应该会解决。
否则,working/failing 机器使用什么 OS 版本?而且,您是否偶然尝试暂时禁用防火墙来对此进行测试?连接时有UAC提示吗?作为测试,您是否以管理员身份在故障机器上尝试过此操作?
我想通了!
开始菜单 -> 设置 -> 隐私设置:
我能够通过进入注册表并将 HKEY_LOCAL_MACHINE\Software\Policies\Microsoft\Windows\AppPrivacy\LetAppsAccessTrustedDevices DWORD 设置为 0 来启用切换。重新启动机器,检查隐私设置以启用设备和打印机开始工作了。