Advertisement
rajath_pai

aug5_ON_or_OFF_toast

Aug 5th, 2021
1,095
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 4.05 KB | None | 0 0
  1. // ScaffoldMessenger working well
  2. // shows if Bluetooth is on or off
  3.  
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter_blue/flutter_blue.dart';
  6.  
  7. import './SelectBondedDevicePage.dart';
  8.  
  9. void main() => runApp(const MaterialApp(
  10.   debugShowCheckedModeBanner: false,
  11.   home: Home(),
  12. ));
  13.  
  14. class Home extends StatelessWidget {
  15.   const Home({Key? key}) : super(key: key);
  16.  
  17.   @override
  18.   Widget build(BuildContext context) {
  19.     FlutterBlue flutterBlue = FlutterBlue.instance;
  20.     return Scaffold(
  21.       appBar: AppBar(
  22.         title: Wrap(
  23.           direction: Axis.horizontal,
  24.           crossAxisAlignment: WrapCrossAlignment.center,
  25.           children: const <Widget>[
  26.             Icon(
  27.               Icons.bluetooth,
  28.               //color : Colors.blue,
  29.             ),
  30.             Text(
  31.               'My BLE',
  32.               //style : TextStyle(color : Colors.white),
  33.             ),
  34.           ],
  35.         ),
  36.         centerTitle: true,
  37.         //backgroundColor: Colors.black,
  38.         backgroundColor: Colors.white54,
  39.       ),
  40.       body: Center(
  41.         child: Column(
  42.           mainAxisAlignment: MainAxisAlignment.start,
  43.           children: [
  44.             Padding(
  45.               padding: const EdgeInsets.all(15.0),
  46.               child: ElevatedButton(
  47.                 onPressed: () async {
  48.                   bool isOn = await flutterBlue.isOn;
  49.                   // print(
  50.                   //       'you pressed button',
  51.                   //       // style: TextStyle(
  52.                   //       //   color: Colors.red,
  53.                   //       // ),
  54.                   //     );
  55.                   // ScaffoldMessenger.of(context).showSnackBar(
  56.                   //     const SnackBar(content: Text('you pressed button')));
  57.                   // bool isSupport = await flutterBlue.isAvailable;
  58.                   // bool isOn = await flutterBlue.isOn;
  59.                   if (isOn == true) {
  60.                     ScaffoldMessenger.of(context).showSnackBar(
  61.                         const SnackBar(content: Text('bluetooth is ON'))
  62.                     );
  63.                     // // Start scanning
  64.                     // flutterBlue.startScan(timeout: Duration(seconds: 10));
  65.                     // // Listen to scan results
  66.                     // var subscription =
  67.                     // flutterBlue.scanResults.listen((results) {
  68.                     //   // do something with scan results
  69.                     //   for (ScanResult r in results) {
  70.                     //     // print(
  71.                     //     //   '${r.device.name} found! rssi: ${r.rssi}',
  72.                     //     //   // style: TextStyle(
  73.                     //     //   //   color: Colors.red,
  74.                     //     //   //   fontSize: 20,
  75.                     //     //   //   fontWeight: FontWeight.bold,
  76.                     //     //   // ),
  77.                     //     // );
  78.                     //
  79.                     //   }
  80.                     //   // Stop scanning
  81.                     //   flutterBlue.stopScan();
  82.                     // });
  83.                   } else if (isOn == false) {
  84.                     //  print(
  85.                     //   'Turn on Bluetooth',
  86.                     //   // style: TextStyle(
  87.                     //   //   color: Colors.white,
  88.                     //   //   fontSize: 20,
  89.                     //   //   fontWeight: FontWeight.bold,
  90.                     //   // ),
  91.                     // );
  92.                     ScaffoldMessenger.of(context).showSnackBar(
  93.                         const SnackBar(content: Text('bluetooth is OFF')));
  94.                   }
  95.                   // Stop scanning
  96.                   flutterBlue.stopScan();
  97.                 },
  98.                 child: Text('Connect to paired device to chat'),
  99.                 style: ElevatedButton.styleFrom(
  100.                   primary: Colors.grey,
  101.                   padding: EdgeInsets.symmetric(horizontal: 30, vertical: 20),
  102.                 ),
  103.               ),
  104.             ),
  105.           ],
  106.         ),
  107.       ),
  108.       backgroundColor: Colors.grey[800],
  109.     );
  110.   }
  111. }
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement