Added CompanionDeviceManager support, added target 29 support, refactored the scan display and UI.

This commit is contained in:
TaaviE 2020-07-20 00:58:21 +03:00
parent 1b43468b9e
commit b76a8267b8
8 changed files with 517 additions and 276 deletions

View File

@ -16,13 +16,13 @@ android {
sourceCompatibility JavaVersion.VERSION_1_7 sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7 targetCompatibility JavaVersion.VERSION_1_7
} }
compileSdkVersion 28 compileSdkVersion 29
buildToolsVersion '29.0.2' buildToolsVersion '29.0.3'
defaultConfig { defaultConfig {
applicationId "nodomain.freeyourgadget.gadgetbridge" applicationId "nodomain.freeyourgadget.gadgetbridge"
minSdkVersion 19 minSdkVersion 19
targetSdkVersion 28 targetSdkVersion 29
// Note: always bump BOTH versionCode and versionName! // Note: always bump BOTH versionCode and versionName!
versionName "0.46.0" versionName "0.46.0"

View File

@ -1,12 +1,21 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="nodomain.freeyourgadget.gadgetbridge"> package="nodomain.freeyourgadget.gadgetbridge">
<!-- <!--
Comment in for testing Pebble Emulator Comment in for testing Pebble Emulator
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
--> -->
<!-- Used for Bluetooth access -->
<uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- Since Android 10 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.READ_CALL_LOG" /> <uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" /> <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
@ -16,19 +25,29 @@
<uses-permission android:name="android.permission.RECEIVE_SMS" /> <uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" /> <uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_CALENDAR" /> <uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission
<uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL" /> android:name="android.permission.MEDIA_CONTENT_CONTROL"
tools:ignore="ProtectedPermissions" />
<!-- Used for background service -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.VIBRATE" /> <!-- Used for reverse find device --> <uses-permission android:name="android.permission.REQUEST_COMPANION_RUN_IN_BACKGROUND" />
<!-- Used for reverse find device -->
<uses-permission android:name="android.permission.VIBRATE" />
<!-- Used for weather provider access -->
<uses-permission android:name="cyanogenmod.permission.ACCESS_WEATHER_MANAGER" /> <uses-permission android:name="cyanogenmod.permission.ACCESS_WEATHER_MANAGER" />
<uses-permission android:name="cyanogenmod.permission.READ_WEATHER" /> <uses-permission android:name="cyanogenmod.permission.READ_WEATHER" />
<uses-permission android:name="lineageos.permission.ACCESS_WEATHER_MANAGER" /> <uses-permission android:name="lineageos.permission.ACCESS_WEATHER_MANAGER" />
<uses-permission android:name="lineageos.permission.READ_WEATHER" /> <uses-permission android:name="lineageos.permission.READ_WEATHER" />
<uses-permission android:name="org.omnirom.omnijaws.READ_WEATHER" /> <uses-permission android:name="org.omnirom.omnijaws.READ_WEATHER" />
<uses-feature <uses-feature
android:name="android.hardware.bluetooth" android:name="android.hardware.bluetooth"
android:required="true" /> android:required="true" />
@ -38,6 +57,9 @@
<uses-feature <uses-feature
android:name="android.hardware.telephony" android:name="android.hardware.telephony"
android:required="false" /> android:required="false" />
<uses-feature
android:name="android.software.companion_device_setup"
android:required="false" />
<application <application
android:name=".GBApplication" android:name=".GBApplication"
@ -47,7 +69,7 @@
android:label="@string/app_name" android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
android:theme="@style/GadgetbridgeTheme"> android:theme="@style/GadgetbridgeTheme">
<activity android:name=".devices.qhybrid.WidgetSettingsActivity"></activity> <activity android:name=".devices.qhybrid.WidgetSettingsActivity" />
<activity <activity
android:name=".activities.ControlCenterv2" android:name=".activities.ControlCenterv2"
android:label="@string/title_activity_controlcenter" android:label="@string/title_activity_controlcenter"
@ -293,6 +315,7 @@
<data android:mimeType="application/octet-stream" /> <data android:mimeType="application/octet-stream" />
</intent-filter> </intent-filter>
<!-- to receive firmwares from the download content provider if recognized as zip --> <!-- to receive firmwares from the download content provider if recognized as zip -->
<intent-filter> <intent-filter>
<action android:name="android.intent.action.VIEW" /> <action android:name="android.intent.action.VIEW" />
@ -302,6 +325,7 @@
<data android:mimeType="application/zip" /> <data android:mimeType="application/zip" />
<data android:mimeType="application/x-zip-compressed" /> <data android:mimeType="application/x-zip-compressed" />
</intent-filter> </intent-filter>
<!-- to receive files from the "share" intent --> <!-- to receive files from the "share" intent -->
<intent-filter> <intent-filter>
<action android:name="android.intent.action.SEND" /> <action android:name="android.intent.action.SEND" />
@ -346,13 +370,21 @@
<category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.DEFAULT" />
</intent-filter> </intent-filter>
</receiver> </receiver>
<receiver <receiver
android:name=".externalevents.BluetoothStateChangeReceiver" android:name=".externalevents.BluetoothStateChangeReceiver"
android:exported="false"> android:exported="false"
android:permission="android.permission.BLUETOOTH,android.permission.BLUETOOTH_ADMIN">
<intent-filter> <intent-filter>
<action android:name="android.bluetooth.adapter.action.STATE_CHANGED" /> <action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />
<action android:name="android.bluetooth.adapter.action.CONNECTION_STATE_CHANGED" />
<action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
<action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
<action android:name="android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED" />
</intent-filter> </intent-filter>
</receiver> </receiver>
<receiver <receiver
android:name=".service.receivers.GBMusicControlReceiver" android:name=".service.receivers.GBMusicControlReceiver"
android:exported="false"> android:exported="false">
@ -370,7 +402,7 @@
<receiver <receiver
android:name=".database.PeriodicExporter" android:name=".database.PeriodicExporter"
android:enabled="true" android:enabled="true"
android:exported="false"></receiver> android:exported="false" />
<!-- <!--
forcing the DebugActivity to portrait mode avoids crashes with the progress forcing the DebugActivity to portrait mode avoids crashes with the progress
dialog when changing orientation dialog when changing orientation
@ -415,10 +447,10 @@
android:label="@string/title_activity_watch9_calibration" /> android:label="@string/title_activity_watch9_calibration" />
<activity <activity
android:name=".devices.lenovo.LenovoWatchPairingActivity" android:name=".devices.lenovo.LenovoWatchPairingActivity"
android:label="@string/title_activity_watch9_pairing" /> android:label="@string/title_activity_watch9_pairing" />
<activity <activity
android:name=".devices.lenovo.LenovoWatchCalibrationActivity" android:name=".devices.lenovo.LenovoWatchCalibrationActivity"
android:label="@string/title_activity_LenovoWatch_calibration" /> android:label="@string/title_activity_LenovoWatch_calibration" />
<activity <activity
android:name=".activities.charts.ChartsActivity" android:name=".activities.charts.ChartsActivity"
android:label="@string/title_activity_charts" android:label="@string/title_activity_charts"
@ -448,10 +480,13 @@
android:name=".activities.FindPhoneActivity" android:name=".activities.FindPhoneActivity"
android:label="Find Phone" /> android:label="Find Phone" />
<!--
<provider <provider
android:name=".contentprovider.PebbleContentProvider" android:name=".contentprovider.PebbleContentProvider"
android:authorities="com.getpebble.android.provider" android:authorities="com.getpebble.android.provider"
android:exported="true" /> android:exported="true" />
-->
<provider <provider
android:name="androidx.core.content.FileProvider" android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.screenshot_provider" android:authorities="${applicationId}.screenshot_provider"
@ -512,6 +547,7 @@
<data android:scheme="gadgetbridge" /> <data android:scheme="gadgetbridge" />
</intent-filter> </intent-filter>
</activity> </activity>
<activity <activity
android:name=".devices.qhybrid.ConfigActivity" android:name=".devices.qhybrid.ConfigActivity"
android:exported="true" /> android:exported="true" />
@ -525,5 +561,4 @@
android:name=".devices.qhybrid.ImageEditActivity" android:name=".devices.qhybrid.ImageEditActivity"
android:exported="true" /> android:exported="true" />
</application> </application>
</manifest> </manifest>

View File

@ -31,19 +31,25 @@ import android.bluetooth.le.ScanFilter;
import android.bluetooth.le.ScanRecord; import android.bluetooth.le.ScanRecord;
import android.bluetooth.le.ScanResult; import android.bluetooth.le.ScanResult;
import android.bluetooth.le.ScanSettings; import android.bluetooth.le.ScanSettings;
import android.companion.AssociationRequest;
import android.companion.BluetoothDeviceFilter;
import android.companion.CompanionDeviceManager;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.IntentSender;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.Message; import android.os.Message;
import android.os.ParcelUuid; import android.os.ParcelUuid;
import android.os.Parcelable; import android.os.Parcelable;
import android.provider.Settings;
import android.view.View; import android.view.View;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.Button; import android.widget.Button;
@ -52,6 +58,7 @@ import android.widget.ProgressBar;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.core.app.ActivityCompat; import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
@ -73,26 +80,51 @@ import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
import nodomain.freeyourgadget.gadgetbridge.util.AndroidUtils; import nodomain.freeyourgadget.gadgetbridge.util.AndroidUtils;
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper; import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.GB;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
import static nodomain.freeyourgadget.gadgetbridge.util.GB.toast;
public class DiscoveryActivity extends AbstractGBActivity implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener { public class DiscoveryActivity extends AbstractGBActivity implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {
private static final Logger LOG = LoggerFactory.getLogger(DiscoveryActivity.class); private static final Logger LOG = LoggerFactory.getLogger(DiscoveryActivity.class);
private static final long SCAN_DURATION = 60000; // 60s private static final long SCAN_DURATION = 30000; // 30s
private static final int REQUEST_CODE = 1;
private ScanCallback newBLEScanCallback = null;
private ScanCallback newLeScanCallback = null; /** Use old BLE scanning **/
private boolean oldBleScanning = false;
// Disabled for testing, it seems worse for a few people /** If already bonded devices are to be ignored when scanning */
private boolean disableNewBLEScanning = false; private boolean ignoreBonded = true;
/** If new CompanionDevice-type pairing is enabled on newer Androids **/
private boolean enableCompanionDevicePairing = false;
private final Handler handler = new Handler(); private final Handler handler = new Handler();
private ProgressBar bluetoothProgress;
private ProgressBar bluetoothLEProgress;
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();
}
}
};
private DeviceCandidateAdapter deviceCandidateAdapter;
private final BroadcastReceiver bluetoothReceiver = new BroadcastReceiver() { private final BroadcastReceiver bluetoothReceiver = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
switch (Objects.requireNonNull(intent.getAction())) { switch (Objects.requireNonNull(intent.getAction())) {
case BluetoothAdapter.ACTION_DISCOVERY_STARTED: case BluetoothAdapter.ACTION_DISCOVERY_STARTED:
if (isScanning != Scanning.SCANNING_BTLE && isScanning != Scanning.SCANNING_NEW_BTLE) { if (isScanning != Scanning.SCANNING_BLE) {
discoveryStarted(Scanning.SCANNING_BT); if (isScanning != Scanning.SCANNING_BT_NEXT_BLE) {
isScanning = Scanning.SCANNING_BT;
}
startButton.setText(getString(R.string.discovery_stop_scanning));
} }
break; break;
case BluetoothAdapter.ACTION_DISCOVERY_FINISHED: case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
@ -100,13 +132,10 @@ public class DiscoveryActivity extends AbstractGBActivity implements AdapterView
@Override @Override
public void run() { public void run() {
// continue with LE scan, if available // continue with LE scan, if available
if (isScanning == Scanning.SCANNING_BT) { if (isScanning == Scanning.SCANNING_BT || isScanning == Scanning.SCANNING_BT_NEXT_BLE) {
checkAndRequestLocationPermission(); checkAndRequestLocationPermission();
if (GBApplication.isRunningLollipopOrLater() && !disableNewBLEScanning) { stopDiscovery();
startDiscovery(Scanning.SCANNING_NEW_BTLE); startDiscovery(Scanning.SCANNING_BLE);
} else {
startDiscovery(Scanning.SCANNING_BTLE);
}
} else { } else {
discoveryFinished(); discoveryFinished();
} }
@ -145,16 +174,16 @@ public class DiscoveryActivity extends AbstractGBActivity implements AdapterView
}; };
private void connectAndFinish(GBDevice device) { private void connectAndFinish(GBDevice device) {
GB.toast(DiscoveryActivity.this, getString(R.string.discovery_trying_to_connect_to, device.getName()), Toast.LENGTH_SHORT, GB.INFO); toast(DiscoveryActivity.this, getString(R.string.discovery_trying_to_connect_to, device.getName()), Toast.LENGTH_SHORT, GB.INFO);
GBApplication.deviceService().connect(device, true); GBApplication.deviceService().connect(device, true);
finish(); finish();
} }
private void createBond(final GBDeviceCandidate deviceCandidate, int bondingStyle) { private void createBond(final GBDeviceCandidate deviceCandidate, int bondingStyle) {
if (bondingStyle == DeviceCoordinator.BONDING_STYLE_NONE) { if (bondingStyle == DeviceCoordinator.BONDING_STYLE_NONE) {
// Do nothing
return; return;
} } else if (bondingStyle == DeviceCoordinator.BONDING_STYLE_ASK) {
if (bondingStyle == DeviceCoordinator.BONDING_STYLE_ASK) {
new AlertDialog.Builder(this) new AlertDialog.Builder(this)
.setCancelable(true) .setCancelable(true)
.setTitle(DiscoveryActivity.this.getString(R.string.discovery_pair_title, deviceCandidate.getName())) .setTitle(DiscoveryActivity.this.getString(R.string.discovery_pair_title, deviceCandidate.getName()))
@ -176,25 +205,20 @@ public class DiscoveryActivity extends AbstractGBActivity implements AdapterView
} else { } else {
doCreatePair(deviceCandidate); doCreatePair(deviceCandidate);
} }
LOG.debug("Bonding initiated");
} }
private void doCreatePair(GBDeviceCandidate deviceCandidate) { private void doCreatePair(GBDeviceCandidate deviceCandidate) {
GB.toast(DiscoveryActivity.this, getString(R.string.discovery_attempting_to_pair, deviceCandidate.getName()), Toast.LENGTH_SHORT, GB.INFO); toast(DiscoveryActivity.this, getString(R.string.discovery_attempting_to_pair, deviceCandidate.getName()), Toast.LENGTH_SHORT, GB.INFO);
if (deviceCandidate.getDevice().createBond()) { if (enableCompanionDevicePairing) {
// async, wait for bonding event to finish this activity if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
LOG.info("Bonding in progress..."); companionDevicePair(deviceCandidate);
bondingDevice = deviceCandidate; }
} else { } else {
GB.toast(DiscoveryActivity.this, getString(R.string.discovery_bonding_failed_immediately, deviceCandidate.getName()), Toast.LENGTH_SHORT, GB.ERROR); deviceBond(deviceCandidate);
} }
} }
private void handleDeviceBonded() {
GB.toast(DiscoveryActivity.this, getString(R.string.discovery_successfully_bonded, bondingDevice.getName()), Toast.LENGTH_SHORT, GB.INFO);
GBDevice device = DeviceHelper.getInstance().toSupportedDevice(bondingDevice);
connectAndFinish(device);
}
private final BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() { private final BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override @Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) { public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
@ -203,38 +227,14 @@ public class DiscoveryActivity extends AbstractGBActivity implements AdapterView
} }
}; };
private void deviceBond(GBDeviceCandidate deviceCandidate) {
// why use a method to get callback? if (deviceCandidate.getDevice().createBond()) {
// because this callback need API >= 21 // Async, wait for bonding event to finish this activity
// we cant add @TARGETAPI("Lollipop") at class header LOG.info("Bonding in progress...");
// so use a method with SDK check to return this callback bondingDevice = deviceCandidate;
private ScanCallback getScanCallback() { } else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { toast(DiscoveryActivity.this, getString(R.string.discovery_bonding_failed_immediately, deviceCandidate.getName()), Toast.LENGTH_SHORT, GB.ERROR);
newLeScanCallback = new ScanCallback() {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@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);
}
}
};
} }
return newLeScanCallback;
} }
public void logMessageContent(byte[] value) { public void logMessageContent(byte[] value) {
@ -243,40 +243,121 @@ public class DiscoveryActivity extends AbstractGBActivity implements AdapterView
} }
} }
private final Runnable stopRunnable = new Runnable() { @RequiresApi(Build.VERSION_CODES.O)
@Override private void companionDevicePair(final GBDeviceCandidate deviceCandidate) {
public void run() { CompanionDeviceManager deviceManager = getSystemService(CompanionDeviceManager.class);
stopDiscovery();
} 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();
}
}
}
private void handleDeviceBonded() {
if (bondingDevice == null) {
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);
}
private ProgressBar progressView;
private BluetoothAdapter adapter; private BluetoothAdapter adapter;
private final ArrayList<GBDeviceCandidate> deviceCandidates = new ArrayList<>(); private final ArrayList<GBDeviceCandidate> deviceCandidates = new ArrayList<>();
private DeviceCandidateAdapter cadidateListAdapter;
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
private ScanCallback getScanCallback() {
if (newBLEScanCallback != null) {
return newBLEScanCallback;
}
newBLEScanCallback = new ScanCallback() {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@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);
}
}
};
return newBLEScanCallback;
}
private Button startButton; private Button startButton;
private Scanning isScanning = Scanning.SCANNING_OFF; private Scanning isScanning = Scanning.SCANNING_OFF;
private GBDeviceCandidate bondingDevice; private GBDeviceCandidate bondingDevice;
private boolean ignoreBonded = true;
private enum Scanning {
SCANNING_BT,
SCANNING_BTLE,
SCANNING_NEW_BTLE,
SCANNING_OFF
}
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
ignoreBonded = GBApplication.getPrefs().getBoolean("ignore_bonded_devices", true); Prefs prefs = GBApplication.getPrefs();
ignoreBonded = prefs.getBoolean("ignore_bonded_devices", true);
disableNewBLEScanning = GBApplication.getPrefs().getBoolean("disable_new_ble_scanning", false); oldBleScanning = prefs.getBoolean("disable_new_ble_scanning", false);
if (disableNewBLEScanning) { if (oldBleScanning) {
LOG.info("new BLE scanning disabled via settings, using old method"); LOG.info("New BLE scanning disabled via settings, using old method");
}
enableCompanionDevicePairing = prefs.getBoolean("enable_companiondevice_pairing", false);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
enableCompanionDevicePairing = false; // No support below 26
} }
setContentView(R.layout.activity_discovery); setContentView(R.layout.activity_discovery);
@ -288,14 +369,19 @@ public class DiscoveryActivity extends AbstractGBActivity implements AdapterView
} }
}); });
progressView = findViewById(R.id.discovery_progressbar); bluetoothProgress = findViewById(R.id.discovery_progressbar);
progressView.setProgress(0); bluetoothProgress.setProgress(0);
progressView.setIndeterminate(true); bluetoothProgress.setIndeterminate(true);
progressView.setVisibility(View.GONE); bluetoothProgress.setVisibility(View.GONE);
ListView deviceCandidatesView = findViewById(R.id.discovery_deviceCandidatesView); ListView deviceCandidatesView = findViewById(R.id.discovery_device_candidates_list);
cadidateListAdapter = new DeviceCandidateAdapter(this, deviceCandidates); bluetoothLEProgress = findViewById(R.id.discovery_ble_progressbar);
deviceCandidatesView.setAdapter(cadidateListAdapter); bluetoothLEProgress.setProgress(0);
bluetoothLEProgress.setIndeterminate(true);
bluetoothLEProgress.setVisibility(View.GONE);
deviceCandidateAdapter = new DeviceCandidateAdapter(this, deviceCandidates);
deviceCandidatesView.setAdapter(deviceCandidateAdapter);
deviceCandidatesView.setOnItemClickListener(this); deviceCandidatesView.setOnItemClickListener(this);
deviceCandidatesView.setOnItemLongClickListener(this); deviceCandidatesView.setOnItemLongClickListener(this);
@ -310,11 +396,42 @@ public class DiscoveryActivity extends AbstractGBActivity implements AdapterView
registerReceiver(bluetoothReceiver, bluetoothIntents); registerReceiver(bluetoothReceiver, bluetoothIntents);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
GB.toast(DiscoveryActivity.this, getString(R.string.error_no_location_access), Toast.LENGTH_SHORT, GB.ERROR); toast(DiscoveryActivity.this, getString(R.string.error_no_location_access), Toast.LENGTH_SHORT, GB.ERROR);
LOG.error("No permission to access coarse location!"); LOG.error("No permission to access coarse location!");
checkAndRequestLocationPermission(); checkAndRequestLocationPermission();
// We can't be sure location was granted, cancel scan start and wait for user action
return;
}
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
return;
}
} catch (Exception ex) {
LOG.error("Exception when checking location status: ", ex);
}
startDiscovery(Scanning.SCANNING_BT_NEXT_BLE);
}
public void onStartButtonClick(View button) {
LOG.debug("Start Button clicked");
if (isScanning()) {
stopDiscovery();
} else { } else {
startDiscovery(); if (GB.supportsBluetoothLE()) {
startDiscovery(Scanning.SCANNING_BT_NEXT_BLE);
} else {
startDiscovery(Scanning.SCANNING_BT);
}
} }
} }
@ -336,28 +453,21 @@ public class DiscoveryActivity extends AbstractGBActivity implements AdapterView
} }
} }
public void onStartButtonClick(View button) {
LOG.debug("Start Button clicked");
if (isScanning()) {
stopDiscovery();
} else {
startDiscovery();
}
}
@Override @Override
protected void onDestroy() { protected void onDestroy() {
try { try {
unregisterReceiver(bluetoothReceiver); unregisterReceiver(bluetoothReceiver);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
LOG.warn("Tried to unregister Bluetooth Receiver that wasn't registered."); LOG.warn("Tried to unregister Bluetooth Receiver that wasn't registered");
LOG.warn(e.getMessage());
} }
super.onDestroy(); super.onDestroy();
} }
private void handleDeviceFound(BluetoothDevice device, short rssi) { private void handleDeviceFound(BluetoothDevice device, short rssi) {
if (device.getName() != null) { if (device.getName() != null) {
if (handleDeviceFound(device,rssi, null)) { if (handleDeviceFound(device, rssi, null)) {
LOG.info("found supported device " + device.getName() + " without scanning services, skipping service scan."); LOG.info("found supported device " + device.getName() + " without scanning services, skipping service scan.");
return; return;
} }
@ -372,7 +482,6 @@ public class DiscoveryActivity extends AbstractGBActivity implements AdapterView
handleDeviceFound(device, rssi, uuids); handleDeviceFound(device, rssi, uuids);
} }
private boolean handleDeviceFound(BluetoothDevice device, short rssi, ParcelUuid[] uuids) { private boolean handleDeviceFound(BluetoothDevice device, short rssi, ParcelUuid[] uuids) {
LOG.debug("found device: " + device.getName() + ", " + device.getAddress()); LOG.debug("found device: " + device.getName() + ", " + device.getAddress());
if (LOG.isDebugEnabled()) { if (LOG.isDebugEnabled()) {
@ -398,46 +507,55 @@ public class DiscoveryActivity extends AbstractGBActivity implements AdapterView
} else { } else {
deviceCandidates.add(candidate); deviceCandidates.add(candidate);
} }
cadidateListAdapter.notifyDataSetChanged(); deviceCandidateAdapter.notifyDataSetChanged();
return true; return true;
} }
return false; return false;
} }
/** private void startDiscovery(Scanning what) {
* Pre: bluetooth is available, enabled and scanning is off.
* Post: BT is discovering
*/
private void startDiscovery() {
if (isScanning()) { if (isScanning()) {
LOG.warn("Not starting discovery, because already scanning."); LOG.warn("Not starting discovery, because already scanning.");
return; return;
} }
startDiscovery(Scanning.SCANNING_BT);
}
private void startDiscovery(Scanning what) {
LOG.info("Starting discovery: " + what); LOG.info("Starting discovery: " + what);
discoveryStarted(what); // just to make sure startButton.setText(getString(R.string.discovery_stop_scanning));
if (ensureBluetoothReady()) { if (ensureBluetoothReady() && isScanning == Scanning.SCANNING_OFF) {
if (what == Scanning.SCANNING_BT) { if (what == Scanning.SCANNING_BT || what == Scanning.SCANNING_BT_NEXT_BLE) {
startBTDiscovery(); startBTDiscovery(what);
} else if (what == Scanning.SCANNING_BTLE) { } else if (what == Scanning.SCANNING_BLE && GB.supportsBluetoothLE()) {
if (GB.supportsBluetoothLE()) { if (oldBleScanning) {
startBTLEDiscovery(); startBTLEDiscovery();
} else { } else {
discoveryFinished();
}
} else if (what == Scanning.SCANNING_NEW_BTLE) {
if (GB.supportsBluetoothLE()) {
startNEWBTLEDiscovery(); startNEWBTLEDiscovery();
} else {
discoveryFinished();
} }
} else {
discoveryFinished();
toast(DiscoveryActivity.this, getString(R.string.discovery_enable_bluetooth), Toast.LENGTH_SHORT, GB.ERROR);
} }
} else { } else {
discoveryFinished(); discoveryFinished();
GB.toast(DiscoveryActivity.this, getString(R.string.discovery_enable_bluetooth), Toast.LENGTH_SHORT, GB.ERROR); toast(DiscoveryActivity.this, getString(R.string.discovery_enable_bluetooth), Toast.LENGTH_SHORT, GB.ERROR);
}
}
private void stopDiscovery() {
LOG.info("Stopping discovery");
if (isScanning()) {
Scanning wasScanning = isScanning;
if (wasScanning == Scanning.SCANNING_BT || wasScanning == Scanning.SCANNING_BT_NEXT_BLE) {
stopBTDiscovery();
} else if (wasScanning == Scanning.SCANNING_BLE) {
if (oldBleScanning) {
stopOldBLEDiscovery();
} else {
stopBLEDiscovery();
}
}
discoveryFinished();
handler.removeMessages(0, stopRunnable);
} }
} }
@ -445,57 +563,110 @@ public class DiscoveryActivity extends AbstractGBActivity implements AdapterView
return isScanning != Scanning.SCANNING_OFF; return isScanning != Scanning.SCANNING_OFF;
} }
private void stopDiscovery() { private void startBTLEDiscovery() {
LOG.info("Stopping discovery"); LOG.info("Starting old BLE discovery");
if (isScanning()) { isScanning = Scanning.SCANNING_BLE;
Scanning wasScanning = isScanning;
// unfortunately, we don't always get a call back when stopping the scan, so
// we do it manually; BEFORE stopping the scan!
discoveryFinished();
if (wasScanning == Scanning.SCANNING_BT) { handler.removeMessages(0, stopRunnable);
stopBTDiscovery(); handler.sendMessageDelayed(getPostMessage(stopRunnable), SCAN_DURATION);
} else if (wasScanning == Scanning.SCANNING_BTLE) { adapter.startLeScan(leScanCallback);
stopBTLEDiscovery();
} else if (wasScanning == Scanning.SCANNING_NEW_BTLE) { bluetoothLEProgress.setVisibility(View.VISIBLE);
stopNewBTLEDiscovery(); }
}
handler.removeMessages(0, stopRunnable); private void stopOldBLEDiscovery() {
if (adapter != null) {
adapter.stopLeScan(leScanCallback);
isScanning = Scanning.SCANNING_OFF;
bluetoothLEProgress.setVisibility(View.GONE);
LOG.info("Stopped old BLE discovery");
} }
} }
private void stopBTLEDiscovery() { // New BTLE Discovery use startScan (List<ScanFilter> filters,
if (adapter != null) // ScanSettings settings,
adapter.stopLeScan(leScanCallback); // ScanCallback callback)
} // It's added on API21
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void startNEWBTLEDiscovery() {
// Only use new API when user uses Lollipop+ device
LOG.info("Starting BLE discovery");
isScanning = Scanning.SCANNING_BLE;
private void stopBTDiscovery() { handler.removeMessages(0, stopRunnable);
if (adapter != null) handler.sendMessageDelayed(getPostMessage(stopRunnable), SCAN_DURATION);
adapter.cancelDiscovery();
// 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());
bluetoothLEProgress.setVisibility(View.VISIBLE);
} }
@TargetApi(Build.VERSION_CODES.LOLLIPOP) @TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void stopNewBTLEDiscovery() { private void stopBLEDiscovery() {
if (adapter == null) if (adapter == null) {
return; return;
}
BluetoothLeScanner bluetoothLeScanner = adapter.getBluetoothLeScanner(); BluetoothLeScanner bluetoothLeScanner = adapter.getBluetoothLeScanner();
if (bluetoothLeScanner == null) { if (bluetoothLeScanner == null) {
LOG.warn("could not get BluetoothLeScanner()!"); LOG.warn("Could not get BluetoothLeScanner()!");
return; return;
} }
if (newLeScanCallback == null) { if (newBLEScanCallback == null) {
LOG.warn("newLeScanCallback == null!"); LOG.warn("newLeScanCallback == null!");
return; return;
} }
try { try {
bluetoothLeScanner.stopScan(newLeScanCallback); bluetoothLeScanner.stopScan(newBLEScanCallback);
} catch (NullPointerException e) { } catch (NullPointerException e) {
LOG.warn("Internal NullPointerException when stopping the scan!"); LOG.warn("Internal NullPointerException when stopping the scan!");
return; return;
} }
isScanning = Scanning.SCANNING_OFF;
bluetoothLEProgress.setVisibility(View.GONE);
LOG.debug("Stopped BLE discovery");
} }
/**
* 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");
isScanning = what;
handler.removeMessages(0, stopRunnable);
handler.sendMessageDelayed(getPostMessage(stopRunnable), SCAN_DURATION);
if (adapter.startDiscovery()) {
LOG.error("Discovery starting failed");
}
bluetoothProgress.setVisibility(View.VISIBLE);
}
private void stopBTDiscovery() {
if (adapter != null) {
adapter.cancelDiscovery();
bluetoothProgress.setVisibility(View.GONE);
isScanning = Scanning.SCANNING_OFF;
LOG.info("Stopped BT discovery");
}
}
private void discoveryFinished() {
if (isScanning != Scanning.SCANNING_OFF) {
LOG.warn("Scan was not properly: " + String.valueOf(isScanning));
}
startButton.setText(getString(R.string.discovery_start_scanning));
}
private void bluetoothStateChanged(int newState) { private void bluetoothStateChanged(int newState) {
discoveryFinished(); discoveryFinished();
if (newState == BluetoothAdapter.STATE_ON) { if (newState == BluetoothAdapter.STATE_ON) {
@ -507,40 +678,16 @@ public class DiscoveryActivity extends AbstractGBActivity implements AdapterView
} }
} }
private void discoveryFinished() {
isScanning = Scanning.SCANNING_OFF;
progressView.setVisibility(View.GONE);
startButton.setText(getString(R.string.discovery_start_scanning));
}
private void discoveryStarted(Scanning what) {
isScanning = what;
progressView.setVisibility(View.VISIBLE);
startButton.setText(getString(R.string.discovery_stop_scanning));
}
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;
}
return false;
}
private boolean checkBluetoothAvailable() { private boolean checkBluetoothAvailable() {
BluetoothManager bluetoothService = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE); BluetoothManager bluetoothService = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
if (bluetoothService == null) { if (bluetoothService == null) {
LOG.warn("No bluetooth available"); LOG.warn("No bluetooth service available");
this.adapter = null; this.adapter = null;
return false; return false;
} }
BluetoothAdapter adapter = bluetoothService.getAdapter(); BluetoothAdapter adapter = bluetoothService.getAdapter();
if (adapter == null) { if (adapter == null) {
LOG.warn("No bluetooth available"); LOG.warn("No bluetooth adapter available");
this.adapter = null; this.adapter = null;
return false; return false;
} }
@ -555,17 +702,40 @@ public class DiscoveryActivity extends AbstractGBActivity implements AdapterView
return true; return true;
} }
// New BTLE Discovery use startScan (List<ScanFilter> filters, private boolean ensureBluetoothReady() {
// ScanSettings settings, boolean available = checkBluetoothAvailable();
// ScanCallback callback) startButton.setEnabled(available);
// It's added on API21 if (available) {
adapter.cancelDiscovery();
// must not return the result of cancelDiscovery()
// appears to return false when currently not scanning
return true;
}
return false;
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP) @TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void startNEWBTLEDiscovery() { private ScanSettings getScanSettings() {
// Only use new API when user uses Lollipop+ device if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
LOG.info("Start New BTLE Discovery"); return new ScanSettings.Builder()
handler.removeMessages(0, stopRunnable); .setCallbackType(android.bluetooth.le.ScanSettings.CALLBACK_TYPE_ALL_MATCHES)
handler.sendMessageDelayed(getPostMessage(stopRunnable), SCAN_DURATION); .setScanMode(android.bluetooth.le.ScanSettings.SCAN_MODE_LOW_LATENCY)
adapter.getBluetoothLeScanner().startScan(getScanFilters(), getScanSettings(), getScanCallback()); .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) {
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)
.setNumOfMatches(android.bluetooth.le.ScanSettings.MATCH_NUM_ONE_ADVERTISEMENT)
.build();
} else {
return new ScanSettings.Builder()
.setScanMode(android.bluetooth.le.ScanSettings.SCAN_MODE_LOW_LATENCY)
.build();
}
} }
private List<ScanFilter> getScanFilters() { private List<ScanFilter> getScanFilters() {
@ -576,32 +746,10 @@ public class DiscoveryActivity extends AbstractGBActivity implements AdapterView
return allFilters; return allFilters;
} }
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private Message getPostMessage(Runnable runnable) {
private ScanSettings getScanSettings() { Message message = Message.obtain(handler, runnable);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { message.obj = runnable;
return new ScanSettings.Builder() return message;
.setScanMode(android.bluetooth.le.ScanSettings.SCAN_MODE_LOW_LATENCY)
.setMatchMode(android.bluetooth.le.ScanSettings.MATCH_MODE_STICKY)
.build();
} else {
return new ScanSettings.Builder()
.setScanMode(android.bluetooth.le.ScanSettings.SCAN_MODE_LOW_LATENCY)
.build();
}
}
private void startBTLEDiscovery() {
LOG.info("Starting BTLE Discovery");
handler.removeMessages(0, stopRunnable);
handler.sendMessageDelayed(getPostMessage(stopRunnable), SCAN_DURATION);
adapter.startLeScan(leScanCallback);
}
private void startBTDiscovery() {
LOG.info("Starting BT Discovery");
handler.removeMessages(0, stopRunnable);
handler.sendMessageDelayed(getPostMessage(stopRunnable), SCAN_DURATION);
adapter.startDiscovery();
} }
private void checkAndRequestLocationPermission() { private void checkAndRequestLocationPermission() {
@ -610,33 +758,6 @@ public class DiscoveryActivity extends AbstractGBActivity implements AdapterView
} }
} }
private Message getPostMessage(Runnable runnable) {
Message m = Message.obtain(handler, runnable);
m.obj = runnable;
return m;
}
@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;
}
@Override @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
GBDeviceCandidate deviceCandidate = deviceCandidates.get(position); GBDeviceCandidate deviceCandidate = deviceCandidates.get(position);
@ -654,7 +775,7 @@ public class DiscoveryActivity extends AbstractGBActivity implements AdapterView
String authKey = sharedPrefs.getString("authkey", null); String authKey = sharedPrefs.getString("authkey", null);
if (authKey == null || authKey.isEmpty() || authKey.getBytes().length < 34 || !authKey.substring(0, 2).equals("0x")) { if (authKey == null || authKey.isEmpty() || authKey.getBytes().length < 34 || !authKey.substring(0, 2).equals("0x")) {
GB.toast(DiscoveryActivity.this, getString(R.string.discovery_need_to_enter_authkey), Toast.LENGTH_LONG, GB.WARN); toast(DiscoveryActivity.this, getString(R.string.discovery_need_to_enter_authkey), Toast.LENGTH_LONG, GB.WARN);
return; return;
} }
} }
@ -680,14 +801,16 @@ public class DiscoveryActivity extends AbstractGBActivity implements AdapterView
createBond(deviceCandidate, bondingStyle); createBond(deviceCandidate, bondingStyle);
break; break;
} }
case BluetoothDevice.BOND_BONDING: case BluetoothDevice.BOND_BONDING: {
// async, wait for bonding event to finish this activity // async, wait for bonding event to finish this activity
bondingDevice = deviceCandidate; bondingDevice = deviceCandidate;
break; break;
case BluetoothDevice.BOND_BONDED: }
case BluetoothDevice.BOND_BONDED: {
bondingDevice = deviceCandidate; bondingDevice = deviceCandidate;
handleDeviceBonded(); handleDeviceBonded();
break; break;
}
} }
} catch (Exception e) { } catch (Exception e) {
LOG.error("Error pairing device: " + deviceCandidate.getMacAddress()); LOG.error("Error pairing device: " + deviceCandidate.getMacAddress());
@ -695,13 +818,56 @@ public class DiscoveryActivity extends AbstractGBActivity implements AdapterView
} }
} }
@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;
}
@Override @Override
protected void onPause() { protected void onPause() {
super.onPause(); super.onPause();
stopBTDiscovery(); stopBTDiscovery();
stopBTLEDiscovery(); if (oldBleScanning) {
if (GBApplication.isRunningLollipopOrLater() && !disableNewBLEScanning) { stopOldBLEDiscovery();
stopNewBTLEDiscovery(); } else {
if (GBApplication.isRunningLollipopOrLater()) {
stopBLEDiscovery();
}
} }
} }
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
}
} }

View File

@ -48,7 +48,7 @@ public class BluetoothStateChangeReceiver extends BroadcastReceiver {
return; return;
} }
LOG.info("Bluetooth turned on => connecting..."); LOG.info("Bluetooth turned on (ACTION_STATE_CHANGED) => connecting...");
GBApplication.deviceService().connect(); GBApplication.deviceService().connect();
} else if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_OFF) { } else if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_OFF) {
LOG.info("Bluetooth turned off => disconnecting..."); LOG.info("Bluetooth turned off => disconnecting...");

View File

@ -1,4 +1,4 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
@ -15,25 +15,58 @@
android:weightSum="1"> android:weightSum="1">
<Button <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/discovery_start_scanning"
android:id="@+id/discovery_start" android:id="@+id/discovery_start"
android:layout_gravity="center_horizontal" />
<ProgressBar
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:id="@+id/discovery_progressbar" android:layout_gravity="center_horizontal"
android:indeterminate="true" android:text="@string/discovery_start_scanning" />
android:indeterminateOnly="true"
android:visibility="gone" <LinearLayout
android:layout_gravity="center_horizontal" /> android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bluetooth scan:" />
<ProgressBar
android:id="@+id/discovery_progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:indeterminate="true"
android:indeterminateOnly="true"
android:visibility="gone" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bluetooth LE scan:" />
<ProgressBar
android:id="@+id/discovery_ble_progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:indeterminate="true"
android:indeterminateOnly="true"
android:visibility="gone" />
</LinearLayout>
<ListView <ListView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="0dp" android:layout_height="0dp"
android:id="@+id/discovery_deviceCandidatesView" android:id="@+id/discovery_device_candidates_list"
android:layout_gravity="bottom|top" android:layout_gravity="bottom|top"
android:layout_weight="0.83" /> android:layout_weight="0.83" />
@ -54,9 +87,7 @@
android:text="@string/discovery_connected_devices_hint" android:text="@string/discovery_connected_devices_hint"
android:id="@+id/discovery_hint" android:id="@+id/discovery_hint"
android:textColor="@color/secondarytext" android:textColor="@color/secondarytext"
android:gravity="bottom"
android:textIsSelectable="true" /> android:textIsSelectable="true" />
</LinearLayout> </LinearLayout>
</LinearLayout>
</RelativeLayout>

View File

@ -886,6 +886,9 @@
<string name="about_additional_contributions">Many thanks to all unlisted contributors for contributing code, support, ideas, motivation, bug reports, money… ✊</string> <string name="about_additional_contributions">Many thanks to all unlisted contributors for contributing code, support, ideas, motivation, bug reports, money… ✊</string>
<string name="permission_granting_mandatory">All these permissions are required and instability might occur if not granted</string> <string name="permission_granting_mandatory">All these permissions are required and instability might occur if not granted</string>
<string name="error_version_check_extreme_caution">CAUTION: Error when checking version information! You should not continue! Saw version name \"%s\"</string> <string name="error_version_check_extreme_caution">CAUTION: Error when checking version information! You should not continue! Saw version name \"%s\"</string>
<string name="require_location_provider">Location must be enabled</string>
<string name="companiondevice_pairing">CompanionDevice Pairing</string>
<string name="companiondevice_pairing_details">Enables the new CompanionDevice API support (above Android 8 only!) which theoretically increases reliability, requires re-pairing to have an effect</string>
<string name="error_background_service">Failed to start background service</string> <string name="error_background_service">Failed to start background service</string>
<string name="error_background_service_reason_truncated">Starting the background service failed because…</string> <string name="error_background_service_reason_truncated">Starting the background service failed because…</string>
<string name="device_is_currently_bonded">ALREADY BONDED</string> <string name="device_is_currently_bonded">ALREADY BONDED</string>
@ -898,6 +901,7 @@
<string name="error_retrieving_devices_database">Error retrieving devices from database</string> <string name="error_retrieving_devices_database">Error retrieving devices from database</string>
<string name="ignore_bonded_devices">Ignore bonded devices</string> <string name="ignore_bonded_devices">Ignore bonded devices</string>
<string name="ignore_bonded_devices_description">Enabling this option will ignore devices that have been bonded/paired already when scanning</string> <string name="ignore_bonded_devices_description">Enabling this option will ignore devices that have been bonded/paired already when scanning</string>
<plurals name="widget_alarm_target_hours"> <plurals name="widget_alarm_target_hours">
<item quantity="one">%d hour</item> <item quantity="one">%d hour</item>
<item quantity="two">%d hours</item> <item quantity="two">%d hours</item>

View File

@ -650,6 +650,12 @@
android:key="disable_new_ble_scanning" android:key="disable_new_ble_scanning"
android:summary="@string/pref_summary_disable_new_ble_scanning" android:summary="@string/pref_summary_disable_new_ble_scanning"
android:title="@string/pref_disable_new_ble_scanning" /> android:title="@string/pref_disable_new_ble_scanning" />
<CheckBoxPreference
android:defaultValue="false"
android:key="enable_companiondevice_pairing"
android:layout="@layout/preference_checkbox"
android:summary="@string/companiondevice_pairing_details"
android:title="@string/companiondevice_pairing" />
</PreferenceCategory> </PreferenceCategory>
</PreferenceScreen> </PreferenceScreen>

View File

@ -10,7 +10,6 @@ buildscript {
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:4.0.0' classpath 'com.android.tools.build:gradle:4.0.0'
classpath "gradle.plugin.com.github.spotbugs:spotbugs-gradle-plugin:2.0.0"
classpath 'gradle.plugin.com.github.spotbugs:spotbugs-gradle-plugin:2.0.0' classpath 'gradle.plugin.com.github.spotbugs:spotbugs-gradle-plugin:2.0.0'
// NOTE: Do not place your application dependencies here; they belong // NOTE: Do not place your application dependencies here; they belong