Hola por aca... tengo una duda
Estoy trabajando con el bluetooth para ir a buscar datos cuando la app esta cerrada.
Tengo todo el codigo:
El AlarmManager para la ejeucion del Broadcast
this.context = this;
Intent alarm = new Intent(this.context, AlarmReceiver.class);
boolean alarmRunning = (PendingIntent.getBroadcast(this.context, 0, alarm, PendingIntent.FLAG_NO_CREATE) != null);
if(alarmRunning == false) {
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.context, 0, alarm, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 120000/*1800000*/, pendingIntent);
}
El BroadcastReceiver
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent background = new Intent(context, BackgroundService.class);
context.startService(background);
}
}
El service donde me conecto y obtengo los datos del bluetoth
package cl.demo.nph.myapplication;
import android.annotation.SuppressLint;
import android.app.*;
import android.content.*;
import android.os.*;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import com.weike.chiginon.BroadcastCommand;
import com.weike.chiginon.BroadcastData;
import com.weike.manager.CommandManager;
import cl.demo.nph.myapplication.ch.BluetoothLeService;
public class BackGroundService1 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.find(); /* METODO PARA SOLICITAR DATOS AL BLUETOOTH*/
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
stopSelf();
}
};
@Override
public void onDestroy() {
Log.e("onDestroy","prepared");
try {
Thread.sleep(15000);
Log.e("onDestroy","YES");
unbindService(mServiceConnection);
this.isRunning = false;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(!this.isRunning) {
this.isRunning = true;
this.backgroundThread.start();
}
return START_STICKY;
}
private final ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder service) {
mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();
Log.i("zgy", "onServiceConnected---==---");
if (!mBluetoothLeService.initialize())
Log.e("STATUS BLUETOOTH", "Unable to initialize Bluetooth");
Log.e("CONECTANDO A","00:00:00:00:00:00");
mBluetoothLeService.connect("00:00:00:00:00:00", false);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
mBluetoothLeService = null;
Log.i("zgy", "onServiceDisconnected");
}
};
private final BroadcastReceiver BLEStatusChangeReceiver = new BroadcastReceiver() {
@SuppressLint("UseValueOf")
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(BroadcastCommand.ACTION_DATA_AVAILABLE) ) {
BroadcastData bData = (BroadcastData) intent
.getSerializableExtra(BroadcastData.keyword);
if (bData.commandID == BroadcastCommand.BLE_SEND_DATA) {
// Recepcion de datos del dispositivo 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;
}
}
BluetoothLeService = es una clase que extiende de service
Bueno, ahora si: mi problema esta en que el servicio abiero desde el BroadcastReceiver
BackGroundService1
y el servicio que ejecuta el bluetooth tienen tiempos de ejecucion distintos. Por lo tanto no consigo que el bluetooth le devuelva los parametros que recibo al BackGroundService1
SI recibo los datos en el
BLEStatusChangeReceiver
pero el
BackGroundService1
LLega a destruirse primero antes de poder hacer algo con los datos