import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:widget_with_codeview/widget_with_codeview.dart'; import 'package:arrow_pad/arrow_pad.dart'; import 'package:flutter_reactive_ble/flutter_reactive_ble.dart'; import 'dart:async'; import 'dart:typed_data'; void main() {runApp(MyApp());} class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, theme: ThemeData.dark(), home: Scaffold( body: WidgetWithCodeView( child: SomeWidget(), filePath: 'lib/main.dart', codeLinkPrefix: 'https://google.com?q=', iconBackgroundColor: Colors.white, iconForegroundColor: Colors.pink, labelBackgroundColor: Theme.of(context).canvasColor, labelTextStyle: TextStyle(color: Theme.of(context).textTheme.bodyLarge!.color), showLabelText: true, )));}} class SomeWidget extends StatelessWidget{ @override Widget build(BuildContext context){ return Scaffold( appBar:AppBar(title: Text('app bar')), drawer: ListView(children:[ ListTile(title:Text('Home'),onTap:()=>Navigator.of(context).push(Home(1))), ListTile(title:Text('Ble'),onTap:()=>Navigator.of(context).push(Ble())), ListTile(title:Text('Exit'),onTap:(){})]), body: Center(child: Text('goto drawer home for help')));}} class Home extends MaterialPageRoute { Home(int id) : super( builder: (BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Home $id'), elevation: 1.0, ), body: Center( child: Text( ''' Demo flutter using to create new project flutter create project_name --empty to add plugin flutter pub add plugin_name flutter_reactive_blue get widget_with_codeview arrow_pad edit pubspec.yaml to include flutter: uses-material-design: true assets: - lib/main.dart on android setting give the app location permission ''' ), ),);},);} class Ble extends MaterialPageRoute { Ble() : super( builder: (BuildContext context) { String _value = ' '; List packet=[0,0]; late BleController ble; ble=Get.put(BleController()); double height = MediaQuery.of(context).size.height; double width = MediaQuery.of(context).size.width; return Scaffold(appBar: AppBar(title: Text('Ble')), body: Center(child: SingleChildScrollView(child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ ElevatedButton(child: Obx(()=>Text('${ble.status.value}')), onPressed:ble.connect), SizedBox(height:50), ArrowPad( padding: const EdgeInsets.all(8.0), height: height / 3, width: width / 2, arrowPadIconStyle: ArrowPadIconStyle.arrow, hoverColor: Colors.green, iconColor: const Color(0xFF631739), outerColor: const Color(0xFF86FC8A), clickTrigger: ClickTrigger.onTapUp, onPressed: (dir){ if (dir.toString().compareTo('right') == 0) { packet[1] = 0x72; ble.send(packet);} else if (dir.toString().compareTo('left') == 0) { packet[1] = 0x41; ble.send(packet);} else if (dir.toString().compareTo('up') == 0) { packet[1] = 0x75; ble.send(packet);} else if (dir.toString().compareTo('down') == 0) { packet[1] = 0x64; ble.send(packet);} }), SizedBox(height:50), ElevatedButton(child: Text('stop'), onPressed:(){packet[1] = 0x73; ble.send(packet);}), ]))) );},);} class BleController { final frb = FlutterReactiveBle(); late StreamSubscription c; late QualifiedCharacteristic tx; var status = 'connect to bluetooth'.obs; void send(val) async{ await frb.writeCharacteristicWithoutResponse(tx, value: val);} void connect() async { status.value = 'connecting...'; c = frb.connectToDevice(id: 'CC:33:31:CF:74:72').listen((state) { if (state.connectionState == DeviceConnectionState.connected) { status.value = 'connected!'; tx=QualifiedCharacteristic( serviceId: Uuid.parse("FFE0"), characteristicId: Uuid.parse("FFE1"), deviceId: 'CC:33:31:CF:74:72');}});}} String comment = ''' micropython code on the pico from machine import UART, Pin import time l=Pin(18,Pin.OUT) l1=Pin(19,Pin.OUT) l2=Pin(20,Pin.OUT) u=UART(0,baudrate=9600,tx=Pin(0),rx=Pin(1)) l.high() l1.high() l2.high() buf=bytearray(2) while 1: time.sleep_ms(50) l.high() l1.high() buf=u.read(2) if buf is not None: print(buf) if buf == b'\x00A': l.low() if buf == b'\x00d': l1.low() time.sleep_ms(50) ''';