I'm writing an application that uses bluetooth connection to read some data from COM port over bluetooth. I am using ViewPager
to display several Fragments which I use to show some data (each Fragment in a different way). The app has 2 activities:
MainActivity
- initialization of the bluetooth connection and managing mentioned fragmentsActivity
used to search for bluetooth devices
When MainActivity
starts, it activates the bluetooth in case it's off and displays first of the fragments. I can scan for devices by pressing the button in the action panel.
My problem is quite simple - I'd like to push some initial data (about connection) to fragments after initialization of the MainActivity
, and after making a BT connection I want to push some data (about 10 reads/sec) only to a visible fragment.
I tried to do this by using LocalBroadcastManager in all of the fragments, but the broadcastReceiver
is not registered that early and it don't receive all of the messages. I tried to move registration to onCreate
in the fragments classes but it didn't work either. I've also tried to implement callback interface to the main activity but I was getting NPE.
Besides that one issue I'm worried it might not be the best solution to the problem of updating one or more fragments about the changes. I'm open to advices.
Mainactivity class:
@Override
protected void onStart() {
super.onStart();
if (!bluetoothAdapter.isEnabled()) {
Intent enableBluetoothIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetoothIntent, REQUEST_ENABLE_BLUETOOTH);
} else {
if (btService == null) {
btService = new BluetoothConnectionService(this, msgHandler);
}
}
}
private final Handler msgHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
Intent intent = new Intent(STATUS_EVENT);
switch (msg.what) {
case MESSAGE_STATE_CHANGE:
switch (msg.arg1) {
case BluetoothConnectionService.STATE_CONNECTED:
intent.putExtra("status", "Connected");
// tell fragments about the state change
break;
case BluetoothConnectionService.STATE_CONNECTING:
intent.putExtra("status", "Connecting");
// tell fragments about the state change
break;
case BluetoothConnectionService.STATE_NONE:
intent.putExtra("status", "Not connected");
// tell fragments about the state change
break;
default:
intent.putExtra("status","Unknown state");
break;
}
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);
}
}
};
BTService part:
public void setCurrentConnectionState(int currentConnectionState) {
this.currentConnectionState = currentConnectionState;
msgHandler.obtainMessage(MainActivity.MESSAGE_STATE_CHANGE, currentConnectionState, -1).sendToTarget();
}
public BluetoothConnectionService(Context context, Handler handler){
this.context = context;
this.msgHandler = handler;
setCurrentConnectionState(STATE_NONE);
}
One of the fragments:
public class ConnectionStatusFragment extends Fragment {
TextView connectionStatus;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_connection_info, container, false);
connectionStatus = (TextView) rootView.findViewById(R.id.connectionStatus);
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(broadcastReceiver, new IntentFilter(MainActivity.STATUS_EVENT));
return rootView;
}
private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
connectionStatus.setText(intent.getStringExtra("status"));
}
};
}