11package eu .basicairdata .bluetoothhelper ;
22
3-
43/**
54 * BluetoothHelper Java Helper Class for Android
65 * Created by G.Capelli (BasicAirData) on 06/02/16.
4544* You can read the incoming messages attaching a Listener or using explicit polling.<br>
4645* Connection, reading and writing processes are asynchronously made using 3 separated Threads.<br>
4746* This Class is compatible with Android 4.0+
48- * @version 1.0.6b_20180513
47+ * @version 1.0.6b_20180526
4948* @author BasicAirData
5049*/
5150
@@ -77,12 +76,12 @@ public ConnectThread(BluetoothDevice device) {
7776try {
7877tmp = (BluetoothSocket )m .invoke (mmDevice ,1 );
7978 }catch (IllegalAccessException e ) {
80- // Log.w("myApp", "[! ] Unable to connect socket (IllegalAccessException): " + e);
79+ Log .w ("myApp" ,"[ ! ] Unable to connect socket (IllegalAccessException): " +e );
8180 }catch (InvocationTargetException e ) {
82- // Log.w("myApp", "[! ] Unable to connect socket (InvocationTargetException): " + e);
81+ Log .w ("myApp" ,"[ ! ] Unable to connect socket (InvocationTargetException): " +e );
8382 }
8483 }catch (NoSuchMethodException e ) {
85- // Log.w("myApp", "[! ] Unable to connect socket (NoSuchMethodException): " + e);
84+ Log .w ("myApp" ,"[ ! ] Unable to connect socket (NoSuchMethodException): " +e );
8685 }
8786mmSocket =tmp ;
8887 }
@@ -93,29 +92,33 @@ public void run() {
9392try {
9493mmSocket .connect ();
9594 }catch (IOException connectException ) {
96- // Log.w("myApp", "[! ] Connection through socket failed: " + connectException);
97- // Log.w("myApp", "[! ] Trying fallback method");
95+ Log .w ("myApp" ,"[ ! ] Connection through socket failed: " +connectException );
96+ Log .w ("myApp" ,"[ ! ] Trying fallback method" );
9897try {
9998// fallback method for android >= 4.2
10099tmp = (BluetoothSocket )mmDevice .getClass ().getMethod ("createRfcommSocket" ,new Class []{int .class }).invoke (mmDevice ,1 );
101100 }catch (IllegalAccessException e ) {
102- // Log.w("myApp", "[! ] Failed to create fallback Illegal Access: " + e);
101+ Log .w ("myApp" ,"[ ! ] Failed to create fallback Illegal Access: " +e );
103102return ;
104103 }catch (IllegalArgumentException e ) {
105- // Log.w("myApp", "[! ] Failed to create fallback Illegal Argument: " + e);
104+ Log .w ("myApp" ,"[ ! ] Failed to create fallback Illegal Argument: " +e );
106105return ;
107106 }catch (InvocationTargetException e ) {
108- // Log.w("myApp", "[! ] Failed to create fallback Invocation Target" + e);
107+ Log .w ("myApp" ,"[ ! ] Failed to create fallback Invocation Target" +e );
109108return ;
110109 }catch (NoSuchMethodException e ) {
111- // Log.w("myApp", "[! ] Failed to create fallback No Such Method" + e);
110+ Log .w ("myApp" ,"[ ! ] Failed to create fallback No Such Method" +e );
112111return ;
113112 }
114113try {
115114// linked to tmp, so basicly a new socket
116115mmSocket .connect ();
117116 }catch (IOException e ) {
118- //Log.w("myApp", "[!] Failed to connect with fallback socket: " + e);
117+ Log .w ("myApp" ,"[ ! ] Failed to connect with fallback socket: " +e );
118+ try {
119+ sleep (200 );
120+ }catch (InterruptedException ex ) {
121+ }
119122handler .post (new Runnable () {
120123@ Override
121124public void run () {
@@ -124,11 +127,11 @@ public void run() {
124127 });
125128return ;
126129 }
127- // Log.w("myApp", "[# ] Succesfully connected with fallback socket");
130+ Log .w ("myApp" ,"[ # ] Succesfully connected with fallback socket" );
128131 }
129132// Do work to manage the connection (in a separate thread)
130133// manageConnectedSocket(mmSocket);
131- // Log.w("myApp", "[# ] Socket connected. Opening streams....");
134+ Log .w ("myApp" ,"[ # ] Socket connected. Opening streams...." );
132135readThread =new ConnectedThreadClass_Read (mmSocket );
133136readThread .start ();
134137writeThread =new ConnectedThreadClass_Write (mmSocket );
@@ -141,7 +144,7 @@ public void run() {
141144try {
142145sleep (100 );
143146 }catch (InterruptedException e ) {
144- // Log.w("myApp", "[! ] ConnectThread Interrupted");
147+ Log .w ("myApp" ,"[ ! ] ConnectThread Interrupted" );
145148break ;
146149 }
147150// check the status of the connection and send listeners in case of changes
@@ -157,7 +160,7 @@ public void run() {
157160 }while (isConnected ());
158161cancel ();
159162//checkConnectionStatus();
160- // Log.w("myApp", "[# ] Socket closed");
163+ Log .w ("myApp" ,"[ # ] Socket closed" );
161164 }
162165
163166//Will cancel an connection and close streams and socket
@@ -177,8 +180,8 @@ public void cancel() {
177180
178181private void InCaseFireonBluetoothHelperConnectionStateChanged () {
179182if (listener !=null ) {
180- if (isConnected ())Log .w ("myApp" ,"[# ] Listener fired: onBluetoothHelperConnectionStateChanged = true" );
181- else Log .w ("myApp" ,"[# ] Listener fired: onBluetoothHelperConnectionStateChanged = false" );
183+ if (isConnected ())Log .w ("myApp" ,"[ # ] Listener fired: onBluetoothHelperConnectionStateChanged = true" );
184+ else Log .w ("myApp" ,"[ # ] Listener fired: onBluetoothHelperConnectionStateChanged = false" );
182185listener .onBluetoothHelperConnectionStateChanged (this ,isConnected ());// <---- fire listener
183186 }
184187 }
@@ -203,7 +206,7 @@ public ConnectedThreadClass_Read(BluetoothSocket socket) {
203206 }
204207
205208public void run () {
206- // Log.w("myApp", "[# ] Input Stream opened");
209+ Log .w ("myApp" ,"[ # ] Input Stream opened" );
207210// Keep listening to the InputStream until an exception occurs
208211while (true ) {
209212byte []buffer =new byte [1024 ];// buffer store for the stream
@@ -223,24 +226,24 @@ public void run() {
223226 }
224227 }
225228isInStreamConnected =false ;
226- // Log.w("myApp", "[# ] Input stream closed");
229+ Log .w ("myApp" ,"[ # ] Input stream closed" );
227230 }
228231 }
229232
230233// Service function: Message received!
231234private void MessageReceived (String msg ) {
232235// if the listener is attached fire it
233236// else put the message into buffer to be read
234- // Log.w("myApp", "[# ] Message received: " + msg);
237+ Log .w ("myApp" ,"[ # ] Message received: " +msg );
235238try {
236239if (listener !=null ) {
237- // Log.w("myApp", "[# ] Listener fired: onBluetoothHelperMessageReceived");
240+ Log .w ("myApp" ,"[ # ] Listener fired: onBluetoothHelperMessageReceived" );
238241listener .onBluetoothHelperMessageReceived (this ,msg );// <---- fire listener
239242 }else if (!inputMessagesQueue .offer (msg ))
240- // Log.w("myApp", "[! ] Message thrown (unable to store into buffer): " + msg)
243+ Log .w ("myApp" ,"[ ! ] Message thrown (unable to store into buffer): " +msg )
241244 ;
242245 }catch (Exception e ) {
243- // Log.w("myApp", "[! ] Failed to receive message: " + e.getMessage());
246+ Log .w ("myApp" ,"[ ! ] Failed to receive message: " +e .getMessage ());
244247 }
245248 }
246249
@@ -264,29 +267,29 @@ public ConnectedThreadClass_Write(BluetoothSocket socket) {
264267 }
265268
266269public void run () {
267- // Log.w("myApp", "[# ] Output Stream opened");
270+ Log .w ("myApp" ,"[ # ] Output Stream opened" );
268271// Keep sending messages to OutputStream until an exception occurs
269272while (true ) {
270273String msg ;
271274try {
272275msg =outputMessagesQueue .take ();
273276 }catch (InterruptedException e ) {
274277isOutStreamConnected =false ;
275- // Log.w("myApp", "[! ] Buffer not available: " + e.getMessage());
278+ Log .w ("myApp" ,"[ ! ] Buffer not available: " +e .getMessage ());
276279break ;
277280 }
278281try {
279282mmOutStream .write (msg .getBytes ());
280283mmOutStream .write (Delimiter );
281- // Log.w("myApp", "[# ] Message send: " + msg);
284+ Log .w ("myApp" ,"[ # ] Message send: " +msg );
282285 }catch (IOException e ) {
283286isOutStreamConnected =false ;
284- // Log.w("myApp", "[! ] Unable to write data to output stream: " + e.getMessage());
287+ Log .w ("myApp" ,"[ ! ] Unable to write data to output stream: " +e .getMessage ());
285288break ;
286289 }
287290 }
288291isOutStreamConnected =false ;
289- // Log.w("myApp", "[# ] Output stream closed");
292+ Log .w ("myApp" ,"[ # ] Output stream closed" );
290293 }
291294 }
292295
@@ -371,21 +374,20 @@ public boolean isConnected() {
371374* @see android.bluetooth.BluetoothDevice#getName()
372375*/
373376public void Connect (String DeviceName ) {
377+ Log .w ("myApp" ,"[ # ] Connect(String DeviceName)" );
378+ mBluetoothAdapter =BluetoothAdapter .getDefaultAdapter ();// Find adapter
374379if ((mBluetoothAdapter !=null ) && (!DeviceName .isEmpty ())) {
375- if (!isConnected ()) {
376- Disconnect (false );
377-
378- mBluetoothAdapter =BluetoothAdapter .getDefaultAdapter ();// Find adapter
379- if (mBluetoothAdapter .isEnabled ()) {// Adapter found
380- Set <BluetoothDevice >devices =mBluetoothAdapter .getBondedDevices ();// Collect all bonded devices
381- for (BluetoothDevice bt :devices ) {
382- if (DeviceName .equals (bt .getName ())) {// Find requested device name
383- //Log.w("myApp", "[#] Devicename match found: " + bt.getName());
384- Connect (bt );
385- }//else Log.w("myApp", "[#] Devicename doesn't match: " + bt.getName());
386- }
380+ if (isConnected ())Disconnect (false );
381+ if (mBluetoothAdapter .isEnabled ()) {// Adapter found
382+ Set <BluetoothDevice >devices =mBluetoothAdapter .getBondedDevices ();// Collect all bonded devices
383+ for (BluetoothDevice bt :devices ) {
384+ if (DeviceName .equals (bt .getName ())) {// Find requested device name
385+ Log .w ("myApp" ,"[ # ] Devicename match found: " +bt .getName ());
386+ Connect (bt );
387+ }//else Log.w("myApp", "[ # ] Devicename doesn't match: " + bt.getName());
387388 }
388389 }
390+
389391 }
390392 }
391393