Controlling LED's from my phone via BluetoothLE

// ArduinoBLE - Version: Latest
#include <ArduinoBLE.h>

/*
LED

This example creates a Bluetooth® Low Energy peripheral with service that contains a
characteristic to control an LED.

The circuit:

  • Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT,
    Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board.

You can use a generic Bluetooth® Low Energy central app, like LightBlue (iOS and Android) or
nRF Connect (Android), to interact with the services and characteristics
created in this sketch.

This example code is in the public domain.
*/

BLEService ledService("180A"); // Bluetooth® Low Energy LED Service

// Bluetooth® Low Energy LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("2A57", BLERead | BLEWrite);

const int led1 = 2; // pin to use for the LED
const int led2 = 3;
const int led3 = 4;
const int ledPin = LED_BUILTIN;
//const int led4 = 5;

void setup() {
Serial.begin(9600);
while (!Serial);

// set LED pin to output mode
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
//pinMode(led4, OUTPUT);

digitalWrite(LED_BUILTIN, LOW); // when the central disconnects, turn off the LED
digitalWrite(led1, HIGH); // will turn the LED off
digitalWrite(led2, HIGH); // will turn the LED off
digitalWrite(led3, HIGH); // will turn the LED off
//digitalWrite(led4, HIGH); // will turn the LED off

// begin initialization
if (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy module failed!");

while (1);

}

// set advertised local name and service UUID:
BLE.setLocalName("Nano 33 BLE Sense");
BLE.setAdvertisedService(ledService);

// add the characteristic to the service
ledService.addCharacteristic(switchCharacteristic);

// add service
BLE.addService(ledService);

// set the initial value for the characeristic:
switchCharacteristic.writeValue(0);

// start advertising
BLE.advertise();

Serial.println("BLE LED Peripheral");
}

void loop() {
// listen for Bluetooth® Low Energy peripherals to connect:
BLEDevice central = BLE.central();

// if a central is connected to peripheral:
if (central) {
Serial.print("Connected to central: ");
// print the central's MAC address:
Serial.println(central.address());
digitalWrite(LED_BUILTIN, HIGH);

// while the central is still connected to peripheral:
while (central.connected()) {

  // if the remote device wrote to the characteristic,
  // use the value to control the LED:

  if (switchCharacteristic.written()) {
    switch (switchCharacteristic.value()) {   // any value other than 0
      case 01:
        Serial.println("led1 on");
        digitalWrite(led1, LOW);            // will turn the LED on
        digitalWrite(led2, HIGH);          // will turn the LED off
        digitalWrite(led3, HIGH);         // will turn the LED off
        break;
      case 02:
        Serial.println("led2 on");
        digitalWrite(led1, HIGH);         // will turn the LED off
        digitalWrite(led2, LOW);         // will turn the LED on
        digitalWrite(led3, HIGH);       // will turn the LED off
        break;
      case 03:
        Serial.println("led3 on");
        digitalWrite(led1, HIGH);         // will turn the LED off
        digitalWrite(led2, HIGH);        // will turn the LED off
        digitalWrite(led3, LOW);        // will turn the LED on
        break;
      default:
        Serial.println(F("LEDs off"));
        digitalWrite(led1, HIGH);          // will turn the LED off
        digitalWrite(led2, HIGH);         // will turn the LED off
        digitalWrite(led3, HIGH);        // will turn the LED off
        break;
    }
  }
}

// when the central disconnects, print it out:
Serial.print(F("Disconnected from central: "));
Serial.println(central.address());
digitalWrite(LED_BUILTIN, LOW);         // when the central disconnects, turn off the LED
digitalWrite(led1, HIGH);              // will turn the LED off
digitalWrite(led2, HIGH);             // will turn the LED off
digitalWrite(led3, HIGH);            // will turn the LED off

}
}

Please ask the forum Muhammed - I can't help you as I have an injury that is currently limiting my time on the PC.

Ow I'm sorry sir, I don't know that.
I hope you get well soon

1 Like

Your connection setup looks like it missing the part where a list of devices to connect to is generated.

Look for samples in

Excuse me, I'm sorry, is there any specialization for lighting the LED, such as UUID, Characteristic, and BLE service?

Can the bluetooth client be paired with BLE?

The BluetoothClient component is ONLY added to declare the missing permissions in the Manifest.

so, if I use Bluetoothclient on my Arduino nano BLE 33 it doesn't matter?

You should NOT use it (so no blocks). All you have to do is drag this component into the Designer.

Really? So BluetoothLE still can work?

Of course. What about trying ...

I'm sorry, I've try it but nothing happen

image

Is there something wrong with my block

the status is still scanning

You didn't ask for permissions.
(Show all relevant blocks and post the aia.)
Android version?

Yes, it's android version

I.m sorry, I forgot it. This is my aia file
cobak.aia (939.2 KB)
and this is my block
image

Which Android version?

Did you try my test aia?