1+ package net .sharksystem .asap .android .LoRaEngine ;
2+
3+ import android .bluetooth .BluetoothAdapter ;
4+ import android .bluetooth .BluetoothDevice ;
5+ import android .bluetooth .BluetoothSocket ;
6+ import android .content .Context ;
7+ import android .support .test .InstrumentationRegistry ;
8+ import android .support .test .runner .AndroidJUnit4 ;
9+
10+ import net .sharksystem .asap .android .lora .ASAPLoRaException ;
11+ import net .sharksystem .asap .android .lora .LoRaBTInputOutputStream ;
12+ import net .sharksystem .asap .android .lora .messages .DiscoverASAPLoRaMessage ;
13+
14+ import org .junit .BeforeClass ;
15+ import org .junit .FixMethodOrder ;
16+ import org .junit .Test ;
17+ import org .junit .runner .RunWith ;
18+ import org .junit .runners .MethodSorters ;
19+
20+ import java .io .BufferedReader ;
21+ import java .io .IOException ;
22+ import java .io .InputStreamReader ;
23+ import java .util .UUID ;
24+
25+ import static org .junit .Assert .assertEquals ;
26+
27+ /**
28+ * Instrumented test, which will execute on an Android device.
29+ *
30+ * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
31+ */
32+ @ RunWith (AndroidJUnit4 .class )
33+ @ FixMethodOrder (MethodSorters .NAME_ASCENDING )
34+ public class BasicCommunicationTest {
35+
36+ public static BluetoothDevice Alice ;
37+ public static BluetoothDevice Bob ;
38+ public static BluetoothSocket AliceSocket ;
39+ public static BluetoothSocket BobSocket ;
40+
41+ @ BeforeClass
42+ public static void setup () throws IOException , InterruptedException {
43+ BluetoothAdapter btAdapter = BluetoothAdapter .getDefaultAdapter ();
44+ btAdapter .cancelDiscovery ();
45+
46+ for (BluetoothDevice btDevice : btAdapter .getBondedDevices ()) {
47+ if (btDevice .getName ().indexOf ("ASAP-LoRa-1" ) == 0 ) {
48+ BasicCommunicationTest .Alice = btDevice ;
49+ }
50+ if (btDevice .getName ().indexOf ("ASAP-LoRa-2" ) == 0 ) {
51+ BasicCommunicationTest .Bob = btDevice ;
52+ }
53+ }
54+ if (BasicCommunicationTest .Alice == null || BasicCommunicationTest .Bob == null )
55+ throw new IOException ("Please Pair BT Modules ASAP-LoRa-1 and ASAP-LoRa-2 to this device!" );
56+
57+ BasicCommunicationTest .AliceSocket = BasicCommunicationTest .Alice .createRfcommSocketToServiceRecord (UUID .fromString ("00001101-0000-1000-8000-00805F9B34FB" ));
58+ BasicCommunicationTest .BobSocket = BasicCommunicationTest .Bob .createRfcommSocketToServiceRecord (UUID .fromString ("00001101-0000-1000-8000-00805F9B34FB" ));
59+
60+ BasicCommunicationTest .AliceSocket .connect ();
61+ BasicCommunicationTest .BobSocket .connect ();
62+
63+ Thread .sleep (5000 ); //Give the BT Modules some time to stabilize
64+ }
65+
66+ @ Test
67+ public void usesAppContext () {
68+ // Test if we are running in App Context
69+ Context appContext = InstrumentationRegistry .getTargetContext ();
70+ assertEquals ("net.sharksystem.asap.example" , appContext .getPackageName ());
71+ }
72+
73+ @ Test (timeout =20000 )
74+ public void deviceDiscoveryTest () throws IOException {
75+ this .AliceSocket .getOutputStream ().write ("{\" COMMAND\" :\" .DiscoverASAPLoRaMessage\" }" .getBytes ());
76+
77+ while (true ){
78+ if (this .BobSocket .getInputStream ().available () > 0 ) {
79+ BufferedReader br = new BufferedReader (new InputStreamReader (this .BobSocket .getInputStream ()));
80+ StringBuilder sb = new StringBuilder (this .BobSocket .getInputStream ().available ());
81+ do {
82+ sb .append (br .readLine ()).append ("\n " );
83+ } while (br .ready ());
84+ String deviceResponse = sb .toString ().trim ();
85+ System .out .print ("ASAP LoRaEngine Test Device Response: " );
86+ System .out .println (deviceResponse );
87+ assertEquals ("{\" COMMAND\" :\" .DeviceDiscoveredASAPLoRaMessage\" ,\" address\" :\" 0000\" }" , deviceResponse );
88+ break ;
89+ }
90+ }
91+
92+ while (true ){
93+ if (this .AliceSocket .getInputStream ().available () > 0 ) {
94+ BufferedReader br = new BufferedReader (new InputStreamReader (this .AliceSocket .getInputStream ()));
95+ StringBuilder sb = new StringBuilder (this .AliceSocket .getInputStream ().available ());
96+ do {
97+ sb .append (br .readLine ()).append ("\n " );
98+ } while (br .ready ());
99+ String deviceResponse = sb .toString ().trim ();
100+ System .out .print ("ASAP LoRaEngine Test Device Response: " );
101+ System .out .println (deviceResponse );
102+ assertEquals ("{\" COMMAND\" :\" .DeviceDiscoveredASAPLoRaMessage\" ,\" address\" :\" 0001\" }" , deviceResponse );
103+ break ;
104+ }
105+ }
106+ }
107+
108+ @ Test (timeout =10000 )
109+ public void simpleAliceToBobMessageTest () throws IOException {
110+ this .AliceSocket .getOutputStream ().write ("{\" COMMAND\" :\" .ASAPLoRaMessage\" ,\" address\" :\" 0001\" ,\" message\" :\" Hello World!\" }" .getBytes ());
111+
112+ while (true ){
113+ if (this .BobSocket .getInputStream ().available () > 0 ) {
114+ BufferedReader br = new BufferedReader (new InputStreamReader (this .BobSocket .getInputStream ()));
115+ StringBuilder sb = new StringBuilder (this .BobSocket .getInputStream ().available ());
116+ do {
117+ sb .append (br .readLine ()).append ("\n " );
118+ } while (br .ready ());
119+ String deviceResponse = sb .toString ().trim ();
120+ System .out .print ("ASAP LoRaEngine Test Device Response: " );
121+ System .out .println (deviceResponse );
122+ assertEquals ("{\" COMMAND\" :\" .ASAPLoRaMessage\" ,\" address\" :\" 0000\" ,\" message\" :\" Hello World!\" }" , deviceResponse );
123+ break ;
124+ }
125+ }
126+ }
127+
128+ @ Test (timeout =10000 )
129+ public void simpleBobToAliceMessageTest () throws IOException {
130+ this .BobSocket .getOutputStream ().write ("{\" COMMAND\" :\" .ASAPLoRaMessage\" ,\" address\" :\" 0000\" ,\" message\" :\" Hello World!\" }" .getBytes ());
131+
132+ while (true ){
133+ if (this .AliceSocket .getInputStream ().available () > 0 ) {
134+ BufferedReader br = new BufferedReader (new InputStreamReader (this .AliceSocket .getInputStream ()));
135+ StringBuilder sb = new StringBuilder (this .AliceSocket .getInputStream ().available ());
136+ do {
137+ sb .append (br .readLine ()).append ("\n " );
138+ } while (br .ready ());
139+ String deviceResponse = sb .toString ().trim ();
140+ System .out .print ("ASAP LoRaEngine Test Device Response: " );
141+ System .out .println (deviceResponse );
142+ assertEquals ("{\" COMMAND\" :\" .ASAPLoRaMessage\" ,\" address\" :\" 0001\" ,\" message\" :\" Hello World!\" }" , deviceResponse );
143+ break ;
144+ }
145+ }
146+ }
147+ }
0 commit comments