Skip to content

Commit 0f55d8e

Browse files
#37 Still trying to setup bluetooth
1 parent 33a5341 commit 0f55d8e

File tree

13 files changed

+318
-163
lines changed

13 files changed

+318
-163
lines changed

iot/.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"sstream": "cpp",
4848
"stdexcept": "cpp",
4949
"streambuf": "cpp",
50-
"cinttypes": "cpp"
50+
"cinttypes": "cpp",
51+
"m5stack.h": "c"
5152
}
5253
}

iot/src/main.cpp

Lines changed: 53 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,65 +4,79 @@
44
#include <BLEUtils.h>
55
#include <BLE2902.h>
66

7+
#define SERVICE_UUID "0693C92E-8A68-41AA-83E2-AA0B17F70168"
8+
#define CHARACTERISTIC_UUID "1F5FF96C-AA4A-4159-BCE4-6C25350CB78B"
9+
710
BLEServer *pServer = NULL;
11+
BLECharacteristic *pCharacteristic = NULL;
12+
bool deviceConnected = false;
813

914
class MyServerCallbacks : public BLEServerCallbacks
1015
{
11-
void onConnect(BLEServer *pServer)
12-
{
13-
Serial.println("Client connected");
14-
}
16+
void onConnect(BLEServer *pServer)
17+
{
18+
Serial.println("Connected");
19+
deviceConnected = true;
20+
}
1521

16-
void onDisconnect(BLEServer *pServer)
17-
{
18-
Serial.println("Client disconnected");
19-
}
22+
void onDisconnect(BLEServer *pServer)
23+
{
24+
Serial.println("Disconnected");
25+
deviceConnected = false;
26+
}
2027
};
2128

2229
class MyCallbacks : public BLECharacteristicCallbacks
2330
{
24-
void onWrite(BLECharacteristic *pCharacteristic)
25-
{
26-
std::string value = pCharacteristic->getValue();
27-
Serial.print("Received data: ");
28-
Serial.println(value.c_str());
29-
}
31+
void onWrite(BLECharacteristic *pCharacteristic)
32+
{
33+
std::string value = pCharacteristic->getValue();
34+
35+
// Handle received data
36+
Serial.print("Received data: ");
37+
Serial.println(value.c_str());
38+
}
3039
};
3140

3241
void setup()
3342
{
34-
M5.begin();
35-
Serial.begin(9600);
43+
Serial.begin(9600);
3644

37-
// Set the advertising name
38-
BLEDevice::init("M5-Stack");
39-
pServer = BLEDevice::createServer();
40-
pServer->setCallbacks(new MyServerCallbacks());
45+
// Initialize BLE
46+
BLEDevice::init("M5-Stack");
47+
pServer = BLEDevice::createServer();
48+
pServer->setCallbacks(new MyServerCallbacks());
4149

42-
// Create a service
43-
BLEService *pService = pServer->createService("4fafc201-1fb5-459e-8fcc-c5c9c331914b");
50+
// Create service and characteristic
51+
BLEService *pService = pServer->createService(SERVICE_UUID);
52+
pCharacteristic = pService->createCharacteristic(
53+
CHARACTERISTIC_UUID,
54+
BLECharacteristic::PROPERTY_READ |
55+
BLECharacteristic::PROPERTY_WRITE |
56+
BLECharacteristic::PROPERTY_NOTIFY |
57+
BLECharacteristic::PROPERTY_INDICATE);
58+
pCharacteristic->setCallbacks(new MyCallbacks());
59+
pCharacteristic->addDescriptor(new BLE2902());
4460

45-
// Create a characteristic
46-
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
47-
"beb5483e-36e1-4688-b7f5-ea07361b26a8",
48-
BLECharacteristic::PROPERTY_READ |
49-
BLECharacteristic::PROPERTY_WRITE |
50-
BLECharacteristic::PROPERTY_NOTIFY |
51-
BLECharacteristic::PROPERTY_INDICATE);
61+
// Start the service
62+
pService->start();
5263

53-
// Add a descriptor
54-
pCharacteristic->setCallbacks(new MyCallbacks());
55-
pCharacteristic->addDescriptor(new BLE2902());
64+
// Start advertising
65+
BLEAdvertising *pAdvertising = pServer->getAdvertising();
66+
pAdvertising->addServiceUUID(SERVICE_UUID);
67+
pAdvertising->start();
5668

57-
// Start the service
58-
pService->start();
59-
// Start advertising
60-
pServer->getAdvertising()->start();
61-
62-
Serial.println("BLE server started");
69+
Serial.println("BLE server started");
6370
}
6471

6572
void loop()
6673
{
67-
// Handle BLE events
74+
// Check if device is connected
75+
if (deviceConnected)
76+
{
77+
M5.Lcd.println("Device connected");
78+
// Do something when device is connected
79+
}
80+
81+
delay(1000);
6882
}

iot/src/main.cpp.bak2

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include <M5Stack.h>
2+
#include <BLEDevice.h>
3+
#include <BLEServer.h>
4+
#include <BLEUtils.h>
5+
#include <BLE2902.h>
6+
#include <set>
7+
8+
#define SERVICEUUID "0693C92E-8A68-41AA-83E2-AA0B17F70168"
9+
#define CHARACTERISTICUUID "1F5FF96C-AA4A-4159-BCE4-6C25350CB78B"
10+
11+
BLEServer *pServer = NULL;
12+
BLEService *pService = NULL;
13+
BLECharacteristic *pCharacteristic = NULL;
14+
std::set<uint16_t> connectedClients;
15+
16+
class MyServerCallbacks : public BLEServerCallbacks
17+
{
18+
void onConnect(BLEServer *pServer)
19+
{
20+
// Get connId of the connected client
21+
uint16_t connId = pServer->getConnId();
22+
connectedClients.insert(connId);
23+
Serial.print("Client connected with connId: ");
24+
Serial.println(connId);
25+
26+
String data = "Hello from Arduino!";
27+
pCharacteristic->setValue(data.c_str());
28+
pCharacteristic->notify();
29+
}
30+
31+
void onDisconnect(BLEServer *pServer)
32+
{
33+
// Get connId of the disconnected client
34+
uint16_t connId = pServer->getConnId();
35+
connectedClients.erase(connId);
36+
Serial.print("Client disconnected with connId: ");
37+
Serial.println(connId);
38+
}
39+
};
40+
41+
class MyCallbacks : public BLECharacteristicCallbacks
42+
{
43+
void onWrite(BLECharacteristic *pCharacteristic)
44+
{
45+
std::string value = pCharacteristic->getValue();
46+
Serial.print("Received data: ");
47+
Serial.println(value.c_str());
48+
}
49+
};
50+
51+
void setup()
52+
{
53+
M5.begin();
54+
Serial.begin(9600);
55+
56+
// Set the advertising name
57+
BLEDevice::init("M5-Stack");
58+
pServer = BLEDevice::createServer();
59+
pServer->setCallbacks(new MyServerCallbacks());
60+
61+
pService = pServer->createService(SERVICEUUID);
62+
pCharacteristic = pService->createCharacteristic(
63+
CHARACTERISTICUUID,
64+
BLECharacteristic::PROPERTY_READ |
65+
BLECharacteristic::PROPERTY_WRITE |
66+
BLECharacteristic::PROPERTY_NOTIFY |
67+
BLECharacteristic::PROPERTY_INDICATE);
68+
69+
// Add a descriptor
70+
pCharacteristic->setCallbacks(new MyCallbacks());
71+
pCharacteristic->addDescriptor(new BLE2902());
72+
73+
// Start the service
74+
pService->start();
75+
// Start advertising
76+
pServer->getAdvertising()->start();
77+
78+
M5.Lcd.println("BLE server started");
79+
Serial.println("BLE server started");
80+
}
81+
82+
void loop()
83+
{
84+
if (M5.BtnA.wasPressed())
85+
{
86+
M5.Lcd.println("Cleaning clients");
87+
for (auto &id : connectedClients)
88+
{
89+
M5.Lcd.println("Cleaning client: " + id);
90+
pServer->disconnect(id);
91+
}
92+
connectedClients.clear();
93+
pServer->getAdvertising()->start();
94+
}
95+
M5.update();
96+
// Handle BLE events
97+
}

