How can i turn off the bluetooh with ble manager?

Viewed 97

I am using ble manager to turn the bluetooth on and off but when I move the switch it remains in the initial state and does not execute the if conditional.

body:Switch(value: state, 
      onChanged: (bool s)async{
          state=s;
          if(s==true){
              print("is true");
                         await Future.delayed(Duration(seconds:3));
          initState();
            _bleManager.enableRadio();
          }else{
          print("is false");
          _bleManager.disableRadio();
          }
            }
      )
1 Answers

Based on the code provided, you need to make some changes:

1. For changing the state variable, you need to call setState, so instead of state=s, use this:

setState(() {
  state = s;
  });

2. Don't call initState() inside the build function.

Switch(value: state,
          onChanged: (bool s)async{
            setState(() {
              state = s;
            });
            if (s == true) {
              print("is true");
              await Future.delayed(Duration(seconds:3));
            _bleManager.enableRadio();
            } else{
              print("is false");
            _bleManager.disableRadio();
            }
          }
    ),
Related