Create Bluetooth ON/OFF Switch


To create a Bluetooth ON/OFF switch in Sketchware android project follow the steps given below.

1. In VIEW area of your sketchware android project, insert a LinearH and inside it insert a Switch switch1.
For switch1 write text as 'Bluetooth', set width as match_parent, and set gravity as left.

2. Add a BluetoothConnect component bt.

3. Add onResume event and here use blocks to set the checked state of switch1 as per the state of bluetooth.
4. Create a more block setBluetooth<enable> and define it by putting following code in an add source directly block.

android.bluetooth.BluetoothAdapter bluetoothAdapter = android.bluetooth.BluetoothAdapter.getDefaultAdapter();
boolean isEnabled = bluetoothAdapter.isEnabled();
if (_enable && !isEnabled) {
bluetoothAdapter.enable();
} else if(!_enable && isEnabled) {
bluetoothAdapter.disable();
}

5. In switch1 onCheckChanged use block the setBluetooth Block to set the bluetooth on or off according to the checked state of switch1.

6. Save and run the project. Now on clicking the Switch, we can switch on or switch off the bluetooth in our device. Watch the videos below.