我正在尝试使用 UWP 应用程序读取我的 air pods pro 电池寿命,并且在没有任何更多信息的情况下socket.ConnectAsync
调用时出现异常错误。Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
您可以在下面找到来源:
private async Task ConnectToAirpods()
{
DeviceInformationCollection connectedDevices = await DeviceInformation.FindAllAsync(BluetoothDevice.GetDeviceSelectorFromConnectionStatus(BluetoothConnectionStatus.Connected));
foreach (DeviceInformation connectedDevice in connectedDevices)
{
if (connectedDevice.Name != "AirPods Pro")
continue;
BluetoothDevice bluetoothDevice = await BluetoothDevice.FromIdAsync(connectedDevice.Id);
RfcommDeviceServicesResult rfcommServices = await bluetoothDevice.GetRfcommServicesForIdAsync(
RfcommServiceId.FromUuid(new Guid("0000111e-0000-1000-8000-00805f9b34fb")), BluetoothCacheMode.Uncached);
if (rfcommServices.Services.Count > 0)
{
var service = rfcommServices.Services[0];
try
{
var socket = new StreamSocket();
await socket.ConnectAsync(service.ConnectionHostName, service.ConnectionServiceName);
}
catch (Exception ex)
{
}
}
}
}
更新 #1 Package.appxmanifest
<Capabilities>
<Capability Name="internetClient"/>
<Capability Name="internetClientServer"/>
<Capability Name="privateNetworkClientServer"/>
<DeviceCapability Name="bluetooth"/>
<DeviceCapability Name="bluetooth.rfcomm">
<Device Id="any">
<Function Type="name:serialPort"/>
</Device>
</DeviceCapability>
</Capabilities>
更新#2
<DeviceCapability Name="bluetooth.rfcomm">
<Device Id="any">
<Function Type="serviceId:0000111e-0000-1000-8000-00805f9b34fb" />
</Device>
</DeviceCapability>