Also move the resource management into DeviceSupport classes

live-sensor-data
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(); 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() { public synchronized GBDeviceProtocol getDeviceProtocol() {
if (gbDeviceProtocol == null) { if (gbDeviceProtocol == null) {
gbDeviceProtocol = createDeviceProtocol(); gbDeviceProtocol = createDeviceProtocol();
@ -26,7 +40,7 @@ public abstract class AbstractBTDeviceSupport extends AbstractDeviceSupport {
} }
protected void sendToDevice(byte[] bytes) { protected void sendToDevice(byte[] bytes) {
if (bytes != null) { if (bytes != null && gbDeviceIOThread != null) {
gbDeviceIOThread.write(bytes); gbDeviceIOThread.write(bytes);
} }
} }

View File

@ -103,15 +103,8 @@ public class BluetoothCommunicationService extends Service {
sharedPrefs.edit().putString("last_device_address", btDeviceAddress).commit(); sharedPrefs.edit().putString("last_device_address", btDeviceAddress).commit();
if (btDeviceAddress != null && !isConnected() && !isConnecting()) { if (btDeviceAddress != null && !isConnected() && !isConnecting()) {
// currently only one thread allowed if (mDeviceSupport != null) {
if (mGBDeviceIoThread != null) { mDeviceSupport.dispose();
mGBDeviceIoThread.quit();
try {
mGBDeviceIoThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
} }
BluetoothDevice btDevice = mBtAdapter.getRemoteDevice(btDeviceAddress); BluetoothDevice btDevice = mBtAdapter.getRemoteDevice(btDeviceAddress);
if (btDevice != null) { if (btDevice != null) {
@ -201,13 +194,8 @@ public class BluetoothCommunicationService extends Service {
GB.setReceiversEnableState(false, this); // disable BroadcastReceivers GB.setReceiversEnableState(false, this); // disable BroadcastReceivers
if (mGBDeviceIoThread != null) { if (mDeviceSupport != null) {
try { mDeviceSupport.dispose();
mGBDeviceIoThread.quit();
mGBDeviceIoThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
} }
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 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 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 BluetoothAdapter getBluetoothAdapter();
public Context getContext(); public Context getContext();
public void dispose();
} }