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
    5. This comment has been removed by the author.

      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. 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
  8. 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
  9. Please make a tutorial how to send data through Bluetooth

    ReplyDelete
  10. What does mean the message "read failed, socket might closed or timeout, read ret: - 1". How to fix it?

    ReplyDelete
    Replies
    1. That is some problem with uuid.
      * Create a String variable UUID.
      * Then use these codes on ListView item click or Bluetooth address click:

      final android.bluetooth.BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(listmap.get((int)_position).get("address").toString());
      final ParcelUuid[] uids;

      if (android.os.Build.VERSION.SDK_INT >= 15){
      uids = device.getUuids();
      UUID = uids[0].toString();
      }
      else {
      try {
      Class cl = Class.forName("android.bluetooth.BluetoothDevice");
      Class[] params = {};
      java.lang.reflect.Method method = cl.getMethod("getUuids", params); Object[] args = {};
      uids = (ParcelUuid[])method.invoke(device, args);
      UUID = uids[0].toString();
      } catch (Exception e) {
      showMessage("Activation of getUuids() failed: " + e); }

      }

      Then use block
      BluetoothConnect start connection with uuid UUID and address ..... with tag ...

      Delete
  11. mBluetoothAdapter cannot be resolved this message is coming

    ReplyDelete
    Replies
    1. The name of bluetooth adapter I used in codes is bluetoothAdapter. So change 'mBluetoothAdapter' to 'bluetoothAdapter' or vice versa.

      Delete
    2. android.bluetooth.BluetoothAdapter mBluetoothAdapter = android.bluetooth.BluetoothAdapter.getDefaultAdapter();

      You can add this line.

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

      Delete
    4. Actually the error comes is
      "read failed, socket might closed or timeout, read ret: - 1"
      Then i put the above code given by you containing UUID. But it gives error message saying mBluetoothAdapter cannot be resolved.

      Delete
    5. Add this before the code

      android.bluetooth.BluetoothAdapter mBluetoothAdapter = android.bluetooth.BluetoothAdapter.getDefaultAdapter();

      Delete
    6. This comment has been removed by the author.

      Delete
    7. After writing your above code now again this message comes
      read failed, socket might closed or timeout, read ret: - 1

      Delete
  12. 1. ERROR in /storage/emulated/0/.sketchware/mysc/609/app/src/main/
    java/com/my/newproject/MainActivity.java (at line 123)
    if (_enable && !isEnabled) {
    AnAnnnn
    enable cannot be resolved to a variable
    2, ERROR in /storage/emulated/0/.sketchware/mysc/60%/app/sre/main/
    java/com/my/newproject/MainActivity. java (at line 125)
    else if(!_enable && isEnabled) {
    Annnnnn
    enable cannot be resolved to a variable
    2 problems (2 errors)

    ReplyDelete
  13. Hello sir can you make frequency analyzer app

    ReplyDelete

Post a Comment

Popular posts from this blog

Simple car racing android game in Sketchware

Enable Fullscreen for Youtube videos in WebView

Creating a Drawing View in Sketchware

How to enable upload from webview in Sketchware?

List of Calendar Format symbols valid in Sketchware