Skip to content

Instantly share code, notes, and snippets.

View sbis04's full-sized avatar

Souvik Biswas sbis04

View GitHub Profile
@sbis04
sbis04 / pubspec.yaml
Created March 4, 2019 16:38
Adding "flutter_bluetooth_serial" dependency
dependencies:
flutter_bluetooth_serial: ^0.0.4
@sbis04
sbis04 / main.dart
Created March 4, 2019 16:49
Base code
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(
@sbis04
sbis04 / main.dart
Created March 4, 2019 17:00
BluetoothApp class
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;
@sbis04
sbis04 / main.dart
Created March 4, 2019 17:14
Bluetooth connection
class _BluetoothAppState extends State<BluetoothApp> {
...
@override
void initState() {
super.initState();
bluetoothConnectionState();
}
// We are using async callback for using await
@sbis04
sbis04 / main.dart
Last active March 4, 2019 17:49
UI Part
...
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
key: _scaffoldKey,
appBar: AppBar(
title: Text("Flutter Bluetooth"),
backgroundColor: Colors.deepPurple,
),
@sbis04
sbis04 / main.dart
Created March 4, 2019 17:53
_getDeviceItems( ) method
...
// 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) {
@sbis04
sbis04 / main.dart
Created March 4, 2019 17:58
Implementing _connect, _disconnect and show methods
...
// Method to connect to bluetooth
void _connect() {
if (_device == null) {
show('No device selected');
} else {
bluetooth.isConnected.then((isConnected) {
if (!isConnected) {
bluetooth
.connect(_device)
@sbis04
sbis04 / main.dart
Created March 4, 2019 18:01
Sending message to Bluetooth Module
...
// 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');
}
});
@sbis04
sbis04 / AndroidManifest.xml
Last active March 5, 2019 08:52
overriding the error in manifest
<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>
@sbis04
sbis04 / MainActivity.java
Created April 8, 2019 08:11
Using Tabbed Layout with ViewPager
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);