This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dependencies: | |
flutter_bluetooth_serial: ^0.0.4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BluetoothApp extends StatefulWidget { | |
@override | |
_BluetoothAppState createState() => _BluetoothAppState(); | |
} | |
class _BluetoothAppState extends State<BluetoothApp> { | |
// Initializing a global key, as it would help us in showing a SnackBar later | |
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>(); | |
// Get the instance of the bluetooth | |
FlutterBluetoothSerial bluetooth = FlutterBluetoothSerial.instance; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class _BluetoothAppState extends State<BluetoothApp> { | |
... | |
@override | |
void initState() { | |
super.initState(); | |
bluetoothConnectionState(); | |
} | |
// We are using async callback for using await |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
key: _scaffoldKey, | |
appBar: AppBar( | |
title: Text("Flutter Bluetooth"), | |
backgroundColor: Colors.deepPurple, | |
), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
// Create the List of devices to be shown in Dropdown Menu | |
List<DropdownMenuItem<BluetoothDevice>> _getDeviceItems() { | |
List<DropdownMenuItem<BluetoothDevice>> items = []; | |
if (_devicesList.isEmpty) { | |
items.add(DropdownMenuItem( | |
child: Text('NONE'), | |
)); | |
} else { | |
_devicesList.forEach((device) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
// Method to connect to bluetooth | |
void _connect() { | |
if (_device == null) { | |
show('No device selected'); | |
} else { | |
bluetooth.isConnected.then((isConnected) { | |
if (!isConnected) { | |
bluetooth | |
.connect(_device) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
// Method to send message, | |
// for turning the bletooth device on | |
void _sendOnMessageToBluetooth() { | |
bluetooth.isConnected.then((isConnected) { | |
if (isConnected) { | |
bluetooth.write("1"); | |
show('Device Turned On'); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<manifest ... | |
<!-- Add this line (inside manifest tag) --> | |
xmlns:tools="http://schemas.android.com/tools"> | |
<!-- and this line (outside manifest tag) --> | |
<uses-sdk tools:overrideLibrary="io.github.edufolly.flutterbluetoothserial"/> | |
.... | |
</manifest> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.os.Bundle; | |
import android.support.design.widget.TabLayout; | |
import android.support.v4.view.ViewPager; | |
import android.support.v7.app.AppCompatActivity; | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); |
OlderNewer