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.


Comments

  1. can you translate all these blocks into java code ?I went back to the old version. sketchware without.bluetooth, because my compiler started to produce non-existent errors in my old project , a large one , which I am working on .I will not adapt it to the new version , because I do not understand what he wants from me .

    ReplyDelete
    Replies
    1. The new class is:

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


      It is used in switch on check changed listner as:

      switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
      _setBluetooth(isChecked);
      }
      });

      Delete
    2. New class have a error, this error is
      ..........................
      syntax error on token"void_setBluetooth",=expected after this token
      ...........................

      Delete
    3. There should be space between void and _setBluetooth.

      Delete
    4. Now I space,but new have error.this error is
      ..................................
      syntax error on token "void", invalid AssignmentOperator
      ..................................

      Delete
  2. How to make a online earning app.
    Please find this

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. А можете подсказать , как кнопкой включить/отключить WiFi & включить/отключить GPS?

    ReplyDelete
  5. Please make send files using Bluetooth in SkeSketchw

    ReplyDelete
  6. How to send 2bits number using blBluetoo ?

    ReplyDelete
  7. Can you plz show tutorial to transfer data using bluetooth!

    ReplyDelete
  8. Hello Developer, I need a tutorial, How to Make an Arduino Remote Control App via Bluetooth n Wifi with Skethware, I like to see Tutorials on YouTube and learn what you upload, I just want to learn. Thank you, Dev ...

    ReplyDelete
  9. Hey develeode plz make a tutorial on Arduino control Like light on off,ultra Sonic sensor , wherever which can conttil the all tyoe arduArd sensors

    ReplyDelete
  10. Please make a tutorial how to send data through Bluetooth

    ReplyDelete

Post a Comment

Popular posts from this blog

A Flash Light App in Sketchware

Create Music Player app in Sketchware

Simple car racing android game in Sketchware

In Sketchware: How to create a calculator for any simple formula?

Firebase auth in Sketchware for Login Activity