bluetooth_state_check 1.0.2 copy "bluetooth_state_check: ^1.0.2" to clipboard
bluetooth_state_check: ^1.0.2 copied to clipboard

Bluetooth State Checker

example/lib/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'),
        ),
      ),
    );
  }
}
0
likes
140
points
30
downloads

Publisher

unverified uploader

Weekly Downloads

Bluetooth State Checker

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on bluetooth_state_check