Skip to content

Commit 786f44c

Browse files
author
Eric
committed
ESP32 | FLUTER | BLE | Local Notifications
1 parent 3a017e7 commit 786f44c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+2465
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
Video: https://www.youtube.com/watch?v=oCMOYS71NIU
3+
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleNotify.cpp
4+
Ported to Arduino ESP32 by Evandro Copercini
5+
updated by chegewara
6+
7+
Create a BLE server that, once we receive a connection, will send periodic notifications.
8+
The service advertises itself as: 4fafc201-1fb5-459e-8fcc-c5c9c331914b
9+
And has a characteristic of: beb5483e-36e1-4688-b7f5-ea07361b26a8
10+
11+
The design of creating the BLE server is:
12+
1. Create a BLE Server
13+
2. Create a BLE Service
14+
3. Create a BLE Characteristic on the Service
15+
4. Create a BLE Descriptor on the characteristic
16+
5. Start the service.
17+
6. Start advertising.
18+
19+
A connect hander associated with the server starts a background task that performs notification
20+
every couple of seconds.
21+
*/
22+
#include <BLEDevice.h>
23+
#include <BLEServer.h>
24+
#include <BLEUtils.h>
25+
#include <BLE2902.h>
26+
27+
BLEServer* pServer = NULL;
28+
BLECharacteristic* pCharacteristic = NULL;
29+
bool deviceConnected = false;
30+
bool oldDeviceConnected = false;
31+
uint32_t value = 0;
32+
33+
// See the following for generating UUIDs:
34+
// https://www.uuidgenerator.net/
35+
36+
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
37+
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
38+
39+
40+
class MyServerCallbacks: public BLEServerCallbacks {
41+
void onConnect(BLEServer* pServer) {
42+
deviceConnected = true;
43+
BLEDevice::startAdvertising();
44+
};
45+
46+
void onDisconnect(BLEServer* pServer) {
47+
deviceConnected = false;
48+
}
49+
};
50+
51+
52+
53+
void setup() {
54+
Serial.begin(115200);
55+
56+
// Create the BLE Device
57+
BLEDevice::init("ESP32 THAT PROJECT");
58+
59+
// Create the BLE Server
60+
pServer = BLEDevice::createServer();
61+
pServer->setCallbacks(new MyServerCallbacks());
62+
63+
// Create the BLE Service
64+
BLEService *pService = pServer->createService(SERVICE_UUID);
65+
66+
// Create a BLE Characteristic
67+
pCharacteristic = pService->createCharacteristic(
68+
CHARACTERISTIC_UUID,
69+
BLECharacteristic::PROPERTY_READ |
70+
BLECharacteristic::PROPERTY_WRITE |
71+
BLECharacteristic::PROPERTY_NOTIFY |
72+
BLECharacteristic::PROPERTY_INDICATE
73+
);
74+
75+
// https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
76+
// Create a BLE Descriptor
77+
pCharacteristic->addDescriptor(new BLE2902());
78+
79+
// Start the service
80+
pService->start();
81+
82+
// Start advertising
83+
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
84+
pAdvertising->addServiceUUID(SERVICE_UUID);
85+
pAdvertising->setScanResponse(false);
86+
pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter
87+
BLEDevice::startAdvertising();
88+
Serial.println("Waiting a client connection to notify...");
89+
}
90+
91+
void loop() {
92+
// notify changed value
93+
if (deviceConnected) {
94+
pCharacteristic->setValue((uint8_t*)&value, 4);
95+
pCharacteristic->notify();
96+
value++;
97+
delay(500); // bluetooth stack will go into congestion, if too many packets are sent, in 6 hours test i was able to go as low as 3ms
98+
}
99+
// disconnecting
100+
if (!deviceConnected && oldDeviceConnected) {
101+
delay(500); // give the bluetooth stack the chance to get things ready
102+
pServer->startAdvertising(); // restart advertising
103+
Serial.println("start advertising");
104+
oldDeviceConnected = deviceConnected;
105+
}
106+
// connecting
107+
if (deviceConnected && !oldDeviceConnected) {
108+
// do stuff here on connecting
109+
oldDeviceConnected = deviceConnected;
110+
}
111+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Miscellaneous
2+
*.class
3+
*.log
4+
*.pyc
5+
*.swp
6+
.DS_Store
7+
.atom/
8+
.buildlog/
9+
.history
10+
.svn/
11+
12+
# IntelliJ related
13+
*.iml
14+
*.ipr
15+
*.iws
16+
.idea/
17+
18+
# The .vscode folder contains launch configuration and tasks you configure in
19+
# VS Code which you may wish to be included in version control, so this line
20+
# is commented out by default.
21+
#.vscode/
22+
23+
# Flutter/Dart/Pub related
24+
**/doc/api/
25+
.dart_tool/
26+
.flutter-plugins
27+
.packages
28+
.pub-cache/
29+
.pub/
30+
/build/
31+
32+
# Android related
33+
**/android/**/gradle-wrapper.jar
34+
**/android/.gradle
35+
**/android/captures/
36+
**/android/gradlew
37+
**/android/gradlew.bat
38+
**/android/local.properties
39+
**/android/**/GeneratedPluginRegistrant.java
40+
41+
# iOS/XCode related
42+
**/ios/**/*.mode1v3
43+
**/ios/**/*.mode2v3
44+
**/ios/**/*.moved-aside
45+
**/ios/**/*.pbxuser
46+
**/ios/**/*.perspectivev3
47+
**/ios/**/*sync/
48+
**/ios/**/.sconsign.dblite
49+
**/ios/**/.tags*
50+
**/ios/**/.vagrant/
51+
**/ios/**/DerivedData/
52+
**/ios/**/Icon?
53+
**/ios/**/Pods/
54+
**/ios/**/.symlinks/
55+
**/ios/**/profile
56+
**/ios/**/xcuserdata
57+
**/ios/.generated/
58+
**/ios/Flutter/App.framework
59+
**/ios/Flutter/Flutter.framework
60+
**/ios/Flutter/Generated.xcconfig
61+
**/ios/Flutter/app.flx
62+
**/ios/Flutter/app.zip
63+
**/ios/Flutter/flutter_assets/
64+
**/ios/ServiceDefinitions.json
65+
**/ios/Runner/GeneratedPluginRegistrant.*
66+
67+
# Exceptions to above rules.
68+
!**/ios/**/default.mode1v3
69+
!**/ios/**/default.mode2v3
70+
!**/ios/**/default.pbxuser
71+
!**/ios/**/default.perspectivev3
72+
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This file tracks properties of this Flutter project.
2+
# Used by Flutter tool to assess capabilities and perform upgrades etc.
3+
#
4+
# This file should be version controlled and should not be manually edited.
5+
6+
version:
7+
revision: 954714c9674af0da6530795407fc66179fdaef88
8+
channel: dev
9+
10+
project_type: app
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ios/Pods
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# flutter_blue_localnotify
2+
3+
A new Flutter application.
4+
5+
## Getting Started
6+
7+
This project is a starting point for a Flutter application.
8+
9+
A few resources to get you started if this is your first Flutter project:
10+
11+
- [Lab: Write your first Flutter app](https://flutter.dev/docs/get-started/codelab)
12+
- [Cookbook: Useful Flutter samples](https://flutter.dev/docs/cookbook)
13+
14+
For help getting started with Flutter, view our
15+
[online documentation](https://flutter.dev/docs), which offers tutorials,
16+
samples, guidance on mobile development, and a full API reference.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
def localProperties = new Properties()
2+
def localPropertiesFile = rootProject.file('local.properties')
3+
if (localPropertiesFile.exists()) {
4+
localPropertiesFile.withReader('UTF-8') { reader ->
5+
localProperties.load(reader)
6+
}
7+
}
8+
9+
def flutterRoot = localProperties.getProperty('flutter.sdk')
10+
if (flutterRoot == null) {
11+
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
12+
}
13+
14+
def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
15+
if (flutterVersionCode == null) {
16+
flutterVersionCode = '1'
17+
}
18+
19+
def flutterVersionName = localProperties.getProperty('flutter.versionName')
20+
if (flutterVersionName == null) {
21+
flutterVersionName = '1.0'
22+
}
23+
24+
apply plugin: 'com.android.application'
25+
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
26+
27+
android {
28+
compileSdkVersion 28
29+
30+
lintOptions {
31+
disable 'InvalidPackage'
32+
}
33+
34+
defaultConfig {
35+
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
36+
applicationId "com.thatproject.flutter_blue_localnotify"
37+
minSdkVersion 19
38+
targetSdkVersion 28
39+
versionCode flutterVersionCode.toInteger()
40+
versionName flutterVersionName
41+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
42+
}
43+
44+
buildTypes {
45+
release {
46+
// TODO: Add your own signing config for the release build.
47+
// Signing with the debug keys for now, so `flutter run --release` works.
48+
signingConfig signingConfigs.debug
49+
}
50+
}
51+
}
52+
53+
flutter {
54+
source '../..'
55+
}
56+
57+
dependencies {
58+
testImplementation 'junit:junit:4.12'
59+
androidTestImplementation 'androidx.test:runner:1.1.1'
60+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
package="com.thatproject.flutter_blue_localnotify">
3+
<!-- Flutter needs it to communicate with the running application
4+
to allow setting breakpoints, to provide hot reload, etc.
5+
-->
6+
<uses-permission android:name="android.permission.INTERNET"/>
7+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
package="com.thatproject.flutter_blue_localnotify">
3+
4+
<!-- io.flutter.app.FlutterApplication is an android.app.Application that
5+
calls FlutterMain.startInitialization(this); in its onCreate method.
6+
In most cases you can leave this as-is, but you if you want to provide
7+
additional functionality it is fine to subclass or reimplement
8+
FlutterApplication and put your custom class here. -->
9+
<application
10+
android:name="io.flutter.app.FlutterApplication"
11+
android:label="flutter_blue_localnotify"
12+
android:icon="@mipmap/ic_launcher">
13+
<activity
14+
android:name=".MainActivity"
15+
android:launchMode="singleTop"
16+
android:theme="@style/LaunchTheme"
17+
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
18+
android:hardwareAccelerated="true"
19+
android:windowSoftInputMode="adjustResize">
20+
<!-- This keeps the window background of the activity showing
21+
until Flutter renders its first frame. It can be removed if
22+
there is no splash screen (such as the default splash screen
23+
defined in @style/LaunchTheme). -->
24+
<meta-data
25+
android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
26+
android:value="true" />
27+
<intent-filter>
28+
<action android:name="android.intent.action.MAIN"/>
29+
<category android:name="android.intent.category.LAUNCHER"/>
30+
</intent-filter>
31+
</activity>
32+
</application>
33+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.thatproject.flutter_blue_localnotify;
2+
3+
import android.os.Bundle;
4+
import io.flutter.app.FlutterActivity;
5+
import io.flutter.plugins.GeneratedPluginRegistrant;
6+
7+
public class MainActivity extends FlutterActivity {
8+
@Override
9+
protected void onCreate(Bundle savedInstanceState) {
10+
super.onCreate(savedInstanceState);
11+
GeneratedPluginRegistrant.registerWith(this);
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- Modify this file to customize your launch splash screen -->
3+
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
4+
<item android:drawable="@android:color/white" />
5+
6+
<!-- You can insert your own image assets here -->
7+
<!-- <item>
8+
<bitmap
9+
android:gravity="center"
10+
android:src="@mipmap/launch_image" />
11+
</item> -->
12+
</layer-list>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar">
4+
<!-- Show a splash screen on the activity. Automatically removed when
5+
Flutter draws its first frame -->
6+
<item name="android:windowBackground">@drawable/launch_background</item>
7+
</style>
8+
</resources>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
package="com.thatproject.flutter_blue_localnotify">
3+
<!-- Flutter needs it to communicate with the running application
4+
to allow setting breakpoints, to provide hot reload, etc.
5+
-->
6+
<uses-permission android:name="android.permission.INTERNET"/>
7+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
buildscript {
2+
repositories {
3+
google()
4+
jcenter()
5+
}
6+
7+
dependencies {
8+
classpath 'com.android.tools.build:gradle:3.2.1'
9+
}
10+
}
11+
12+
allprojects {
13+
repositories {
14+
google()
15+
jcenter()
16+
}
17+
}
18+
19+
rootProject.buildDir = '../build'
20+
subprojects {
21+
project.buildDir = "${rootProject.buildDir}/${project.name}"
22+
}
23+
subprojects {
24+
project.evaluationDependsOn(':app')
25+
}
26+
27+
task clean(type: Delete) {
28+
delete rootProject.buildDir
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
org.gradle.jvmargs=-Xmx1536M
2+
3+
android.useAndroidX=true
4+
android.enableJetifier=true

0 commit comments

Comments
 (0)