I need some advice or direction how to solve this problem. My app has got 3 tabs based on 3 fragment classes and one MainActivity. When I start app, Bluetooth connection is established in MainActivity. For that reason I have got threads which establish connection and start listen for incomming data, this data are also saving in thread in buffer queue. Some other class parses this data and put it in public class, that is accessiable from everywhere.
So my problem is: First tab is called INFO tab, and from info fragment or MainActivity I need to send over BT info request immediately after BT connection is established. But I can't do that on onCreateView method in info fragment, because connection needs few seconds to get establisehd, so it will crash. I have for indicating established connection variable boolean deviceConnected, which is set on true when device is connected.
So, I think that I need some event, that will test variable deviceConnected, and when it will be true, it will send info request (method for sending data over BT is called from MainActivity). I also need some event, that will test when parsed data is available, to referesh UI tab. Please, help! I can give code or another explanation, if you don't understand what I'm looking for.
This is code for fragment tab. When "button" is pressed, we call method from MainActivity for start connection. We also send Intention, but just with msg "blah". How do I tell, that I'm waiting on connected Bluetooth?
public class FragmentA extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.fragment_a, container, false);
Button button = (Button) myFragmentView.findViewById(R.id.button1);
//when button connect is clicked, call method from MainActivity
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//start bluetooth connection in MainActivity
((MainActivity)getActivity()).startConnectionFromActivity();
final Intent intent = new Intent();
intent.setAction(android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED);
getActivity().sendBroadcast(intent);
}
});
return myFragmentView;
}
}
Here are my parts of MainActivity:
public class MainActivity extends Activity {
private BroadcastReceiver mReceiver;
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
IntentFilter intentFilter = new IntentFilter(
android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED);
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//from there should I send request info??
}
};
//registering our receiver
this.registerReceiver(mReceiver, intentFilter);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
//unregister our receiver
this.unregisterReceiver(this.mReceiver);
}
}
If I understand correct, when this is all setup up, when BT will be established, onReceive methd will be fired? So from there I can send info request over bluetooth? Thanks for help!