Connecting Layout with Java (MainActivity.java)

This class poweres the home screen of the app. It connects all the UI components you created in activity_main.xml with logic to:

  1. Open other screens

  2. Connect to devices

  3. Control brightness

  4. Handle permissions

  5. react to BLE events

We’ll now walk through each section carefully and explain exaclty what it does.

1. Class Declaration and Imports

At the top of every Java file, you’ll find two important things:

  1. Imports - tools you’ll be using from Android and other libraries.

  2. The class declaration - this defines the screen (Activity) you’re building.

  
    public class MainActivity extends AppCompatActivity {
  

public class MainActivity

This defines a public class (meaning: it can be used from anywhere in the app) named MainActivity. This class represents the main screen of the app.

extends AppCompactActivity

This means MainActivity is a type of a screen (Activity) that inherits all the default behaviours provided by Android such as:

  • showing a layout.

  • responding to touches.

  • handling lifecycle events like start/stop.

By extending AppCompactActivity, we can also gain support for modern Android features and backwards compatibility.

Imports

At the very top of the file, there’s a long list of import lines like:

  
    import android.os.Bundle;
    import android.content.Intent;
    import android.widget.Button;
    import android.widget.SeekBar;
    import android.widget.TextView;
  

This tells Java: “We’ll be using these tools from other libraries, so make them available in this file”.

You don’t need to memorize or write them manually - Android Studio will auto-insert them when you use a new class (like Button or Intent).

2. onCreate() - What happens When the Screen Opens

The onCreate() method is where everything begins for a screen (called Activity in Android). When this screen (MainActivity) is launched, android calls this function to set up your layout and prepare your UI.

  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
  

@Override

Tells the compiler: “We are replacing a default Android behavior with your own version”.

protected void onCreate(Bundle savedInstanceState)

This is a special method that Android calls automatically when this screen is created for the first time.

  • protected means other classes can see and use this function.

  • void means this function doesn't return anything.

  • Bundle savedInstanceState is a data object that stores things like screen state - we don’t use it here, but it must be included for compatibility.

super.onCreate(savedInstanceState)

This calls the original version of onCreate() from AppCompactActivity (the class our activity extends). We need this to make sure Android’s internal setup is complete.

setContentView(R.layout.activity_main.xml)

This tells Android to use our activity_main.xml layout as the screen’s UI. Without this, our UI wouldn’t show at all.

Handling Button Clicks & Opening Another Screen

We’ll start by making the “Animations” button work

  
    // 1. Declare the Button (at the top of the class)
    private Button openAnimationsButton;

    // 2. Inside onCreate() — connect the Button with its layout ID
    openAnimationsButton = findViewById(R.id.openAnimationsButton);

    // 3. Set up the click listener
    openAnimationsButton.setOnClickListener(v -> {
        // Create an Intent to switch to the Animations screen
        Intent intent = new Intent(this, Animations.class);
        startActivity(intent);
    });
  

How does it work?

  1. Button openAnimationsButton
    We create a variable to reference the Animations button from the layout.

  2. findViewById(…)
    We use this function to link the Java variable to the button you created in activity_main.xml

  3. setOnClickListener(…)
    This is where we say: “When this button is clicked, run the following code.”

  4. Intent and startActivity(…)
    1. An Intent tells Android: “I want to do something - like open a new screen.”
    2. new Intent(this, Animations.class) means: “Open the screen called Animations from this current screen.”
    3. startActivity(intent) tells Android to actually start the new screen.

Static Color Button - Open StaticColorActivity

  
    // Declare the button at the top of the class
    private Button staticColorButton;

    // Inside onCreate()
    staticColorButton = findViewById(R.id.staticColorBtn);

    staticColorButton.setOnClickListener(v -> {
        Intent intent = new Intent(this, StaticColorActivity.class);
        startActivity(intent);
    });
  

This button worsk exactly the same as Animations button, but openes a screen called StaticColorActivity.

ImageButton (SelectDevice)

At the top-right corner of the screen, we placed an icon button for opening the device selection screen. It’s wrapped in a FrameLayout , and in this implementation, the entire frame is made clickable, not just the icon.

This approach:

  • Makes it easier for the users to tap.

  • Keeps styling (like background and border) separated from the actual icon.

  
    // Declare the FrameLayout container and the ImageButton
    private FrameLayout selectDeviceFrame;
    private ImageButton selectDeviceButton;

    // Inside onCreate()
    selectDeviceFrame = findViewById(R.id.selectDeviceFrame);
    selectDeviceButton = findViewById(R.id.selectDeviceBtn);

    // Apply a single click listener to the FrameLayout
    selectDeviceFrame.setOnClickListener(v -> {
        Intent intent = new Intent(this, SelectDeviceActivity.class);
        startActivity(intent);
    });
  

Why FrameLayout for the Click?

in the layout, the ImageButton is wrapped like this:

  
    <FrameLayout
      android:id="@+id/selectDeviceFrame"
      span class="xml-attribute">android:clickable="true"
      android:focusable="true"
      android:background="@drawable/button_background_with_outline">

      <ImageButton
        android:id="@+id/selectDeviceBtn"
        android:contentDescription="@string/device_icon_desc"
        android:src="@drawable/ic_device"
        android:background="@null"/>

    </FrameLayout>
  

By attaching the click listener to FrameLayout , we ensure:

  • Larger tap area (great for accessibility).

  • Cleaner code - all logic is attached to one container.

  • Visual effects like ripple and border apply to the whole touch area.

Bottom Navigation Handling

At the bottom of the main screen, there’s a BottomNavigationView that allows users to quickly switch between key sections of the app: Home, Animations, Static Color and Settings.

To make this interactive, we need to:

  1. Connect it to the layout.

  2. Detect which item the user taps

  3. Navigate to the correct screen (except Home which we’re already on)

  
    // Declare at the top of the class
    private BottomNavigationView bottomNavigationView;

    // Inside onCreate()
    bottomNavigationView = findViewById(R.id.bottomNavigation);

    bottomNavigationView.setOnItemSelectedListener(item -> {
        int itemId = item.getItemId();

        if (itemId == R.id.home) {
            // Already on Home, do nothing
            return true;

        } else if (itemId == R.id.animations) {
            Intent intent = new Intent(this, Animations.class);
            startActivity(intent);
            return true;

        } else if (itemId == R.id.settings) {
            Intent intent = new Intent(this, TestingNewActivities.class);
            startActivity(intent);
            return false;

        } else if (itemId == R.id.static_color) {
            Intent intent = new Intent(this, StaticColorActivity.class);
            startActivity(intent);
            return true;
        }

        return false;
    });
  

Explanation

1. setOnItemSelectedListener(…)

This function is called whenever the user taps on one of the navigation items.

2. Intent Navigation

We already explained intent earlier - and now you see it used again for switching between activities based on which navigation item was selected.

3. Return true;

Tells Android: “Yes, we’ve handled the tap - don’t do anything else.”

With this in place your bottom navigation is now fully functional.

Bluetooth Availability

Before we interact with Bluetooth, we must:

  1. Get the device’s Bluetooth adapter.

  2. Make sure Bluetooth is available and enabled.

Code

  
    // Get the BluetoothAdapter from the system
    bluetoothAdapter = ((BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter();

    // Check if Bluetooth is supported on this device
    if (bluetoothAdapter == null) {
        Toast.makeText(this, "Bluetooth is not supported on this device", Toast.LENGTH_SHORT).show();
        finish(); // Close the app
        return;
    }
  

Explanation

1. Getting Bluetooth Adapter

We access the systems BluetoothManager and ask for the default adapter.

2. Null Check

If the adapter is null, the device does not support Bluetooth, so we show a message and close the app.

Brightness Control with SeekBar

In our layout, we added a SeekBar that lets the user choose a brightness level. Now it’s time to connect that UI element to our Java code and handle how its value changes.

Brightness SeekBar

  
    // Declare at the top of the class
    private SeekBar brightnessSeekBar;
    private static final String TAG = "MainActivity";
    private int lastSentCommand = -1;
    private int minSeekBarValue = 48;
    private int maxSeekBarValue = 60;
    private int SKIP_THRESHOLD = 5;

    // Inside onCreate()
    brightnessSeekBar = findViewById(R.id.brightnessSeekBar);

    brightnessSeekBar.setMax(maxSeekBarValue - minSeekBarValue);
  
    brightnessSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            if (fromUser) {
                    Log.d(TAG, "Progress changed:");

                    if (Math.abs(progress - lastSentCommand) >= SKIP_THRESHOLD) {
                        Log.d("onProgressChanged", "Command sent");
                        sendBrightness(progress);
                        lastSentCommand = progress;
                    }
            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
                
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
                Log.d(TAG, "Progress stopped");
                new Handler().postDelayed(() -> sendBrightness(lastSentCommand), 200);
                new Handler().postDelayed(() -> sendBrightness(14), 400);
        }
    });
  

Explanation

In this part of the app, we added a SeekBar to control LED brightness. Here’s what each part of the logic does.

  1. onProgressChanged(…)
    This method is triggered every time the user moves the SeekBar.
    The progress value tells us how far the user has dragged.
    We use lastSentCommand to avoid sending repeated brightness values that are too similar
    If the user has dragged far enough (SKIP_TRESHOLD), we call a function sendBrightness(progress) to update the LED strip - this function will be explained later.

  2. onStartTrackingTouch(…)
    This method runs once the user first touches the slider.
    In a later version of this app, we’ll check whether the user is connected to a BLE device here.
    For now, this method stays empty.

  3. onStopTrackingTouch(…)
    This is called once the user releases the SeekBar.
    Even if the app skipped some values while the user was dragging, this section makes sure the final value is sent, like a confirmation.

  4. setMax(…)
    This method sets the SeekBar’s max value.
    The SeekBar itself works with values starting at 0. That’s why we subtract minSeekBarValue when setting the max. The real brightness sent to the LED strip will be adjusted by adding minSeekBarValue later inside sendBrightness()

  5. Log.d(…)
    This sends debug messages to Android Studio’s Logcat window, so you can track values or events while testing.

  6. Handler().postDelayed(…)
    A Handler lets us schedule code to run later - in this case, to resend the brightness after a delay (e.g. 200ms or 400ms). This gives the BLE device time to catch up and ensures stable communication.

Summary of onCreate()

So far you’ve created the full logic for the main screen of the app:

  1. set the layout using setContentView().

  2. Connected and configured the Animations, Static Color, and Select Device buttons.

  3. Set the brightness SeekBar to throttle updates and send final values after release.

  4. Connected the bottom navigation bar using if statements for reliable screen switching.

  5. Added Log.d(…) statements for helpful debugging during development.

Everything is now tied together inside onCreate(). You can compare your version with the final version below to make sure everything is working exactly as expected. Don’t worry if things aren’t perfect - this guide will help you to get there step by step.

▶ Final onCreate() Code
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Setup buttons
        Button openAnimationsButton = findViewById(R.id.animationsBtn);
        Button staticColorButton = findViewById(R.id.staticColorBtn);
        FrameLayout selectDeviceFrame = findViewById(R.id.selectDeviceFrame);

        openAnimationsButton.setOnClickListener(v -> {
            Intent intent = new Intent(this, Animations.class);
            startActivity(intent);
        });

        staticColorButton.setOnClickListener(v -> {
            Intent intent = new Intent(this, StaticColorActivity.class);
            startActivity(intent);
        });

        selectDeviceFrame.setOnClickListener(v -> {
            Intent intent = new Intent(this, SelectDeviceActivity.class);
            startActivity(intent);
        });

        // Setup SeekBar
        SeekBar brightnessSeekBar = findViewById(R.id.brightnessSeekBar);
        int lastSentCommand = -1;
        int SKIP_THRESHOLD = 5;

        brightnessSeekBar.setMax(maxSeekBarValue - minSeekBarValue);

        brightnessSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser) {
                    Log.d(TAG, "Progress changed:");
                    if (Math.abs(progress - lastSentCommand) >= SKIP_THRESHOLD) {
                        Log.d("onProgressChanged", "Command sent");
                        sendBrightness(progress);
                        lastSentCommand = progress;
                    }
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // Placeholder: logic for device connection will be added later
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                Log.d(TAG, "Progress stopped");
                new Handler().postDelayed(() -> sendBrightness(lastSentCommand), 200);
                new Handler().postDelayed(() -> sendBrightness(14), 400);
            }
        });

        // Setup bottom navigation
        BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigation);

        bottomNavigationView.setOnItemSelectedListener(item -> {
            int itemId = item.getItemId();

            if (itemId == R.id.home) {
                return true;

            } else if (itemId == R.id.animations) {
                Intent intent = new Intent(this, Animations.class);
                startActivity(intent);
                return true;

            } else if (itemId == R.id.settings) {
                Intent intent = new Intent(this, TestingNewActivities.class);
                startActivity(intent);
                return false;

            } else if (itemId == R.id.static_color) {
                Intent intent = new Intent(this, StaticColorActivity.class);
                startActivity(intent);
                return true;
            }

            return false;
        });
    }
  

