ble get_it textfile (html stripped <> chars>
// example of ble using get_it plugin
////////////main.dart///////////////////
import 'package:ble_demo/app_model.dart';
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import './ble.dart';
GetIt getIt = GetIt.instance;
void main() {
getIt.registerSingleton(AppModelImplementation(),
signalsReady: true);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: MyHomePage());}}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();}
class _MyHomePageState extends State {
BLE ble = BLE();
@override
void initState() {
getIt.isReady()
.then((_) => getIt().addListener(update));
super.initState();
ble.init();}
@override
void dispose(){
getIt().removeListener(update);
super.dispose();}
update() => setState(() => {});
@override
Widget build(BuildContext ctx) {
var data = getIt().data;
return Scaffold(
appBar: AppBar(title: Text("ble with get it plugin")),
body: Text("received: $data", // show anything coming in from notify characteristic
style: TextStyle(color: Colors.black)),);}}
///////////ble.dart////////////////////////////////////////
import 'package:flutter_blue/flutter_blue.dart';
import './main.dart';
import 'dart:async';
import './app_model.dart';
class BLE {
BLE();
static BluetoothCharacteristic txChar;
static BluetoothCharacteristic notifyChar;
static String receivedText;
static final String baseUuid = "-b5a3-f393-e0a9-e50e24dcca9e";
final String serviceUuid = "6e400001" + baseUuid;
final String txCharUuid = "6e400002" + baseUuid;
final String notifyCharUuid = "6e400003" + baseUuid;
final String deviceName = "Adafruit Bluefruit LE";
FlutterBlue flutterBlue = FlutterBlue.instance;
StreamSubscription scanSubScription;
BluetoothDevice targetDevice;
init(){
scanSubScription = flutterBlue.scan().listen((scanResult) {
if (scanResult.device.name == deviceName) {
stopScan();
targetDevice = scanResult.device;
connectToDevice();
}
}, onDone: () => stopScan());}
stopScan() {
scanSubScription?.cancel();
scanSubScription = null;}
connectToDevice() async {
if (targetDevice == null) return;
await targetDevice.connect();
print("device connectd");
discoverServices();}
disconnectFromDevice() {
if (targetDevice == null) return;
targetDevice.disconnect();}
discoverServices() async {
print("discoverservices func");
if (targetDevice == null) return;
List services = await targetDevice.discoverServices();
services.forEach((service) {
if (service.uuid.toString() == serviceUuid) {
service.characteristics.forEach((characteristic) {
if (characteristic.uuid.toString() == notifyCharUuid) {
notifyChar = characteristic;
characteristic.setNotifyValue(true);
characteristic.value.listen((v){
if (v.isNotEmpty) {
getIt().setData(String.fromCharCodes(v));}});}
if (characteristic.uuid.toString() == txCharUuid) {
print("found txchar");
txChar = characteristic;}});}});}}
///////////app_model.dart
// example straight from get_it repository
import 'package:flutter/material.dart';
import 'main.dart';
abstract class AppModel extends ChangeNotifier {
void setData(String s);
String get data;
}
class AppModelImplementation extends AppModel {
String _data = "";
AppModelImplementation() {
/// lets pretend we have to do some async initilization
Future.delayed(Duration(seconds: 1)).then((_) => getIt.signalReady(this));}
@override
String get data => _data;
@override
void setData(String s) {
_data = s;
notifyListeners();}}