65 lines
2.2 KiB
Dart
65 lines
2.2 KiB
Dart
import 'dart:convert';
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bluetooth_serial/flutter_bluetooth_serial.dart';
|
|
|
|
void main() => runApp(const AdbMeApp());
|
|
|
|
class AdbMeApp extends StatelessWidget {
|
|
const AdbMeApp({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) => const MaterialApp(home: PairPage());
|
|
}
|
|
|
|
class PairPage extends StatefulWidget {
|
|
const PairPage({super.key});
|
|
|
|
@override
|
|
State<PairPage> createState() => _PairPageState();
|
|
}
|
|
|
|
class _PairPageState extends State<PairPage> {
|
|
final _token = TextEditingController();
|
|
final _serial = TextEditingController();
|
|
String _result = 'Pair a laptop running scripts/adbme_bluetooth_listener.py';
|
|
|
|
Future<void> _status() async {
|
|
final devices = await FlutterBluetoothSerial.instance.getBondedDevices();
|
|
final laptop = await showDialog<BluetoothDevice>(
|
|
context: context,
|
|
builder: (context) => SimpleDialog(children: [
|
|
for (final device in devices) SimpleDialogOption(
|
|
onPressed: () => Navigator.pop(context, device),
|
|
child: Text(device.name ?? device.address),
|
|
),
|
|
]),
|
|
);
|
|
if (laptop == null) return;
|
|
final connection = await BluetoothConnection.toAddress(laptop.address);
|
|
connection.output.add(Uint8List.fromList(utf8.encode(jsonEncode({
|
|
'token': _token.text.trim(), 'action': 'status', 'serial': _serial.text.trim(),
|
|
}))));
|
|
await connection.output.allSent;
|
|
final reply = await connection.input!.first;
|
|
setState(() => _result = utf8.decode(reply));
|
|
await connection.close();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) => Scaffold(
|
|
appBar: AppBar(title: const Text('ADBMe')),
|
|
body: Padding(
|
|
padding: const EdgeInsets.all(16),
|
|
child: Column(children: [
|
|
TextField(controller: _token, obscureText: true, decoration: const InputDecoration(labelText: 'Laptop pairing token')),
|
|
TextField(controller: _serial, decoration: const InputDecoration(labelText: 'ADB serial')),
|
|
const SizedBox(height: 12),
|
|
ElevatedButton(onPressed: _status, child: const Text('Check device status')),
|
|
const SizedBox(height: 12),
|
|
SelectableText(_result),
|
|
]),
|
|
),
|
|
);
|
|
}
|