Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. class _BluetoothAppState extends State<BluetoothApp> {
  2.  
  3. @override
  4. void initState() {
  5. super.initState();
  6. bluetoothConnectionState();
  7. }
  8.  
  9. // Aqui nós vamos utilizar um callback async utilizando await
  10. Future<void> bluetoothConnectionState() async {
  11. List<BluetoothDevice> devices = [];
  12.  
  13. // Obtemos a lista de dispositivos pareados
  14. try {
  15. devices = await bluetooth.getBondedDevices();
  16. } on PlatformException {
  17. print("Error");
  18. }
  19.  
  20. // neste ponto verificamos se o bluetooth está conectado ou desconectado
  21. bluetooth.onStateChanged().listen((state) {
  22. switch (state) {
  23. case FlutterBluetoothSerial.CONNECTED:
  24. setState(() {
  25. _connected = true;
  26. _pressed = false;
  27. });
  28.  
  29. break;
  30.  
  31. case FlutterBluetoothSerial.DISCONNECTED:
  32. setState(() {
  33. _connected = false;
  34. _pressed = false;
  35. });
  36. break;
  37.  
  38. default:
  39. print(state);
  40. break;
  41. }
  42. });
  43.  
  44. if (!mounted) {
  45. return;
  46. }
  47.  
  48. setState(() {
  49. _devicesList = devices;
  50. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement