From 65ae1829796e7afb149d52c02f7311e36301b632 Mon Sep 17 00:00:00 2001 From: Paul W Date: Wed, 2 May 2018 12:52:41 +0200 Subject: [PATCH 1/3] Not trying to connect when Bluetooth is disabled. Improved LogCat messages. Caught NullPointerExceptions (occured when Bluetooth was disabled AND Android Location services was disabled) --- .../cordova/BluetoothSerialService.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/android/com/megster/cordova/BluetoothSerialService.java b/src/android/com/megster/cordova/BluetoothSerialService.java index 63ce6c53..7ad14143 100644 --- a/src/android/com/megster/cordova/BluetoothSerialService.java +++ b/src/android/com/megster/cordova/BluetoothSerialService.java @@ -120,6 +120,12 @@ public synchronized void start() { public synchronized void connect(BluetoothDevice device, boolean secure) { if (D) Log.d(TAG, "connect to: " + device); + + if (mAdapter == null || !mAdapter.isEnabled()){ + Log.e(TAG, "Bluetooth is disabled! Not connecting"); + return; + } + // Cancel any thread attempting to make a connection if (mState == STATE_CONNECTING) { if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;} @@ -221,6 +227,7 @@ public void write(byte[] out) { * Indicate that the connection attempt failed and notify the UI Activity. */ private void connectionFailed() { + Log.e(TAG, "Connection failed!"); // Send a failure message back to the Activity Message msg = mHandler.obtainMessage(BluetoothSerial.MESSAGE_TOAST); Bundle bundle = new Bundle(); @@ -371,8 +378,7 @@ public void run() { // This is a blocking call and will only return on a successful connection or an exception Log.i(TAG,"Connecting to socket..."); mmSocket.connect(); - Log.i(TAG,"Connected"); - } catch (IOException e) { + } catch (IOException | NullPointerException e) { Log.e(TAG, e.toString()); // Some 4.1 devices have problems, try an alternative way to connect @@ -381,12 +387,11 @@ public void run() { Log.i(TAG,"Trying fallback..."); mmSocket = (BluetoothSocket) mmDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class}).invoke(mmDevice,1); mmSocket.connect(); - Log.i(TAG,"Connected"); } catch (Exception e2) { Log.e(TAG, "Couldn't establish a Bluetooth connection."); try { mmSocket.close(); - } catch (IOException e3) { + } catch (IOException | NullPointerException e3) { Log.e(TAG, "unable to close() " + mSocketType + " socket during connection failure", e3); } connectionFailed(); @@ -394,6 +399,8 @@ public void run() { } } + Log.i(TAG,"Successfully connected!"); + // Reset the ConnectThread because we're done synchronized (BluetoothSerialService.this) { mConnectThread = null; @@ -406,7 +413,7 @@ public void run() { public void cancel() { try { mmSocket.close(); - } catch (IOException e) { + } catch (IOException | NullPointerException e) { Log.e(TAG, "close() of connect " + mSocketType + " socket failed", e); } } @@ -497,3 +504,4 @@ public void cancel() { } } } + From b7822c15af2da2e77c51d97715f37b62eac684b5 Mon Sep 17 00:00:00 2001 From: Paul W Date: Thu, 14 Jun 2018 10:07:59 +0200 Subject: [PATCH 2/3] Enforcing java 7 --- build-extras.gradle | 15 +++++++++++++++ plugin.xml | 3 +++ 2 files changed, 18 insertions(+) create mode 100644 build-extras.gradle diff --git a/build-extras.gradle b/build-extras.gradle new file mode 100644 index 00000000..442b420e --- /dev/null +++ b/build-extras.gradle @@ -0,0 +1,15 @@ +ext.postBuildExtras = { + android { + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_7 + targetCompatibility JavaVersion.VERSION_1_7 + } + allprojects { + compileOptions { + sourceCompatibility = JavaVersion.VERSION_1_7 + targetCompatibility = JavaVersion.VERSION_1_7 + } + } + } +} + diff --git a/plugin.xml b/plugin.xml index 19558bbf..af97c845 100644 --- a/plugin.xml +++ b/plugin.xml @@ -35,6 +35,9 @@ + + + From 52862ea811d10c33b6bd29cb1a333e42a53e888e Mon Sep 17 00:00:00 2001 From: Paul W Date: Tue, 17 Jul 2018 14:47:07 +0200 Subject: [PATCH 3/3] Updated some layout, updated gitignore --- .gitignore | 1 + .../com/megster/cordova/BluetoothSerial.java | 12 ++-- .../cordova/BluetoothSerialService.java | 70 +++++++++++++------ 3 files changed, 53 insertions(+), 30 deletions(-) diff --git a/.gitignore b/.gitignore index 5509140f..26e8d30b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *.DS_Store +.idea/ diff --git a/src/android/com/megster/cordova/BluetoothSerial.java b/src/android/com/megster/cordova/BluetoothSerial.java index 8be5575f..d65911e7 100644 --- a/src/android/com/megster/cordova/BluetoothSerial.java +++ b/src/android/com/megster/cordova/BluetoothSerial.java @@ -92,7 +92,7 @@ public class BluetoothSerial extends CordovaPlugin { @Override public boolean execute(String action, CordovaArgs args, CallbackContext callbackContext) throws JSONException { - LOG.d(TAG, "action = " + action); + //LOG.d(TAG, "action = " + action); if (bluetoothAdapter == null) { bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); @@ -110,14 +110,12 @@ public boolean execute(String action, CordovaArgs args, CallbackContext callback } else if (action.equals(CONNECT)) { - boolean secure = true; - connect(args, secure, callbackContext); + connect(args, true, callbackContext); } else if (action.equals(CONNECT_INSECURE)) { // see Android docs about Insecure RFCOMM http://goo.gl/1mFjZY - boolean secure = false; - connect(args, secure, callbackContext); + connect(args, false, callbackContext); } else if (action.equals(DISCONNECT)) { @@ -350,7 +348,7 @@ private void connect(CordovaArgs args, boolean secure, CallbackContext callbackC callbackContext.sendPluginResult(result); } else { - callbackContext.error("Could not connect to " + macAddress); + callbackContext.error("Could not connect to " + macAddress + "(device not found/unreachable)"); } } @@ -400,7 +398,7 @@ public void handleMessage(Message msg) { // Log.i(TAG, "Wrote: " + writeMessage); break; case MESSAGE_DEVICE_NAME: - Log.i(TAG, msg.getData().getString(DEVICE_NAME)); + Log.i(TAG, "device name: " + msg.getData().getString(DEVICE_NAME)); break; case MESSAGE_TOAST: String message = msg.getData().getString(TOAST); diff --git a/src/android/com/megster/cordova/BluetoothSerialService.java b/src/android/com/megster/cordova/BluetoothSerialService.java index 7ad14143..7eb832fa 100644 --- a/src/android/com/megster/cordova/BluetoothSerialService.java +++ b/src/android/com/megster/cordova/BluetoothSerialService.java @@ -20,7 +20,7 @@ * connections with other devices. It has a thread that listens for * incoming connections, a thread for connecting with a device, and a * thread for performing data transmissions when connected. - * + *

* This code was based on the Android SDK BluetoothChat Sample * $ANDROID_SDK/samples/android-17/BluetoothChat */ @@ -58,7 +58,8 @@ public class BluetoothSerialService { /** * Constructor. Prepares a new BluetoothSerial session. - * @param handler A Handler to send messages back to the UI Activity + * + * @param handler A Handler to send messages back to the UI Activity */ public BluetoothSerialService(Handler handler) { mAdapter = BluetoothAdapter.getDefaultAdapter(); @@ -68,7 +69,8 @@ public BluetoothSerialService(Handler handler) { /** * Set the current state of the chat connection - * @param state An integer defining the current connection state + * + * @param state An integer defining the current connection state */ private synchronized void setState(int state) { if (D) Log.d(TAG, "setState() " + mState + " -> " + state); @@ -79,22 +81,30 @@ private synchronized void setState(int state) { } /** - * Return the current connection state. */ + * Return the current connection state. + */ public synchronized int getState() { return mState; } /** * Start the chat service. Specifically start AcceptThread to begin a - * session in listening (server) mode. Called by the Activity onResume() */ + * session in listening (server) mode. Called by the Activity onResume() + */ public synchronized void start() { if (D) Log.d(TAG, "start"); // Cancel any thread attempting to make a connection - if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;} + if (mConnectThread != null) { + mConnectThread.cancel(); + mConnectThread = null; + } // Cancel any thread currently running a connection - if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;} + if (mConnectedThread != null) { + mConnectedThread.cancel(); + mConnectedThread = null; + } setState(STATE_NONE); @@ -114,25 +124,30 @@ public synchronized void start() { /** * Start the ConnectThread to initiate a connection to a remote device. - * @param device The BluetoothDevice to connect + * + * @param device The BluetoothDevice to connect * @param secure Socket Security type - Secure (true) , Insecure (false) */ public synchronized void connect(BluetoothDevice device, boolean secure) { - if (D) Log.d(TAG, "connect to: " + device); + if (D) Log.d(TAG, "Connecting to: " + device); - if (mAdapter == null || !mAdapter.isEnabled()){ + if (mAdapter == null || !mAdapter.isEnabled()) { Log.e(TAG, "Bluetooth is disabled! Not connecting"); return; } // Cancel any thread attempting to make a connection - if (mState == STATE_CONNECTING) { - if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;} + if (mState == STATE_CONNECTING && mConnectThread != null) { + mConnectThread.cancel(); + mConnectThread = null; } // Cancel any thread currently running a connection - if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;} + if (mConnectedThread != null) { + mConnectedThread.cancel(); + mConnectedThread = null; + } // Start the thread to connect with the given device mConnectThread = new ConnectThread(device, secure); @@ -142,17 +157,24 @@ public synchronized void connect(BluetoothDevice device, boolean secure) { /** * Start the ConnectedThread to begin managing a Bluetooth connection - * @param socket The BluetoothSocket on which the connection was made - * @param device The BluetoothDevice that has been connected + * + * @param socket The BluetoothSocket on which the connection was made + * @param device The BluetoothDevice that has been connected */ public synchronized void connected(BluetoothSocket socket, BluetoothDevice device, final String socketType) { if (D) Log.d(TAG, "connected, Socket Type:" + socketType); // Cancel the thread that completed the connection - if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;} + if (mConnectThread != null) { + mConnectThread.cancel(); + mConnectThread = null; + } // Cancel any thread currently running a connection - if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;} + if (mConnectedThread != null) { + mConnectedThread.cancel(); + mConnectedThread = null; + } // Cancel the accept thread because we only want to connect to one device if (mSecureAcceptThread != null) { @@ -208,6 +230,7 @@ public synchronized void stop() { /** * Write to the ConnectedThread in an unsynchronized manner + * * @param out The bytes to write * @see ConnectedThread#write(byte[]) */ @@ -266,7 +289,7 @@ private class AcceptThread extends Thread { public AcceptThread(boolean secure) { BluetoothServerSocket tmp = null; - mSocketType = secure ? "Secure":"Insecure"; + mSocketType = secure ? "Secure" : "Insecure"; // Create a new listening server socket try { @@ -376,7 +399,7 @@ public void run() { // Make a connection to the BluetoothSocket try { // This is a blocking call and will only return on a successful connection or an exception - Log.i(TAG,"Connecting to socket..."); + Log.i(TAG, "Connecting to socket..."); mmSocket.connect(); } catch (IOException | NullPointerException e) { Log.e(TAG, e.toString()); @@ -384,8 +407,8 @@ public void run() { // Some 4.1 devices have problems, try an alternative way to connect // See https://github.com/don/BluetoothSerial/issues/89 try { - Log.i(TAG,"Trying fallback..."); - mmSocket = (BluetoothSocket) mmDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class}).invoke(mmDevice,1); + Log.i(TAG, "Trying fallback..."); + mmSocket = (BluetoothSocket) mmDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class}).invoke(mmDevice, 1); mmSocket.connect(); } catch (Exception e2) { Log.e(TAG, "Couldn't establish a Bluetooth connection."); @@ -399,7 +422,7 @@ public void run() { } } - Log.i(TAG,"Successfully connected!"); + Log.i(TAG, "Successfully connected!"); // Reset the ConnectThread because we're done synchronized (BluetoothSerialService.this) { @@ -481,7 +504,8 @@ public void run() { /** * Write to the connected OutStream. - * @param buffer The bytes to write + * + * @param buffer The bytes to write */ public void write(byte[] buffer) { try {