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>
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.
User contributions licensed under CC BY-SA 3.0