I am working in Android with Bluetooth. I can obtain a connection and results from bluetooth device inside MainActivity
now, i want to connect and to interact with the bluetoth device from a AlarManager when app is dead.
actually, i have the source but runtimes do not match
what is my problem? The Thread of BackgroundService does not wait the Thread of bluetooth. I use Thread.sleep(2000); for to wait the bluetooth.
The bluetooth connect and connects and returns the information but I can not process the result because BackgroundService dies first
(BluetoothLeService extend from Service)
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent alarm = new Intent(this, AlarmReceiver.class);
boolean alarmRunning = (PendingIntent.getBroadcast(this, 0, alarm, PendingIntent.FLAG_NO_CREATE) != null);
if (alarmRunning == false) {
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarm, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 120000, pendingIntent); // 1800000
}
}
}
My BroadcastReceiver
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent background = new Intent(context, BackgroundService.class);
context.startService(background);
}
}
My BackgroundService
public class BackgroundService extends Service {
private boolean isRunning;
private Thread backgroundThread;
private CommandManager manager;
private BluetoothLeService mBluetoothLeService;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
this.isRunning = false;
manager = CommandManager.getInstance(getApplicationContext());
Intent bindIntent = new Intent(getApplicationContext(), BluetoothLeService.class);
bindService(bindIntent, mServiceConnection, Context.BIND_AUTO_CREATE);
LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(
BLEStatusChangeReceiver, makeGattUpdateIntentFilter());
this.backgroundThread = new Thread(myTask);
}
private Runnable myTask = new Runnable() {
public void run() {
// Do something here
manager.findBand();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
manager.getDataFromDevice();// for obtain data from bluetooth
stopSelf();
}
};
@Override
public void onDestroy() {
Log.e("onDestroy","prepared");
try { Thread.sleep(15000); }
catch (InterruptedException e) { e.printStackTrace(); }
Log.e("onDestroy","YES");
unbindService(mServiceConnection);
this.isRunning = false;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(!this.isRunning) {
this.isRunning = true;
this.backgroundThread.start();
}
return START_STICKY;
}
// Code to manage Service lifecycle.
private final ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder service) {
mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();
Log.i("STATUS", "onServiceConnected---==---");
if (!mBluetoothLeService.initialize()) {
Log.e("STATUS BLUETOOTH", "Unable to initialize Bluetooth");
}
mBluetoothLeService.connect("mac device here", false);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
mBluetoothLeService = null;
Log.i("STATUS", "onServiceDisconnected");
}
};
private final BroadcastReceiver BLEStatusChangeReceiver = new BroadcastReceiver() {
@SuppressLint("UseValueOf")
public void onReceive(Context context, Intent intent) {
/**
* HERE RECEPTION DATA BLUETOOTH
*/
}
};
private IntentFilter makeGattUpdateIntentFilter() {
final IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BroadcastCommand.ACTION_DATA_AVAILABLE);
intentFilter.addAction(BroadcastCommand.ACTION_GATT_CONNECTED);
intentFilter.addAction(BroadcastCommand.ACTION_GATT_DISCONNECTED);
return intentFilter;
}
}
PD: I hope it is enough information ... if I miss something, I'll respond soon
You should either move to a Foreground service or acquire a PowerManager.WakeLock before launching your Service and release it when your task is done. However, still some resource access might be limited while the phone is in Doze mode.