Skip to content
This repository was archived by the owner on Dec 9, 2025. It is now read-only.
This repository was archived by the owner on Dec 9, 2025. It is now read-only.

Add runtime BLE permission checks for Android #11

Description

@unforced

Problem

The app has BLE permissions declared in AndroidManifest.xml, but doesn't check at runtime if permissions are granted before scanning. This could cause crashes or unexpected behavior.

Current State: Permissions declared ✅ but no runtime checks ❌

Required Permissions (Android)

  • BLUETOOTH_SCAN
  • BLUETOOTH_CONNECT
  • ACCESS_FINE_LOCATION (required for BLE scanning on Android <12)

Suggested Implementation

// Add permission_handler to pubspec.yaml
dependencies:
  permission_handler: ^11.0.0

// In OmiBluetoothService
Future<bool> _checkBlePermissions() async {
  if (!Platform.isAndroid) {
    return true; // iOS handles via Info.plist
  }

  final scanGranted = await Permission.bluetoothScan.isGranted;
  final connectGranted = await Permission.bluetoothConnect.isGranted;
  
  if (!scanGranted || !connectGranted) {
    debugPrint('[OmiBluetoothService] BLE permissions not granted');
    
    // Request permissions
    final status = await [
      Permission.bluetoothScan,
      Permission.bluetoothConnect,
    ].request();
    
    return status[Permission.bluetoothScan]!.isGranted &&
           status[Permission.bluetoothConnect]!.isGranted;
  }
  
  return true;
}

// Call before scanning
Future<void> scanForDevices({...}) async {
  final hasPermissions = await _checkBlePermissions();
  if (!hasPermissions) {
    throw Exception('BLE permissions not granted');
  }
  
  // ... continue with scan
}

Benefits

  • Prevents crashes from missing permissions
  • Better UX with permission request dialogs
  • Follows Android best practices

Priority

High - Required for production Android app

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions