2020-01-09 10:44:32 +01:00
|
|
|
/* Copyright (C) 2015-2020 Andreas Shimokawa, Carsten Pfeiffer, Daniele
|
2019-02-13 20:43:30 +01:00
|
|
|
Gobbetti
|
2017-03-10 14:53:19 +01:00
|
|
|
|
|
|
|
This file is part of Gadgetbridge.
|
|
|
|
|
|
|
|
Gadgetbridge is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Gadgetbridge is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
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;
|
|
|
|
|
2017-01-29 22:56:56 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2019-01-26 15:52:40 +01:00
|
|
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
2015-08-21 00:58:18 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
2016-06-18 23:35:34 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceManager;
|
2016-04-25 23:18:55 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
|
2015-05-30 21:40:42 +02:00
|
|
|
|
2015-03-17 21:41:58 +01:00
|
|
|
public class BluetoothStateChangeReceiver extends BroadcastReceiver {
|
2017-01-29 22:56:56 +01:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(BluetoothStateChangeReceiver.class);
|
|
|
|
|
2015-03-17 21:41:58 +01:00
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
String action = intent.getAction();
|
|
|
|
|
2018-05-11 20:47:37 +02:00
|
|
|
if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
|
2015-03-17 21:41:58 +01:00
|
|
|
if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_ON) {
|
2015-03-22 12:46:28 +01:00
|
|
|
|
2016-06-18 23:35:34 +02:00
|
|
|
Intent refreshIntent = new Intent(DeviceManager.ACTION_REFRESH_DEVICELIST);
|
2015-03-29 23:43:05 +02:00
|
|
|
LocalBroadcastManager.getInstance(context).sendBroadcast(refreshIntent);
|
2015-03-22 12:46:28 +01:00
|
|
|
|
2016-04-25 23:18:55 +02:00
|
|
|
Prefs prefs = GBApplication.getPrefs();
|
|
|
|
if (!prefs.getBoolean("general_autoconnectonbluetooth", false)) {
|
2015-03-17 21:41:58 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-03-21 18:18:07 +01:00
|
|
|
|
2020-07-19 23:58:21 +02:00
|
|
|
LOG.info("Bluetooth turned on (ACTION_STATE_CHANGED) => connecting...");
|
2015-08-21 00:58:18 +02:00
|
|
|
GBApplication.deviceService().connect();
|
2015-03-17 22:03:54 +01:00
|
|
|
} else if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_OFF) {
|
2018-05-11 20:47:37 +02:00
|
|
|
LOG.info("Bluetooth turned off => disconnecting...");
|
2017-05-01 17:33:34 +02:00
|
|
|
GBApplication.deviceService().disconnect();
|
2015-03-17 21:41:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|