gpt_flutter_控制蓝牙.dart 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_blue/flutter_blue.dart';
  4. void main() => runApp(MyApp());
  5. const String SERVICE_UUID = "a00e8cb8-1267-4e62-9e61-9c9d2c27f23d";
  6. const String CHARACTERISTIC_UUID = "a00e8cb9-1267-4e62-9e61-9c9d2c27f23d";
  7. class MyApp extends StatelessWidget {
  8. @override
  9. Widget build(BuildContext context) {
  10. return MaterialApp(
  11. title: 'LED Control',
  12. theme: ThemeData(primarySwatch: Colors.blue),
  13. home: MyHomePage(title: 'LED Control'),
  14. );
  15. }
  16. }
  17. class MyHomePage extends StatefulWidget {
  18. MyHomePage({Key? key, required this.title}) : super(key: key);
  19. final String title;
  20. @override
  21. _MyHomePageState createState() => _MyHomePageState();
  22. }
  23. class _MyHomePageState extends State<MyHomePage> {
  24. FlutterBlue flutterBlue = FlutterBlue.instance;
  25. StreamSubscription<ScanResult>? scanSubscription;
  26. BluetoothDevice? device;
  27. BluetoothCharacteristic? characteristic;
  28. bool connected = false;
  29. int brightness = 0;
  30. String input = "";
  31. @override
  32. void initState() {
  33. super.initState();
  34. startScan();
  35. }
  36. @override
  37. void dispose() {
  38. stopScan();
  39. super.dispose();
  40. }
  41. void startScan() {
  42. scanSubscription = flutterBlue.scan().listen((scanResult) {
  43. if (scanResult.device.name == "LED Control") {
  44. stopScan();
  45. setState(() {
  46. device = scanResult.device;
  47. });
  48. }
  49. });
  50. }
  51. void stopScan() {
  52. scanSubscription?.cancel();
  53. scanSubscription = null;
  54. }
  55. void connectToDevice() async {
  56. await device?.connect();
  57. List<BluetoothService> services = await device?.discoverServices();
  58. for (BluetoothService service in services) {
  59. if (service.uuid.toString() == SERVICE_UUID) {
  60. for (BluetoothCharacteristic c in service.characteristics) {
  61. if (c.uuid.toString() == CHARACTERISTIC_UUID) {
  62. characteristic = c;
  63. }
  64. }
  65. }
  66. }
  67. setState(() {
  68. connected = true;
  69. });
  70. }
  71. void disconnectFromDevice() async {
  72. await device?.disconnect();
  73. setState(() {
  74. connected = false;
  75. });
  76. }
  77. void setBrightness(int value) async {
  78. if (characteristic != null) {
  79. await characteristic?.write([value]);
  80. }
  81. }
  82. @override
  83. Widget build(BuildContext context) {
  84. return Scaffold(
  85. appBar: AppBar(
  86. title: Text(widget.title),
  87. ),
  88. body: Center(
  89. child: Column(
  90. mainAxisAlignment: MainAxisAlignment.center,
  91. children: <Widget>[
  92. Text(connected ? "Connected" : "Disconnected"),
  93. Text("Brightness: $brightness"),
  94. Slider(
  95. value: brightness.toDouble(),
  96. min: 0,
  97. max: 100,
  98. onChanged: (double value) {
  99. setState(() {
  100. brightness = value.round();
  101. });
  102. },
  103. onChangeEnd: (double value) {
  104. setBrightness(value.round());
  105. },
  106. ),
  107. TextField(
  108. decoration: InputDecoration(labelText: 'Brightness value'),
  109. keyboardType: TextInputType.number,
  110. onChanged: (String value) {
  111. input = value;
  112. },
  113. onSubmitted: (String value) {
  114. setBrightness(int.parse(value));
  115. },
  116. ),
  117. ElevatedButton(
  118. child: Text
  119. connected ? "Disconnect" : "Connect",
  120. onPressed: connected ? disconnectFromDevice : connectToDevice,
  121. ),
  122. ],
  123. ),
  124. ),
  125. );
  126. }
  127. }