bluetooth_state_checker

The Bluetooth State checker for IOS to monitor the bluetooth state without showing OS related bluetooth dialog in IOS.

The package currently supports only IOS.

Usage/Examples

main.dart

import 'package:bluetooth_state_check/enum/bluetooth_state.dart';
import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:bluetooth_state_check/bluetooth_state_check.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final _bluetoothStateCheckPlugin = BluetoothStateCheck();
  late StreamSubscription _bluetoothStateStream;
  BluetoothState _bluetoothState = BluetoothState.unknown;

  @override
  void initState() {
    super.initState();

    //Future method
    _isTurnOnFuture();

    //Stream to listen
    _bluetoothStateStream = _bluetoothStateCheckPlugin.getBluetoothState().listen((state) {
      _bluetoothState = state;
      debugPrint('Bluetooth state : ${_bluetoothState.name}');
      setState(() {});
    });
  }

  @override
  void dispose() {
    super.dispose();
    _bluetoothStateStream.cancel();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> _isTurnOnFuture() async {
    bool? isTurnOn;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      isTurnOn = await _bluetoothStateCheckPlugin.isTurnOn();
    } on PlatformException {
      isTurnOn = null;
    }

    if (isTurnOn != null) {
      _bluetoothState = isTurnOn ? BluetoothState.on : BluetoothState.off;
    } else {
      _bluetoothState = BluetoothState.unknown;
    }

    debugPrint('isTurnOn : $isTurnOn');
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Bluetooth State Checker'),
        ),
        body: Center(
          child: Text('Bluetooth state : $_bluetoothState'),
        ),
      ),
    );
  }
}