diff options
author | Alex Blasche <alexander.blasche@qt.io> | 2019-04-15 16:05:01 +0200 |
---|---|---|
committer | Alex Blasche <alexander.blasche@qt.io> | 2019-05-03 06:55:37 +0000 |
commit | 23ab3c692c9be5e086f1c0f0ce615480a41eccb6 (patch) | |
tree | eed4c0dd86fb8b7d6d32e7047a342fa8b3fe6a89 /src/android/bluetooth | |
parent | c69da57a28de0d2b69bb58eceee8cfb61e7f720c (diff) |
Remove use of deprecated btle device scan API on Android
The new API requires Android SDK v21 or higher and Qt 5.13 just raised
the minimum API version to 21. The PendingIntent version of the same API
cannot be used yet as it requires v24+.
Fixes: QTBUG-67482
Change-Id: Ided02e36796ef66f0244934ef67262f1e6f69b8c
Reviewed-by: Oliver Wolff <oliver.wolff@qt.io>
Reviewed-by: Andrew Dolby <andrewdolby@gmail.com>
Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
Diffstat (limited to 'src/android/bluetooth')
-rw-r--r-- | src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothLE.java | 54 |
1 files changed, 40 insertions, 14 deletions
diff --git a/src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothLE.java b/src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothLE.java index 6c548f84..9f10c32a 100644 --- a/src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothLE.java +++ b/src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothLE.java @@ -1,6 +1,6 @@ /**************************************************************************** ** - ** Copyright (C) 2016 The Qt Company Ltd. + ** Copyright (C) 2019 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtBluetooth module of the Qt Toolkit. @@ -47,6 +47,11 @@ import android.bluetooth.BluetoothGattCharacteristic; import android.bluetooth.BluetoothGattDescriptor; import android.bluetooth.BluetoothGattService; import android.bluetooth.BluetoothProfile; +import android.bluetooth.le.BluetoothLeScanner; +import android.bluetooth.le.ScanCallback; +import android.bluetooth.le.ScanFilter; +import android.bluetooth.le.ScanResult; +import android.bluetooth.le.ScanSettings; import android.content.Context; import android.os.Build; import android.os.Handler; @@ -90,6 +95,9 @@ public class QtBluetoothLE { private final int RUNNABLE_TIMEOUT = 3000; // 3 seconds private final Handler timeoutHandler = new Handler(Looper.getMainLooper()); + /* New BTLE scanner setup since Android SDK v21 */ + private BluetoothLeScanner mBluetoothLeScanner = null; + private class TimeoutRunnable implements Runnable { public TimeoutRunnable(int handle) { pendingJobHandle = handle; } @Override @@ -123,6 +131,7 @@ public class QtBluetoothLE { @SuppressWarnings("WeakerAccess") public QtBluetoothLE() { mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); + mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner(); } public QtBluetoothLE(final String remoteAddress, Context context) { @@ -131,7 +140,6 @@ public class QtBluetoothLE { mRemoteGattAddress = remoteAddress; } - /*************************************************************/ /* Device scan */ /*************************************************************/ @@ -144,27 +152,45 @@ public class QtBluetoothLE { return true; if (isEnabled) { - mLeScanRunning = mBluetoothAdapter.startLeScan(leScanCallback); + Log.d(TAG, "New BTLE scanning API"); + ScanSettings.Builder settingsBuilder = new ScanSettings.Builder(); + settingsBuilder = settingsBuilder.setScanMode(ScanSettings.SCAN_MODE_BALANCED); + ScanSettings settings = settingsBuilder.build(); + + List<ScanFilter> filterList = new ArrayList<ScanFilter>(2); + + mBluetoothLeScanner.startScan(filterList, settings, leScanCallback21); + mLeScanRunning = true; } else { - mBluetoothAdapter.stopLeScan(leScanCallback); + mBluetoothLeScanner.stopScan(leScanCallback21); mLeScanRunning = false; } return (mLeScanRunning == isEnabled); } - // Device scan callback - private final BluetoothAdapter.LeScanCallback leScanCallback = - new BluetoothAdapter.LeScanCallback() { + // Device scan callback (SDK v21+) + private final ScanCallback leScanCallback21 = new ScanCallback() { + @Override + public void onScanResult(int callbackType, ScanResult result) { + super.onScanResult(callbackType, result); + leScanResult(qtObject, result.getDevice(), result.getRssi(), result.getScanRecord().getBytes()); + } - @Override - public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) { - if (qtObject == 0) - return; + @Override + public void onBatchScanResults(List<ScanResult> results) { + super.onBatchScanResults(results); + for (ScanResult result : results) + leScanResult(qtObject, result.getDevice(), result.getRssi(), result.getScanRecord().getBytes()); - leScanResult(qtObject, device, rssi, scanRecord); - } - }; + } + + @Override + public void onScanFailed(int errorCode) { + super.onScanFailed(errorCode); + Log.d(TAG, "BTLE device scan failed with " + errorCode); + } + }; public native void leScanResult(long qtObject, BluetoothDevice device, int rssi, byte[] scanRecord); |