SlideShare a Scribd company logo
Agenda
1. Overview and BLE Basics
2. Arduino
3. Android
4. Demo + Q&A
The big picture
Effect
Color
O Off
O Steady
O Blink
Remote controlling of the Christmas tree lights
How to do?
Android
Smartphone App
Bluetooth Low Energy
Connectivity
Arduino-based
prototype
What is Arduino?
Arduino is a global leader
in open-source hardware and software
It provides a platform for building electronic prototypes,
designed to be accessible to all
It promotes sharing and transparency,
creating a collaborative ecosystem
Large and active community (> 30M developers)
The magic within the tree
The board
Arduino Nano 33 BLE
The strip light
NeoPixel WS2812B
Addressable color LEDs
The power supply
12VDC
● Development board part of Nano series
● BLE feature
● Integrated sensors: acc, gyro, …
● I/O pins: digital and analog
Bluetooth Low Energy 101
BLE is a communication technology based on Bluetooth and
designed for energy efficiency
Client-Server architecture (Central-Peripheral)
Supported by a wide range of devices
Widely used in fitness trackers, smart home devices, industrial
sensors, and other IoT applications
Central
(Client)
Peripheral
(Server)
BLE: Services & Characteristics
A Peripheral organizes and exchanges data through:
● Services: a collection of related functionalities
● Characteristics: a specific piece of information within a Service
Effect
Characteristic
Color
Characteristic
Led
Service
A Characteristic:
● Has Read/Write/Notify properties
● Is identified by an UUID
● Has a fixed length (up to 20 bytes)
Represents the light effect
(0=Off, 1=Steady, etc.)
Represents the light color
(3 bytes: R,G,B)
BLE: Scan & Advertising
Scan: The action performed by a central to
find nearby peripherals
Advertising: A peripheral actively
announces its presence and share
information about its identity and features
Central
Peripheral
“Hi all! My name is
GDG-Arduino and
I offer Led Service feature.”
“Hey! Is anyone there?”
Arduino: Let’s code!
The sketch
A program written for an Arduino board
Two main parts:
● setup(): Initializes variables, features,
runs once at the beginning
● loop(): main code that runs repeatedly
Key tasks:
● Advertising to make device discoverable by the
Android app
● Provide LED strip control functionalities
Let’s begin!
#include <ArduinoBLE.h> // Arduino BLE Library
void setup() {
// BLE initialization
BLE.begin();
// Set advertised local name
BLE.setLocalName("GDG-Arduino");
Exposing features
BLEService ledService("1e03ce00-b8bc-4152-85e2-f096236d2833");
BLEByteCharacteristic ledEffectCharacteristic("...", BLERead | BLEWrite);
BLECharacteristic ledColorCharacteristic("...", BLERead | BLEWrite, 3);
ledService.addCharacteristic(ledEffectCharacteristic);
ledService.addCharacteristic(ledColorCharacteristic);
BLE.addService(ledService);
BLE.setAdvertisedService(ledService);
BLE.advertise();
Add Characteristics inside
the Led service
Services and Characteristics
declaration
Add Service
inside the peripheral BLE stack
Start advertising
UUID: randomically generated
Properties
Managing App requests (1/2)
void loop() {
BLEDevice central = BLE.central(); // listen for BLE central to connect
if (central) { // if a central is connected to peripheral
while (central.connected()) {
// Check ledEffect characteristic write
if (ledEffectCharacteristic.written()) {
ledEffect = ledEffectCharacteristic.value();
}
applyEffect(ledEffect);
}
}
ledStrip is an instance of the library
<Adafruit_NeoPixel.h> used to simplify the
control of NeoPixel RGB LED strips
Managing App requests (2/2)
void applyEffect(byte effect) {
…
switch(effect) {
case OFF_EFFECT:
ledStrip.clear();
ledStrip.show();
break;
case STEADY_EFFECT:
// Set all pixels to the specified color
for (int i = 0; i < LED_NUM; i++) {
ledStrip.setPixelColor(i, ledColor[0], ledColor[1], ledColor[2]);
}
ledStrip.show();
break;
…
Use BLE on Android
Permissions on Android <= 11
ACCESS_FINE_LOCATION
BLUETOOTH BLUETOOTH_ADMIN
Permissions on Android >= 12
BLUETOOTH_SCAN
BLUETOOTH_ADVERTISE
BLUETOOTH_CONNECT ACCESS_FINE_LOCATION
BLE Adapter
val bleManager = context.getSystemService(BLUETOOTH_SERVICE) as BluetoothManager
BLE Key Steps
SCAN CONNECT
BLE Scan Configuration
SCAN SETTINGS
SCAN FILTER ScanFilter.Builder().setServiceUuid(<UUID>)
ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_POWER)
You can also filter for:
MAC Address, device name, Manufacturer Data
There are 4 types of scan mode!
BLE Scan Callback
val scanCallback = object : ScanCallback() {
override fun onScanResult(callbackType: Int, result: ScanResult) {
with(result.device) {
Log.i(“ScanCallback”, "Found BLE device! Name: ${name ?: "Unnamed"},
address: $address")
}
}
}
SCAN RESULT
BLUETOOTH DEVICE
RSSI
SCAN RECORD
BLE Scan Execution
_bleManager.adapter.bluetoothLeScanner?.startScan(
mutableListOf(scanFilter), scanSettings, scanCallback
)
_bleManager.adapter.bluetoothLeScanner?.stopScan(scanCallback)
BLE GATT Callback
override fun onConnectionStateChange(gatt: BluetoothGatt?, status: Int, newState: Int)
override fun onServicesDiscovered(gatt: BluetoothGatt?, status: Int)
override fun onCharacteristicRead(
gatt: BluetoothGatt?,
characteristic: BluetoothGattCharacteristic?,
status: Int
)
override fun onCharacteristicWrite(
gatt: BluetoothGatt?,
characteristic: BluetoothGattCharacteristic?,
status: Int
)
override fun onCharacteristicChanged(
gatt: BluetoothGatt?,
characteristic: BluetoothGattCharacteristic?
)
BLE Device Connection
bleManager.adapter.getRemoteDevice(“macAddress”)?.let {
mBluetoothGatt = bluetoothDevice?.connectGatt(context, false, gattCallback)
}
BLE Device Disconnection
mBluetoothGatt?.disconnect()
BLE Discover Services
mBluetoothGatt?.discoverServices()
BLE Device Read Characteristic
fun readLEDColor(): Boolean? {
val service = mBluetoothGatt?.getService(LedUUID.LedService.uuid)
val characteristic =
service?.getCharacteristic(LedUUID.LedService.Color.uuid)
return mBluetoothGatt?.readCharacteristic(characteristic)
}
BLE Device Write Characteristic (1/2)
fun setLEDEffect(effectIndex: Int): Boolean? {
val service = mBluetoothGatt?.getService(LedUUID.LedService.uuid)
val characteristic =
service?.getCharacteristic(LedUUID.LedService.Effect.uuid)
val payload = byteArrayOf(effectIndex.toByte())
return sendCommand(characteristic, payload)
}
BLE Device Write Characteristic (2/2)
fun sendCommand(
characteristic: BluetoothGattCharacteristic?,
payload: ByteArray
): Boolean? {
characteristic?.let {
val result = mBluetoothGatt?.writeCharacteristic(
characteristic,
payload,
BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT
)
val res = when (result) {
BluetoothStatusCodes.SUCCESS -> true
else -> false
}
return res
}
}
BLE Common Problems
1. Android can manage up to 32 BLE connections
2. You cannot run more than 5 scan in 30 sec.
3. You cannot connect a device directly without scan unless you have bonded it
4. You have to manage your BLE instance yourself, if you saturate it an 133 Error can occur
5. Before Scan or Connect you have to check yourself if BLE is on/off
(you can do it by using bleManager.adapter.isEnabled).
“FUN FACT”: This information is not present in the documentation 😁
Q&A
DEMO TIME
Android Code: https://github.com/appersiano/GDG_LedStripTree_Android
Arduino Code: https://github.com/leonardocavagnis/GDG_LedStripTree_Arduino
THANK YOU!
Alessandro Persiano
Email: appersiano@gmail.com
LinkedIn: alessandro-persiano
GitHub: appersiano
Leonardo Cavagnis
Email: l.cavagnis@arduino.cc
LinkedIn: leonardocavagnis
GitHub: leonardocavagnis
CHALLENGE TIME!
https://kahoot.it

More Related Content

Similar to Da Arduino ad Android_ illumina il Natale con il BLE

TestowanieIoT2016
TestowanieIoT2016TestowanieIoT2016
TestowanieIoT2016
kraqa
 
Introduction to Bluetooth Low Energy
Introduction to Bluetooth Low EnergyIntroduction to Bluetooth Low Energy
Introduction to Bluetooth Low Energy
yeokm1
 
Dynamic sorting algorithm vizualizer.pdf
Dynamic sorting algorithm vizualizer.pdfDynamic sorting algorithm vizualizer.pdf
Dynamic sorting algorithm vizualizer.pdf
AgneshShetty
 
Mini project final presentation
Mini project final presentationMini project final presentation
Mini project final presentation
GianlucaCapozzi1
 
Eddystone beacons demo
Eddystone beacons demoEddystone beacons demo
Eddystone beacons demo
Angelo Rüggeberg
 
Everything About Bluetooth (淺談藍牙 4.0) - Central 篇
Everything About Bluetooth (淺談藍牙 4.0) - Central 篇Everything About Bluetooth (淺談藍牙 4.0) - Central 篇
Everything About Bluetooth (淺談藍牙 4.0) - Central 篇
Johnny Sung
 
IoT on Raspberry Pi
IoT on Raspberry PiIoT on Raspberry Pi
IoT on Raspberry Pi
John Staveley
 
summer training report (2)
summer training report (2)summer training report (2)
summer training report (2)Kavya Gupta
 
Arduino and Circuits.docx
Arduino and Circuits.docxArduino and Circuits.docx
Arduino and Circuits.docx
Ajay578679
 
JavaScript Robotics #NodeWeek
JavaScript Robotics #NodeWeekJavaScript Robotics #NodeWeek
JavaScript Robotics #NodeWeek
Suz Hinton
 
Microsoft's view of the Internet of Things (IoT) by Imran Shafqat
Microsoft's view of the Internet of Things (IoT) by Imran ShafqatMicrosoft's view of the Internet of Things (IoT) by Imran Shafqat
Microsoft's view of the Internet of Things (IoT) by Imran Shafqat
Allied Consultants
 
Internet of Things (IoT) reference architecture using Azure -MIC - Lahore
Internet of Things (IoT) reference architecture using Azure -MIC - LahoreInternet of Things (IoT) reference architecture using Azure -MIC - Lahore
Internet of Things (IoT) reference architecture using Azure -MIC - Lahore
Information Technology University
 
Optimizing NN inference performance on Arm NEON and Vulkan
Optimizing NN inference performance on Arm NEON and VulkanOptimizing NN inference performance on Arm NEON and Vulkan
Optimizing NN inference performance on Arm NEON and Vulkan
ax inc.
 
Easy2park - A smarter way to find a parking lot
Easy2park - A smarter way to find a parking lotEasy2park - A smarter way to find a parking lot
Easy2park - A smarter way to find a parking lot
Daniele Davoli
 
Gas leakage detection system
Gas leakage detection systemGas leakage detection system
Gas leakage detection system
Aashiq Ahamed N
 
Development of a Low Cost, Reliable & Scalable Home Automation System.
Development of a Low Cost, Reliable & Scalable Home Automation System.Development of a Low Cost, Reliable & Scalable Home Automation System.
Development of a Low Cost, Reliable & Scalable Home Automation System.
imtiyazEEE
 
Eclipse Edje: A Java API for Microcontrollers
Eclipse Edje: A Java API for MicrocontrollersEclipse Edje: A Java API for Microcontrollers
Eclipse Edje: A Java API for Microcontrollers
MicroEJ
 
Iaetsd vlsi based implementation of a digital
Iaetsd vlsi based implementation of a digitalIaetsd vlsi based implementation of a digital
Iaetsd vlsi based implementation of a digital
Iaetsd Iaetsd
 
RAHUL NASKAR IOT.ppt
RAHUL NASKAR IOT.pptRAHUL NASKAR IOT.ppt
RAHUL NASKAR IOT.ppt
PrakasBhowmik
 

Similar to Da Arduino ad Android_ illumina il Natale con il BLE (20)

TestowanieIoT2016
TestowanieIoT2016TestowanieIoT2016
TestowanieIoT2016
 
Introduction to Bluetooth Low Energy
Introduction to Bluetooth Low EnergyIntroduction to Bluetooth Low Energy
Introduction to Bluetooth Low Energy
 
Dynamic sorting algorithm vizualizer.pdf
Dynamic sorting algorithm vizualizer.pdfDynamic sorting algorithm vizualizer.pdf
Dynamic sorting algorithm vizualizer.pdf
 
Mini project final presentation
Mini project final presentationMini project final presentation
Mini project final presentation
 
Eddystone beacons demo
Eddystone beacons demoEddystone beacons demo
Eddystone beacons demo
 
Everything About Bluetooth (淺談藍牙 4.0) - Central 篇
Everything About Bluetooth (淺談藍牙 4.0) - Central 篇Everything About Bluetooth (淺談藍牙 4.0) - Central 篇
Everything About Bluetooth (淺談藍牙 4.0) - Central 篇
 
IoT on Raspberry Pi
IoT on Raspberry PiIoT on Raspberry Pi
IoT on Raspberry Pi
 
summer training report (2)
summer training report (2)summer training report (2)
summer training report (2)
 
Arduino and Circuits.docx
Arduino and Circuits.docxArduino and Circuits.docx
Arduino and Circuits.docx
 
JavaScript Robotics #NodeWeek
JavaScript Robotics #NodeWeekJavaScript Robotics #NodeWeek
JavaScript Robotics #NodeWeek
 
Microsoft's view of the Internet of Things (IoT) by Imran Shafqat
Microsoft's view of the Internet of Things (IoT) by Imran ShafqatMicrosoft's view of the Internet of Things (IoT) by Imran Shafqat
Microsoft's view of the Internet of Things (IoT) by Imran Shafqat
 
Internet of Things (IoT) reference architecture using Azure -MIC - Lahore
Internet of Things (IoT) reference architecture using Azure -MIC - LahoreInternet of Things (IoT) reference architecture using Azure -MIC - Lahore
Internet of Things (IoT) reference architecture using Azure -MIC - Lahore
 
Optimizing NN inference performance on Arm NEON and Vulkan
Optimizing NN inference performance on Arm NEON and VulkanOptimizing NN inference performance on Arm NEON and Vulkan
Optimizing NN inference performance on Arm NEON and Vulkan
 
Easy2park - A smarter way to find a parking lot
Easy2park - A smarter way to find a parking lotEasy2park - A smarter way to find a parking lot
Easy2park - A smarter way to find a parking lot
 
Gas leakage detection system
Gas leakage detection systemGas leakage detection system
Gas leakage detection system
 
Development of a Low Cost, Reliable & Scalable Home Automation System.
Development of a Low Cost, Reliable & Scalable Home Automation System.Development of a Low Cost, Reliable & Scalable Home Automation System.
Development of a Low Cost, Reliable & Scalable Home Automation System.
 
Eclipse Edje: A Java API for Microcontrollers
Eclipse Edje: A Java API for MicrocontrollersEclipse Edje: A Java API for Microcontrollers
Eclipse Edje: A Java API for Microcontrollers
 
Iaetsd vlsi based implementation of a digital
Iaetsd vlsi based implementation of a digitalIaetsd vlsi based implementation of a digital
Iaetsd vlsi based implementation of a digital
 
Materi 3 Sim
Materi 3 SimMateri 3 Sim
Materi 3 Sim
 
RAHUL NASKAR IOT.ppt
RAHUL NASKAR IOT.pptRAHUL NASKAR IOT.ppt
RAHUL NASKAR IOT.ppt
 

Recently uploaded

Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
AmarGB2
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
Jayaprasanna4
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
AafreenAbuthahir2
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
thanhdowork
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
Divya Somashekar
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
AhmedHussein950959
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
Pipe Restoration Solutions
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
Vijay Dialani, PhD
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 

Recently uploaded (20)

Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 

Da Arduino ad Android_ illumina il Natale con il BLE

  • 1.
  • 2. Agenda 1. Overview and BLE Basics 2. Arduino 3. Android 4. Demo + Q&A
  • 3. The big picture Effect Color O Off O Steady O Blink Remote controlling of the Christmas tree lights
  • 4. How to do? Android Smartphone App Bluetooth Low Energy Connectivity Arduino-based prototype
  • 5. What is Arduino? Arduino is a global leader in open-source hardware and software It provides a platform for building electronic prototypes, designed to be accessible to all It promotes sharing and transparency, creating a collaborative ecosystem Large and active community (> 30M developers)
  • 6. The magic within the tree The board Arduino Nano 33 BLE The strip light NeoPixel WS2812B Addressable color LEDs The power supply 12VDC ● Development board part of Nano series ● BLE feature ● Integrated sensors: acc, gyro, … ● I/O pins: digital and analog
  • 7. Bluetooth Low Energy 101 BLE is a communication technology based on Bluetooth and designed for energy efficiency Client-Server architecture (Central-Peripheral) Supported by a wide range of devices Widely used in fitness trackers, smart home devices, industrial sensors, and other IoT applications Central (Client) Peripheral (Server)
  • 8. BLE: Services & Characteristics A Peripheral organizes and exchanges data through: ● Services: a collection of related functionalities ● Characteristics: a specific piece of information within a Service Effect Characteristic Color Characteristic Led Service A Characteristic: ● Has Read/Write/Notify properties ● Is identified by an UUID ● Has a fixed length (up to 20 bytes) Represents the light effect (0=Off, 1=Steady, etc.) Represents the light color (3 bytes: R,G,B)
  • 9. BLE: Scan & Advertising Scan: The action performed by a central to find nearby peripherals Advertising: A peripheral actively announces its presence and share information about its identity and features Central Peripheral “Hi all! My name is GDG-Arduino and I offer Led Service feature.” “Hey! Is anyone there?”
  • 11. The sketch A program written for an Arduino board Two main parts: ● setup(): Initializes variables, features, runs once at the beginning ● loop(): main code that runs repeatedly Key tasks: ● Advertising to make device discoverable by the Android app ● Provide LED strip control functionalities
  • 12. Let’s begin! #include <ArduinoBLE.h> // Arduino BLE Library void setup() { // BLE initialization BLE.begin(); // Set advertised local name BLE.setLocalName("GDG-Arduino");
  • 13. Exposing features BLEService ledService("1e03ce00-b8bc-4152-85e2-f096236d2833"); BLEByteCharacteristic ledEffectCharacteristic("...", BLERead | BLEWrite); BLECharacteristic ledColorCharacteristic("...", BLERead | BLEWrite, 3); ledService.addCharacteristic(ledEffectCharacteristic); ledService.addCharacteristic(ledColorCharacteristic); BLE.addService(ledService); BLE.setAdvertisedService(ledService); BLE.advertise(); Add Characteristics inside the Led service Services and Characteristics declaration Add Service inside the peripheral BLE stack Start advertising UUID: randomically generated Properties
  • 14. Managing App requests (1/2) void loop() { BLEDevice central = BLE.central(); // listen for BLE central to connect if (central) { // if a central is connected to peripheral while (central.connected()) { // Check ledEffect characteristic write if (ledEffectCharacteristic.written()) { ledEffect = ledEffectCharacteristic.value(); } applyEffect(ledEffect); } }
  • 15. ledStrip is an instance of the library <Adafruit_NeoPixel.h> used to simplify the control of NeoPixel RGB LED strips Managing App requests (2/2) void applyEffect(byte effect) { … switch(effect) { case OFF_EFFECT: ledStrip.clear(); ledStrip.show(); break; case STEADY_EFFECT: // Set all pixels to the specified color for (int i = 0; i < LED_NUM; i++) { ledStrip.setPixelColor(i, ledColor[0], ledColor[1], ledColor[2]); } ledStrip.show(); break; …
  • 16. Use BLE on Android
  • 17. Permissions on Android <= 11 ACCESS_FINE_LOCATION BLUETOOTH BLUETOOTH_ADMIN
  • 18. Permissions on Android >= 12 BLUETOOTH_SCAN BLUETOOTH_ADVERTISE BLUETOOTH_CONNECT ACCESS_FINE_LOCATION
  • 19. BLE Adapter val bleManager = context.getSystemService(BLUETOOTH_SERVICE) as BluetoothManager
  • 21. BLE Scan Configuration SCAN SETTINGS SCAN FILTER ScanFilter.Builder().setServiceUuid(<UUID>) ScanSettings.Builder() .setScanMode(ScanSettings.SCAN_MODE_LOW_POWER) You can also filter for: MAC Address, device name, Manufacturer Data There are 4 types of scan mode!
  • 22. BLE Scan Callback val scanCallback = object : ScanCallback() { override fun onScanResult(callbackType: Int, result: ScanResult) { with(result.device) { Log.i(“ScanCallback”, "Found BLE device! Name: ${name ?: "Unnamed"}, address: $address") } } } SCAN RESULT BLUETOOTH DEVICE RSSI SCAN RECORD
  • 23. BLE Scan Execution _bleManager.adapter.bluetoothLeScanner?.startScan( mutableListOf(scanFilter), scanSettings, scanCallback ) _bleManager.adapter.bluetoothLeScanner?.stopScan(scanCallback)
  • 24. BLE GATT Callback override fun onConnectionStateChange(gatt: BluetoothGatt?, status: Int, newState: Int) override fun onServicesDiscovered(gatt: BluetoothGatt?, status: Int) override fun onCharacteristicRead( gatt: BluetoothGatt?, characteristic: BluetoothGattCharacteristic?, status: Int ) override fun onCharacteristicWrite( gatt: BluetoothGatt?, characteristic: BluetoothGattCharacteristic?, status: Int ) override fun onCharacteristicChanged( gatt: BluetoothGatt?, characteristic: BluetoothGattCharacteristic? )
  • 25. BLE Device Connection bleManager.adapter.getRemoteDevice(“macAddress”)?.let { mBluetoothGatt = bluetoothDevice?.connectGatt(context, false, gattCallback) } BLE Device Disconnection mBluetoothGatt?.disconnect()
  • 27. BLE Device Read Characteristic fun readLEDColor(): Boolean? { val service = mBluetoothGatt?.getService(LedUUID.LedService.uuid) val characteristic = service?.getCharacteristic(LedUUID.LedService.Color.uuid) return mBluetoothGatt?.readCharacteristic(characteristic) }
  • 28. BLE Device Write Characteristic (1/2) fun setLEDEffect(effectIndex: Int): Boolean? { val service = mBluetoothGatt?.getService(LedUUID.LedService.uuid) val characteristic = service?.getCharacteristic(LedUUID.LedService.Effect.uuid) val payload = byteArrayOf(effectIndex.toByte()) return sendCommand(characteristic, payload) }
  • 29. BLE Device Write Characteristic (2/2) fun sendCommand( characteristic: BluetoothGattCharacteristic?, payload: ByteArray ): Boolean? { characteristic?.let { val result = mBluetoothGatt?.writeCharacteristic( characteristic, payload, BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT ) val res = when (result) { BluetoothStatusCodes.SUCCESS -> true else -> false } return res } }
  • 30. BLE Common Problems 1. Android can manage up to 32 BLE connections 2. You cannot run more than 5 scan in 30 sec. 3. You cannot connect a device directly without scan unless you have bonded it 4. You have to manage your BLE instance yourself, if you saturate it an 133 Error can occur 5. Before Scan or Connect you have to check yourself if BLE is on/off (you can do it by using bleManager.adapter.isEnabled). “FUN FACT”: This information is not present in the documentation 😁
  • 31. Q&A
  • 32. DEMO TIME Android Code: https://github.com/appersiano/GDG_LedStripTree_Android Arduino Code: https://github.com/leonardocavagnis/GDG_LedStripTree_Arduino
  • 33. THANK YOU! Alessandro Persiano Email: appersiano@gmail.com LinkedIn: alessandro-persiano GitHub: appersiano Leonardo Cavagnis Email: l.cavagnis@arduino.cc LinkedIn: leonardocavagnis GitHub: leonardocavagnis