0

我正在尝试使用 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>
4

1 回答 1

1

我对此进行了测试。问题可能出现在这行代码中:

RfcommDeviceServicesResult rfcommServices = await bluetoothDevice.GetRfcommServicesForIdAsync(RfcommServiceId.FromUuid(new Guid("0000111e-0000-1000-8000-00805f9b34fb")), BluetoothCacheMode.Uncached);

建议将 Guid 替换为:

RfcommDeviceServicesResult rfcommServices = await bluetoothDevice.GetRfcommServicesForIdAsync(
    RfcommServiceId.FromUuid(RfcommServiceId.SerialPort.Uuid), BluetoothCacheMode.Uncached);

我使用类似的无线耳机设备进行测试。在更换之前,我收到了拒绝访问错误。之后,连接可以正常进行。

谢谢。

于 2020-06-22T23:46:47.177 回答