|
- // /*
- // 蓝牙测试场景
- // */
- // import 'package:flutter/material.dart';
- // import 'package:flutter_blue/flutter_blue.dart';
-
- // class BluetoothScene extends StatefulWidget {
- // @override
- // _BluetoothSceneState createState() => _BluetoothSceneState();
- // }
-
- // class _BluetoothSceneState extends State<BluetoothScene> {
- // FlutterBlue flutterBlue = FlutterBlue.instance;
- // List<BluetoothDevice> devicesList = [];
- // BluetoothDevice? connectedDevice;
- // BluetoothState? bluetoothState;
-
- // @override
- // void initState() {
- // super.initState();
- // flutterBlue.state.listen((state) {
- // setState(() {
- // bluetoothState = state;
- // });
- // });
-
- // flutterBlue.scanResults.listen((results) {
- // setState(() {
- // devicesList = results
- // .where((result) => result.device.name.isNotEmpty)
- // .map((result) => result.device)
- // .toList();
- // });
- // });
- // }
-
- // void startScan() {
- // flutterBlue.startScan(timeout: Duration(seconds: 4));
- // }
-
- // void stopScan() {
- // flutterBlue.stopScan();
- // }
-
- // void connectToDevice(BluetoothDevice device) async {
- // await device.connect();
- // setState(() {
- // connectedDevice = device;
- // });
- // listenToDeviceEvents(device);
- // }
-
- // void listenToDeviceEvents(BluetoothDevice device) {
- // device.state.listen((state) {
- // if (state == BluetoothDeviceState.connected) {
- // print('Device connected');
- // } else if (state == BluetoothDeviceState.disconnected) {
- // print('Device disconnected');
- // }
- // });
-
- // device.discoverServices().then((services) {
- // services.forEach((service) {
- // service.characteristics.forEach((characteristic) {
- // characteristic.setNotifyValue(true);
- // characteristic.value.listen((value) {
- // print('Received value: $value');
- // });
- // });
- // });
- // });
- // }
-
- // @override
- // Widget build(BuildContext context) {
- // return Scaffold(
- // appBar: AppBar(title: Text('Flutter Bluetooth Demo')),
- // body: Column(
- // children: [
- // if (bluetoothState == BluetoothState.off)
- // Text('Bluetooth is off, please turn it on'),
- // if (bluetoothState == BluetoothState.on)
- // Column(
- // children: [
- // ElevatedButton(
- // onPressed: startScan,
- // child: Text('Start Scan'),
- // ),
- // ElevatedButton(
- // onPressed: stopScan,
- // child: Text('Stop Scan'),
- // ),
- // ListView.builder(
- // shrinkWrap: true,
- // itemCount: devicesList.length,
- // itemBuilder: (context, index) {
- // return ListTile(
- // title: Text(devicesList[index].name),
- // subtitle: Text(devicesList[index].id.toString()),
- // onTap: () {
- // connectToDevice(devicesList[index]);
- // },
- // );
- // },
- // ),
- // if (connectedDevice != null)
- // Text('Connected to: ${connectedDevice?.name}')
- // ],
- // ),
- // ],
- // ),
- // );
- // }
- // }
|