0

I am trying to read my air pods pro battery life using UWP app and I get an exception error when socket.ConnectAsync is called Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) without any more info. Below you can find the source:

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)
            {

            }
        }
    }
}

Update #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>

Update #2

 <DeviceCapability Name="bluetooth.rfcomm">
    <Device Id="any">
      <Function Type="serviceId:0000111e-0000-1000-8000-00805f9b34fb" />
    </Device>
  </DeviceCapability>
pantonis
  • 5,601
  • 12
  • 58
  • 115
  • Hello, did you check the `Private Networks (Client and Server)` capability in `package.appxmanifest`? – Richard Zhang Jun 22 '20 at 00:58
  • @RichardZhang-MSFT I just did. same error – pantonis Jun 22 '20 at 08:17
  • Hi, If you have checked `Private Networks (Client and Server)`, then you may not have defined the capability of the device. If you plan to transfer data via Rfcomm, please check `Bluetooth` in `package.appxmanifest` and refer to [this Documentation](https://learn.microsoft.com/en-us/uwp/schemas/appxpackage/how-to-specify-device-capabilities-for-bluetooth), defining Device Capability. – Richard Zhang Jun 22 '20 at 08:30
  • @RichardZhang-MSFT I have already done that. Please check my question updated with the app manifest – pantonis Jun 22 '20 at 10:15

1 Answers1

1

I tested this. The problem may appear in this line of code:

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

It is recommended to replace the Guid with:

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

I used a similar wireless headset device for testing. Before the replacement, I got an access denied error. After that, the connection can proceed normally.

Thanks.

Richard Zhang
  • 7,523
  • 1
  • 7
  • 13
  • I have already done that and the result is that ```rfcommServices.Services.Count = 0``` so I cannot connect – pantonis Jun 23 '20 at 06:37
  • Hi, If the number of services returned is 0, it means that the application cannot obtain Rfcomm Services from the current device, and the device you are currently connecting may not be suitable for this connection method. – Richard Zhang Jun 23 '20 at 06:44
  • But If I use the device Id that I hard code (which is the device id of the Airpods) it gets 1 service. – pantonis Jun 23 '20 at 06:48
  • The Uuid entered in the method call represents the interface type of the device. The function added by `DeviceCapability` that you defined in `package.appxmanifest` is **SerialPort**, which means that the application is granted the permission to access the device through **SerialPort**. If you use the device ID as the Uuid, even if you can get Rfcomm Services, you will receive an access denied error. So you need to first determine whether the device can be accessed through SerialPort. – Richard Zhang Jun 23 '20 at 06:53
  • I updated the appmanifest to use the serviceid directly (check Update #2 in initial post) and I still get the Access Denied – pantonis Jun 23 '20 at 06:59
  • You can try using this code snippet: `` If you still receive an access denied error, the device may have blocked the application from accessing it. – Richard Zhang Jun 23 '20 at 08:33
  • I tried this. I am still getting the same error. If the device blocked the application from accessing it then why it allows me to listen music (other apps are able to access it) ? – pantonis Jun 23 '20 at 08:43