2020-01-09 10:44:32 +01:00
|
|
|
/* Copyright (C) 2015-2020 Andreas Shimokawa, boun, Carsten Pfeiffer, Daniel
|
2019-12-06 22:49:44 +01:00
|
|
|
Dakhno, Daniele Gobbetti, JohnnySun, jonnsoft, Lem Dulfo, Taavi Eomäe,
|
|
|
|
Uwe Hermann
|
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.activities;
|
2015-05-05 00:48:02 +02:00
|
|
|
|
2016-05-26 14:58:36 +02:00
|
|
|
import android.Manifest;
|
2015-05-05 00:48:02 +02:00
|
|
|
import android.app.Activity;
|
2017-04-06 23:47:35 +02:00
|
|
|
import android.app.AlertDialog;
|
2015-05-05 00:48:02 +02:00
|
|
|
import android.bluetooth.BluetoothAdapter;
|
|
|
|
import android.bluetooth.BluetoothDevice;
|
|
|
|
import android.bluetooth.BluetoothManager;
|
2018-03-30 15:04:53 +02:00
|
|
|
import android.bluetooth.le.BluetoothLeScanner;
|
2016-09-23 05:55:34 +02:00
|
|
|
import android.bluetooth.le.ScanCallback;
|
2016-11-27 01:09:20 +01:00
|
|
|
import android.bluetooth.le.ScanFilter;
|
2016-11-27 02:41:52 +01:00
|
|
|
import android.bluetooth.le.ScanRecord;
|
2016-09-23 05:55:34 +02:00
|
|
|
import android.bluetooth.le.ScanResult;
|
|
|
|
import android.bluetooth.le.ScanSettings;
|
2020-07-19 23:58:21 +02:00
|
|
|
import android.companion.AssociationRequest;
|
|
|
|
import android.companion.BluetoothDeviceFilter;
|
|
|
|
import android.companion.CompanionDeviceManager;
|
2015-05-05 00:48:02 +02:00
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
2017-04-06 23:47:35 +02:00
|
|
|
import android.content.DialogInterface;
|
2015-05-05 00:48:02 +02:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.IntentFilter;
|
2020-07-19 23:58:21 +02:00
|
|
|
import android.content.IntentSender;
|
2019-10-12 19:44:45 +02:00
|
|
|
import android.content.SharedPreferences;
|
2016-05-26 14:58:36 +02:00
|
|
|
import android.content.pm.PackageManager;
|
2020-07-19 23:58:21 +02:00
|
|
|
import android.location.LocationManager;
|
2016-09-23 05:55:34 +02:00
|
|
|
import android.os.Build;
|
2015-05-05 00:48:02 +02:00
|
|
|
import android.os.Bundle;
|
2015-05-06 22:06:09 +02:00
|
|
|
import android.os.Handler;
|
2015-05-10 23:14:32 +02:00
|
|
|
import android.os.Message;
|
2016-08-14 23:21:09 +02:00
|
|
|
import android.os.ParcelUuid;
|
2015-05-05 00:48:02 +02:00
|
|
|
import android.os.Parcelable;
|
2020-07-19 23:58:21 +02:00
|
|
|
import android.provider.Settings;
|
2015-05-05 00:48:02 +02:00
|
|
|
import android.view.View;
|
|
|
|
import android.widget.AdapterView;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.ListView;
|
|
|
|
import android.widget.ProgressBar;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
2019-08-03 23:40:07 +02:00
|
|
|
import androidx.annotation.NonNull;
|
2020-07-19 23:58:21 +02:00
|
|
|
import androidx.annotation.RequiresApi;
|
2019-04-21 21:18:08 +02:00
|
|
|
import androidx.core.app.ActivityCompat;
|
|
|
|
|
2015-05-12 06:28:11 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2015-05-05 00:48:02 +02:00
|
|
|
|
2015-05-18 20:56:19 +02:00
|
|
|
import java.util.ArrayList;
|
2016-11-27 01:09:20 +01:00
|
|
|
import java.util.List;
|
2018-03-30 15:04:53 +02:00
|
|
|
import java.util.Objects;
|
2015-05-18 20:56:19 +02:00
|
|
|
|
2016-09-26 22:30:15 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
2015-09-24 14:45:21 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
2019-04-21 21:18:08 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsActivity;
|
2015-09-24 14:45:21 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.adapter.DeviceCandidateAdapter;
|
2015-08-03 23:09:49 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
|
2015-09-24 14:45:21 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
2015-08-03 23:09:49 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate;
|
2016-11-27 01:09:20 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
|
2016-11-27 02:41:52 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.AndroidUtils;
|
2015-08-03 23:09:49 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
2020-07-19 23:58:21 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
|
|
|
|
|
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.util.GB.toast;
|
2015-05-05 00:48:02 +02:00
|
|
|
|
2016-09-23 05:55:34 +02:00
|
|
|
|
2019-04-21 21:18:08 +02:00
|
|
|
public class DiscoveryActivity extends AbstractGBActivity implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {
|
2015-05-12 06:28:11 +02:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(DiscoveryActivity.class);
|
2020-07-19 23:58:21 +02:00
|
|
|
private static final long SCAN_DURATION = 30000; // 30s
|
|
|
|
private static final int REQUEST_CODE = 1;
|
2020-08-01 21:20:53 +02:00
|
|
|
private final Handler handler = new Handler();
|
|
|
|
private final ArrayList<GBDeviceCandidate> deviceCandidates = new ArrayList<>();
|
2020-07-19 23:58:21 +02:00
|
|
|
private ScanCallback newBLEScanCallback = null;
|
2020-08-01 21:20:53 +02:00
|
|
|
/**
|
|
|
|
* Use old BLE scanning
|
|
|
|
**/
|
2020-07-19 23:58:21 +02:00
|
|
|
private boolean oldBleScanning = false;
|
2020-08-01 21:20:53 +02:00
|
|
|
/**
|
|
|
|
* If already bonded devices are to be ignored when scanning
|
|
|
|
*/
|
2020-07-19 23:58:21 +02:00
|
|
|
private boolean ignoreBonded = true;
|
2020-08-01 21:20:53 +02:00
|
|
|
/**
|
|
|
|
* If new CompanionDevice-type pairing is enabled on newer Androids
|
|
|
|
**/
|
2020-07-19 23:58:21 +02:00
|
|
|
private boolean enableCompanionDevicePairing = false;
|
|
|
|
private ProgressBar bluetoothProgress;
|
|
|
|
private ProgressBar bluetoothLEProgress;
|
2020-08-01 21:20:53 +02:00
|
|
|
private DeviceCandidateAdapter deviceCandidateAdapter;
|
|
|
|
private final BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() {
|
|
|
|
@Override
|
|
|
|
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
|
|
|
|
//logMessageContent(scanRecord);
|
|
|
|
handleDeviceFound(device, (short) rssi);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
private BluetoothAdapter adapter;
|
|
|
|
private Button startButton;
|
|
|
|
private Scanning isScanning = Scanning.SCANNING_OFF;
|
2020-07-19 23:58:21 +02:00
|
|
|
private final Runnable stopRunnable = new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (isScanning == Scanning.SCANNING_BT_NEXT_BLE) {
|
|
|
|
// Start the next scan in the series
|
|
|
|
stopDiscovery();
|
|
|
|
startDiscovery(Scanning.SCANNING_BLE);
|
|
|
|
} else {
|
|
|
|
stopDiscovery();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-08-01 21:20:53 +02:00
|
|
|
private GBDeviceCandidate bondingDevice;
|
2015-11-23 23:04:46 +01:00
|
|
|
private final BroadcastReceiver bluetoothReceiver = new BroadcastReceiver() {
|
2015-05-05 00:48:02 +02:00
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
2018-03-30 15:04:53 +02:00
|
|
|
switch (Objects.requireNonNull(intent.getAction())) {
|
2020-08-02 22:43:00 +02:00
|
|
|
case BluetoothAdapter.ACTION_DISCOVERY_STARTED: {
|
|
|
|
LOG.debug("ACTION_DISCOVERY_STARTED");
|
2020-07-19 23:58:21 +02:00
|
|
|
if (isScanning != Scanning.SCANNING_BLE) {
|
|
|
|
if (isScanning != Scanning.SCANNING_BT_NEXT_BLE) {
|
2020-08-02 03:56:40 +02:00
|
|
|
setIsScanning(Scanning.SCANNING_BT);
|
2020-07-19 23:58:21 +02:00
|
|
|
}
|
2016-10-03 22:31:33 +02:00
|
|
|
}
|
2015-05-05 00:48:02 +02:00
|
|
|
break;
|
2020-08-02 22:43:00 +02:00
|
|
|
}
|
|
|
|
case BluetoothAdapter.ACTION_DISCOVERY_FINISHED: {
|
|
|
|
LOG.debug("ACTION_DISCOVERY_FINISHED");
|
2016-10-28 23:48:13 +02:00
|
|
|
handler.post(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
// continue with LE scan, if available
|
2020-07-19 23:58:21 +02:00
|
|
|
if (isScanning == Scanning.SCANNING_BT || isScanning == Scanning.SCANNING_BT_NEXT_BLE) {
|
2016-10-28 23:48:13 +02:00
|
|
|
checkAndRequestLocationPermission();
|
2020-07-19 23:58:21 +02:00
|
|
|
stopDiscovery();
|
|
|
|
startDiscovery(Scanning.SCANNING_BLE);
|
2016-10-28 23:48:13 +02:00
|
|
|
}
|
2016-09-26 22:30:15 +02:00
|
|
|
}
|
2016-10-28 23:48:13 +02:00
|
|
|
});
|
2015-05-05 00:48:02 +02:00
|
|
|
break;
|
2020-08-02 22:43:00 +02:00
|
|
|
}
|
|
|
|
case BluetoothAdapter.ACTION_STATE_CHANGED: {
|
|
|
|
LOG.debug("ACTION_STATE_CHANGED ");
|
2015-05-05 00:48:02 +02:00
|
|
|
int newState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF);
|
2018-03-30 15:04:53 +02:00
|
|
|
bluetoothStateChanged(newState);
|
2015-05-05 00:48:02 +02:00
|
|
|
break;
|
2020-08-02 22:43:00 +02:00
|
|
|
}
|
2015-05-10 21:32:41 +02:00
|
|
|
case BluetoothDevice.ACTION_FOUND: {
|
2020-08-02 22:43:00 +02:00
|
|
|
LOG.debug("ACTION_FOUND");
|
2015-05-09 23:54:47 +02:00
|
|
|
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
|
|
|
|
short rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, GBDevice.RSSI_UNKNOWN);
|
|
|
|
handleDeviceFound(device, rssi);
|
2015-05-05 00:48:02 +02:00
|
|
|
break;
|
2015-05-10 21:32:41 +02:00
|
|
|
}
|
2016-11-27 02:41:52 +01:00
|
|
|
case BluetoothDevice.ACTION_UUID: {
|
2020-08-02 22:43:00 +02:00
|
|
|
LOG.debug("ACTION_UUID");
|
2016-11-27 02:41:52 +01:00
|
|
|
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
|
|
|
|
short rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, GBDevice.RSSI_UNKNOWN);
|
|
|
|
Parcelable[] uuids = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);
|
2018-03-30 15:38:29 +02:00
|
|
|
ParcelUuid[] uuids2 = AndroidUtils.toParcelUuids(uuids);
|
2016-11-27 02:41:52 +01:00
|
|
|
handleDeviceFound(device, rssi, uuids2);
|
|
|
|
break;
|
|
|
|
}
|
2015-05-10 21:32:41 +02:00
|
|
|
case BluetoothDevice.ACTION_BOND_STATE_CHANGED: {
|
2020-08-02 22:43:00 +02:00
|
|
|
LOG.debug("ACTION_BOND_STATE_CHANGED");
|
2015-05-10 21:32:41 +02:00
|
|
|
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
|
2017-04-06 23:47:35 +02:00
|
|
|
if (device != null && bondingDevice != null && device.getAddress().equals(bondingDevice.getMacAddress())) {
|
2015-05-10 21:32:41 +02:00
|
|
|
int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.BOND_NONE);
|
|
|
|
if (bondState == BluetoothDevice.BOND_BONDED) {
|
2017-03-04 16:03:36 +01:00
|
|
|
handleDeviceBonded();
|
2015-05-10 21:32:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2015-05-09 23:54:47 +02:00
|
|
|
|
2017-04-06 23:47:35 +02:00
|
|
|
private void connectAndFinish(GBDevice device) {
|
2020-07-19 23:58:21 +02:00
|
|
|
toast(DiscoveryActivity.this, getString(R.string.discovery_trying_to_connect_to, device.getName()), Toast.LENGTH_SHORT, GB.INFO);
|
2017-04-07 00:40:33 +02:00
|
|
|
GBApplication.deviceService().connect(device, true);
|
2017-03-04 16:03:36 +01:00
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
2017-04-06 23:47:35 +02:00
|
|
|
private void createBond(final GBDeviceCandidate deviceCandidate, int bondingStyle) {
|
|
|
|
if (bondingStyle == DeviceCoordinator.BONDING_STYLE_NONE) {
|
2020-07-19 23:58:21 +02:00
|
|
|
// Do nothing
|
2017-04-06 23:47:35 +02:00
|
|
|
return;
|
2020-07-19 23:58:21 +02:00
|
|
|
} else if (bondingStyle == DeviceCoordinator.BONDING_STYLE_ASK) {
|
2017-04-06 23:47:35 +02:00
|
|
|
new AlertDialog.Builder(this)
|
|
|
|
.setCancelable(true)
|
|
|
|
.setTitle(DiscoveryActivity.this.getString(R.string.discovery_pair_title, deviceCandidate.getName()))
|
|
|
|
.setMessage(DiscoveryActivity.this.getString(R.string.discovery_pair_question))
|
|
|
|
.setPositiveButton(DiscoveryActivity.this.getString(R.string.discovery_yes_pair), new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
doCreatePair(deviceCandidate);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.setNegativeButton(R.string.discovery_dont_pair, new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
GBDevice device = DeviceHelper.getInstance().toSupportedDevice(deviceCandidate);
|
|
|
|
connectAndFinish(device);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.show();
|
|
|
|
} else {
|
|
|
|
doCreatePair(deviceCandidate);
|
|
|
|
}
|
2020-07-19 23:58:21 +02:00
|
|
|
LOG.debug("Bonding initiated");
|
2017-04-06 23:47:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void doCreatePair(GBDeviceCandidate deviceCandidate) {
|
2020-07-19 23:58:21 +02:00
|
|
|
toast(DiscoveryActivity.this, getString(R.string.discovery_attempting_to_pair, deviceCandidate.getName()), Toast.LENGTH_SHORT, GB.INFO);
|
2020-07-29 13:32:00 +02:00
|
|
|
if (enableCompanionDevicePairing && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
companionDevicePair(deviceCandidate);
|
2017-04-06 23:47:35 +02:00
|
|
|
} else {
|
2020-07-19 23:58:21 +02:00
|
|
|
deviceBond(deviceCandidate);
|
2017-04-06 23:47:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-19 23:58:21 +02:00
|
|
|
private void deviceBond(GBDeviceCandidate deviceCandidate) {
|
|
|
|
if (deviceCandidate.getDevice().createBond()) {
|
|
|
|
// Async, wait for bonding event to finish this activity
|
|
|
|
LOG.info("Bonding in progress...");
|
|
|
|
bondingDevice = deviceCandidate;
|
|
|
|
} else {
|
|
|
|
toast(DiscoveryActivity.this, getString(R.string.discovery_bonding_failed_immediately, deviceCandidate.getName()), Toast.LENGTH_SHORT, GB.ERROR);
|
2016-09-23 05:55:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-14 23:21:09 +02:00
|
|
|
public void logMessageContent(byte[] value) {
|
|
|
|
if (value != null) {
|
2018-07-22 14:52:17 +02:00
|
|
|
LOG.warn("DATA: " + GB.hexdump(value, 0, value.length));
|
2016-08-14 23:21:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-19 23:58:21 +02:00
|
|
|
@RequiresApi(Build.VERSION_CODES.O)
|
|
|
|
private void companionDevicePair(final GBDeviceCandidate deviceCandidate) {
|
|
|
|
CompanionDeviceManager deviceManager = getSystemService(CompanionDeviceManager.class);
|
|
|
|
|
|
|
|
BluetoothDeviceFilter deviceFilter = new BluetoothDeviceFilter.Builder()
|
|
|
|
.setAddress(deviceCandidate.getMacAddress())
|
|
|
|
.build();
|
|
|
|
|
|
|
|
AssociationRequest pairingRequest = new AssociationRequest.Builder()
|
|
|
|
.addDeviceFilter(deviceFilter)
|
|
|
|
.setSingleDevice(true)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
deviceManager.associate(pairingRequest,
|
|
|
|
new CompanionDeviceManager.Callback() {
|
|
|
|
@Override
|
|
|
|
public void onFailure(CharSequence error) {
|
|
|
|
toast(DiscoveryActivity.this, getString(R.string.discovery_bonding_failed_immediately, deviceCandidate.getName()), Toast.LENGTH_SHORT, GB.ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDeviceFound(IntentSender chooserLauncher) {
|
|
|
|
try {
|
|
|
|
startIntentSenderForResult(chooserLauncher,
|
|
|
|
REQUEST_CODE, null, 0, 0, 0);
|
|
|
|
} catch (IntentSender.SendIntentException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
null
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@RequiresApi(Build.VERSION_CODES.O)
|
|
|
|
@Override
|
|
|
|
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
|
|
super.onActivityResult(requestCode, resultCode, data);
|
|
|
|
if (requestCode == REQUEST_CODE &&
|
|
|
|
resultCode == Activity.RESULT_OK) {
|
|
|
|
|
|
|
|
BluetoothDevice deviceToPair =
|
|
|
|
data.getParcelableExtra(CompanionDeviceManager.EXTRA_DEVICE);
|
|
|
|
|
|
|
|
if (deviceToPair != null) {
|
|
|
|
deviceBond(new GBDeviceCandidate(deviceToPair, (short) 0, null));
|
|
|
|
handleDeviceBonded();
|
|
|
|
}
|
2015-05-06 22:06:09 +02:00
|
|
|
}
|
2020-07-19 23:58:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void handleDeviceBonded() {
|
|
|
|
if (bondingDevice == null) {
|
2020-07-29 17:20:17 +02:00
|
|
|
LOG.error("deviceCandidate was null! Can't handle bonded device!");
|
2020-07-19 23:58:21 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
toast(DiscoveryActivity.this, getString(R.string.discovery_successfully_bonded, bondingDevice.getName()), Toast.LENGTH_SHORT, GB.INFO);
|
|
|
|
GBDevice device = DeviceHelper.getInstance().toSupportedDevice(bondingDevice);
|
|
|
|
connectAndFinish(device);
|
|
|
|
}
|
2015-05-05 00:48:02 +02:00
|
|
|
|
2020-07-19 23:58:21 +02:00
|
|
|
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
|
|
|
|
private ScanCallback getScanCallback() {
|
|
|
|
if (newBLEScanCallback != null) {
|
|
|
|
return newBLEScanCallback;
|
|
|
|
}
|
2020-08-01 02:06:13 +02:00
|
|
|
|
2020-07-19 23:58:21 +02:00
|
|
|
newBLEScanCallback = new ScanCallback() {
|
2020-07-29 13:32:00 +02:00
|
|
|
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
|
2020-07-19 23:58:21 +02:00
|
|
|
@Override
|
|
|
|
public void onScanResult(int callbackType, ScanResult result) {
|
|
|
|
super.onScanResult(callbackType, result);
|
|
|
|
try {
|
|
|
|
ScanRecord scanRecord = result.getScanRecord();
|
|
|
|
ParcelUuid[] uuids = null;
|
|
|
|
if (scanRecord != null) {
|
|
|
|
//logMessageContent(scanRecord.getBytes());
|
|
|
|
List<ParcelUuid> serviceUuids = scanRecord.getServiceUuids();
|
|
|
|
if (serviceUuids != null) {
|
|
|
|
uuids = serviceUuids.toArray(new ParcelUuid[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LOG.warn(result.getDevice().getName() + ": " +
|
|
|
|
((scanRecord != null) ? scanRecord.getBytes().length : -1));
|
|
|
|
handleDeviceFound(result.getDevice(), (short) result.getRssi(), uuids);
|
|
|
|
} catch (NullPointerException e) {
|
|
|
|
LOG.warn("Error handling scan result", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-08-01 02:06:13 +02:00
|
|
|
|
2020-07-19 23:58:21 +02:00
|
|
|
return newBLEScanCallback;
|
2015-05-09 23:54:47 +02:00
|
|
|
}
|
2020-07-29 13:32:00 +02:00
|
|
|
|
2015-05-05 00:48:02 +02:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
2020-07-19 23:58:21 +02:00
|
|
|
Prefs prefs = GBApplication.getPrefs();
|
|
|
|
ignoreBonded = prefs.getBoolean("ignore_bonded_devices", true);
|
|
|
|
|
|
|
|
oldBleScanning = prefs.getBoolean("disable_new_ble_scanning", false);
|
|
|
|
if (oldBleScanning) {
|
|
|
|
LOG.info("New BLE scanning disabled via settings, using old method");
|
|
|
|
}
|
2020-08-01 02:06:13 +02:00
|
|
|
|
2020-07-29 13:32:00 +02:00
|
|
|
enableCompanionDevicePairing = prefs.getBoolean("enable_companiondevice_pairing", true);
|
2020-07-19 23:58:21 +02:00
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
|
|
|
|
enableCompanionDevicePairing = false; // No support below 26
|
2019-12-07 22:38:30 +01:00
|
|
|
}
|
|
|
|
|
2015-05-05 00:48:02 +02:00
|
|
|
setContentView(R.layout.activity_discovery);
|
2018-03-30 15:04:53 +02:00
|
|
|
startButton = findViewById(R.id.discovery_start);
|
2015-05-05 00:48:02 +02:00
|
|
|
startButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
onStartButtonClick(startButton);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-07-19 23:58:21 +02:00
|
|
|
bluetoothProgress = findViewById(R.id.discovery_progressbar);
|
|
|
|
bluetoothProgress.setProgress(0);
|
|
|
|
bluetoothProgress.setIndeterminate(true);
|
|
|
|
bluetoothProgress.setVisibility(View.GONE);
|
|
|
|
ListView deviceCandidatesView = findViewById(R.id.discovery_device_candidates_list);
|
2015-05-05 00:48:02 +02:00
|
|
|
|
2020-07-19 23:58:21 +02:00
|
|
|
bluetoothLEProgress = findViewById(R.id.discovery_ble_progressbar);
|
|
|
|
bluetoothLEProgress.setProgress(0);
|
|
|
|
bluetoothLEProgress.setIndeterminate(true);
|
|
|
|
bluetoothLEProgress.setVisibility(View.GONE);
|
|
|
|
|
|
|
|
deviceCandidateAdapter = new DeviceCandidateAdapter(this, deviceCandidates);
|
|
|
|
deviceCandidatesView.setAdapter(deviceCandidateAdapter);
|
2015-05-05 00:48:02 +02:00
|
|
|
deviceCandidatesView.setOnItemClickListener(this);
|
2019-04-21 21:18:08 +02:00
|
|
|
deviceCandidatesView.setOnItemLongClickListener(this);
|
2015-05-05 00:48:02 +02:00
|
|
|
|
|
|
|
IntentFilter bluetoothIntents = new IntentFilter();
|
|
|
|
bluetoothIntents.addAction(BluetoothDevice.ACTION_FOUND);
|
2016-11-27 02:41:52 +01:00
|
|
|
bluetoothIntents.addAction(BluetoothDevice.ACTION_UUID);
|
2015-05-10 21:32:41 +02:00
|
|
|
bluetoothIntents.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
|
2015-05-05 00:48:02 +02:00
|
|
|
bluetoothIntents.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
|
|
|
|
bluetoothIntents.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
|
|
|
|
bluetoothIntents.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
|
|
|
|
|
|
|
|
registerReceiver(bluetoothReceiver, bluetoothIntents);
|
|
|
|
|
2020-08-01 21:20:53 +02:00
|
|
|
checkAndRequestLocationPermission();
|
2020-07-19 23:58:21 +02:00
|
|
|
|
|
|
|
startDiscovery(Scanning.SCANNING_BT_NEXT_BLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onStartButtonClick(View button) {
|
2020-08-01 21:20:53 +02:00
|
|
|
LOG.debug("Start button clicked");
|
2020-07-19 23:58:21 +02:00
|
|
|
if (isScanning()) {
|
|
|
|
stopDiscovery();
|
2020-01-31 06:01:04 +01:00
|
|
|
} else {
|
2020-07-19 23:58:21 +02:00
|
|
|
if (GB.supportsBluetoothLE()) {
|
|
|
|
startDiscovery(Scanning.SCANNING_BT_NEXT_BLE);
|
|
|
|
} else {
|
|
|
|
startDiscovery(Scanning.SCANNING_BT);
|
|
|
|
}
|
2020-01-31 06:01:04 +01:00
|
|
|
}
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-08-03 23:40:07 +02:00
|
|
|
protected void onSaveInstanceState(@NonNull Bundle outState) {
|
2015-05-05 00:48:02 +02:00
|
|
|
super.onSaveInstanceState(outState);
|
|
|
|
outState.putParcelableArrayList("deviceCandidates", deviceCandidates);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onRestoreInstanceState(Bundle savedInstanceState) {
|
|
|
|
super.onRestoreInstanceState(savedInstanceState);
|
|
|
|
ArrayList<Parcelable> restoredCandidates = savedInstanceState.getParcelableArrayList("deviceCandidates");
|
|
|
|
if (restoredCandidates != null) {
|
|
|
|
deviceCandidates.clear();
|
|
|
|
for (Parcelable p : restoredCandidates) {
|
2015-08-03 23:09:49 +02:00
|
|
|
deviceCandidates.add((GBDeviceCandidate) p);
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onDestroy() {
|
2016-10-03 22:31:33 +02:00
|
|
|
try {
|
|
|
|
unregisterReceiver(bluetoothReceiver);
|
|
|
|
} catch (IllegalArgumentException e) {
|
2020-07-19 23:58:21 +02:00
|
|
|
LOG.warn("Tried to unregister Bluetooth Receiver that wasn't registered");
|
|
|
|
LOG.warn(e.getMessage());
|
2016-10-03 22:31:33 +02:00
|
|
|
}
|
2020-07-19 23:58:21 +02:00
|
|
|
|
2015-05-05 00:48:02 +02:00
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
2015-05-09 23:54:47 +02:00
|
|
|
private void handleDeviceFound(BluetoothDevice device, short rssi) {
|
2019-10-12 20:15:09 +02:00
|
|
|
if (device.getName() != null) {
|
2020-07-19 23:58:21 +02:00
|
|
|
if (handleDeviceFound(device, rssi, null)) {
|
2019-10-12 20:15:09 +02:00
|
|
|
LOG.info("found supported device " + device.getName() + " without scanning services, skipping service scan.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-11-27 02:41:52 +01:00
|
|
|
ParcelUuid[] uuids = device.getUuids();
|
|
|
|
if (uuids == null) {
|
|
|
|
if (device.fetchUuidsWithSdp()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleDeviceFound(device, rssi, uuids);
|
|
|
|
}
|
|
|
|
|
2019-10-12 20:15:09 +02:00
|
|
|
private boolean handleDeviceFound(BluetoothDevice device, short rssi, ParcelUuid[] uuids) {
|
2016-07-05 22:39:05 +02:00
|
|
|
LOG.debug("found device: " + device.getName() + ", " + device.getAddress());
|
2016-08-14 23:21:09 +02:00
|
|
|
if (LOG.isDebugEnabled()) {
|
|
|
|
if (uuids != null && uuids.length > 0) {
|
|
|
|
for (ParcelUuid uuid : uuids) {
|
|
|
|
LOG.debug(" supports uuid: " + uuid.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-01 02:06:13 +02:00
|
|
|
|
|
|
|
if (device.getBondState() == BluetoothDevice.BOND_BONDED && ignoreBonded) {
|
|
|
|
return true; // Ignore already bonded devices
|
2016-07-05 20:46:47 +02:00
|
|
|
}
|
|
|
|
|
2016-11-27 02:41:52 +01:00
|
|
|
GBDeviceCandidate candidate = new GBDeviceCandidate(device, rssi, uuids);
|
2016-11-27 01:09:20 +01:00
|
|
|
DeviceType deviceType = DeviceHelper.getInstance().getSupportedType(candidate);
|
|
|
|
if (deviceType.isSupported()) {
|
|
|
|
candidate.setDeviceType(deviceType);
|
2017-01-28 22:52:22 +01:00
|
|
|
LOG.info("Recognized supported device: " + candidate);
|
2015-05-09 23:54:47 +02:00
|
|
|
int index = deviceCandidates.indexOf(candidate);
|
|
|
|
if (index >= 0) {
|
|
|
|
deviceCandidates.set(index, candidate); // replace
|
|
|
|
} else {
|
|
|
|
deviceCandidates.add(candidate);
|
|
|
|
}
|
2020-07-19 23:58:21 +02:00
|
|
|
deviceCandidateAdapter.notifyDataSetChanged();
|
2019-10-12 20:15:09 +02:00
|
|
|
return true;
|
2015-05-09 23:54:47 +02:00
|
|
|
}
|
2019-10-12 20:15:09 +02:00
|
|
|
return false;
|
2015-05-09 23:54:47 +02:00
|
|
|
}
|
|
|
|
|
2020-07-19 23:58:21 +02:00
|
|
|
private void startDiscovery(Scanning what) {
|
2015-05-09 23:54:47 +02:00
|
|
|
if (isScanning()) {
|
2015-05-12 06:28:11 +02:00
|
|
|
LOG.warn("Not starting discovery, because already scanning.");
|
2015-05-06 22:06:09 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-12 06:28:11 +02:00
|
|
|
LOG.info("Starting discovery: " + what);
|
2020-07-19 23:58:21 +02:00
|
|
|
startButton.setText(getString(R.string.discovery_stop_scanning));
|
|
|
|
if (ensureBluetoothReady() && isScanning == Scanning.SCANNING_OFF) {
|
|
|
|
if (what == Scanning.SCANNING_BT || what == Scanning.SCANNING_BT_NEXT_BLE) {
|
|
|
|
startBTDiscovery(what);
|
|
|
|
} else if (what == Scanning.SCANNING_BLE && GB.supportsBluetoothLE()) {
|
2020-07-29 13:32:00 +02:00
|
|
|
if (oldBleScanning || Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
|
|
|
|
startOldBTLEDiscovery();
|
2015-05-10 00:18:42 +02:00
|
|
|
} else {
|
2020-07-29 13:32:00 +02:00
|
|
|
startBTLEDiscovery();
|
2016-09-23 05:55:34 +02:00
|
|
|
}
|
2020-07-19 23:58:21 +02:00
|
|
|
} else {
|
|
|
|
discoveryFinished();
|
|
|
|
toast(DiscoveryActivity.this, getString(R.string.discovery_enable_bluetooth), Toast.LENGTH_SHORT, GB.ERROR);
|
2015-05-10 00:18:42 +02:00
|
|
|
}
|
2015-05-05 00:48:02 +02:00
|
|
|
} else {
|
|
|
|
discoveryFinished();
|
2020-07-19 23:58:21 +02:00
|
|
|
toast(DiscoveryActivity.this, getString(R.string.discovery_enable_bluetooth), Toast.LENGTH_SHORT, GB.ERROR);
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void stopDiscovery() {
|
2015-05-12 06:28:11 +02:00
|
|
|
LOG.info("Stopping discovery");
|
2015-05-09 23:54:47 +02:00
|
|
|
if (isScanning()) {
|
2015-05-11 00:50:18 +02:00
|
|
|
Scanning wasScanning = isScanning;
|
2020-07-19 23:58:21 +02:00
|
|
|
if (wasScanning == Scanning.SCANNING_BT || wasScanning == Scanning.SCANNING_BT_NEXT_BLE) {
|
2015-05-09 23:54:47 +02:00
|
|
|
stopBTDiscovery();
|
2020-07-19 23:58:21 +02:00
|
|
|
} else if (wasScanning == Scanning.SCANNING_BLE) {
|
2020-07-29 13:32:00 +02:00
|
|
|
if (oldBleScanning || Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
|
2020-07-19 23:58:21 +02:00
|
|
|
stopOldBLEDiscovery();
|
|
|
|
} else {
|
|
|
|
stopBLEDiscovery();
|
|
|
|
}
|
2015-05-09 23:54:47 +02:00
|
|
|
}
|
2020-07-19 23:58:21 +02:00
|
|
|
|
|
|
|
discoveryFinished();
|
2015-05-06 22:06:09 +02:00
|
|
|
handler.removeMessages(0, stopRunnable);
|
2020-08-02 03:56:40 +02:00
|
|
|
} else {
|
|
|
|
discoveryFinished();
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-19 23:58:21 +02:00
|
|
|
private boolean isScanning() {
|
|
|
|
return isScanning != Scanning.SCANNING_OFF;
|
|
|
|
}
|
|
|
|
|
2020-07-29 13:32:00 +02:00
|
|
|
private void startOldBTLEDiscovery() {
|
2020-07-19 23:58:21 +02:00
|
|
|
LOG.info("Starting old BLE discovery");
|
|
|
|
|
|
|
|
handler.removeMessages(0, stopRunnable);
|
|
|
|
handler.sendMessageDelayed(getPostMessage(stopRunnable), SCAN_DURATION);
|
2020-08-02 20:55:16 +02:00
|
|
|
if(adapter.startLeScan(leScanCallback)) {
|
|
|
|
LOG.info("Old Bluetooth LE scan started successfully");
|
|
|
|
bluetoothLEProgress.setVisibility(View.VISIBLE);
|
2020-08-02 21:06:18 +02:00
|
|
|
setIsScanning(Scanning.SCANNING_BLE);
|
2020-08-02 20:55:16 +02:00
|
|
|
} else {
|
|
|
|
LOG.info("Old Bluetooth LE scan starting failed");
|
|
|
|
setIsScanning(Scanning.SCANNING_OFF);
|
|
|
|
}
|
2020-07-19 23:58:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void stopOldBLEDiscovery() {
|
|
|
|
if (adapter != null) {
|
2018-07-12 18:25:18 +02:00
|
|
|
adapter.stopLeScan(leScanCallback);
|
2020-07-19 23:58:21 +02:00
|
|
|
LOG.info("Stopped old BLE discovery");
|
|
|
|
}
|
2020-08-02 03:56:40 +02:00
|
|
|
|
2020-08-02 20:55:16 +02:00
|
|
|
setIsScanning(Scanning.SCANNING_OFF);
|
2015-05-09 23:54:47 +02:00
|
|
|
}
|
|
|
|
|
2020-07-29 13:32:00 +02:00
|
|
|
/* New BTLE Discovery uses startScan (List<ScanFilter> filters,
|
|
|
|
ScanSettings settings,
|
|
|
|
ScanCallback callback) */
|
|
|
|
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
|
|
|
|
private void startBTLEDiscovery() {
|
2020-07-19 23:58:21 +02:00
|
|
|
LOG.info("Starting BLE discovery");
|
|
|
|
|
|
|
|
handler.removeMessages(0, stopRunnable);
|
|
|
|
handler.sendMessageDelayed(getPostMessage(stopRunnable), SCAN_DURATION);
|
|
|
|
|
|
|
|
// Filters being non-null would be a very good idea with background scan, but in this case,
|
|
|
|
// not really required.
|
|
|
|
adapter.getBluetoothLeScanner().startScan(null, getScanSettings(), getScanCallback());
|
|
|
|
|
2020-08-02 20:55:16 +02:00
|
|
|
LOG.debug("Bluetooth LE discovery started successfully");
|
2020-07-19 23:58:21 +02:00
|
|
|
bluetoothLEProgress.setVisibility(View.VISIBLE);
|
2020-08-02 21:06:18 +02:00
|
|
|
setIsScanning(Scanning.SCANNING_BLE);
|
2015-05-09 23:54:47 +02:00
|
|
|
}
|
|
|
|
|
2020-07-29 13:32:00 +02:00
|
|
|
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
|
2020-07-19 23:58:21 +02:00
|
|
|
private void stopBLEDiscovery() {
|
|
|
|
if (adapter == null) {
|
2018-07-12 18:25:18 +02:00
|
|
|
return;
|
2020-07-19 23:58:21 +02:00
|
|
|
}
|
2018-07-12 18:25:18 +02:00
|
|
|
|
2018-03-30 15:04:53 +02:00
|
|
|
BluetoothLeScanner bluetoothLeScanner = adapter.getBluetoothLeScanner();
|
|
|
|
if (bluetoothLeScanner == null) {
|
2020-07-19 23:58:21 +02:00
|
|
|
LOG.warn("Could not get BluetoothLeScanner()!");
|
2018-03-30 15:04:53 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-07-19 23:58:21 +02:00
|
|
|
if (newBLEScanCallback == null) {
|
2018-07-06 15:15:14 +02:00
|
|
|
LOG.warn("newLeScanCallback == null!");
|
|
|
|
return;
|
|
|
|
}
|
2020-06-15 15:34:17 +02:00
|
|
|
try {
|
2020-07-19 23:58:21 +02:00
|
|
|
bluetoothLeScanner.stopScan(newBLEScanCallback);
|
2020-06-15 15:34:17 +02:00
|
|
|
} catch (NullPointerException e) {
|
|
|
|
LOG.warn("Internal NullPointerException when stopping the scan!");
|
|
|
|
return;
|
|
|
|
}
|
2020-07-19 23:58:21 +02:00
|
|
|
|
|
|
|
LOG.debug("Stopped BLE discovery");
|
2020-08-02 21:06:18 +02:00
|
|
|
setIsScanning(Scanning.SCANNING_OFF);
|
2016-09-23 05:55:34 +02:00
|
|
|
}
|
|
|
|
|
2020-07-19 23:58:21 +02:00
|
|
|
/**
|
|
|
|
* Starts a regular Bluetooth scan
|
|
|
|
*
|
|
|
|
* @param what The scan type, only either SCANNING_BT or SCANNING_BT_NEXT_BLE!
|
|
|
|
*/
|
|
|
|
private void startBTDiscovery(Scanning what) {
|
|
|
|
LOG.info("Starting BT discovery");
|
2020-08-02 22:59:42 +02:00
|
|
|
try {
|
|
|
|
// LineageOS quirk, can't start scan properly,
|
|
|
|
// if scan has been started by something else
|
|
|
|
stopBTDiscovery();
|
|
|
|
} catch (Exception ignored) {
|
|
|
|
}
|
2020-07-19 23:58:21 +02:00
|
|
|
handler.removeMessages(0, stopRunnable);
|
|
|
|
handler.sendMessageDelayed(getPostMessage(stopRunnable), SCAN_DURATION);
|
|
|
|
if (adapter.startDiscovery()) {
|
2020-08-02 20:55:16 +02:00
|
|
|
LOG.debug("Discovery started successfully");
|
2020-08-02 20:23:14 +02:00
|
|
|
bluetoothProgress.setVisibility(View.VISIBLE);
|
|
|
|
setIsScanning(what);
|
|
|
|
} else {
|
2020-07-19 23:58:21 +02:00
|
|
|
LOG.error("Discovery starting failed");
|
2020-08-02 20:23:14 +02:00
|
|
|
setIsScanning(Scanning.SCANNING_OFF);
|
2020-07-19 23:58:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void stopBTDiscovery() {
|
|
|
|
if (adapter != null) {
|
|
|
|
adapter.cancelDiscovery();
|
|
|
|
LOG.info("Stopped BT discovery");
|
2016-03-26 20:45:07 +01:00
|
|
|
}
|
2020-08-02 21:06:18 +02:00
|
|
|
setIsScanning(Scanning.SCANNING_OFF);
|
2015-05-09 23:54:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void discoveryFinished() {
|
2020-07-19 23:58:21 +02:00
|
|
|
if (isScanning != Scanning.SCANNING_OFF) {
|
2020-07-29 13:32:00 +02:00
|
|
|
LOG.warn("Scan was not properly stopped: " + isScanning);
|
2020-07-19 23:58:21 +02:00
|
|
|
}
|
2020-08-02 03:56:40 +02:00
|
|
|
|
|
|
|
setIsScanning(Scanning.SCANNING_OFF);
|
2015-05-09 23:54:47 +02:00
|
|
|
}
|
|
|
|
|
2020-08-02 03:56:40 +02:00
|
|
|
private void setIsScanning(Scanning to) {
|
|
|
|
this.isScanning = to;
|
|
|
|
|
|
|
|
if (isScanning == Scanning.SCANNING_OFF) {
|
|
|
|
startButton.setText(getString(R.string.discovery_start_scanning));
|
2020-08-02 21:06:18 +02:00
|
|
|
bluetoothProgress.setVisibility(View.GONE);
|
|
|
|
bluetoothLEProgress.setVisibility(View.GONE);
|
2020-08-02 03:56:40 +02:00
|
|
|
} else {
|
|
|
|
startButton.setText(getString(R.string.discovery_stop_scanning));
|
|
|
|
}
|
|
|
|
}
|
2015-05-09 23:54:47 +02:00
|
|
|
|
2020-07-19 23:58:21 +02:00
|
|
|
private void bluetoothStateChanged(int newState) {
|
|
|
|
if (newState == BluetoothAdapter.STATE_ON) {
|
|
|
|
this.adapter = BluetoothAdapter.getDefaultAdapter();
|
|
|
|
startButton.setEnabled(true);
|
|
|
|
} else {
|
|
|
|
this.adapter = null;
|
|
|
|
startButton.setEnabled(false);
|
2020-08-02 20:55:16 +02:00
|
|
|
bluetoothProgress.setVisibility(View.GONE);
|
|
|
|
bluetoothLEProgress.setVisibility(View.GONE);
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|
2020-08-02 03:56:40 +02:00
|
|
|
|
|
|
|
discoveryFinished();
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private boolean checkBluetoothAvailable() {
|
|
|
|
BluetoothManager bluetoothService = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
|
|
|
|
if (bluetoothService == null) {
|
2020-07-19 23:58:21 +02:00
|
|
|
LOG.warn("No bluetooth service available");
|
2015-05-05 00:48:02 +02:00
|
|
|
this.adapter = null;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
BluetoothAdapter adapter = bluetoothService.getAdapter();
|
2016-04-09 03:12:40 +02:00
|
|
|
if (adapter == null) {
|
2020-07-19 23:58:21 +02:00
|
|
|
LOG.warn("No bluetooth adapter available");
|
2016-04-09 03:12:40 +02:00
|
|
|
this.adapter = null;
|
|
|
|
return false;
|
|
|
|
}
|
2015-05-05 00:48:02 +02:00
|
|
|
if (!adapter.isEnabled()) {
|
2015-05-12 06:28:11 +02:00
|
|
|
LOG.warn("Bluetooth not enabled");
|
2016-03-26 20:45:07 +01:00
|
|
|
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
|
|
|
|
startActivity(enableBtIntent);
|
2015-05-05 00:48:02 +02:00
|
|
|
this.adapter = null;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
this.adapter = adapter;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-19 23:58:21 +02:00
|
|
|
private boolean ensureBluetoothReady() {
|
|
|
|
boolean available = checkBluetoothAvailable();
|
|
|
|
startButton.setEnabled(available);
|
|
|
|
if (available) {
|
|
|
|
adapter.cancelDiscovery();
|
|
|
|
// must not return the result of cancelDiscovery()
|
|
|
|
// appears to return false when currently not scanning
|
|
|
|
return true;
|
2016-11-27 01:09:20 +01:00
|
|
|
}
|
2020-07-19 23:58:21 +02:00
|
|
|
return false;
|
2016-09-23 05:55:34 +02:00
|
|
|
}
|
|
|
|
|
2020-07-29 13:32:00 +02:00
|
|
|
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
|
2016-09-23 05:55:34 +02:00
|
|
|
private ScanSettings getScanSettings() {
|
2020-07-19 23:58:21 +02:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
return new ScanSettings.Builder()
|
|
|
|
.setCallbackType(android.bluetooth.le.ScanSettings.CALLBACK_TYPE_ALL_MATCHES)
|
|
|
|
.setScanMode(android.bluetooth.le.ScanSettings.SCAN_MODE_LOW_LATENCY)
|
|
|
|
.setMatchMode(android.bluetooth.le.ScanSettings.MATCH_MODE_AGGRESSIVE)
|
|
|
|
.setPhy(android.bluetooth.le.ScanSettings.PHY_LE_ALL_SUPPORTED)
|
|
|
|
.setNumOfMatches(android.bluetooth.le.ScanSettings.MATCH_NUM_ONE_ADVERTISEMENT)
|
|
|
|
.build();
|
|
|
|
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
2016-09-23 05:55:34 +02:00
|
|
|
return new ScanSettings.Builder()
|
2020-07-19 23:58:21 +02:00
|
|
|
.setCallbackType(android.bluetooth.le.ScanSettings.CALLBACK_TYPE_ALL_MATCHES)
|
2018-03-30 15:04:53 +02:00
|
|
|
.setScanMode(android.bluetooth.le.ScanSettings.SCAN_MODE_LOW_LATENCY)
|
2020-07-19 23:58:21 +02:00
|
|
|
.setMatchMode(android.bluetooth.le.ScanSettings.MATCH_MODE_AGGRESSIVE)
|
|
|
|
.setNumOfMatches(android.bluetooth.le.ScanSettings.MATCH_NUM_ONE_ADVERTISEMENT)
|
2016-09-23 05:55:34 +02:00
|
|
|
.build();
|
|
|
|
} else {
|
|
|
|
return new ScanSettings.Builder()
|
2018-03-30 15:04:53 +02:00
|
|
|
.setScanMode(android.bluetooth.le.ScanSettings.SCAN_MODE_LOW_LATENCY)
|
2016-09-23 05:55:34 +02:00
|
|
|
.build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-19 23:58:21 +02:00
|
|
|
private List<ScanFilter> getScanFilters() {
|
|
|
|
List<ScanFilter> allFilters = new ArrayList<>();
|
|
|
|
for (DeviceCoordinator coordinator : DeviceHelper.getInstance().getAllCoordinators()) {
|
|
|
|
allFilters.addAll(coordinator.createBLEScanFilters());
|
|
|
|
}
|
|
|
|
return allFilters;
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|
|
|
|
|
2020-07-19 23:58:21 +02:00
|
|
|
private Message getPostMessage(Runnable runnable) {
|
|
|
|
Message message = Message.obtain(handler, runnable);
|
|
|
|
message.obj = runnable;
|
|
|
|
return message;
|
2015-05-09 23:54:47 +02:00
|
|
|
}
|
|
|
|
|
2016-05-26 14:58:36 +02:00
|
|
|
private void checkAndRequestLocationPermission() {
|
|
|
|
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
2020-08-01 21:20:53 +02:00
|
|
|
LOG.error("No permission to access coarse location!");
|
|
|
|
toast(DiscoveryActivity.this, getString(R.string.error_no_location_access), Toast.LENGTH_SHORT, GB.ERROR);
|
2016-05-26 14:58:36 +02:00
|
|
|
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 0);
|
|
|
|
}
|
2020-08-01 21:20:53 +02:00
|
|
|
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
LOG.error("No permission to access fine location!");
|
|
|
|
toast(DiscoveryActivity.this, getString(R.string.error_no_location_access), Toast.LENGTH_SHORT, GB.ERROR);
|
|
|
|
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 0);
|
|
|
|
}
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
|
|
|
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_BACKGROUND_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
LOG.error("No permission to access background location!");
|
|
|
|
toast(DiscoveryActivity.this, getString(R.string.error_no_location_access), Toast.LENGTH_SHORT, GB.ERROR);
|
|
|
|
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_BACKGROUND_LOCATION}, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LocationManager locationManager = (LocationManager) DiscoveryActivity.this.getSystemService(Context.LOCATION_SERVICE);
|
|
|
|
try {
|
|
|
|
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
|
|
|
|
// Do nothing
|
|
|
|
LOG.debug("Some location provider is enabled, assuming location is enabled");
|
|
|
|
} else {
|
|
|
|
toast(DiscoveryActivity.this, getString(R.string.require_location_provider), Toast.LENGTH_LONG, GB.ERROR);
|
|
|
|
DiscoveryActivity.this.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
|
|
|
|
// We can't be sure location was enabled, cancel scan start and wait for new user action
|
|
|
|
toast(DiscoveryActivity.this, getString(R.string.error_location_enabled_mandatory), Toast.LENGTH_SHORT, GB.ERROR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} catch (Exception ex) {
|
|
|
|
LOG.error("Exception when checking location status: ", ex);
|
|
|
|
}
|
2016-05-26 14:58:36 +02:00
|
|
|
}
|
|
|
|
|
2015-05-05 00:48:02 +02:00
|
|
|
@Override
|
|
|
|
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
2015-08-03 23:09:49 +02:00
|
|
|
GBDeviceCandidate deviceCandidate = deviceCandidates.get(position);
|
2015-05-05 00:48:02 +02:00
|
|
|
if (deviceCandidate == null) {
|
2015-05-12 06:28:11 +02:00
|
|
|
LOG.error("Device candidate clicked, but item not found");
|
2015-05-05 00:48:02 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-10 21:35:31 +02:00
|
|
|
stopDiscovery();
|
2015-05-05 00:48:02 +02:00
|
|
|
DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(deviceCandidate);
|
2017-01-28 22:52:22 +01:00
|
|
|
LOG.info("Using device candidate " + deviceCandidate + " with coordinator: " + coordinator.getClass());
|
2019-10-12 19:44:45 +02:00
|
|
|
|
|
|
|
if (coordinator.getBondingStyle() == DeviceCoordinator.BONDING_STYLE_REQUIRE_KEY) {
|
|
|
|
SharedPreferences sharedPrefs = GBApplication.getDeviceSpecificSharedPrefs(deviceCandidate.getMacAddress());
|
|
|
|
|
|
|
|
String authKey = sharedPrefs.getString("authkey", null);
|
2020-07-29 13:32:00 +02:00
|
|
|
if (authKey == null ||
|
|
|
|
authKey.isEmpty() ||
|
|
|
|
authKey.getBytes().length < 34 ||
|
|
|
|
!authKey.startsWith("0x")) {
|
2020-07-19 23:58:21 +02:00
|
|
|
toast(DiscoveryActivity.this, getString(R.string.discovery_need_to_enter_authkey), Toast.LENGTH_LONG, GB.WARN);
|
2019-10-12 19:44:45 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-10 16:27:31 +02:00
|
|
|
Class<? extends Activity> pairingActivity = coordinator.getPairingActivity();
|
|
|
|
if (pairingActivity != null) {
|
|
|
|
Intent intent = new Intent(this, pairingActivity);
|
2017-01-26 00:11:52 +01:00
|
|
|
intent.putExtra(DeviceCoordinator.EXTRA_DEVICE_CANDIDATE, deviceCandidate);
|
2015-05-10 16:27:31 +02:00
|
|
|
startActivity(intent);
|
2015-05-18 20:56:19 +02:00
|
|
|
} else {
|
2017-04-06 23:47:35 +02:00
|
|
|
GBDevice device = DeviceHelper.getInstance().toSupportedDevice(deviceCandidate);
|
2019-10-12 19:44:45 +02:00
|
|
|
int bondingStyle = coordinator.getBondingStyle();
|
2017-04-06 23:47:35 +02:00
|
|
|
if (bondingStyle == DeviceCoordinator.BONDING_STYLE_NONE) {
|
|
|
|
LOG.info("No bonding needed, according to coordinator, so connecting right away");
|
|
|
|
connectAndFinish(device);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-10 16:27:31 +02:00
|
|
|
try {
|
|
|
|
BluetoothDevice btDevice = adapter.getRemoteDevice(deviceCandidate.getMacAddress());
|
2017-03-04 16:03:36 +01:00
|
|
|
switch (btDevice.getBondState()) {
|
|
|
|
case BluetoothDevice.BOND_NONE: {
|
2017-04-06 23:47:35 +02:00
|
|
|
createBond(deviceCandidate, bondingStyle);
|
2017-03-04 16:03:36 +01:00
|
|
|
break;
|
|
|
|
}
|
2020-07-19 23:58:21 +02:00
|
|
|
case BluetoothDevice.BOND_BONDING: {
|
2017-03-04 16:03:36 +01:00
|
|
|
// async, wait for bonding event to finish this activity
|
2017-04-06 23:47:35 +02:00
|
|
|
bondingDevice = deviceCandidate;
|
2017-03-04 16:03:36 +01:00
|
|
|
break;
|
2020-07-19 23:58:21 +02:00
|
|
|
}
|
|
|
|
case BluetoothDevice.BOND_BONDED: {
|
2020-02-02 19:45:14 +01:00
|
|
|
bondingDevice = deviceCandidate;
|
2017-03-04 16:03:36 +01:00
|
|
|
handleDeviceBonded();
|
|
|
|
break;
|
2020-07-19 23:58:21 +02:00
|
|
|
}
|
2015-05-10 21:32:41 +02:00
|
|
|
}
|
2015-05-10 16:27:31 +02:00
|
|
|
} catch (Exception e) {
|
2015-05-12 06:28:11 +02:00
|
|
|
LOG.error("Error pairing device: " + deviceCandidate.getMacAddress());
|
2015-05-10 16:27:31 +02:00
|
|
|
}
|
|
|
|
}
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|
2018-06-18 20:26:28 +02:00
|
|
|
|
2020-07-19 23:58:21 +02:00
|
|
|
@Override
|
|
|
|
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {
|
|
|
|
GBDeviceCandidate deviceCandidate = deviceCandidates.get(position);
|
|
|
|
if (deviceCandidate == null) {
|
|
|
|
LOG.error("Device candidate clicked, but item not found");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(deviceCandidate);
|
|
|
|
GBDevice device = DeviceHelper.getInstance().toSupportedDevice(deviceCandidate);
|
|
|
|
if (coordinator.getSupportedDeviceSpecificSettings(device) == null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Intent startIntent;
|
|
|
|
startIntent = new Intent(this, DeviceSettingsActivity.class);
|
|
|
|
startIntent.putExtra(GBDevice.EXTRA_DEVICE, device);
|
|
|
|
startActivity(startIntent);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-18 20:26:28 +02:00
|
|
|
@Override
|
|
|
|
protected void onPause() {
|
|
|
|
super.onPause();
|
|
|
|
stopBTDiscovery();
|
2020-07-19 23:58:21 +02:00
|
|
|
if (oldBleScanning) {
|
|
|
|
stopOldBLEDiscovery();
|
|
|
|
} else {
|
|
|
|
if (GBApplication.isRunningLollipopOrLater()) {
|
|
|
|
stopBLEDiscovery();
|
|
|
|
}
|
2018-06-18 20:26:28 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-19 23:58:21 +02:00
|
|
|
|
|
|
|
private enum Scanning {
|
|
|
|
/**
|
|
|
|
* Regular Bluetooth scan
|
|
|
|
*/
|
|
|
|
SCANNING_BT,
|
|
|
|
/**
|
|
|
|
* Regular Bluetooth scan but when ends, start BLE scan
|
|
|
|
*/
|
|
|
|
SCANNING_BT_NEXT_BLE,
|
|
|
|
/**
|
|
|
|
* Regular BLE scan
|
|
|
|
*/
|
|
|
|
SCANNING_BLE,
|
|
|
|
/**
|
|
|
|
* Scanning has ended or hasn't been started
|
|
|
|
*/
|
|
|
|
SCANNING_OFF
|
|
|
|
}
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|