Currently I am working on bluetooth scan, that means I will keep scanning the device, if there is device nearby , I will show it on screen, however, if the app is at the background, I need to show notification to the user.
And now my working logic is like this, there is a main class that fire the scan service, the scan service job is to return a list of scanned device. And the main class job is to display/ process the result.
The code is like this:
Service
public class Detector extends Service implements IBeaconConsumer {
protected static final String TAG = "RangingActivity";
private IBeaconManager iBeaconManager;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
iBeaconManager = IBeaconManager.getInstanceForApplication(this);
iBeaconManager.bind(this);
Log.d("test1","bind");
return super.onStartCommand(intent, flags, startId);
}
}
Main
private class DataUpdateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("scanResult")) {
ArrayList<IBeacon> beacons = (ArrayList<IBeacon>) intent.getSerializableExtra("beacons");
for(IBeacon be : beacons){
if (be.getProximityUuid().equals("ebefd083-70a2-47c8-9837-e7b5634df524")) {
if (be.getMajor() == 2) {
a_rssi = be.getRssi();
} else if (be.getMajor() == 1) {
b_rssi = be.getRssi();
}
}
}
if(a_rssi > b_rssi) {
Log.d("test1","at shop");
//at shop,need case handling if more than 1 shop beacon
showAd(R.drawable.offer1);
if (timer != null)
timer.cancel();
timer = null;
timeCount = 0;
Intent msg = new Intent("timerUpdate");
msg.putExtra("timeCount", timeCount);
sendBroadcast(msg);
} else {
Log.d("test1","at park");
//at car park
if (timer == null) {
timer = new Timer(true);
timer.schedule(new MyTimer(), 1000, 1000);
}
}
}
}
}
The problem is , if I need to add notification, does I need to move the process part code (which is in the main class) to service? How can I achieve it? Thanks