2017-03-16 17:36:15 +01:00
|
|
|
/* Copyright (C) 2015-2017 Andreas Shimokawa, Carsten Pfeiffer, Daniele
|
|
|
|
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-08-03 23:09:49 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.devices.miband;
|
2015-05-05 00:48:02 +02:00
|
|
|
|
2016-01-06 00:14:24 +01:00
|
|
|
import android.bluetooth.BluetoothAdapter;
|
|
|
|
import android.bluetooth.BluetoothDevice;
|
2015-05-05 00:48:02 +02:00
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.IntentFilter;
|
2015-05-18 20:56:19 +02:00
|
|
|
import android.os.Bundle;
|
2016-01-06 00:14:24 +01:00
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.Looper;
|
2015-05-05 00:48:02 +02:00
|
|
|
import android.support.v4.content.LocalBroadcastManager;
|
|
|
|
import android.widget.TextView;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
2015-12-06 23:37:02 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2015-08-21 00:58:18 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
2015-09-24 14:45:21 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
2017-03-11 19:49:57 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.activities.ControlCenterv2;
|
2015-09-24 14:45:21 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.activities.DiscoveryActivity;
|
2016-07-05 22:13:11 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.activities.GBActivity;
|
2015-08-03 23:09:49 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
2017-01-26 00:11:52 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.AndroidUtils;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
|
2015-12-13 00:43:07 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
2016-04-25 23:18:55 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
|
2015-05-05 00:48:02 +02:00
|
|
|
|
2016-07-05 22:13:11 +02:00
|
|
|
public class MiBandPairingActivity extends GBActivity {
|
2015-12-06 23:37:02 +01:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(MiBandPairingActivity.class);
|
2015-05-05 00:48:02 +02:00
|
|
|
|
2015-05-10 21:13:05 +02:00
|
|
|
private static final int REQ_CODE_USER_SETTINGS = 52;
|
2017-01-28 22:52:22 +01:00
|
|
|
private static final String STATE_DEVICE_CANDIDATE = "stateDeviceCandidate";
|
2016-01-06 00:14:24 +01:00
|
|
|
private static final long DELAY_AFTER_BONDING = 1000; // 1s
|
2015-05-05 00:48:02 +02:00
|
|
|
private TextView message;
|
|
|
|
private boolean isPairing;
|
2017-01-26 00:11:52 +01:00
|
|
|
private GBDeviceCandidate deviceCandidate;
|
2016-01-06 00:14:24 +01:00
|
|
|
private String bondingMacAddress;
|
|
|
|
|
2015-11-23 23:04:46 +01:00
|
|
|
private final BroadcastReceiver mPairingReceiver = new BroadcastReceiver() {
|
2015-05-05 00:48:02 +02:00
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
if (GBDevice.ACTION_DEVICE_CHANGED.equals(intent.getAction())) {
|
|
|
|
GBDevice device = intent.getParcelableExtra(GBDevice.EXTRA_DEVICE);
|
2015-12-06 23:37:02 +01:00
|
|
|
LOG.debug("pairing activity: device changed: " + device);
|
2017-01-26 00:11:52 +01:00
|
|
|
if (deviceCandidate.getMacAddress().equals(device.getAddress())) {
|
2016-07-14 23:53:25 +02:00
|
|
|
if (device.isInitialized()) {
|
2017-01-26 00:11:52 +01:00
|
|
|
pairingFinished(true, deviceCandidate);
|
2016-07-14 23:53:25 +02:00
|
|
|
} else if (device.isConnecting() || device.isInitializing()) {
|
|
|
|
LOG.info("still connecting/initializing device...");
|
|
|
|
}
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-01-06 00:14:24 +01:00
|
|
|
private final BroadcastReceiver mBondingReceiver = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(intent.getAction())) {
|
|
|
|
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
|
2016-07-10 00:10:50 +02:00
|
|
|
LOG.info("Bond state changed: " + device + ", state: " + device.getBondState() + ", expected address: " + bondingMacAddress);
|
2016-01-06 00:14:24 +01:00
|
|
|
if (bondingMacAddress != null && bondingMacAddress.equals(device.getAddress())) {
|
|
|
|
int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.BOND_NONE);
|
|
|
|
if (bondState == BluetoothDevice.BOND_BONDED) {
|
|
|
|
LOG.info("Bonded with " + device.getAddress());
|
|
|
|
bondingMacAddress = null;
|
2016-07-14 23:53:25 +02:00
|
|
|
attemptToConnect();
|
2016-07-10 00:10:50 +02:00
|
|
|
} else if (bondState == BluetoothDevice.BOND_BONDING) {
|
|
|
|
LOG.info("Bonding in progress with " + device.getAddress());
|
|
|
|
} else if (bondState == BluetoothDevice.BOND_NONE) {
|
2016-07-14 23:53:25 +02:00
|
|
|
LOG.info("Not bonded with " + device.getAddress() + ", attempting to connect anyway.");
|
|
|
|
bondingMacAddress = null;
|
|
|
|
attemptToConnect();
|
2016-07-10 00:10:50 +02:00
|
|
|
} else {
|
|
|
|
LOG.warn("Unknown bond state for device " + device.getAddress() + ": " + bondState);
|
2017-01-26 00:11:52 +01:00
|
|
|
pairingFinished(false, deviceCandidate);
|
2016-01-06 00:14:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-07-14 23:53:25 +02:00
|
|
|
private void attemptToConnect() {
|
|
|
|
Looper mainLooper = Looper.getMainLooper();
|
|
|
|
new Handler(mainLooper).postDelayed(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2017-01-25 21:13:10 +01:00
|
|
|
performApplicationLevelPair();
|
2016-07-14 23:53:25 +02:00
|
|
|
}
|
|
|
|
}, DELAY_AFTER_BONDING);
|
|
|
|
}
|
|
|
|
|
2015-05-05 00:48:02 +02:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_mi_band_pairing);
|
|
|
|
|
|
|
|
message = (TextView) findViewById(R.id.miband_pair_message);
|
|
|
|
Intent intent = getIntent();
|
2017-01-26 00:11:52 +01:00
|
|
|
deviceCandidate = intent.getParcelableExtra(DeviceCoordinator.EXTRA_DEVICE_CANDIDATE);
|
|
|
|
if (deviceCandidate == null && savedInstanceState != null) {
|
2017-01-28 22:52:22 +01:00
|
|
|
deviceCandidate = savedInstanceState.getParcelable(STATE_DEVICE_CANDIDATE);
|
2015-05-07 23:51:03 +02:00
|
|
|
}
|
2017-01-26 00:11:52 +01:00
|
|
|
if (deviceCandidate == null) {
|
2015-05-05 00:48:02 +02:00
|
|
|
Toast.makeText(this, getString(R.string.message_cannot_pair_no_mac), Toast.LENGTH_SHORT).show();
|
2015-05-06 00:17:28 +02:00
|
|
|
startActivity(new Intent(this, DiscoveryActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
|
2015-05-05 00:48:02 +02:00
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-07 23:51:03 +02:00
|
|
|
if (!MiBandCoordinator.hasValidUserInfo()) {
|
|
|
|
Intent userSettingsIntent = new Intent(this, MiBandPreferencesActivity.class);
|
2015-05-10 21:13:05 +02:00
|
|
|
startActivityForResult(userSettingsIntent, REQ_CODE_USER_SETTINGS, null);
|
2015-05-07 23:51:03 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// already valid user info available, use that and pair
|
2015-05-05 00:48:02 +02:00
|
|
|
startPairing();
|
|
|
|
}
|
|
|
|
|
2015-05-07 23:51:03 +02:00
|
|
|
@Override
|
|
|
|
protected void onSaveInstanceState(Bundle outState) {
|
|
|
|
super.onSaveInstanceState(outState);
|
2017-01-28 22:52:22 +01:00
|
|
|
outState.putParcelable(STATE_DEVICE_CANDIDATE, deviceCandidate);
|
2015-05-07 23:51:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onRestoreInstanceState(Bundle savedInstanceState) {
|
|
|
|
super.onRestoreInstanceState(savedInstanceState);
|
2017-01-28 22:52:22 +01:00
|
|
|
deviceCandidate = savedInstanceState.getParcelable(STATE_DEVICE_CANDIDATE);
|
2015-05-07 23:51:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
|
|
super.onActivityResult(requestCode, resultCode, data);
|
|
|
|
// start pairing immediately when we return from the user settings
|
2015-05-10 21:13:05 +02:00
|
|
|
if (requestCode == REQ_CODE_USER_SETTINGS) {
|
2015-05-07 23:51:03 +02:00
|
|
|
if (!MiBandCoordinator.hasValidUserInfo()) {
|
2015-12-13 00:43:07 +01:00
|
|
|
GB.toast(this, getString(R.string.miband_pairing_using_dummy_userdata), Toast.LENGTH_LONG, GB.WARN);
|
2015-05-07 23:51:03 +02:00
|
|
|
}
|
|
|
|
startPairing();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-05 00:48:02 +02:00
|
|
|
@Override
|
|
|
|
protected void onDestroy() {
|
2017-01-26 00:11:52 +01:00
|
|
|
// just to be sure, remove the receivers -- might actually be already unregistered
|
|
|
|
AndroidUtils.safeUnregisterBroadcastReceiver(LocalBroadcastManager.getInstance(this), mPairingReceiver);
|
|
|
|
AndroidUtils.safeUnregisterBroadcastReceiver(this, mBondingReceiver);
|
2015-05-05 00:48:02 +02:00
|
|
|
if (isPairing) {
|
|
|
|
stopPairing();
|
|
|
|
}
|
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void startPairing() {
|
|
|
|
isPairing = true;
|
2017-01-26 00:11:52 +01:00
|
|
|
message.setText(getString(R.string.pairing, deviceCandidate));
|
|
|
|
|
|
|
|
IntentFilter filter = new IntentFilter(GBDevice.ACTION_DEVICE_CHANGED);
|
|
|
|
LocalBroadcastManager.getInstance(this).registerReceiver(mPairingReceiver, filter);
|
2016-01-06 00:14:24 +01:00
|
|
|
|
2017-01-25 22:59:32 +01:00
|
|
|
if (!shouldSetupBTLevelPairing()) {
|
|
|
|
// there are connection problems on certain Galaxy S devices at least;
|
|
|
|
// try to connect without BT pairing (bonding)
|
|
|
|
attemptToConnect();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-06 00:14:24 +01:00
|
|
|
filter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
|
|
|
|
registerReceiver(mBondingReceiver, filter);
|
|
|
|
|
2017-01-26 00:11:52 +01:00
|
|
|
performBluetoothPair(deviceCandidate);
|
2017-01-25 22:59:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private boolean shouldSetupBTLevelPairing() {
|
|
|
|
Prefs prefs = GBApplication.getPrefs();
|
|
|
|
return prefs.getPreferences().getBoolean(MiBandConst.PREF_MIBAND_SETUP_BT_PAIRING, true);
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|
|
|
|
|
2017-01-26 00:11:52 +01:00
|
|
|
private void pairingFinished(boolean pairedSuccessfully, GBDeviceCandidate candidate) {
|
2016-02-13 00:09:35 +01:00
|
|
|
LOG.debug("pairingFinished: " + pairedSuccessfully);
|
|
|
|
if (!isPairing) {
|
|
|
|
// already gone?
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-05 00:48:02 +02:00
|
|
|
isPairing = false;
|
2017-01-26 00:11:52 +01:00
|
|
|
AndroidUtils.safeUnregisterBroadcastReceiver(LocalBroadcastManager.getInstance(this), mPairingReceiver);
|
|
|
|
AndroidUtils.safeUnregisterBroadcastReceiver(this, mBondingReceiver);
|
2015-05-05 00:48:02 +02:00
|
|
|
|
2016-07-10 00:10:50 +02:00
|
|
|
if (pairedSuccessfully) {
|
2016-07-14 23:53:25 +02:00
|
|
|
// remember the device since we do not necessarily pair... temporary -- we probably need
|
|
|
|
// to query the db for available devices in ControlCenter. But only remember un-bonded
|
|
|
|
// devices, as bonded devices are displayed anyway.
|
2017-01-26 00:11:52 +01:00
|
|
|
String macAddress = deviceCandidate.getMacAddress();
|
2016-07-14 23:53:25 +02:00
|
|
|
BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(macAddress);
|
|
|
|
if (device != null && device.getBondState() == BluetoothDevice.BOND_NONE) {
|
|
|
|
Prefs prefs = GBApplication.getPrefs();
|
|
|
|
prefs.getPreferences().edit().putString(MiBandConst.PREF_MIBAND_ADDRESS, macAddress).apply();
|
|
|
|
}
|
2017-03-11 19:49:57 +01:00
|
|
|
Intent intent = new Intent(this, ControlCenterv2.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
2016-07-10 00:10:50 +02:00
|
|
|
startActivity(intent);
|
|
|
|
}
|
2015-05-05 00:48:02 +02:00
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void stopPairing() {
|
|
|
|
// TODO
|
|
|
|
isPairing = false;
|
|
|
|
}
|
2016-01-06 00:14:24 +01:00
|
|
|
|
2017-01-26 00:11:52 +01:00
|
|
|
protected void performBluetoothPair(GBDeviceCandidate deviceCandidate) {
|
|
|
|
BluetoothDevice device = deviceCandidate.getDevice();
|
|
|
|
|
2016-01-06 00:14:24 +01:00
|
|
|
int bondState = device.getBondState();
|
|
|
|
if (bondState == BluetoothDevice.BOND_BONDED) {
|
2016-11-19 22:13:00 +01:00
|
|
|
GB.toast(getString(R.string.pairing_already_bonded, device.getName(), device.getAddress()), Toast.LENGTH_SHORT, GB.INFO);
|
2017-01-25 21:13:10 +01:00
|
|
|
performApplicationLevelPair();
|
2016-01-06 00:14:24 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bondingMacAddress = device.getAddress();
|
|
|
|
if (bondState == BluetoothDevice.BOND_BONDING) {
|
2016-11-19 22:13:00 +01:00
|
|
|
GB.toast(this, getString(R.string.pairing_in_progress, device.getName(), bondingMacAddress), Toast.LENGTH_LONG, GB.INFO);
|
2016-01-06 00:14:24 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-19 22:13:00 +01:00
|
|
|
GB.toast(this, getString(R.string.pairing_creating_bond_with, device.getName(), bondingMacAddress), Toast.LENGTH_LONG, GB.INFO);
|
2016-01-06 00:14:24 +01:00
|
|
|
if (!device.createBond()) {
|
2016-11-19 22:13:00 +01:00
|
|
|
GB.toast(this, getString(R.string.pairing_unable_to_pair_with, device.getName(), bondingMacAddress), Toast.LENGTH_LONG, GB.ERROR);
|
2016-01-06 00:14:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-25 21:13:10 +01:00
|
|
|
private void performApplicationLevelPair() {
|
2016-07-14 23:53:25 +02:00
|
|
|
GBApplication.deviceService().disconnect(); // just to make sure...
|
2017-01-26 00:11:52 +01:00
|
|
|
GBDevice device = DeviceHelper.getInstance().toSupportedDevice(deviceCandidate);
|
|
|
|
if (device != null) {
|
|
|
|
GBApplication.deviceService().connect(device, true);
|
|
|
|
} else {
|
|
|
|
GB.toast(this, "Unable to connect, can't recognize the device type: " + deviceCandidate, Toast.LENGTH_LONG, GB.ERROR);
|
|
|
|
}
|
2016-01-06 00:14:24 +01:00
|
|
|
}
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|