diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/AbstractBTDeviceSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/AbstractBTDeviceSupport.java index e6b6f4f1f..a76e45bf9 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/AbstractBTDeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/AbstractBTDeviceSupport.java @@ -11,6 +11,20 @@ public abstract class AbstractBTDeviceSupport extends AbstractDeviceSupport { protected abstract GBDeviceIoThread createDeviceIOThread(); + @Override + public void dispose() { + // currently only one thread allowed + if (gbDeviceIOThread != null) { + gbDeviceIOThread.quit(); + try { + gbDeviceIOThread.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + gbDeviceIOThread = null; + } + } + public synchronized GBDeviceProtocol getDeviceProtocol() { if (gbDeviceProtocol == null) { gbDeviceProtocol = createDeviceProtocol(); @@ -26,7 +40,7 @@ public abstract class AbstractBTDeviceSupport extends AbstractDeviceSupport { } protected void sendToDevice(byte[] bytes) { - if (bytes != null) { + if (bytes != null && gbDeviceIOThread != null) { gbDeviceIOThread.write(bytes); } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothCommunicationService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothCommunicationService.java index 11b6c5ba4..83f594450 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothCommunicationService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothCommunicationService.java @@ -103,15 +103,8 @@ public class BluetoothCommunicationService extends Service { sharedPrefs.edit().putString("last_device_address", btDeviceAddress).commit(); if (btDeviceAddress != null && !isConnected() && !isConnecting()) { - // currently only one thread allowed - if (mGBDeviceIoThread != null) { - mGBDeviceIoThread.quit(); - try { - mGBDeviceIoThread.join(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - + if (mDeviceSupport != null) { + mDeviceSupport.dispose(); } BluetoothDevice btDevice = mBtAdapter.getRemoteDevice(btDeviceAddress); if (btDevice != null) { @@ -201,13 +194,8 @@ public class BluetoothCommunicationService extends Service { GB.setReceiversEnableState(false, this); // disable BroadcastReceivers - if (mGBDeviceIoThread != null) { - try { - mGBDeviceIoThread.quit(); - mGBDeviceIoThread.join(); - } catch (InterruptedException e) { - e.printStackTrace(); - } + if (mDeviceSupport != null) { + mDeviceSupport.dispose(); } NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); nm.cancel(GB.NOTIFICATION_ID); // need to do this because the updated notification wont be cancelled when service stops diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/DeviceSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/DeviceSupport.java index 5b34b35c7..1f6c76337 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/DeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/DeviceSupport.java @@ -13,4 +13,6 @@ public interface DeviceSupport extends EventHandler { public BluetoothAdapter getBluetoothAdapter(); public Context getContext(); + + public void dispose(); }