Hola gente, aquí estamos de nuevo a ver si me pueden sacar del follon que yo solo me he metido.
el trabajo: basicamente se trata de crear un servicio desde la actividad principal y que cuando la app se encuentre en segundo el servicio siga activo. el servicio va a ser un escaner de dispositivos bluetooth. el controlador del dispositivo sigue un patrón singleton, es decir, tengo una instancia del objeto que se va instanciando en todas las actividades.
Problema: A continuación os dejo un trozo de código con el desarrollo. Lo que me ocurre es que cuando pongo a escanear y mato el proceso el scaneo continua y no entiendo muy bien porque.
Para el escaneo y para ahorrar bateria hago un scaneo y luego descanso, inicio escaneo y descanso.
public class ServiceDetectionTag extends IntentService {
private static final String NAMECLASS = "ServiceDetectionTag";
private static final long STOP_SCAN_TIMER = 10 * 1000;
private static final long START_SCAN_TIMER = 30 * 1000;
private static Context mContext;
private static HandlerBLE mHandlerBLE;
private static ServiceBroadcastReceiver mServiceBroadcastReceiver;
private static Thread workerThread = null;
private static Boolean alive;
public ServiceDetectionTag()
{
super(NAMECLASS);
}
/*private volatile Timer mTimerStart;
private volatile Timer mTimerStop;*/
@Override
protected void onHandleIntent(Intent intent)
{
alive = true;
mHandlerBLE = ((BLE_Application) getApplication()).getmHandlerBLEInstance(this.getApplicationContext());
((BLE_Application) getApplication()).resetHandlerBLE();
mContext = getApplicationContext();
mServiceBroadcastReceiver = new ServiceBroadcastReceiver();
IntentFilter i = new IntentFilter(ServiceBroadcastReceiver.ACTION_NOTIFY_NEW_DEVICE_FOUND);
registerReceiver(mServiceBroadcastReceiver, i);
if(workerThread == null || !workerThread.isAlive())
{
workerThread = new Thread(new Runnable()
{
public void run()
{
while(isAlive())
{
try
{
if (Constant.DEBUG)
Log.i(Constant.TAG, NAMECLASS + " ## -- onHandleIntent -> Start scanning");
mHandlerBLE.startLeScan();
workerThread.sleep(START_SCAN_TIMER);
if (Constant.DEBUG)
Log.i(Constant.TAG, NAMECLASS + " ## -- onHandleIntent -> Stop scanning");
mHandlerBLE.stopLeScan();
workerThread.sleep(STOP_SCAN_TIMER);
} catch (Exception e) {
e.printStackTrace();
}
}
/*
if (Constant.DEBUG)
Log.i(Constant.TAG, NAMECLASS + " ## -- onHandleIntent -> 1º Start scanning");
mHandlerBLE.startLeScan();
mTimerStart = new Timer();
mTimerStop = new Timer();
mTimerStart.scheduleAtFixedRate(new TimerTask() {
@Override
public void run()
{
if (Constant.DEBUG)
Log.i(Constant.TAG, NAMECLASS + " ## -- onHandleIntent -> Start scanning");
mHandlerBLE.startLeScan();
}
}, 0, START_SCAN_TIMER + STOP_SCAN_TIMER);
mTimerStop.scheduleAtFixedRate(new TimerTask() {
@Override
public void run()
{
if (Constant.DEBUG)
Log.i(Constant.TAG, NAMECLASS + " ## -- onHandleIntent -> Stop scanning");
mHandlerBLE.stopLeScan();
}
}, 0, START_SCAN_TIMER);*/
}
});
workerThread.start();
}
}
@Override
public void onDestroy()
{
super.onDestroy();
alive = false;
if (Constant.DEBUG)
Log.i(Constant.TAG, NAMECLASS + " ## -- onDestroy() -> Stop scanning");
mHandlerBLE.stopLeScan();
try
{
workerThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
unregisterReceiver(mServiceBroadcastReceiver);
}
public static Boolean isAlive()
{
if(alive != null)
return !alive;
return false;
}
public class ServiceBroadcastReceiver extends BroadcastReceiver {
public static final String ACTION_NOTIFY_NEW_DEVICE_FOUND = "iot_ble.NOTIFY_NEW_DEVICE";
public ServiceBroadcastReceiver() {
super();
}
@Override
public void onReceive(Context context, Intent intent) {
if (Constant.DEBUG)
Log.i(Constant.TAG, " ## ServiceBroadcastReceiver -- onReceive -> inside");
String action = intent.getAction();
if (action.equals(ServiceBroadcastReceiver.ACTION_NOTIFY_NEW_DEVICE_FOUND)) {
if (Constant.DEBUG)
Log.i(Constant.TAG, " ## ServiceBroadcastReceiver -- onReceive -> sending notication(statusBar)");
MyNotificationHandler myNotificationHandler;
myNotificationHandler = new MyNotificationHandler(mContext);
myNotificationHandler.SendNotify();
}
}
}