在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):NordicSemiconductor/Android-Scanner-Compat-Library开源软件地址(OpenSource Url):https://github.com/NordicSemiconductor/Android-Scanner-Compat-Library开源编程语言(OpenSource Language):Java 85.1%开源软件介绍(OpenSource Introduction):Android BLE Scanner Compat libraryThe Scanner Compat library solves the problem with scanning for Bluetooth Low Energy devices on Android.
The scanner API, initially created in Android 4.3, has changed in Android 5.0 and has been extended in 6.0 and 8.0.
This library allows to use modern API even on older phones, emulating not supported features. If a feature
(for example offloaded filtering or batching) is not available natively, it will be emulated by
the compat library. Also, native filtering, batching and reporting first match or match lost may
be disabled if you find them not working on some devices. Advertising Extension ( Background scanning
Note, that for unfiltered scans, scanning is stopped on screen off to save power. Scanning is resumed when screen is turned on again. To avoid this, use scanning with desired ScanFilter. UsageThe compat library may be found on Maven Central repository. Add it to your project by adding the following dependency: implementation 'no.nordicsemi.android.support.v18:scanner:1.6.0' Project not targeting API 31 (Android 12) or newer should use version 1.5.1. Projects not migrated to Android Jetpack should use version 1.3.1, which is feature-equal to 1.4.0. As JCenter has shut down, starting from version 1.4.4 the library is available only on Maven Central.
Make sure you have buildscript {
repositories {
mavenCentral()
}
}
allprojects {
repositories {
mavenCentral()
}
} Since version 1.5 you will need to enable desugaring of Java 8 language features if you have not already done so.(And if you are releasing an Android library, then anyone who uses that library will also have to enable desugaring.) We expect for nearly all Android projects to have already enabled desugaring. But if this causes problems for you, please use version 1.4.5. PermissionsFollowing this link:
APIThe Scanner Compat API is very similar to the original one, known from Android Oreo. Instead of getting it from the BluetoothAdapter, acquire the scanner instance using: BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner(); You also need to change the packets for ScanSettings, ScanFilter and ScanCallback classes to: no.nordicsemi.android.support.v18.scanner SampleTo start scanning use (example): BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
ScanSettings settings = new ScanSettings.Builder()
.setLegacy(false)
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
.setReportDelay(5000)
.setUseHardwareBatchingIfSupported(true)
.build();
List<ScanFilter> filters = new ArrayList<>();
filters.add(new ScanFilter.Builder().setServiceUuid(mUuid).build());
scanner.startScan(filters, settings, scanCallback); to stop scanning use: BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
scanner.stopScan(scanCallback); Scanning modesThere are 4 scanning modes available in native ScanSettings. 3 of them are available since Android Lollipop while the opportunistic scan mode has been added in Marshmallow. This library tries to emulate them on platforms where they are not supported natively.
3 first modes are emulated on Android 4.3 and 4.4.x by starting a handler task that scans for a period of time
and rests in between. To set scanning and rest intervals use Opportunistic scanning is not possible to emulate and will fallback to Scan filters and batchingOffloaded filtering is available on Lollipop or newer devices where
BluetoothAdapter#isOffloadedFilteringSupported()
returns true (when Bluetooth is enabled). If it is not supported, this library will scan without a filter and
apply the filter to the results. If you find offloaded filtering unreliable you may force using compat filtering by calling
Android Scanner Compat Library may also emulate batching. To enable scan batching call Scanning with Pending IntentAndroid 8.0 Oreo introduced Background Execution Limits
which made background running services short-lived. At the same time, to make background scanning possible, a new
method
was added to BluetoothLeScanner
which allows registering a PendingIntent
that will be sent whenever a device matching filter criteria is found. This will also work after
your application has been killed (the receiver must be added in AndroidManifest and the
Starting from version 1.3.0, this library may emulate such feature on older Android versions.
In order to do that, a background service will be started after calling
On Android Oreo or newer this library will use the native scanning mechanism. However, as it may also
emulate batching or apply filtering (when The receiver and service will be added automatically to the manifest even if they are not used by the application. No changes are required to make it work. To use this feature: Intent intent = new Intent(context, MyReceiver.class); // explicit intent
intent.setAction("com.example.ACTION_FOUND");
intent.putExtra("some.extra", value); // optional
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
ScanSettings settings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_POWER)
.setReportDelay(10000)
.build();
List<ScanFilter> filters = new ArrayList<>();
filters.add(new ScanFilter.Builder().setServiceUuid(mUuid).build());
scanner.startScan(filters, settings, context, pendingIntent, requestCode); Add your To stop scanning call: // To stop scanning use the same PendingIntent and request code as one used to start scanning.
Intent intent = new Intent(context, MyReceiver.class);
intent.setAction("com.example.ACTION_FOUND");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, requestCode, intent, PendingIntent.FLAG_CANCEL_CURRENT);
BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
scanner.stopScan(context, pendingIntent, requestCode); Note: Android versions 6 and 7 will not report any advertising packets when in Doze mode. Read more about it here: https://developer.android.com/training/monitoring-device-state/doze-standby Note 2: An additional parameter called Background scanning guidelinesTo save power it is recommended to use as low power settings as possible and and use filters.
However, the more battery friendly settings are used, the longest time to finding a device.
In general, scanning with Background scanning on Android 4.3 and 4.4.x will use a lot of power, as all those properties will have to be emulated. It is recommended to scan in background only on Lollipop or newer, or even Oreo or newer devices and giving the user an option to disable this feature. Note, that for unfiltered scans, scanning is stopped on screen off to save power. Scanning is resumed when screen is turned on again. To avoid this, use scanning with desired ScanFilter. LicenseThe Scanner Compat library is available under BSD 3-Clause license. See the LICENSE file for more info. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论