scan.dart 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import 'dart:async';
  2. import 'dart:ffi';
  3. import 'dart:typed_data';
  4. import 'dart:convert';
  5. import 'package:flutter/material.dart';
  6. import 'package:flutter_bluetooth_serial/flutter_bluetooth_serial.dart';
  7. class ConnectionPage extends StatefulWidget {
  8. const ConnectionPage({super.key});
  9. @override
  10. State<ConnectionPage> createState() => _ConnectionPage();
  11. }
  12. void printDiscovered() {}
  13. List<BluetoothDiscoveryResult> scanBluetoothDevice(
  14. List<BluetoothDiscoveryResult> results) {
  15. print("Scanning started!");
  16. FlutterBluetoothSerial.instance.startDiscovery().listen((r) {
  17. print(r.device.address);
  18. final existingIndex = results
  19. .indexWhere((element) => element.device.address == r.device.address);
  20. if (existingIndex >= 0)
  21. results[existingIndex] = r;
  22. else
  23. results.add(r);
  24. });
  25. return results;
  26. }
  27. class _ConnectionPage extends State<ConnectionPage> {
  28. late BluetoothConnection connection;
  29. bool _isConnected = false;
  30. var _latestDiscoveredId = "Nothing Yet";
  31. @override
  32. Widget build(BuildContext context) {
  33. return Scaffold(
  34. appBar: AppBar(
  35. title: const Text('Second Route'),
  36. backgroundColor: Colors.blueGrey,
  37. ),
  38. body: Column(
  39. children: [
  40. Center(
  41. child: ElevatedButton(
  42. onPressed: () {
  43. if (_isConnected && connection.isConnected) {
  44. connection.close();
  45. print('Connection Closed');
  46. _isConnected = false;
  47. }
  48. },
  49. child: const Text('Disconnect Bluetooth'),
  50. ),
  51. ),
  52. Center(
  53. child: ElevatedButton(
  54. onPressed: () async {
  55. try {
  56. connection =
  57. await BluetoothConnection.toAddress("98:D3:11:FC:39:EC");
  58. print('Connected to the device');
  59. _isConnected = true;
  60. connection.input!.listen((Uint8List data) {
  61. print(data);
  62. });
  63. } catch (ex) {
  64. print(ex);
  65. }
  66. },
  67. child: const Text('Connect Bluetooth'),
  68. ),
  69. ),
  70. Center(
  71. child: ElevatedButton(
  72. onPressed: () {
  73. FlutterBluetoothSerial.instance
  74. .getBondedDevices()
  75. .then((List<BluetoothDevice> bondedDevices) {
  76. _latestDiscoveredId = "";
  77. for (var device in bondedDevices) {
  78. print(device.address + " / " + device.name!);
  79. setState(() {
  80. _latestDiscoveredId +=
  81. "\n" + device.address + " / " + device.name!;
  82. });
  83. }
  84. });
  85. },
  86. child: const Text('Select Bluetooth'),
  87. ),
  88. ),
  89. Text(
  90. //Show the latest found Bluetooth addr
  91. _latestDiscoveredId),
  92. Center(
  93. child: ElevatedButton(
  94. onPressed: () {
  95. Navigator.pop(context);
  96. },
  97. child: const Text('Go back!'),
  98. ),
  99. )
  100. ],
  101. ),
  102. );
  103. }
  104. }