-
Notifications
You must be signed in to change notification settings - Fork 12
Description
Description
The application is designed for a headless device that scans BLE advertisements and pushes them to the cloud via MQTT (message pump). However, after running for a few hours, it crashes with the following exception:
Unhandled exception. Tmds.DBus.DBusException: org.freedesktop.DBus.Error.LimitsExceeded: Connection ":1.290" is not allowed to add more match rules (increase limits in configuration file if required; max_match_rules_per_connection=2048)
at Tmds.DBus.DBusConnection.CallMethodAsync(Message msg, Boolean checkConnected, Boolean checkReplyType)
at Tmds.DBus.DBusConnection.WatchSignalAsync(ObjectPath path, String interface, String signalName, SignalHandler handler)
at Tmds.DBus.Connection.WatchSignalAsync(ObjectPath path, String interface, String signalName, SignalHandler handler)
at Tmds.DBus.CodeGen.DBusObjectProxy.WatchNonVoidSignalAsync[T](String iface, String member, Action'1 error, Action'1 action, ReadMethodDelegate'1 readValue, Boolean isPropertiesChanged)
at Linux.Bluetooth.Device.CreateAsync(IDevice1 proxy)
at Linux.Bluetooth.Adapter.OnDeviceAddedAsync(ValueTuple'2 args)
at System.Threading.Tasks.Task.<>c.b__128_1(Object state)
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()
...
Here is the Initialization Code:
private async Task InitializeBleScannerAsync()
{
if (_adapter != null)
{
Logger.LogWarning("Bluetooth adapter already initialized");
return;
}
// Get the list of available Bluetooth adapters
var adapters = await BlueZManager.GetAdaptersAsync();
if (adapters.Count == 0)
{
throw new InvalidOperationException("No Bluetooth adapters found");
}
// Use the first available adapter
_adapter = adapters[0];
// Extract the adapter name from the adapter path
var adapterPath = _adapter.ObjectPath.ToString();
var adapterName = adapterPath.Substring(adapterPath.LastIndexOf("/", StringComparison.OrdinalIgnoreCase) + 1);
Logger.LogDebug("Using Bluetooth adapter {AdapterName}", adapterName);
// Register event handlers for the adapter
_adapter.PoweredOn += adapter_OnPowerOnCompletedAsync;
_adapter.PoweredOff += adapter_PoweredOffAsync;
_adapter.DeviceFound += Adapter_DeviceFound;
// Power on the adapter
await _adapter.SetPoweredAsync(true);
}Here is the adapter_OnPowerOnCompletedAsync event function
private async Task adapter_OnPowerOnCompletedAsync(Adapter adapter, BlueZEventArgs e)
{
try
{
if (e.IsStateChange)
{
Logger.LogInformation("Bluetooth adapter {Adapter} powered on.", adapter.Name);
}
else
{
Logger.LogInformation("Bluetooth adapter {Adapter} already powered on.", adapter.Name);
}
// do an active registration for all devices
using var watcher = await adapter.WatchDevicesAddedAsync(dev => _activeScanBlock.Post(new DeviceFoundEventArgs(dev, false)));
// Set discovery filter, Setting DuplicateData to true ensures that the DeviceFound event is triggered for every advertisement, even for devices already detected.
Logger.LogDebug("Setting BLE Filter...");
await adapter.SetDiscoveryFilterAsync(new Dictionary<string, object>
{
{ "Transport", "le" }, // Use BLE
{ "DuplicateData", true }
});
Logger.LogDebug("Scanning for devices...");
await adapter.StartDiscoveryAsync();
}
catch (Exception ex)
{
Logger.LogError(ex, "Exception occurred in powerOn event.");
}
}And here is the Adapter_DeviceFound event function
private Task Adapter_DeviceFound(Adapter sender, DeviceFoundEventArgs eventArgs)
{
// Instrument and trace
IncrementMeter(MtrAdvCount);
// Post the event to the first block
_activeScanBlock.Post(eventArgs);
return Task.CompletedTask;
}Environment
- OS Distro: _Ubuntu v22.04
- Linux.Bluetooth Version: 5.67.1
Severity (1-5)
4=Error: Need to reset the adapter
Steps To Reproduce
keep the adapter scanning for a few hours.
Expected Behavior
No crash