Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class _BluetoothAppState extends State<BluetoothApp> {
- @override
- void initState() {
- super.initState();
- bluetoothConnectionState();
- }
- // Aqui nós vamos utilizar um callback async utilizando await
- Future<void> bluetoothConnectionState() async {
- List<BluetoothDevice> devices = [];
- // Obtemos a lista de dispositivos pareados
- try {
- devices = await bluetooth.getBondedDevices();
- } on PlatformException {
- print("Error");
- }
- // neste ponto verificamos se o bluetooth está conectado ou desconectado
- bluetooth.onStateChanged().listen((state) {
- switch (state) {
- case FlutterBluetoothSerial.CONNECTED:
- setState(() {
- _connected = true;
- _pressed = false;
- });
- break;
- case FlutterBluetoothSerial.DISCONNECTED:
- setState(() {
- _connected = false;
- _pressed = false;
- });
- break;
- default:
- print(state);
- break;
- }
- });
- if (!mounted) {
- return;
- }
- setState(() {
- _devicesList = devices;
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement