-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsta_BLE.ino
296 lines (248 loc) · 9.34 KB
/
Insta_BLE.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
Author: Patrick Chwalek (09/22/2023)
Description: Example code used to control Insta360 X3 and RS 1-inch cameras
*/
#include <BLE2902.h>
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLEAdvertising.h>
#define SERVICE_UUID "ce80"
#define CHARACTERISTIC_UUID1 "ce81"
#define CHARACTERISTIC_UUID2 "ce82"
#define CHARACTERISTIC_UUID3 "ce83"
#define SECONDARY_SERVICE_UUID "0000D0FF-3C17-D293-8E48-14FE2E4DA212"
#define SECONDARY_CHARACTERISTIC_UUID1 "ffd1"
#define SECONDARY_CHARACTERISTIC_UUID2 "ffd2"
#define SECONDARY_CHARACTERISTIC_UUID3 "ffd3"
#define SECONDARY_CHARACTERISTIC_UUID4 "ffd4"
#define SECONDARY_CHARACTERISTIC_UUID5 "ffd5"
#define SECONDARY_CHARACTERISTIC_UUID8 "ffd8"
#define SECONDARY_CHARACTERISTIC_UUID9 "fff1"
#define SECONDARY_CHARACTERISTIC_UUID10 "fff2"
#define SECONDARY_CHARACTERISTIC_UUID11 "ffe0"
#define CAPTURE_DELAY 120000
uint32_t data_32;
uint16_t data_16;
uint8_t data_8[30];
BLEServer *pServer = NULL;
BLE2902 *pDescriptor2902;
bool deviceConnected = false;
bool oldDeviceConnected = false;
BLECharacteristic *pCharacteristicRx;
unsigned long myTime;
uint8_t manuf_data[30];
class MyServerCallbacks : public BLEServerCallbacks {
void onConnect(BLEServer *pServer) {
deviceConnected = true;
myTime = millis();
BLEDevice::startAdvertising();
};
void onDisconnect(BLEServer *pServer) { deviceConnected = false; }
};
void setup() {
Serial.begin(115200);
Serial.println("Starting BLE work!");
BLEDevice::init("Insta360 GPS Remote");
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharacteristic1 = pService->createCharacteristic(
CHARACTERISTIC_UUID1, BLECharacteristic::PROPERTY_WRITE);
pCharacteristicRx = pService->createCharacteristic(
CHARACTERISTIC_UUID2, BLECharacteristic::PROPERTY_NOTIFY);
pDescriptor2902 = new BLE2902();
pDescriptor2902->setNotifications(true);
pDescriptor2902->setIndications(true);
pCharacteristicRx->addDescriptor(pDescriptor2902);
data_8[0] = 0;
pCharacteristicRx->setValue(data_8, 1);
BLECharacteristic *pCharacteristic3 = pService->createCharacteristic(
CHARACTERISTIC_UUID3, BLECharacteristic::PROPERTY_READ);
data_16 = 0x0201;
pCharacteristic3->setValue(data_16);
BLEService *pService2 = pServer->createService(SECONDARY_SERVICE_UUID);
BLECharacteristic *secondary_pCharacteristic1 =
pService2->createCharacteristic(SECONDARY_CHARACTERISTIC_UUID1,
BLECharacteristic::PROPERTY_WRITE);
BLECharacteristic *secondary_pCharacteristic2 =
pService2->createCharacteristic(SECONDARY_CHARACTERISTIC_UUID2,
BLECharacteristic::PROPERTY_READ);
BLECharacteristic *secondary_pCharacteristic3 =
pService2->createCharacteristic(SECONDARY_CHARACTERISTIC_UUID3,
BLECharacteristic::PROPERTY_READ);
data_32 = 0x301e9001;
secondary_pCharacteristic3->setValue(data_32);
BLECharacteristic *secondary_pCharacteristic4 =
pService2->createCharacteristic(SECONDARY_CHARACTERISTIC_UUID4,
BLECharacteristic::PROPERTY_READ);
data_32 = 0x18002001;
secondary_pCharacteristic4->setValue(data_32);
BLECharacteristic *secondary_pCharacteristic5 =
pService2->createCharacteristic(SECONDARY_CHARACTERISTIC_UUID5,
BLECharacteristic::PROPERTY_READ);
BLECharacteristic *secondary_pCharacteristic8 =
pService2->createCharacteristic(SECONDARY_CHARACTERISTIC_UUID8,
BLECharacteristic::PROPERTY_WRITE);
BLECharacteristic *secondary_pCharacteristic9 =
pService2->createCharacteristic(SECONDARY_CHARACTERISTIC_UUID9,
BLECharacteristic::PROPERTY_READ);
BLECharacteristic *secondary_pCharacteristic10 =
pService2->createCharacteristic(SECONDARY_CHARACTERISTIC_UUID10,
BLECharacteristic::PROPERTY_WRITE);
BLECharacteristic *secondary_pCharacteristic11 =
pService2->createCharacteristic(SECONDARY_CHARACTERISTIC_UUID11,
BLECharacteristic::PROPERTY_READ);
pService->start();
pService2->start();
// BLEAdvertising *pAdvertising = pServer->getAdvertising(); // this still
// is working for backward compatibility
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->addServiceUUID(SECONDARY_SERVICE_UUID);
pAdvertising->setScanResponse(true);
pAdvertising->setMinPreferred(
0x06); // functions that help with iPhone connections issue
pAdvertising->setMinPreferred(0x12);
BLEDevice::startAdvertising();
Serial.println(
"Characteristic defined! Now you can read it in your phone!");
/* set the manufacturing data for wakeon packet */
manuf_data[0] = 0x4c;
manuf_data[1] = 0x00;
manuf_data[2] = 0x02;
manuf_data[3] = 0x15;
manuf_data[4] = 0x09;
manuf_data[5] = 0x4f;
manuf_data[6] = 0x52;
manuf_data[7] = 0x42;
manuf_data[8] = 0x49;
manuf_data[9] = 0x54;
manuf_data[10] = 0x09;
manuf_data[11] = 0xff;
manuf_data[12] = 0x0f;
manuf_data[13] = 0x00;
/* note: see powerOnPrevConnectedCameras() for bytes 14-19 */
manuf_data[20] = 0x00;
manuf_data[21] = 0x00;
manuf_data[22] = 0x00;
manuf_data[23] = 0x00;
manuf_data[24] = 0xe4;
manuf_data[25] = 0x01;
}
void loop() {
// notify changed value
if (deviceConnected) {
delay(10); // bluetooth stack will go into congestion, if too many packets
}
// disconnecting
if (!deviceConnected && oldDeviceConnected) {
delay(500); // give the bluetooth stack the chance to get things ready
pServer->startAdvertising(); // restart advertising
Serial.println("start advertising");
oldDeviceConnected = deviceConnected;
}
// connecting
if (deviceConnected && !oldDeviceConnected) {
// do stuff here on connecting
Serial.println("connecting");
oldDeviceConnected = deviceConnected;
}
/* this loop just goes through all the features */
if (((millis() - myTime) > CAPTURE_DELAY)) {
Serial.println("capture");
myTime = millis();
shutterButton(pCharacteristicRx);
delay(30000);
Serial.println("screen off");
screenToggle(pCharacteristicRx);
delay(5000);
Serial.println("screen on");
screenToggle(pCharacteristicRx);
delay(5000);
Serial.println("power off");
powerOff(pCharacteristicRx);
delay(30000);
Serial.println("power on");
BLEDevice::stopAdvertising();
powerOnPrevConnectedCameras();
}
}
void screenToggle(BLECharacteristic *characteristic) {
data_8[0] = 0xfc;
data_8[1] = 0xef;
data_8[2] = 0xfe;
data_8[3] = 0x86;
data_8[4] = 0x00;
data_8[5] = 0x03;
data_8[6] = 0x01;
data_8[7] = 0x00;
data_8[8] = 0x00;
characteristic->setValue(data_8, 9);
characteristic->notify();
}
void powerOff(BLECharacteristic *characteristic) {
data_8[0] = 0xfc;
data_8[1] = 0xef;
data_8[2] = 0xfe;
data_8[3] = 0x86;
data_8[4] = 0x00;
data_8[5] = 0x03;
data_8[6] = 0x01;
data_8[7] = 0x00;
data_8[8] = 0x03;
characteristic->setValue(data_8, 9);
characteristic->notify();
}
void powerOnPrevConnectedCameras() {
/* the below might be camera specific and you may need to sniff them yourself for your camera */
/* used for Insta360 X3 */
// manuf_data[14] = 0x37;
// manuf_data[15] = 0x4b;
// manuf_data[16] = 0x43;
// manuf_data[17] = 0x4d;
// manuf_data[18] = 0x54;
// manuf_data[19] = 0x4b;
/* used for Insta360 RS 1-inch */
manuf_data[14] = 0x38;
manuf_data[15] = 0x51;
manuf_data[16] = 0x53;
manuf_data[17] = 0x4a;
manuf_data[18] = 0x38;
manuf_data[19] = 0x52;
// BLEAdvertisementData advertisementData;
// advertisementData.setName("Insta360 GPS Remote");
// advertisementData.setFlags(ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_DMT_CONTROLLER_SPT | ESP_BLE_ADV_FLAG_DMT_HOST_SPT);
// advertisementData.setManufacturerData((char *) manuf_data);
// BLEAdvertising* advertisement = new BLEAdvertising();
// advertisement->setAdvertisementData(advertisementData);
// advertisement->start();
// custom function that adds manufacturing data and modifies flags of advertisement
BLEDevice::startAdvertisingWithManufData(manuf_data);
}
void modeButton(BLECharacteristic *characteristic) {
data_8[0] = 0xfc;
data_8[1] = 0xef;
data_8[2] = 0xfe;
data_8[3] = 0x86;
data_8[4] = 0x00;
data_8[5] = 0x03;
data_8[6] = 0x01;
data_8[7] = 0x01;
data_8[8] = 0x00;
characteristic->setValue(data_8, 9);
characteristic->notify();
}
void shutterButton(BLECharacteristic *characteristic) {
data_8[0] = 0xfc;
data_8[1] = 0xef;
data_8[2] = 0xfe;
data_8[3] = 0x86;
data_8[4] = 0x00;
data_8[5] = 0x03;
data_8[6] = 0x01;
data_8[7] = 0x02;
data_8[8] = 0x00;
characteristic->setValue(data_8, 9);
characteristic->notify();
}