Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash when enabling bluetooth after starting the app #108

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.app.Activity;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
Expand Down Expand Up @@ -49,6 +50,7 @@ public class MainActivity extends AppCompatActivity implements ConnectionListene
private ViewPager mViewPager;
private Menu menu;
private BroadcastReceiver mUsbReceiver;
private BroadcastReceiver mBluetoothReceiver;
private PendingIntent mPermissionIntent;
BTService bt;
UDPService udp;
Expand Down Expand Up @@ -147,7 +149,7 @@ protected void onCreate(Bundle savedInstanceState) {

TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);

initBroadcastReceiverForBluetooth();
initBluetooth();
initUDP();
initBroadcastReceiverForUsbPermissions();
Expand Down Expand Up @@ -341,8 +343,15 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == BluetoothState.REQUEST_CONNECT_DEVICE) {
if (resultCode == Activity.RESULT_OK) {
bt.connect(data);
useBT();
if(bt.isServiceAvailable()) {
bt.connect(data);
useBT();
} else {
Toast.makeText(getApplicationContext()
, "Bluetooth busy. Try again in a few seconds."
, Toast.LENGTH_SHORT).show();
bt.runService(); // Try to start the service again, in case it failed somewhere
}
}
} else if (requestCode == BluetoothState.REQUEST_ENABLE_BT) {
if (resultCode == Activity.RESULT_OK) {
Expand All @@ -366,6 +375,22 @@ private void ensurePermissions() {
}
}

private void initBroadcastReceiverForBluetooth() {
mBluetoothReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) {
if(intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_ON) {
bt.runService();
} else {
bt.stopService();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stopping the service on each state change except ON seems not quite safe to me

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BluetoothAdapter.EXTRA_STATE can be either be STATE_OFF, STATE_TURNING_ON, STATE_ON or STATE_TURNING_OFF. In each case except STATE_ON we want the service to be stopped to avoid any invalid state and cover all edge cases. It is correct, that we might be stopping the service several times, but stopping an already stopped service shouldn't change the program state at all.

Stopping the service multiple times, seems even more safe to me, so I am not sure what you mean by "seems not quite safe to me"?

}
}
}
};
registerReceiver(mBluetoothReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
}

private void connectToUsbDevice() {
usb.connect();
useUSB();
Expand Down