Also move the resource management into DeviceSupport classes

This commit is contained in:
cpfeiffer 2015-04-14 01:39:00 +02:00
parent 1f31c1d79c
commit d0ff14bc0e
3 changed files with 21 additions and 17 deletions

View File

@ -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);
}
}

View File

@ -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

View File

@ -13,4 +13,6 @@ public interface DeviceSupport extends EventHandler {
public BluetoothAdapter getBluetoothAdapter();
public Context getContext();
public void dispose();
}