I have a connection screen where I have a search button, after I clicked it a list of discoverable devices are posted, I click on the desired device to make pair and connection with, the connection is established and when I lock the screen the connection is lost, how can I improve my code in order to have the service still working? This is how I tried to implement it, onCreate(), onStart() and onResume() are also implemented inside launchApplication().
public static void launchActivity(Context context, String deviceAddress){
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mCurrentDeviceAddress = deviceAddress; //getIntent().getExtras().getString(ConnectionScreen.PREFS_DEVICE_ADDR);
mChatService = new BluetoothMeterService(context, mHandler);
connectDevice();
if (mChatService != null) {
if (mChatService.getState() == BluetoothMeterService.STATE_NONE) {
mChatService.start();
}
}
}
// Automatically try to connec with the known mac address;
private static class ConnectThread extends Thread {
private final BluetoothDevice device;
public ConnectThread(BluetoothDevice d) {
this.device = d;
}
public void run() {
while (mConnectThread == Thread.currentThread()) {
if (mChatService.getState() == BluetoothMeterService.STATE_CONNECTED) {
Log.e(TAG, "STATE_CONNECTED");
break;
} else if (mChatService.getState() == BluetoothMeterService.STATE_CONNECTING) {
try {
//Thread.sleep(2000);
Log.e(TAG, "STATE_CONNECTING");
mChatService.connect(device);
} catch (Exception e) {
// Log.e(TAG, e.getMessage());
}
} else
try {
Log.e(TAG, "STATE_DISCONECTED");
mChatService.start();
Thread.sleep(3000);
} catch (Exception e) {
// Log.e(TAG, e.getMessage());
Thread.currentThread().interrupt();
}
}
}
}
// create the bluetooth device object, and try to connect with it
// consistantly and automatically.
private static void connectDevice() {
if (mCurrentDeviceAddress == null) {
Toast.makeText(context, "Bluetooth MAC address is not assigned.",
Toast.LENGTH_SHORT).show();
//context.finish();
return;
}
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(mCurrentDeviceAddress);
// showDialog(Dialog_Connect);
mConnectThread = new ConnectThread(device);
mConnectThread.start();
}
// The Handler that gets information back from the BluetoothMeterService
private static final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
Log.e(TAG, msg.toString());
switch (msg.what) {
case MESSAGE_STATE_CHANGE:
switch (msg.arg1) {
case BluetoothMeterService.STATE_CONNECTED:
Log.e(TAG, "handler - STATE_CONNECTED");
for(CustomizedBluetoothDevice device : mDeviceList){
if(device.getAddress() == mCurrentDeviceAddress){
device.setStatus(BluetoothMeterService.STATE_CONNECTED);
}
}
updateUI(/*mDeviceList*/);
// mTextViewTitle.setText("Device: " + mConnectedDeviceName);
// mTextViewStatus.setText(R.string.title_connected_to);
break;
case BluetoothMeterService.STATE_CONNECTING:
Log.e(TAG, "handler - STATE_CONNECTING");
for(CustomizedBluetoothDevice device : mDeviceList){
if(device.getAddress() == mCurrentDeviceAddress){
device.setStatus(BluetoothMeterService.STATE_CONNECTING);
}
}
updateUI(/*mDeviceList*/);
// mTextViewStatus.setText(R.string.title_connecting);
break;
case BluetoothMeterService.STATE_NONE:
Log.e(TAG, "handler - STATE_NONE");
for(CustomizedBluetoothDevice device : mDeviceList){
if(device.getAddress() == mCurrentDeviceAddress){
device.setStatus(BluetoothMeterService.STATE_NONE);
}
}
updateUI(/*mDeviceList*/);
// mTextViewStatus.setText(R.string.title_not_connected);
break;
case BluetoothMeterService.STATE_DISCONNECTING:
Log.e(TAG, "handler - STATE_DISCONNECTING");
for(CustomizedBluetoothDevice device : mDeviceList){
if(device.getAddress() == mCurrentDeviceAddress){
device.setStatus(BluetoothMeterService.STATE_DISCONNECTING);
}
}
updateUI(/*mDeviceList*/);
break;
}
break;
case MESSAGE_WRITE:
break;
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
// construct a string from the valid bytes in the buffer
String readMessage = new String(readBuf, 0, msg.arg1);
Log.e(TAG, "handler - MESSAGE_READ " + readMessage);
// bufferMessege += readMessage;
/* if (mMessage != null) {
mMessage.add(new CustomizedMessage(readMessage, true));
updateUI();
}*/
// bufferMessege = "";
break;
case MESSAGE_DEVICE_NAME:
Log.e(TAG, "handler - MESSAGE_READ " + MESSAGE_DEVICE_NAME);
// save the connected device's name
mConnectedDeviceName = msg.getData().getString(DEVICE_NAME);
break;
case MESSAGE_TOAST:
Log.e(TAG, "handler - MESSAGE_READ " + MESSAGE_TOAST);
break;
}
}
};
public void stopActivity(){
if (mChatService != null)
{
mChatService.stop();
mChatService = null;
}
}
This is actually normal. As far you leave the app and the android lifecycle calls the onDestroy()
method, all connections should go offline. Therefore you need a process which is working further on in the background. Here you can use the Android Service
component. This will work as long you are using the resources, as soon its finished with the process the Service
is going to close. So it depends, what you need. If you need a background service which shall work forever in the background, you should use the startForeground()
method of Service
.
More infomration about Service
here: http://developer.android.com/guide/components/services.html