Skip to content

Commit 7046d58

Browse files
committed
Setup Test App + LoRa Engine
Added Start/Stop LoRa Buttons + Messages for the Example App Initialized LoRaEngine, added connection test for ASAP-LoRa-Board.
1 parent 54c7303 commit 7046d58

9 files changed

Lines changed: 204 additions & 2 deletions

File tree

app/src/main/java/net/sharksystem/asap/android/ASAPServiceMethods.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ public class ASAPServiceMethods {
2424
public static final int START_RECONNECT_PAIRED_DEVICES = 106;
2525
public static final int STOP_RECONNECT_PAIRED_DEVICES = 107;
2626

27+
public static final int START_LORA = 108;
28+
public static final int STOP_LORA = 109;
29+
2730
///////////// asap engine management
2831
public static final int START_BROADCASTS = 200;
2932
public static final int STOP_BROADCASTS = 201;

app/src/main/java/net/sharksystem/asap/android/apps/ASAPActivity.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,22 @@ public void stopWifiP2P() {
254254
this.sendMessage2Service(ASAPServiceMethods.STOP_WIFI_DIRECT);
255255
}
256256

257+
/**
258+
* Call this message to start LoRa.
259+
*/
260+
public void startLoRa() {
261+
Log.d(this.getLogStart(), "send message to service: start LoRa");
262+
this.sendMessage2Service(ASAPServiceMethods.START_LORA);
263+
}
264+
265+
/**
266+
* Call this message to stop LoRa.
267+
*/
268+
public void stopLoRa() {
269+
Log.d(this.getLogStart(), "send message to service: stop LoRa");
270+
this.sendMessage2Service(ASAPServiceMethods.STOP_LORA);
271+
}
272+
257273
/**
258274
* Call this message to make this device discoverable with Bluetooth.
259275
* asapNotifyBTDiscoverableStarted() is called later.

app/src/main/java/net/sharksystem/asap/android/example/ASAPExampleActivity.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public void onClick(View view) {
2929
View stopWifiButton = findViewById(R.id.stopWifiDirect);
3030
View startBTButton = findViewById(R.id.startBT);
3131
View stopBTButton = findViewById(R.id.stopBT);
32+
View startLoRaButton = findViewById(R.id.startLoRa);
33+
View stopLoRaButton = findViewById(R.id.stopLoRa);
3234

3335
if(view == startWifiButton) {
3436
Log.d(this.getLogStart(), "start wifi button pressed - send message");
@@ -62,6 +64,14 @@ else if(view == findViewById(R.id.startDiscoverableAndDiscovery)) {
6264
super.startBluetoothDiscovery();
6365
super.startBluetoothDiscoverable();
6466
}
67+
else if(view == startLoRaButton) {
68+
Log.d(this.getLogStart(), "start LoRa button pressed - send message");
69+
super.startLoRa();
70+
}
71+
else if(view == stopLoRaButton) {
72+
Log.d(this.getLogStart(), "stop LoRa button pressed - send message");
73+
super.stopLoRa();
74+
}
6575
}
6676

6777
///////////////////////////////////////////////////////////////////////////////////////////

app/src/main/java/net/sharksystem/asap/android/example/ASAPExampleApplication.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import net.sharksystem.asap.ASAP;
66
import net.sharksystem.asap.android.apps.ASAPApplication;
7+
import net.sharksystem.asap.android.apps.ASAPComponentNotYetInitializedException;
78

89
import java.util.ArrayList;
910
import java.util.Collection;
@@ -13,6 +14,14 @@ public class ASAPExampleApplication extends ASAPApplication {
1314
private CharSequence id;
1415
static ASAPExampleApplication instance = null;
1516

17+
public static ASAPExampleApplication getASAPApplication() {
18+
if(ASAPExampleApplication.instance == null) {
19+
throw new ASAPComponentNotYetInitializedException("ASAP Example Application not yet initialized");
20+
}
21+
22+
return ASAPExampleApplication.instance;
23+
}
24+
1625
static ASAPExampleApplication initializeASAPExampleApplication(Activity initialActivity) {
1726
if(ASAPExampleApplication.instance == null) {
1827
Collection<CharSequence> formats = new ArrayList<>();
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package net.sharksystem.asap.android.lora;
2+
3+
import net.sharksystem.asap.ASAPException;
4+
5+
public class ASAPLoRaException extends ASAPException {
6+
7+
public ASAPLoRaException(String message) {
8+
super(message);
9+
}
10+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package net.sharksystem.asap.android.lora;
2+
3+
import android.content.Context;
4+
import android.util.Log;
5+
6+
import net.sharksystem.asap.android.service.ASAPService;
7+
import net.sharksystem.asap.android.service.MacLayerEngine;
8+
9+
import android.bluetooth.BluetoothAdapter;
10+
import android.bluetooth.BluetoothDevice;
11+
import android.bluetooth.BluetoothSocket;
12+
13+
import java.io.BufferedReader;
14+
import java.io.IOException;
15+
import java.io.InputStreamReader;
16+
import java.util.UUID;
17+
18+
public class LoRaEngine extends MacLayerEngine {
19+
20+
private static final String CLASS_LOG_TAG = "ASAPLoRaEngine";
21+
22+
23+
private static LoRaEngine engine = null;
24+
private BluetoothDevice btDevice = null;
25+
26+
/**
27+
* - Aufbau Verbindung zu BLE o. Bluetooth UART Schnittstelle
28+
* - Prüfen der Verbindung zum LoRa Board AT <-> AT+OK
29+
*
30+
* @param ASAPService
31+
* @param context
32+
* @return
33+
*/
34+
public static LoRaEngine getASAPLoRaEngine(ASAPService ASAPService,
35+
Context context) {
36+
if (LoRaEngine.engine == null) {
37+
LoRaEngine.engine = new LoRaEngine(ASAPService, context);
38+
}
39+
40+
return LoRaEngine.engine;
41+
}
42+
43+
public static LoRaEngine getASAPLoRaEngine() {
44+
return LoRaEngine.engine;
45+
}
46+
47+
public LoRaEngine(ASAPService asapService, Context context) {
48+
super(asapService, context);
49+
}
50+
51+
private void initBluetooth() throws ASAPLoRaException {
52+
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
53+
btAdapter.cancelDiscovery();
54+
55+
for (BluetoothDevice btDevice : btAdapter.getBondedDevices()) {
56+
if (btDevice.getName().indexOf("ASAP-LoRa") == 0) { //TODO: What about more than 1 paired ASAP-LoRa Board? Or 1 avail and 1 unavail?
57+
this.btDevice = btDevice;
58+
break;
59+
}
60+
}
61+
if (this.btDevice == null)
62+
throw new ASAPLoRaException("Please pair to an ASAP-LoRa Board before Starting LoRa!");
63+
64+
/**
65+
* uses UUID of Serial Devices for now - https://www.bluetooth.com/specifications/assigned-numbers/
66+
* Might be a good idea to move another uuid to define a ASAP-LoRa Node
67+
*/
68+
try {
69+
BluetoothSocket btSocket = btDevice.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
70+
btSocket.connect();
71+
btSocket.getOutputStream().write("AT".getBytes());
72+
73+
while(true){ //TODO: Do not Activewait...
74+
if(btSocket.getInputStream().available() > 0) {
75+
BufferedReader br = new BufferedReader(new InputStreamReader(btSocket.getInputStream()));
76+
StringBuilder sb = new StringBuilder(btSocket.getInputStream().available());
77+
do {
78+
sb.append(br.readLine()).append("\n");
79+
} while(br.ready());
80+
Log.i(this.CLASS_LOG_TAG, "LoRa Board said: "+sb.toString());
81+
break;
82+
}
83+
}
84+
} catch (IOException e) {
85+
e.printStackTrace();
86+
}
87+
}
88+
89+
@Override
90+
public void start() {
91+
Log.i(this.CLASS_LOG_TAG, "MacLayerEngine.start() called");
92+
try {
93+
this.initBluetooth();
94+
} catch (ASAPLoRaException e) {
95+
Log.e(this.CLASS_LOG_TAG, e.getMessage());
96+
}
97+
}
98+
99+
@Override
100+
public void stop() {
101+
Log.i(this.CLASS_LOG_TAG, "MacLayerEngine.stop() called");
102+
103+
}
104+
105+
@Override
106+
public boolean tryReconnect() {
107+
Log.i(this.CLASS_LOG_TAG, "MacLayerEngine.tryReconnect() called");
108+
return false;
109+
}
110+
111+
@Override
112+
public void checkConnectionStatus() {
113+
Log.i(this.CLASS_LOG_TAG, "MacLayerEngine.checkConnectionStatus() called");
114+
115+
}
116+
}