3. Define the Permission Methods

To make sure the app can access Bluetooth and scan for devices, we need to verify that the user has granted all required permissions. We do this by calling checkPermissions() and, if needed, requestPermissions() early in the onCreate() method.

  
    private boolean checkPermissions() {
        // Check if all required Bluetooth permissions are granted
        return ContextCompat.checkSelfPermission(
            this, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED
            && ContextCompat.checkSelfPermission(
            this, Manifest.permission.BLUETOOTH_SCAN) == PackageManager.PERMISSION_GRANTED
            && ContextCompat.checkSelfPermission(
            this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
    }

    private void requestPermissions() {
        // Request all necessary permissions at once
        ActivityCompat.requestPermissions(
            this,
            new String[] {
                Manifest.permission.BLUETOOTH_CONNECT,
                Manifest.permission.BLUETOOTH_SCAN,
                Manifest.permission.ACCESS_FINE_LOCATION
            },
            101 // Request code (can be any number)
        );
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        
        if (requestCode == 101) {
            if (checkPermissions()) {
                Toast.makeText(this, "Permissions granted.", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "Permissions are required to use Bluetooth.", Toast.LENGTH_LONG).show();
            }
        }
    }
  

3.1. Explanation

  1. checkPermissions()
    This method checks if the app already has Bluetooth and location access.
    We use ContextCompact.checkSelfPermission(…) to check each one individually.
    This method returns true if everything is granted, otherwise false.

  2. requestPermissions()
    If permissions are not granted, this method asks the user to approve them.
    We group all permissions into one request.
    The 101 is just a request ID we’ll use later to match the response.

  3. onRequestPermissionResult(…)
    Android calls this method after the user responds to a permission request.
    If everything is granted, we show a short “Permissoins Granted” toast.
    If denied, we explain that Bluetooth features won’t work

3.2. Where Should I Call checkPermissions()?

Once you’ve added these methods, call them inside onCreate() like this:

  
    if (!checkPermissions()) {
        requestPermissions();
    }
  

This ensures that every time the user opens the app, they’re prompted only if needed.

4. onResume(), onPause(), and onDestroy() - core Android lifecycle callbacks

Android provides lifecycle methods like onResume(), onPause(), and onDestroy() to help your app react to important system events - like when the app comes back into view or is about to close.

These methods go inside the same class as onCreate(), but outside of this method, You can add them directly underneath onCreate() so they’re easy to find and manage later.

We’ll add these methods now so they’re in place for later, but we’ll update their actual logic once we introduce the components they depend on.

  
    @Override
    protected void onResume() {
        super.onResume();
        // Ensure Home is selected in the bottom navigation bar
        BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigation);
        bottomNavigationView.setSelectedItemId(R.id.home);
    }

    @Override
    protected void onPause() {
        super.onPause();
        // Will be used to unregister or pause logic later
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // Cleanup resources or disconnect BLE later
    }
  

4.1. Explanation

These methods are part of the Android Activity lifecycle. They help your app respond to different stages of its visibility and lifespan:

  1. onResume()
    Called when your activity comes to the foreground and becomes active again.
    We’ll later use this to update the UI.
    We also use this method to set the selectedItemId() bask to home once the user comes back to this activity.

  2. onPause()
    Called when the activity is partially obscured (like when switching apps).
    Ideal for temporarily stopping operations or unregistering receivers.

  3. onDestroy()
    Called when your activity is about to be destroyed completely.
    We’ll use this to clean up resources and stop background services like BLE connections.

5. Sending Brightness Values

Whenever the user interacts with the Seekbar, we want to send a command to the connected LED strip to update its brightness. This is done through a centralized component called BLEService, which we’ll implement in later stages of this guide.

  
    private void sendBrightness(int brightnessValue) {
        // First: Send a trigger command to prepare the BLE driver
        Intent sendInitiatorValue = new Intent(this, BLEService.class);
        sendInitiatorValue.putExtra("management_BLEService", "DRIVER_COMMAND");
        sendInitiatorValue.putExtra("command", 12);
        startService(sendInitiatorValue);

        // Second: Send actual brightness value after a 100ms delay
        new Handler().postDelayed(() -> {
            int finalProgress = brightnessValue + minSeekBarValue;
            Intent intent = new Intent(this, BLEService.class);
            intent.putExtra("management_BLEService", "DRIVER_COMMAND");
            intent.putExtra("command", finalProgress);
            startService(intent);
            Log.d(TAG, "Brightness sent: " + finalProgress);
        }, 100);
    }
  

5.1 Explanation

  1. What does it do?
    It sends brightness instructions to the LED strip using BLEService, a background component that handles all Bluetooth communication.

  2. Why two commands
    The first command = 12 acts like an initliaization step it “tells” the LED strip that a brightness value is comming next.

  3. Why delay the seccond command
    The seccond command - which includes the accual brightness - is delayed 100ms to give the system and BLE driver enough time to proces the initial command.

  4. What is minSeekBarValue
    It’s added to brightness value to ensure it stays within the LED strip’s required range (like 48-60). This variable was defined earlyier in the SeekBar setup.

Summary

By now, you’ve successfully built the entire logic for your main screen:

  • Linked layout components to Java variables.

  • Added navigation between screens with Intent.

  • Implemented SeekBar with throttled updates, and delayed commands.

  • Integrated a clean Bluetooth permission check system.

  • Handled lifecycle methods.

  • Sent brightness values to your custom BLEService.

Refactor Tip:

To make your code more maintainable and accessible across different methods, it’s a good habit to define layout-related variables (like Buttons, SeekBar, and BottomNavigationView) at the class level.

This avoids repeated findViewById(…) calls and lets you reference them from anywhere in the activity.

Full MainActivity Code

▶ Reveal the full code
  
    public class MainActivity extends AppCompatActivity {

        // Class-level variables
        private Button openAnimationsButton, staticColorButton;
        private FrameLayout selectDeviceFrame;
        private SeekBar brightnessSeekBar;
        private BottomNavigationView bottomNavigationView;
        private int lastSentCommand = -1;
        private int SKIP_THRESHOLD = 5;
        private int minSeekBarValue = 48;
        private int maxSeekBarValue = 60;
        private static final String TAG = "MainActivity";

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            // Check permissions first
            if (!checkPermissions()) {
                requestPermissions();
            }

            // Connect layout elements
            openAnimationsButton = findViewById(R.id.animationsBtn);
            staticColorButton = findViewById(R.id.staticColorBtn);
            selectDeviceFrame = findViewById(R.id.selectDeviceFrame);
            brightnessSeekBar = findViewById(R.id.brightnessSeekBar);
            bottomNavigationView = findViewById(R.id.bottomNavigation);

            // Set up button click actions
            openAnimationsButton.setOnClickListener(v -> {
                Intent intent = new Intent(this, Animations.class);
                startActivity(intent);
            });

            staticColorButton.setOnClickListener(v -> {
                Intent intent = new Intent(this, StaticColorActivity.class);
                startActivity(intent);
            });

            selectDeviceFrame.setOnClickListener(v -> {
                Intent intent = new Intent(this, SelectDeviceActivity.class);
                startActivity(intent);
            });

            // Setup SeekBar
            brightnessSeekBar.setMax(maxSeekBarValue - minSeekBarValue);
            brightnessSeekBar.setProgress(lastSentCommand);

            brightnessSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                    if (fromUser) {
                        Log.d(TAG, "Progress changed:");
                        if (Math.abs(progress - lastSentCommand) >= SKIP_THRESHOLD) {
                            Log.d("onProgressChanged", "Command sent");
                            sendBrightness(progress);
                            lastSentCommand = progress;
                        }
                    }
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {}

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
                    Log.d(TAG, "Progress stopped");
                    new Handler().postDelayed(() -> sendBrightness(lastSentCommand), 200);
                    new Handler().postDelayed(() -> sendBrightness(14), 400);
                }
            });

            // Bottom navigation setup
            bottomNavigationView.setOnItemSelectedListener(item -> {
                int itemId = item.getItemId();

                if (itemId == R.id.home) {
                    return true;
                } else if (itemId == R.id.animations) {
                    Intent intent = new Intent(this, Animations.class);
                    startActivity(intent);
                    return true;
                } else if (itemId == R.id.settings) {
                    Intent intent = new Intent(this, TestingNewActivities.class);
                    startActivity(intent);
                    return false;
                } else if (itemId == R.id.static_color) {
                    Intent intent = new Intent(this, StaticColorActivity.class);
                    startActivity(intent);
                    return true;
                }

                return false;
            });
        }

        @Override
        protected void onResume() {
            super.onResume();
            bottomNavigationView.setSelectedItemId(R.id.home);
        }

        @Override
        protected void onPause() {
            super.onPause();
        }

        @Override
        protected void onDestroy() {
            super.onDestroy();
        }

        private boolean checkPermissions() {
            return ContextCompat.checkSelfPermission(
                this, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED
                && ContextCompat.checkSelfPermission(
                this, Manifest.permission.BLUETOOTH_SCAN) == PackageManager.PERMISSION_GRANTED
                && ContextCompat.checkSelfPermission(
                this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
        }

        private void requestPermissions() {
            ActivityCompat.requestPermissions(
                this,
                new String[] {
                    Manifest.permission.BLUETOOTH_CONNECT,
                    Manifest.permission.BLUETOOTH_SCAN,
                    Manifest.permission.ACCESS_FINE_LOCATION
                },
                101
            );
        }

        @Override
        public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            if (requestCode == 101) {
                if (checkPermissions()) {
                    Toast.makeText(this, "Permissions granted.", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, "Permissions are required to use Bluetooth.", Toast.LENGTH_LONG).show();
                }
            }
        }

        private void sendBrightness(int brightnessValue) {
            Intent sendInitiatorValue = new Intent(this, BLEService.class);
            sendInitiatorValue.putExtra("management_BLEService", "DRIVER_COMMAND");
            sendInitiatorValue.putExtra("command", 12);
            startService(sendInitiatorValue);

            new Handler().postDelayed(() -> {
                int finalProgress = brightnessValue + minSeekBarValue;
                Intent intent = new Intent(this, BLEService.class);
                intent.putExtra("management_BLEService", "DRIVER_COMMAND");
                intent.putExtra("command", finalProgress);
                startService(intent);
                Log.d(TAG, "Brightness sent: " + finalProgress);
            }, 100);
        }
    }
  
Previous
Previous

Missing Resources

Next
Next

Managing Device State and Preferences