BluetoothScanManager.cs 820 B

12345678910111213141516171819202122232425262728
  1. using Android.Bluetooth.LE;
  2. namespace InABox.Avalonia.Platform.Android;
  3. public class BluetoothScanManager : ScanCallback
  4. {
  5. private readonly Action<ScanResult> _onDeviceFound;
  6. private readonly Action _onScanStopped;
  7. public BluetoothScanManager(Action<ScanResult> onDeviceFound, Action onScanStopped)
  8. {
  9. _onDeviceFound = onDeviceFound;
  10. _onScanStopped = onScanStopped;
  11. }
  12. public override void OnScanResult(ScanCallbackType callbackType, ScanResult result)
  13. {
  14. base.OnScanResult(callbackType, result);
  15. _onDeviceFound?.Invoke(result);
  16. }
  17. public override void OnScanFailed(ScanFailure errorCode)
  18. {
  19. base.OnScanFailed(errorCode);
  20. _onScanStopped?.Invoke();
  21. throw new Exception($"Scan failed with error code: {errorCode}");
  22. }
  23. }