// // flutter bluetooth notify characteristic example // // change android/app/build.gradle // line 42 minSDKVersion to at least 19 // // pubspec yaml: flutter_ble_lib: ^2.2.4 // // troubleshooting things to try: // // 0) use nRF Connect app to find out info: // 01: device name // 02: services uuid // 03: characteristic uuid // 04: characteristic read | write | notify // // 1) make sure bluetooth is enable on phone // 2) make sure location is enable on phone // 3) in settings make sure this app has location permission // 4) turn on/off bluetooth on the phone // 5) on bluetooth scan setting on the phone, unpair the device // 6) unplug/plug the bluetooth device // 7) if app seems stuck, disconnect wait a few seconds // 8) start app again // 9) if problem persist kill app task then start again // 10) how to kill app in samsung galaxy s8 // 11) swipe first of 3 bottom bar // 12) close all app or just this app // 13) on linux use cutecom terminal - /dev/ttyUSB0 to test // 14) it's helpful if your bluetooth device has indication of connection // 15) on Adafruit I'm using, blinking red 2x means disconnected // // device assumptions - https://www.adafruit.com/product/2269 // name: "Adafruit Bluefruit LE" // service: "6e400001-b5a3-f393-e0a9-e50e24dcca9e" // notify characteristic: "6// // flutter bluetooth notify characteristic example // // change android/app/build.gradle // line 42 minSDKVersion to at least 19 // // pubspec yaml: flutter_ble_lib: ^2.2.4 // // troubleshooting things to try: // // 0) use nRF Connect app to find out info: // 01: device name // 02: services uuid // 03: characteristic uuid // 04: characteristic read | write | notify // // 1) make sure bluetooth is enable on phone // 2) make sure location is enable on phone // 3) in settings make sure this app has location permission // 4) turn on/off bluetooth on the phone // 5) on bluetooth scan setting on the phone, unpair the device // 6) unplug/plug the bluetooth device // 7) if app seems stuck, disconnect wait a few seconds // 8) start app again // 9) if problem persist kill app task then start again // 10) how to kill app in samsung galaxy s8 // 11) swipe first of 3 bottom bar // 12) close all app or just this app // 13) on linux use cutecom terminal - /dev/ttyUSB0 to test // 14) it's helpful if your bluetooth device has indication of connection // 15) on Adafruit I'm using, blinking red 2x means disconnected // // device assumptions - https://www.adafruit.com/product/2269 // name: "Adafruit Bluefruit LE" // service: "6e400001-b5a3-f393-e0a9-e50e24dcca9e" // notify characteristic: "6e400003-b5a3-f393-e0a9-e50e24dcca9e" // // note: this device uses UART service which is very slow // sometimes the characters are overrun so give it plenty // of time before sending data, but it's convenient to // debug your flutter bluetooth app // import 'dart:async'; //import 'dart:io'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_ble_lib/flutter_ble_lib.dart'; void main() {runApp(MyApp());} class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp(title: 'Flutter Demo', home: Home());}} class Home extends StatefulWidget { Home({Key key, this.title}) : super(key: key); final String title; @override _HomeState createState() => _HomeState();} class _HomeState extends State { static bool isConnected = false; BleManager ble = BleManager(); Peripheral peri; StreamSubscription streamSub; String dataReceived = "starting app..."; String status = ""; connectToBle() async { await ble.createClient(); ble.startPeripheralScan() .listen((scanResult) async { setState(() => status = "********** scanning for Adafruit Bluefruit LE"); if (scanResult.peripheral.name == "Adafruit Bluefruit LE") {//device name setState(() => status = "********** found device"); peri = scanResult.peripheral; await ble.stopPeripheralScan(); Timer(Duration(seconds: 1), (){}); isConnected = await peri.isConnected(); print("******** ${isConnected.toString}"); Timer(Duration(seconds: 1), (){}); setState(() => status = "************* connecting..."); if (!isConnected && peri != null) { await peri.connect(isAutoConnect: false);} else { setState(() => "********* error, restarts the program"); Timer(Duration(seconds: 3), () => SystemNavigator.pop(), ); } setState(() => status = "***** discovering services and chars"); await peri.discoverAllServicesAndCharacteristics(); setState(() => status = "****** setting monitor char"); streamSub = peri.monitorCharacteristic( //FlutterBleLib issue 447 "6e400001-b5a3-f393-e0a9-e50e24dcca9e", // service "6e400003-b5a3-f393-e0a9-e50e24dcca9e") // characteristic .listen((data) { setState(() => dataReceived = String.fromCharCodes(data.value)); setState(() => status = ""); }); setState(() => status = "**** ready to received !!!"); }});} // disconnect bluetooth then quit program disconnect() async { setState(() => status = "******* disconnecting now..."); await streamSub.cancel(); await peri.disconnectOrCancelConnection(); await ble.destroyClient(); setState(() => status = "####### exiting app now... ********"); Timer(Duration(seconds: 3), () => SystemNavigator.pop(), ); } @override void initState() { super.initState(); connectToBle();} @override Widget build(BuildContext context){ return Scaffold( body: Column( crossAxisAlignment: CrossAxisAlignment.start, children:[ SizedBox(height: 50.0), Text(" status: ---> $status", style: TextStyle(fontWeight: FontWeight.bold),), SizedBox(height: 20.0), Padding( padding: const EdgeInsets.all(20.0), child: Text("if app is stuck, press disconnect a few times, kill the app then restarts"), ), SizedBox(height: 50.0), Padding( padding: const EdgeInsets.all(30.0), child: Text("data received: $dataReceived"), ), Padding( padding: const EdgeInsets.all(20.0), child: RaisedButton( onPressed:() => disconnect(), child: Text('disconnect'), ), ), ]),);}}