mobile/android/app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ android {
8383
buildToolsVersion rootProject.ext.buildToolsVersion
8484
compileSdk rootProject.ext.compileSdkVersion
8585

86-
namespace 'com.smartbin.app'
86+
namespace 'app.smartbin.epi'
8787
defaultConfig {
88-
applicationId 'com.smartbin.app'
88+
applicationId 'app.smartbin.epi'
8989
minSdkVersion rootProject.ext.minSdkVersion
9090
targetSdkVersion rootProject.ext.targetSdkVersion
9191
versionCode 1

mobile/android/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<category android:name="android.intent.category.BROWSABLE"/>
3636
<data android:scheme="myapp"/>
3737
<data android:scheme="com.smartbin.app"/>
38+
<data android:scheme="app.smartbin.epi"/>
3839
</intent-filter>
3940
</activity>
4041
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" android:exported="false"/>

mobile/android/app/src/main/java/com/smartbin/app/MainActivity.kt renamed to mobile/android/app/src/main/java/app/smartbin/epi/MainActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.smartbin.app
1+
package app.smartbin.epi
22

33
import android.os.Build
44
import android.os.Bundle

mobile/android/app/src/main/java/com/smartbin/app/MainApplication.kt renamed to mobile/android/app/src/main/java/app/smartbin/epi/MainApplication.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.smartbin.app
1+
package app.smartbin.epi
22

33
import android.app.Application
44
import android.content.res.Configuration

mobile/app.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default ({ config }: ConfigContext): ExpoConfig => {
1010
extra: {
1111
env: "production",
1212
production: true,
13-
apiUrl: undefined,
13+
apiUrl: "https://smart-bin-jade.vercel.app/api",
1414
},
1515
};
1616
break;
@@ -21,7 +21,7 @@ export default ({ config }: ConfigContext): ExpoConfig => {
2121
extra: {
2222
env: "development",
2323
production: false,
24-
apiUrl: "http://172.20.10.2:3000/api",
24+
apiUrl: "https://smart-bin-jade.vercel.app/api",
2525
},
2626
};
2727
break;

mobile/ios/smartbinesp.xcodeproj/project.pbxproj

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
13B07FB51A68108700A75B9A /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; name = Images.xcassets; path = smartbinesp/Images.xcassets; sourceTree = "<group>"; };
2525
13B07FB61A68108700A75B9A /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = Info.plist; path = smartbinesp/Info.plist; sourceTree = "<group>"; };
2626
13B07FB71A68108700A75B9A /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = main.m; path = smartbinesp/main.m; sourceTree = "<group>"; };
27+
4B01632B2BC5981A00794DDE /* smartbinespRelease.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; name = smartbinespRelease.entitlements; path = smartbinesp/smartbinespRelease.entitlements; sourceTree = "<group>"; };
2728
58EEBF8E8E6FB1BC6CAF49B5 /* libPods-smartbinesp.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-smartbinesp.a"; sourceTree = BUILT_PRODUCTS_DIR; };
2829
6C2E3173556A471DD304B334 /* Pods-smartbinesp.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-smartbinesp.debug.xcconfig"; path = "Target Support Files/Pods-smartbinesp/Pods-smartbinesp.debug.xcconfig"; sourceTree = "<group>"; };
2930
7A4D352CD337FB3A3BF06240 /* Pods-smartbinesp.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-smartbinesp.release.xcconfig"; path = "Target Support Files/Pods-smartbinesp/Pods-smartbinesp.release.xcconfig"; sourceTree = "<group>"; };
@@ -50,6 +51,7 @@
5051
13B07FAE1A68108700A75B9A /* smartbinesp */ = {
5152
isa = PBXGroup;
5253
children = (
54+
4B01632B2BC5981A00794DDE /* smartbinespRelease.entitlements */,
5355
BB2F792B24A3F905000567C9 /* Supporting */,
5456
13B07FAF1A68108700A75B9A /* AppDelegate.h */,
5557
13B07FB01A68108700A75B9A /* AppDelegate.mm */,
@@ -365,7 +367,7 @@
365367
buildSettings = {
366368
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
367369
CLANG_ENABLE_MODULES = YES;
368-
CODE_SIGN_ENTITLEMENTS = smartbinesp/smartbinesp.entitlements;
370+
CODE_SIGN_ENTITLEMENTS = smartbinesp/smartbinespRelease.entitlements;
369371
CODE_SIGN_IDENTITY = "Apple Development";
370372
CODE_SIGN_STYLE = Automatic;
371373
CURRENT_PROJECT_VERSION = 1;
@@ -450,10 +452,7 @@
450452
ONLY_ACTIVE_ARCH = YES;
451453
OTHER_CFLAGS = "$(inherited)";
452454
OTHER_CPLUSPLUSFLAGS = "$(inherited)";
453-
OTHER_LDFLAGS = (
454-
"$(inherited)",
455-
" ",
456-
);
455+
OTHER_LDFLAGS = "$(inherited) ";
457456
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
458457
SDKROOT = iphoneos;
459458
USE_HERMES = true;
@@ -510,10 +509,7 @@
510509
MTL_ENABLE_DEBUG_INFO = NO;
511510
OTHER_CFLAGS = "$(inherited)";
512511
OTHER_CPLUSPLUSFLAGS = "$(inherited)";
513-
OTHER_LDFLAGS = (
514-
"$(inherited)",
515-
" ",
516-
);
512+
OTHER_LDFLAGS = "$(inherited) ";
517513
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
518514
SDKROOT = iphoneos;
519515
USE_HERMES = true;

0 commit comments

Comments
 (0)