Advertisement
rajath_pai

aug6_doesntPrintScanRes

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