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 org .junit .BeforeClass ;
11+ import org .junit .FixMethodOrder ;
12+ import org .junit .Test ;
13+ import org .junit .runner .RunWith ;
14+ import org .junit .runners .MethodSorters ;
15+
16+ import java .io .BufferedReader ;
17+ import java .io .IOException ;
18+ import java .io .InputStreamReader ;
19+ import java .util .UUID ;
20+
21+ import static org .junit .Assert .assertEquals ;
22+
23+ /**
24+ * Instrumented test, which will execute on an Android device.
25+ *
26+ * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
27+ */
28+ @ RunWith (AndroidJUnit4 .class )
29+ @ FixMethodOrder (MethodSorters .NAME_ASCENDING )
30+ public class SimultaneousCommunicationTest {
31+
32+ public static BluetoothDevice Alice ;
33+ public static BluetoothDevice Bob ;
34+ public static BluetoothSocket AliceSocket ;
35+ public static BluetoothSocket BobSocket ;
36+
37+ @ BeforeClass
38+ public static void setup () throws IOException , InterruptedException {
39+ BluetoothAdapter btAdapter = BluetoothAdapter .getDefaultAdapter ();
40+ btAdapter .cancelDiscovery ();
41+
42+ for (BluetoothDevice btDevice : btAdapter .getBondedDevices ()) {
43+ if (btDevice .getName ().indexOf ("ASAP-LoRa-1" ) == 0 ) {
44+ SimultaneousCommunicationTest .Alice = btDevice ;
45+ }
46+ if (btDevice .getName ().indexOf ("ASAP-LoRa-2" ) == 0 ) {
47+ SimultaneousCommunicationTest .Bob = btDevice ;
48+ }
49+ }
50+ if (SimultaneousCommunicationTest .Alice == null || SimultaneousCommunicationTest .Bob == null )
51+ throw new IOException ("Please Pair BT Modules ASAP-LoRa-1 and ASAP-LoRa-2 to this device!" );
52+
53+ SimultaneousCommunicationTest .AliceSocket = SimultaneousCommunicationTest .Alice .createRfcommSocketToServiceRecord (UUID .fromString ("00001101-0000-1000-8000-00805F9B34FB" ));
54+ SimultaneousCommunicationTest .BobSocket = SimultaneousCommunicationTest .Bob .createRfcommSocketToServiceRecord (UUID .fromString ("00001101-0000-1000-8000-00805F9B34FB" ));
55+
56+ SimultaneousCommunicationTest .AliceSocket .connect ();
57+ SimultaneousCommunicationTest .BobSocket .connect ();
58+
59+ Thread .sleep (2000 ); //Give the BT Modules some time to stabilize
60+ }
61+
62+ @ Test
63+ public void usesAppContext () {
64+ // Test if we are running in App Context
65+ Context appContext = InstrumentationRegistry .getTargetContext ();
66+ assertEquals ("net.sharksystem.asap.example" , appContext .getPackageName ());
67+ }
68+
69+ @ Test (timeout = 60000 )
70+ public void simultaneousMessageTest () throws IOException {
71+ this .BobSocket .getOutputStream ().write ("MSSGE@1000:Simultan Hello Alice!\n " .getBytes ());
72+ this .AliceSocket .getOutputStream ().write ("MSSGE@1001:Simultan Hello Bob!\n " .getBytes ());
73+
74+ while (true ) {
75+ if (this .BobSocket .getInputStream ().available () > 0 ) {
76+ BufferedReader br = new BufferedReader (new InputStreamReader (this .BobSocket .getInputStream ()));
77+ String deviceResponse = br .readLine ().trim ();
78+ System .out .print ("ASAP LoRaEngine Test Device Response: " );
79+ System .out .println (deviceResponse );
80+ assertEquals ("MSSGE@1000:Simultan Hello Bob!" , deviceResponse );
81+ break ;
82+ }
83+ }
84+
85+ while (true ) {
86+ if (this .AliceSocket .getInputStream ().available () > 0 ) {
87+ BufferedReader br = new BufferedReader (new InputStreamReader (this .AliceSocket .getInputStream ()));
88+ String deviceResponse = br .readLine ().trim ();
89+ System .out .print ("ASAP LoRaEngine Test Device Response: " );
90+ System .out .println (deviceResponse );
91+ assertEquals ("MSSGE@1001:Simultan Hello Alice!" , deviceResponse );
92+ break ;
93+ }
94+ }
95+ }
96+ /*
97+ @Test(timeout = 240000)
98+ public void tenSimultaneousMessageTest() throws IOException {
99+ int rounds = 10;
100+ for (int i = 0; i < rounds; i++) {
101+ this.BobSocket.getOutputStream().write("MSSGE@1000:Simultan Hello Alice!\n".getBytes());
102+ this.AliceSocket.getOutputStream().write("MSSGE@1001:Simultan Hello Bob!\n".getBytes());
103+ }
104+
105+ int AliceCounter = 0;
106+ int BobCounter = 0;
107+ while (true) {
108+ if (this.AliceSocket.getInputStream().available() > 0) {
109+ BufferedReader br = new BufferedReader(new InputStreamReader(this.AliceSocket.getInputStream()));
110+ String deviceResponse = br.readLine().trim();
111+ System.out.print("ASAP LoRaEngine Test Device Response: ");
112+ System.out.println(deviceResponse);
113+ assertEquals("MSSGE@1001:Simultan Hello Alice!", deviceResponse);
114+ AliceCounter++;
115+ System.out.print("Found that much correct answers from Bob: ");
116+ System.out.println(AliceCounter);
117+ }
118+ if (this.BobSocket.getInputStream().available() > 0) {
119+ BufferedReader br = new BufferedReader(new InputStreamReader(this.BobSocket.getInputStream()));
120+ String deviceResponse = br.readLine().trim();
121+ System.out.print("ASAP LoRaEngine Test Device Response: ");
122+ System.out.println(deviceResponse);
123+ assertEquals("MSSGE@1000:Simultan Hello Bob!", deviceResponse);
124+ BobCounter++;
125+ System.out.print("Found that much correct answers from Alice: ");
126+ System.out.println(BobCounter);
127+ }
128+ if (AliceCounter == rounds && BobCounter == rounds) break;
129+ }
130+ assertEquals(rounds, AliceCounter);
131+ assertEquals(rounds, BobCounter);
132+ }
133+ */
134+ @ Test (timeout = 240000 )
135+ public void tenSimultaneousOrderedMessageTest () throws IOException {
136+ int rounds = 10 ;
137+ for (int i = 0 ; i < rounds ; i ++) {
138+ this .BobSocket .getOutputStream ().write (("MSSGE@1000:Hello Alice Nr. " + String .valueOf (i ) + "\n " ).getBytes ());
139+ this .AliceSocket .getOutputStream ().write (("MSSGE@1001:Hello Bob Nr. " + String .valueOf (i ) + "\n " ).getBytes ());
140+ }
141+
142+ int AliceCounter = 0 ;
143+ int BobCounter = 0 ;
144+ while (AliceCounter < rounds || BobCounter < rounds ) {
145+ if (this .AliceSocket .getInputStream ().available () > 0 ) {
146+ BufferedReader br = new BufferedReader (new InputStreamReader (this .AliceSocket .getInputStream ()));
147+ String deviceResponse = br .readLine ().trim ();
148+ System .out .print ("ASAP LoRaEngine Test Device Response: " );
149+ System .out .println (deviceResponse );
150+ assertEquals ("MSSGE@1001:Hello Alice Nr. " + String .valueOf (AliceCounter ), deviceResponse );
151+ AliceCounter ++;
152+ System .out .print ("Found that much correct answers from Bob: " );
153+ System .out .println (AliceCounter );
154+ }
155+ if (this .BobSocket .getInputStream ().available () > 0 ) {
156+ BufferedReader br = new BufferedReader (new InputStreamReader (this .BobSocket .getInputStream ()));
157+ String deviceResponse = br .readLine ().trim ();
158+ System .out .print ("ASAP LoRaEngine Test Device Response: " );
159+ System .out .println (deviceResponse );
160+ assertEquals ("MSSGE@1000:Hello Bob Nr. " + String .valueOf (BobCounter ), deviceResponse );
161+ BobCounter ++;
162+ System .out .print ("Found that much correct answers from Alice: " );
163+ System .out .println (BobCounter );
164+ }
165+ }
166+ assertEquals (rounds , AliceCounter );
167+ assertEquals (rounds , BobCounter );
168+ }
169+ }
0 commit comments