The Digi IoT Library for .NET MAUI includes a set of delegates that can be used in the DigiBLEManager class to subscribe and get notified about the different events taking place with the Bluetooth Low Energy interface.
Subscribe to device scan process started event
From the DigiBLEManager class you can start and stop the scanning process, which looks for nearby Bluetooth Low Energy devices. If you want to get notified when that process is started, you can subscribe to the ScanStarted event of the DigiBLEManager class.
// Instantiate the Digi BLE manager.
DigiBLEManager digiBLEManager = new DigiBLEManager();
// Subscribe to the scan process started event.
BluetoothManager.ScanStartedHandler scanStartedHandler = () => Console.WriteLine(">> Scan started");
digiBLEManager.ScanStarted += scanStartedHandler;
Whenever the scan process is started, the handler callback is executed.
Subscribe to device scan process stopped event
Continuing with the scan process, you can also get notified when it is stopped by subscribing to the ScanStopped event of the DigiBLEManager class.
// Instantiate the Digi BLE manager.
DigiBLEManager digiBLEManager = new DigiBLEManager();
// Subscribe to the scan process stopped event.
BluetoothManager.ScanStoppedHandler scanStoppedHandler = () => Console.WriteLine(">> Scan stopped");
digiBLEManager.ScanStopped += scanStoppedHandler;
Whenever the scan process is stopped, the handler callback is executed.
Subscribe to Bluetooth device advertised event
You can be notified when a Bluetooth device is advertised after starting the Bluetooth device scan process if you are subscribed to the DeviceAdvertised event of the DigiBLEManager class.
// Instantiate the Digi BLE manager.
DigiBLEManager digiBLEManager = new DigiBLEManager();
// Subscribe to the device advertised event.
BluetoothManager.DeviceAdvertisedHandler deviceAdvertisedHandler = (device) => Console.WriteLine(">> Device advertised: " + device.Name);
digiBLEManager.DeviceAdvertised += deviceAdvertisedHandler;
Whenever a BLE device is advertised, the handler callback is executed and provides as parameter the IDevice object (device) which contains the representation of the advertised device.
Subscribe to Bluetooth interface state changed event
If you want to get notified when the status of the Bluetooth interface changes, you can subscribe to the BluetoothStateChanged event of the DigiBLEManager class.
// Instantiate the Digi BLE manager.
DigiBLEManager digiBLEManager = new DigiBLEManager();
// Subscribe to the Bluetooth state changed event.
BluetoothManager.BluetoothStateChangedHandler bluetoothStateChangedHandler = (state) => Console.WriteLine(">> BLE state changed to " + state.ToString());
digiBLEManager.BluetoothStateChanged += bluetoothStateChangedHandler;
Whenever the Bluetooth interface changes, the handler callback is executed and provides as parameter the BluetoothState object (state), which contains the value of the new state.
Subscribe to Bluetooth connection lost event
You can be notified when the Bluetooth Low Energy connection has been lost if you subscribe to the ConnectionLost event of the DigiBLEManager class.
// Instantiate the Digi BLE manager.
DigiBLEManager digiBLEManager = new DigiBLEManager();
// Subscribe to the connection lost event.
BluetoothManager.ConnectionLostHandler connectionLostHandler = () => Console.WriteLine(">> Connection lost");
digiBLEManager.ConnectionLost += connectionLostHandler;
Whenever the Bluetooth is lost, the handler callback is executed.