app/src/main/java/net/sharksystem/asap/android/service/ASAPMessageHandler.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ public void handleMessage(Message msg) {
8282
this.asapService.startReconnectPairedDevices();
8383
break;
8484

85+
case ASAPServiceMethods.START_LORA:
86+
this.asapService.startLoRa();
87+
break;
88+
89+
case ASAPServiceMethods.STOP_LORA:
90+
this.asapService.stopLoRa();
91+
break;
92+
8593
default:
8694
super.handleMessage(msg);
8795
}

app/src/main/java/net/sharksystem/asap/android/service/ASAPService.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import net.sharksystem.asap.android.ASAPServiceCreationIntent;
2323
import net.sharksystem.asap.android.Util;
2424
import net.sharksystem.asap.android.bluetooth.BluetoothEngine;
25+
import net.sharksystem.asap.android.lora.LoRaEngine;
2526
import net.sharksystem.asap.android.service2AppMessaging.ASAPServiceRequestNotifyIntent;
2627
import net.sharksystem.asap.android.wifidirect.WifiP2PEngine;
2728
import net.sharksystem.asap.util.Helper;
@@ -270,6 +271,23 @@ public void startBluetoothDiscovery() throws ASAPException {
270271
}
271272
}
272273

274+
//////////////////////////////////////////////////////////////////////////////////////
275+
// LoRa via BT //
276+
//////////////////////////////////////////////////////////////////////////////////////
277+
278+
void startLoRa() {
279+
Log.d(this.getLogStart(), "start LoRa");
280+
LoRaEngine.getASAPLoRaEngine(this, this).start();
281+
}
282+
283+
void stopLoRa() {
284+
Log.d(this.getLogStart(), "start LoRa");
285+
LoRaEngine ASAPLoRaEngine = LoRaEngine.getASAPLoRaEngine();
286+
if(ASAPLoRaEngine != null) {
287+
ASAPLoRaEngine.stop();
288+
}
289+
}
290+
273291
//////////////////////////////////////////////////////////////////////////////////////
274292
// status management //
275293
//////////////////////////////////////////////////////////////////////////////////////

app/src/main/res/layout/example_layout.xml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232

3333
<Button
3434
android:id="@+id/startBT"
35+
android:layout_gravity="left|top"
3536
android:onClick="onClick"
36-
android:text="startBT"
37-
android:layout_gravity="left|top" />
37+
android:text="startBT" />
3838

3939
<Button
4040
android:id="@+id/stopBT"
@@ -68,6 +68,18 @@
6868
android:text="nothing"
6969
android:layout_gravity="left|top" />
7070

71+
<Button
72+
android:id="@+id/startLoRa"
73+
android:onClick="onClick"
74+
android:text="StartLoRa"
75+
android:layout_gravity="left|top" />
76+
77+
<Button
78+
android:id="@+id/stopLoRa"
79+
android:onClick="onClick"
80+
android:text="StopLoRa"
81+
android:layout_gravity="left|top" />
82+
7183
<Button
7284
android:id="@+id/setupCleanASAPStorageButton"
7385
android:onClick="onSetupCleanASAPStorageClick"

0 commit comments

Comments
 (0)