Advertisement
rajath_pai

Flutter Bluetooth

Jul 16th, 2021
1,333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 16.37 KB | None | 0 0
  1. // For performing some operations asynchronously
  2. import 'dart:async';
  3. import 'dart:convert';
  4.  
  5. // For using PlatformException
  6. import 'package:flutter/services.dart';
  7.  
  8. import 'package:flutter/material.dart';
  9. import 'package:flutter_bluetooth_serial/flutter_bluetooth_serial.dart';
  10.  
  11. void main() => runApp(MyApp());
  12.  
  13. class MyApp extends StatelessWidget {
  14.   @override
  15.   Widget build(BuildContext context) {
  16.     return MaterialApp(
  17.       title: 'Flutter Demo',
  18.       theme: ThemeData(
  19.         primarySwatch: Colors.blue,
  20.       ),
  21.       home: BluetoothApp(),
  22.     );
  23.   }
  24. }
  25.  
  26. class BluetoothApp extends StatefulWidget {
  27.   @override
  28.   _BluetoothAppState createState() => _BluetoothAppState();
  29. }
  30.  
  31. class _BluetoothAppState extends State<BluetoothApp> {
  32.   // Initializing the Bluetooth connection state to be unknown
  33.   BluetoothState _bluetoothState = BluetoothState.UNKNOWN;
  34.   // Initializing a global key, as it would help us in showing a SnackBar later
  35.   final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
  36.   // Get the instance of the Bluetooth
  37.   FlutterBluetoothSerial _bluetooth = FlutterBluetoothSerial.instance;
  38.   // Track the Bluetooth connection with the remote device
  39.   BluetoothConnection connection;
  40.  
  41.   int _deviceState;
  42.  
  43.   bool isDisconnecting = false;
  44.  
  45.   Map<String, Color> colors = {
  46.     'onBorderColor': Colors.green,
  47.     'offBorderColor': Colors.red,
  48.     'neutralBorderColor': Colors.transparent,
  49.     'onTextColor': Colors.green[700],
  50.     'offTextColor': Colors.red[700],
  51.     'neutralTextColor': Colors.blue,
  52.   };
  53.  
  54.   // To track whether the device is still connected to Bluetooth
  55.   bool get isConnected => connection != null && connection.isConnected;
  56.  
  57.   // Define some variables, which will be required later
  58.   List<BluetoothDevice> _devicesList = [];
  59.   BluetoothDevice _device;
  60.   bool _connected = false;
  61.   bool _isButtonUnavailable = false;
  62.  
  63.   @override
  64.   void initState() {
  65.     super.initState();
  66.  
  67.     // Get current state
  68.     FlutterBluetoothSerial.instance.state.then((state) {
  69.       setState(() {
  70.         _bluetoothState = state;
  71.       });
  72.     });
  73.  
  74.     _deviceState = 0; // neutral
  75.  
  76.     // If the bluetooth of the device is not enabled,
  77.     // then request permission to turn on bluetooth
  78.     // as the app starts up
  79.     enableBluetooth();
  80.  
  81.     // Listen for further state changes
  82.     FlutterBluetoothSerial.instance
  83.         .onStateChanged()
  84.         .listen((BluetoothState state) {
  85.       setState(() {
  86.         _bluetoothState = state;
  87.         if (_bluetoothState == BluetoothState.STATE_OFF) {
  88.           _isButtonUnavailable = true;
  89.         }
  90.         getPairedDevices();
  91.       });
  92.     });
  93.   }
  94.  
  95.   @override
  96.   void dispose() {
  97.     // Avoid memory leak and disconnect
  98.     if (isConnected) {
  99.       isDisconnecting = true;
  100.       connection.dispose();
  101.       connection = null;
  102.     }
  103.  
  104.     super.dispose();
  105.   }
  106.  
  107.   // Request Bluetooth permission from the user
  108.   Future<void> enableBluetooth() async {
  109.     // Retrieving the current Bluetooth state
  110.     _bluetoothState = await FlutterBluetoothSerial.instance.state;
  111.  
  112.     // If the bluetooth is off, then turn it on first
  113.     // and then retrieve the devices that are paired.
  114.     if (_bluetoothState == BluetoothState.STATE_OFF) {
  115.       await FlutterBluetoothSerial.instance.requestEnable();
  116.       await getPairedDevices();
  117.       return true;
  118.     } else {
  119.       await getPairedDevices();
  120.     }
  121.     return false;
  122.   }
  123.  
  124.   // For retrieving and storing the paired devices
  125.   // in a list.
  126.   Future<void> getPairedDevices() async {
  127.     List<BluetoothDevice> devices = [];
  128.  
  129.     // To get the list of paired devices
  130.     try {
  131.       devices = await _bluetooth.getBondedDevices();
  132.     } on PlatformException {
  133.       print("Error");
  134.     }
  135.  
  136.     // It is an error to call [setState] unless [mounted] is true.
  137.     if (!mounted) {
  138.       return;
  139.     }
  140.  
  141.     // Store the [devices] list in the [_devicesList] for accessing
  142.     // the list outside this class
  143.     setState(() {
  144.       _devicesList = devices;
  145.     });
  146.   }
  147.  
  148.   // Now, its time to build the UI
  149.   @override
  150.   Widget build(BuildContext context) {
  151.     return MaterialApp(
  152.       home: Scaffold(
  153.         key: _scaffoldKey,
  154.         appBar: AppBar(
  155.           title: Text("Flutter Bluetooth"),
  156.           backgroundColor: Colors.deepPurple,
  157.           actions: <Widget>[
  158.             FlatButton.icon(
  159.               icon: Icon(
  160.                 Icons.refresh,
  161.                 color: Colors.white,
  162.               ),
  163.               label: Text(
  164.                 "Refresh",
  165.                 style: TextStyle(
  166.                   color: Colors.white,
  167.                 ),
  168.               ),
  169.               shape: RoundedRectangleBorder(
  170.                 borderRadius: BorderRadius.circular(30),
  171.               ),
  172.               splashColor: Colors.deepPurple,
  173.               onPressed: () async {
  174.                 // So, that when new devices are paired
  175.                 // while the app is running, user can refresh
  176.                 // the paired devices list.
  177.                 await getPairedDevices().then((_) {
  178.                   show('Device list refreshed');
  179.                 });
  180.               },
  181.             ),
  182.           ],
  183.         ),
  184.         body: Container(
  185.           child: Column(
  186.             mainAxisSize: MainAxisSize.max,
  187.             children: <Widget>[
  188.               Visibility(
  189.                 visible: _isButtonUnavailable &&
  190.                     _bluetoothState == BluetoothState.STATE_ON,
  191.                 child: LinearProgressIndicator(
  192.                   backgroundColor: Colors.yellow,
  193.                   valueColor: AlwaysStoppedAnimation<Color>(Colors.red),
  194.                 ),
  195.               ),
  196.               Padding(
  197.                 padding: const EdgeInsets.all(10),
  198.                 child: Row(
  199.                   mainAxisAlignment: MainAxisAlignment.start,
  200.                   children: <Widget>[
  201.                     Expanded(
  202.                       child: Text(
  203.                         'Enable Bluetooth',
  204.                         style: TextStyle(
  205.                           color: Colors.black,
  206.                           fontSize: 16,
  207.                         ),
  208.                       ),
  209.                     ),
  210.                     Switch(
  211.                       value: _bluetoothState.isEnabled,
  212.                       onChanged: (bool value) {
  213.                         future() async {
  214.                           if (value) {
  215.                             await FlutterBluetoothSerial.instance
  216.                                 .requestEnable();
  217.                           } else {
  218.                             await FlutterBluetoothSerial.instance
  219.                                 .requestDisable();
  220.                           }
  221.  
  222.                           await getPairedDevices();
  223.                           _isButtonUnavailable = false;
  224.  
  225.                           if (_connected) {
  226.                             _disconnect();
  227.                           }
  228.                         }
  229.  
  230.                         future().then((_) {
  231.                           setState(() {});
  232.                         });
  233.                       },
  234.                     )
  235.                   ],
  236.                 ),
  237.               ),
  238.               Stack(
  239.                 children: <Widget>[
  240.                   Column(
  241.                     children: <Widget>[
  242.                       Padding(
  243.                         padding: const EdgeInsets.only(top: 10),
  244.                         child: Text(
  245.                           "PAIRED DEVICES",
  246.                           style: TextStyle(fontSize: 24, color: Colors.blue),
  247.                           textAlign: TextAlign.center,
  248.                         ),
  249.                       ),
  250.                       Padding(
  251.                         padding: const EdgeInsets.all(8.0),
  252.                         child: Row(
  253.                           mainAxisAlignment: MainAxisAlignment.spaceBetween,
  254.                           children: <Widget>[
  255.                             Text(
  256.                               'Device:',
  257.                               style: TextStyle(
  258.                                 fontWeight: FontWeight.bold,
  259.                               ),
  260.                             ),
  261.                             DropdownButton(
  262.                               items: _getDeviceItems(),
  263.                               onChanged: (value) =>
  264.                                   setState(() => _device = value),
  265.                               value: _devicesList.isNotEmpty ? _device : null,
  266.                             ),
  267.                             RaisedButton(
  268.                               onPressed: _isButtonUnavailable
  269.                                   ? null
  270.                                   : _connected ? _disconnect : _connect,
  271.                               child:
  272.                                   Text(_connected ? 'Disconnect' : 'Connect'),
  273.                             ),
  274.                           ],
  275.                         ),
  276.                       ),
  277.                       Padding(
  278.                         padding: const EdgeInsets.all(16.0),
  279.                         child: Card(
  280.                           shape: RoundedRectangleBorder(
  281.                             side: new BorderSide(
  282.                               color: _deviceState == 0
  283.                                   ? colors['neutralBorderColor']
  284.                                   : _deviceState == 1
  285.                                       ? colors['onBorderColor']
  286.                                       : colors['offBorderColor'],
  287.                               width: 3,
  288.                             ),
  289.                             borderRadius: BorderRadius.circular(4.0),
  290.                           ),
  291.                           elevation: _deviceState == 0 ? 4 : 0,
  292.                           child: Padding(
  293.                             padding: const EdgeInsets.all(8.0),
  294.                             child: Row(
  295.                               children: <Widget>[
  296.                                 Expanded(
  297.                                   child: Text(
  298.                                     "DEVICE 1",
  299.                                     style: TextStyle(
  300.                                       fontSize: 20,
  301.                                       color: _deviceState == 0
  302.                                           ? colors['neutralTextColor']
  303.                                           : _deviceState == 1
  304.                                               ? colors['onTextColor']
  305.                                               : colors['offTextColor'],
  306.                                     ),
  307.                                   ),
  308.                                 ),
  309.                                 FlatButton(
  310.                                   onPressed: _connected
  311.                                       ? _sendOnMessageToBluetooth
  312.                                       : null,
  313.                                   child: Text("ON"),
  314.                                 ),
  315.                                 FlatButton(
  316.                                   onPressed: _connected
  317.                                       ? _sendOffMessageToBluetooth
  318.                                       : null,
  319.                                   child: Text("OFF"),
  320.                                 ),
  321.                               ],
  322.                             ),
  323.                           ),
  324.                         ),
  325.                       ),
  326.                     ],
  327.                   ),
  328.                   Container(
  329.                     color: Colors.blue,
  330.                   ),
  331.                 ],
  332.               ),
  333.               Expanded(
  334.                 child: Padding(
  335.                   padding: const EdgeInsets.all(20),
  336.                   child: Center(
  337.                     child: Column(
  338.                       mainAxisAlignment: MainAxisAlignment.center,
  339.                       children: <Widget>[
  340.                         Text(
  341.                           "NOTE: If you cannot find the device in the list, please pair the device by going to the bluetooth settings",
  342.                           style: TextStyle(
  343.                             fontSize: 15,
  344.                             fontWeight: FontWeight.bold,
  345.                             color: Colors.red,
  346.                           ),
  347.                         ),
  348.                         SizedBox(height: 15),
  349.                         RaisedButton(
  350.                           elevation: 2,
  351.                           child: Text("Bluetooth Settings"),
  352.                           onPressed: () {
  353.                             FlutterBluetoothSerial.instance.openSettings();
  354.                           },
  355.                         ),
  356.                       ],
  357.                     ),
  358.                   ),
  359.                 ),
  360.               )
  361.             ],
  362.           ),
  363.         ),
  364.       ),
  365.     );
  366.   }
  367.  
  368.   // Create the List of devices to be shown in Dropdown Menu
  369.   List<DropdownMenuItem<BluetoothDevice>> _getDeviceItems() {
  370.     List<DropdownMenuItem<BluetoothDevice>> items = [];
  371.     if (_devicesList.isEmpty) {
  372.       items.add(DropdownMenuItem(
  373.         child: Text('NONE'),
  374.       ));
  375.     } else {
  376.       _devicesList.forEach((device) {
  377.         items.add(DropdownMenuItem(
  378.           child: Text(device.name),
  379.           value: device,
  380.         ));
  381.       });
  382.     }
  383.     return items;
  384.   }
  385.  
  386.   // Method to connect to bluetooth
  387.   void _connect() async {
  388.     setState(() {
  389.       _isButtonUnavailable = true;
  390.     });
  391.     if (_device == null) {
  392.       show('No device selected');
  393.     } else {
  394.       if (!isConnected) {
  395.         await BluetoothConnection.toAddress(_device.address)
  396.             .then((_connection) {
  397.           print('Connected to the device');
  398.           connection = _connection;
  399.           setState(() {
  400.             _connected = true;
  401.           });
  402.  
  403.           connection.input.listen(null).onDone(() {
  404.             if (isDisconnecting) {
  405.               print('Disconnecting locally!');
  406.             } else {
  407.               print('Disconnected remotely!');
  408.             }
  409.             if (this.mounted) {
  410.               setState(() {});
  411.             }
  412.           });
  413.         }).catchError((error) {
  414.           print('Cannot connect, exception occurred');
  415.           print(error);
  416.         });
  417.         show('Device connected');
  418.  
  419.         setState(() => _isButtonUnavailable = false);
  420.       }
  421.     }
  422.   }
  423.  
  424.   // void _onDataReceived(Uint8List data) {
  425.   //   // Allocate buffer for parsed data
  426.   //   int backspacesCounter = 0;
  427.   //   data.forEach((byte) {
  428.   //     if (byte == 8 || byte == 127) {
  429.   //       backspacesCounter++;
  430.   //     }
  431.   //   });
  432.   //   Uint8List buffer = Uint8List(data.length - backspacesCounter);
  433.   //   int bufferIndex = buffer.length;
  434.  
  435.   //   // Apply backspace control character
  436.   //   backspacesCounter = 0;
  437.   //   for (int i = data.length - 1; i >= 0; i--) {
  438.   //     if (data[i] == 8 || data[i] == 127) {
  439.   //       backspacesCounter++;
  440.   //     } else {
  441.   //       if (backspacesCounter > 0) {
  442.   //         backspacesCounter--;
  443.   //       } else {
  444.   //         buffer[--bufferIndex] = data[i];
  445.   //       }
  446.   //     }
  447.   //   }
  448.   // }
  449.  
  450.   // Method to disconnect bluetooth
  451.   void _disconnect() async {
  452.     setState(() {
  453.       _isButtonUnavailable = true;
  454.       _deviceState = 0;
  455.     });
  456.  
  457.     await connection.close();
  458.     show('Device disconnected');
  459.     if (!connection.isConnected) {
  460.       setState(() {
  461.         _connected = false;
  462.         _isButtonUnavailable = false;
  463.       });
  464.     }
  465.   }
  466.  
  467.   // Method to send message,
  468.   // for turning the Bluetooth device on
  469.   void _sendOnMessageToBluetooth() async {
  470.     connection.output.add(utf8.encode("1" + "\r\n"));
  471.     await connection.output.allSent;
  472.     show('Device Turned On');
  473.     setState(() {
  474.       _deviceState = 1; // device on
  475.     });
  476.   }
  477.  
  478.   // Method to send message,
  479.   // for turning the Bluetooth device off
  480.   void _sendOffMessageToBluetooth() async {
  481.     connection.output.add(utf8.encode("0" + "\r\n"));
  482.     await connection.output.allSent;
  483.     show('Device Turned Off');
  484.     setState(() {
  485.       _deviceState = -1; // device off
  486.     });
  487.   }
  488.  
  489.   // Method to show a Snackbar,
  490.   // taking message as the text
  491.   Future show(
  492.     String message, {
  493.     Duration duration: const Duration(seconds: 3),
  494.   }) async {
  495.     await new Future.delayed(new Duration(milliseconds: 100));
  496.     _scaffoldKey.currentState.showSnackBar(
  497.       new SnackBar(
  498.         content: new Text(
  499.           message,
  500.         ),
  501.         duration: duration,
  502.       ),
  503.     );
  504.   }
  505. }
  506.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement