Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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,
- ),
- Text(
- 'My BLE',
- ),
- ],
- ),
- centerTitle: true,
- 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;
- if (isOn == true) {
- ScaffoldMessenger.of(context).showSnackBar(
- SnackBar(content: Text('bluetooth is ON')),
- );
- print('STARTED SCANNING');
- flutterBlue.startScan(timeout: Duration(seconds: 10));
- print('started scanning and maybe 10 seconds have passed');
- var subscription = flutterBlue.scanResults.listen((results) {
- print('INSIDE SCAN RESULTS');
- for (ScanResult r in results) {
- print('INSIDE FOR LOOP');
- print('${r.device.name} found this and it works = ! rssi: ${r.rssi}');
- }
- });
- print('It will STOP scanning now');
- flutterBlue.stopScan();
- print('STOPPED SCANNING');
- } else if (isOn == false) {
- ScaffoldMessenger.of(context).showSnackBar(
- SnackBar(content: Text('bluetooth is OFF'))
- );
- }
- 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