1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-11 20:49:25 +01:00
Gadgetbridge/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/BluetoothStateChangeReceiver.java

44 lines
1.9 KiB
Java
Raw Normal View History

package nodomain.freeyourgadget.gadgetbridge.externalevents;
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;
import nodomain.freeyourgadget.gadgetbridge.activities.ControlCenter;
import nodomain.freeyourgadget.gadgetbridge.service.DeviceCommunicationService;
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) {
Intent refreshIntent = new Intent(ControlCenter.ACTION_REFRESH_DEVICELIST);
LocalBroadcastManager.getInstance(context).sendBroadcast(refreshIntent);
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
if (!sharedPrefs.getBoolean("general_autoconnectonbluetooth", false)) {
return;
}
Intent connectIntent = new Intent(context, DeviceCommunicationService.class);
connectIntent.setAction(DeviceCommunicationService.ACTION_CONNECT);
context.startService(connectIntent);
} else if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_OFF) {
Intent stopIntent = new Intent(context, DeviceCommunicationService.class);
context.stopService(stopIntent);
Intent quitIntent = new Intent(ControlCenter.ACTION_QUIT);
2015-03-27 10:56:08 +01:00
LocalBroadcastManager.getInstance(context).sendBroadcast(quitIntent);
}
}
}
}