I have a method where I register broadcast receivers using intent filters to discover bluetooth devices.
// global variable
String xpto = "empty";
Here is the void method:
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
App appState = ((App)getApplicationContext());
appState.setTeste("OLAAAAA");
String action = intent.getAction();
elementos = new Vector<String>();
String delimiter = "_";
String[] temp = null;
xpto = "OLA";
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// SO VAI VERIFICAR DO QUE ESTAO PRESENTES (DISCOVERABLE) OS QUE ESTAO PAIRED
if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
Log.v("TAG","PAIRED AND PRESENT="+device.getName());
temp = device.getName().split(delimiter);
}
int aux = 0;
if(temp != null)
{
for(int i =0; i < temp.length ; i++)
{
if(temp[aux].equals("SapoFit"))
{
elementos.add(temp[aux]+"_"+temp[aux+1]);
Log.v("TAG","Seleccionado="+temp[aux]+"_"+temp[aux+1]);
}
aux++;
}
elSelecionado = temp[0]+"_"+temp[0+1];
}
}
}
};
this.registerReceiver(mReceiver, filter);
Log.v("TAG","HERE COMES empty="+xpto.toString());
My problem is: because the code in that method is executed sequentially, by the time I try to use (in that method in the sequence) some global variables I allocate in Broadcast Receiver, they are still null or empty.
I've though in some "solutions" like moving my "main code" from place to broadcast receiver, or having some other global variable alocated to 1 when B.R is finished (and a while x =! 1 in main code to wait) but this is not good programming, I'm sure there is a correct way to do that.
I've found PendingResult but is API Level 11, too high for me. Any suggestions?