hashtagchris / DotNet-BlueZ

A quick and dirty library for BlueZ's D-Bus APIs. Focus is on Bluetooth Low Energy APIs.
Apache License 2.0
103 stars 37 forks source link

ConnectAsync() unhandled exception #11

Open digital-spinner opened 4 years ago

digital-spinner commented 4 years ago

I'm trying to connect to ELM327 bluetooth device (serial communication) but I'm getting an error..

Unhandled exception. Tmds.DBus.DBusException: org.bluez.Error.NotAvailable: Operation currently not available
   at Tmds.DBus.DBusConnection.CallMethodAsync(Message msg, Boolean checkConnected, Boolean checkReplyType)
   at Tmds.DBus.Connection.CallMethodAsync(Message message)
   at Tmds.DBus.CodeGen.DBusObjectProxy.SendMethodReturnReaderAsync(String iface, String member, Nullable`1 inSignature, MessageWriter writer)
   at *****.ConnectAsync() in ********************

I have also tried to use ConnectProfileAsync() method with proper service UUID - same exception. Elevating privileges with sudo also doesn't help. Equivalent test code in Python3 with PyBluez works without problems.

I'm using Linux Ubuntu 20.04 on VM with USB BT dongle and dotnet sdk 3.1.402. My test source code looks like this:

IAdapter1 adapter = BlueZManager.GetAdaptersAsync().Result.FirstOrDefault();
IReadOnlyList<Device> devices = adapter.GetDevicesAsync().Result;
device = devices.Single(x => x.ObjectPath.ToString().EndsWith(macAddress.Replace(':', '_')));

TimeSpan timeout = TimeSpan.FromSeconds(15);
await device.ConnectAsync();
await device.WaitForPropertyValueAsync("Connected", value: true, timeout);
await device.WaitForPropertyValueAsync("ServicesResolved", value: true, timeout);