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 HandlerBLE mHandlerBLE;
private static ServiceBroadcastReceiver mServiceBroadcastReceiver;
private static Thread workerThread
= null;
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())
{
{
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);
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();
e.printStackTrace();
}
unregisterReceiver(mServiceBroadcastReceiver);
}
{
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();
}
}
}