123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- import 'dart:async';
- import 'package:flutter/material.dart';
- import 'package:flutter_blue/flutter_blue.dart';
- void main() => runApp(MyApp());
- const String SERVICE_UUID = "a00e8cb8-1267-4e62-9e61-9c9d2c27f23d";
- const String CHARACTERISTIC_UUID = "a00e8cb9-1267-4e62-9e61-9c9d2c27f23d";
- class MyApp extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- title: 'LED Control',
- theme: ThemeData(primarySwatch: Colors.blue),
- home: MyHomePage(title: 'LED Control'),
- );
- }
- }
- class MyHomePage extends StatefulWidget {
- MyHomePage({Key? key, required this.title}) : super(key: key);
- final String title;
- @override
- _MyHomePageState createState() => _MyHomePageState();
- }
- class _MyHomePageState extends State<MyHomePage> {
- FlutterBlue flutterBlue = FlutterBlue.instance;
- StreamSubscription<ScanResult>? scanSubscription;
- BluetoothDevice? device;
- BluetoothCharacteristic? characteristic;
- bool connected = false;
- int brightness = 0;
- String input = "";
- @override
- void initState() {
- super.initState();
- startScan();
- }
- @override
- void dispose() {
- stopScan();
- super.dispose();
- }
- void startScan() {
- scanSubscription = flutterBlue.scan().listen((scanResult) {
- if (scanResult.device.name == "LED Control") {
- stopScan();
- setState(() {
- device = scanResult.device;
- });
- }
- });
- }
- void stopScan() {
- scanSubscription?.cancel();
- scanSubscription = null;
- }
- void connectToDevice() async {
- await device?.connect();
- List<BluetoothService> services = await device?.discoverServices();
- for (BluetoothService service in services) {
- if (service.uuid.toString() == SERVICE_UUID) {
- for (BluetoothCharacteristic c in service.characteristics) {
- if (c.uuid.toString() == CHARACTERISTIC_UUID) {
- characteristic = c;
- }
- }
- }
- }
- setState(() {
- connected = true;
- });
- }
- void disconnectFromDevice() async {
- await device?.disconnect();
- setState(() {
- connected = false;
- });
- }
- void setBrightness(int value) async {
- if (characteristic != null) {
- await characteristic?.write([value]);
- }
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: Text(widget.title),
- ),
- body: Center(
- child: Column(
- mainAxisAlignment: MainAxisAlignment.center,
- children: <Widget>[
- Text(connected ? "Connected" : "Disconnected"),
- Text("Brightness: $brightness"),
- Slider(
- value: brightness.toDouble(),
- min: 0,
- max: 100,
- onChanged: (double value) {
- setState(() {
- brightness = value.round();
- });
- },
- onChangeEnd: (double value) {
- setBrightness(value.round());
- },
- ),
- TextField(
- decoration: InputDecoration(labelText: 'Brightness value'),
- keyboardType: TextInputType.number,
- onChanged: (String value) {
- input = value;
- },
- onSubmitted: (String value) {
- setBrightness(int.parse(value));
- },
- ),
- ElevatedButton(
- child: Text
- connected ? "Disconnect" : "Connect",
- onPressed: connected ? disconnectFromDevice : connectToDevice,
- ),
- ],
- ),
- ),
- );
- }
- }
|