Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ScaffoldMessenger working well
- // shows if Bluetooth is on or off
- import 'package:flutter/material.dart';
- import 'package:flutter_blue/flutter_blue.dart';
- import './SelectBondedDevicePage.dart';
- void main() => runApp(const MaterialApp(
- debugShowCheckedModeBanner: false,
- home: Home(),
- ));
- class Home extends StatelessWidget {
- const Home({Key? key}) : super(key: key);
- @override
- Widget build(BuildContext context) {
- FlutterBlue flutterBlue = FlutterBlue.instance;
- return Scaffold(
- appBar: AppBar(
- title: Wrap(
- direction: Axis.horizontal,
- crossAxisAlignment: WrapCrossAlignment.center,
- children: const <Widget>[
- Icon(
- Icons.bluetooth,
- //color : Colors.blue,
- ),
- Text(
- 'My BLE',
- //style : TextStyle(color : Colors.white),
- ),
- ],
- ),
- centerTitle: true,
- //backgroundColor: Colors.black,
- backgroundColor: Colors.white54,
- ),
- body: Center(
- child: Column(
- mainAxisAlignment: MainAxisAlignment.start,
- children: [
- Padding(
- padding: const EdgeInsets.all(15.0),
- child: ElevatedButton(
- onPressed: () async {
- bool isOn = await flutterBlue.isOn;
- // print(
- // 'you pressed button',
- // // style: TextStyle(
- // // color: Colors.red,
- // // ),
- // );
- // ScaffoldMessenger.of(context).showSnackBar(
- // const SnackBar(content: Text('you pressed button')));
- // bool isSupport = await flutterBlue.isAvailable;
- // bool isOn = await flutterBlue.isOn;
- if (isOn == true) {
- ScaffoldMessenger.of(context).showSnackBar(
- const SnackBar(content: Text('bluetooth is ON'))
- );
- // // Start scanning
- // flutterBlue.startScan(timeout: Duration(seconds: 10));
- // // Listen to scan results
- // var subscription =
- // flutterBlue.scanResults.listen((results) {
- // // do something with scan results
- // for (ScanResult r in results) {
- // // print(
- // // '${r.device.name} found! rssi: ${r.rssi}',
- // // // style: TextStyle(
- // // // color: Colors.red,
- // // // fontSize: 20,
- // // // fontWeight: FontWeight.bold,
- // // // ),
- // // );
- //
- // }
- // // Stop scanning
- // flutterBlue.stopScan();
- // });
- } else if (isOn == false) {
- // print(
- // 'Turn on Bluetooth',
- // // style: TextStyle(
- // // color: Colors.white,
- // // fontSize: 20,
- // // fontWeight: FontWeight.bold,
- // // ),
- // );
- ScaffoldMessenger.of(context).showSnackBar(
- const SnackBar(content: Text('bluetooth is OFF')));
- }
- // Stop scanning
- flutterBlue.stopScan();
- },
- child: Text('Connect to paired device to chat'),
- style: ElevatedButton.styleFrom(
- primary: Colors.grey,
- padding: EdgeInsets.symmetric(horizontal: 30, vertical: 20),
- ),
- ),
- ),
- ],
- ),
- ),
- backgroundColor: Colors.grey[800],
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement