2015-05-30 21:40:42 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.externalevents;
|
2015-03-17 21:41:58 +01:00
|
|
|
|
|
|
|
import android.bluetooth.BluetoothAdapter;
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.preference.PreferenceManager;
|
2015-03-27 10:56:08 +01:00
|
|
|
import android.support.v4.content.LocalBroadcastManager;
|
2015-03-17 21:41:58 +01:00
|
|
|
|
2015-08-03 23:09:49 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.activities.ControlCenter;
|
2015-08-06 23:17:41 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.DeviceCommunicationService;
|
2015-05-30 21:40:42 +02:00
|
|
|
|
2015-03-17 21:41:58 +01:00
|
|
|
public class BluetoothStateChangeReceiver extends BroadcastReceiver {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
String action = intent.getAction();
|
|
|
|
|
|
|
|
if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
|
|
|
|
if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_ON) {
|
2015-03-22 12:46:28 +01:00
|
|
|
|
|
|
|
Intent refreshIntent = new Intent(ControlCenter.ACTION_REFRESH_DEVICELIST);
|
2015-03-29 23:43:05 +02:00
|
|
|
LocalBroadcastManager.getInstance(context).sendBroadcast(refreshIntent);
|
2015-03-22 12:46:28 +01:00
|
|
|
|
2015-03-17 21:41:58 +01:00
|
|
|
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
if (!sharedPrefs.getBoolean("general_autoconnectonbluetooth", false)) {
|
|
|
|
return;
|
|
|
|
}
|
2015-03-21 18:18:07 +01:00
|
|
|
|
2015-08-06 23:17:41 +02:00
|
|
|
Intent connectIntent = new Intent(context, DeviceCommunicationService.class);
|
|
|
|
connectIntent.setAction(DeviceCommunicationService.ACTION_CONNECT);
|
|
|
|
context.startService(connectIntent);
|
2015-03-17 22:03:54 +01:00
|
|
|
} else if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_OFF) {
|
2015-08-04 01:01:14 +02:00
|
|
|
Intent stopIntent = new Intent(context, DeviceCommunicationService.class);
|
2015-03-17 22:03:54 +01:00
|
|
|
context.stopService(stopIntent);
|
|
|
|
|
|
|
|
Intent quitIntent = new Intent(ControlCenter.ACTION_QUIT);
|
2015-03-27 10:56:08 +01:00
|
|
|
|
|
|
|
LocalBroadcastManager.getInstance(context).sendBroadcast(quitIntent);
|
2015-03-17 21:41:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|