diff --git a/.gitignore b/.gitignore index 9c9035b0ff2709ba6291bbd67c5cb34b2822afa6..603b14077394cd2294ac6922fe619669630ef3ab 100644 --- a/.gitignore +++ b/.gitignore @@ -1,29 +1,14 @@ -# Built application files -*.apk -*.ap_ - -# Files for the Dalvik VM -*.dex - -# Java class files -*.class - -# Generated files -bin/ -gen/ - -# Gradle files -.gradle/ -build/ -/*/build/ - -# Local configuration file (sdk path, etc) -local.properties -/.idea *.iml - -# Proguard folder generated by Eclipse -proguard/ - -# Log Files -*.log +.gradle +/local.properties +/.idea/caches +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +/.idea/navEditor.xml +/.idea/assetWizardSettings.xml +.DS_Store +/build +/captures +.externalNativeBuild +.cxx diff --git a/.idea/$PROJECT_FILE$ b/.idea/$PROJECT_FILE$ new file mode 100644 index 0000000000000000000000000000000000000000..58b7e3e570f8289344d345f6e3634608e74c690d --- /dev/null +++ b/.idea/$PROJECT_FILE$ @@ -0,0 +1,11 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..26d33521af10bcc7fd8cea344038eaaeb78d0ef5 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/checkstyle-idea.xml b/.idea/checkstyle-idea.xml new file mode 100644 index 0000000000000000000000000000000000000000..04546e080a2c37b6f2fcf1d259c8ef4222b41d82 --- /dev/null +++ b/.idea/checkstyle-idea.xml @@ -0,0 +1,18 @@ + + + + + + \ No newline at end of file diff --git a/.idea/checkstyleidea-libs/readme.txt b/.idea/checkstyleidea-libs/readme.txt new file mode 100644 index 0000000000000000000000000000000000000000..3bb2010fe0dc9fe399aa172ffa6552cf028f47b8 --- /dev/null +++ b/.idea/checkstyleidea-libs/readme.txt @@ -0,0 +1,6 @@ +This folder contains libraries copied from the "ohos-Scanner-Compat-Library-master" project. +It is managed by the CheckStyle-IDEA IDE plugin. +Do not modify this folder while the IDE is running. +When the IDE is stopped, you may delete this folder at any time. It will be recreated as needed. +In order to prevent the CheckStyle-IDEA IDE plugin from creating this folder, +uncheck the "Copy libraries from project directory" option in the CheckStyle-IDEA settings dialog. diff --git a/.idea/code-check/java/codemars.log b/.idea/code-check/java/codemars.log new file mode 100644 index 0000000000000000000000000000000000000000..03b4dec34c39a35cebdbf6795ef79375f38d1ce1 --- /dev/null +++ b/.idea/code-check/java/codemars.log @@ -0,0 +1,30 @@ +2021-05-11 19:29:31.986 [main] INFO . - user input: D:\soft\deveco\DevEco Studio 2.1.0.301\tools\openjdk\bin\java,-j,-source,@D:/ohos/trunk/SOW-24/ohos-Scanner-Compat-Library-master/.idea/code-check/java/detect.txt,-output,D:/ohos/trunk/SOW-24/ohos-Scanner-Compat-Library-master/.idea/code-check/java/output.xml +2021-05-11 19:29:31.989 [main] INFO . - CodeMars Version:2.1.2.sp4 +2021-05-11 19:29:32.033 [main] INFO . - starting analyzing. +2021-05-11 19:29:32.040 [main] INFO . - start collecting report. +2021-05-11 19:29:32.044 [CodeMars1] INFO . - Command: "D:\soft\deveco\DevEco Studio 2.1.0.301\plugins\codecheck\lib\CodeMars\engines\SecFinder-J\bin\run_SecFinder-J.bat",-filelist,D:\ohos\trunk\SOW-24\ohos-Scanner-Compat-Library-master\.idea\code-check\java\filelist_2021_05_11_19_29_32_040_5.txt,-f,xml,-default,-progress,-r,D:\ohos\trunk\SOW-24\ohos-Scanner-Compat-Library-master\.idea\code-check\java\\errorreport_2021_05_11_19_29_31_910_33.xml,-ruleclasspath,file:///D:\ohos\trunk\SOW-24\ohos-Scanner-Compat-Library-master\.idea\code-check\java\ruleclasspath.txt +2021-05-11 19:29:32.164 [Thread-2] INFO . - 五月 11, 2021 7:29:32 下午 com.huawei.secfinderj.SecFinderJ needScan +2021-05-11 19:29:32.164 [Thread-2] INFO . - 信息: SecFinder-J Version: 2.1.3 +2021-05-11 19:29:32.233 [Thread-1] INFO . - 2021-05-11 19:29:32.197: SecFinder-J Output: Inspect start... +2021-05-11 19:29:32.285 [Thread-1] INFO . - 2021-05-11 19:29:32.284: SecFinder-J Output: Load checkers... +2021-05-11 19:29:32.409 [Thread-1] INFO . - 2021-05-11 19:29:32.409: SecFinder-J Output: Load config... +2021-05-11 19:29:32.436 [Thread-1] INFO . - 2021-05-11 19:29:32.436: SecFinder-J Output: step 1/4: Find files +2021-05-11 19:29:32.459 [Thread-1] INFO . - 2021-05-11 19:29:32.459: SecFinder-J Output: step 2/4: Process files +2021-05-11 19:29:32.498 [Thread-1] INFO . - 2021-05-11 19:29:32.498: SecFinder-J Output: step 3/4: Run analysis... +2021-05-11 19:29:32.499 [Thread-1] INFO . - 2021-05-11 19:29:32.499: SecFinder-J Output: [SecFinder-J--Thread--1] - during processing of [BluetoothScannerImplV5.java] +2021-05-11 19:29:32.954 [Thread-1] INFO . - 2021-05-11 19:29:32.954: SecFinder-J Output: step 4/4: Result output... +2021-05-11 19:29:32.955 [Thread-1] INFO . - 2021-05-11 19:29:32.954: SecFinder-J Output: Inspect finish... +2021-05-11 19:29:32.955 [Thread-1] INFO . - Analysis result: +2021-05-11 19:29:32.955 [Thread-1] INFO . - files analyzed : 1 +2021-05-11 19:29:32.955 [Thread-1] INFO . - lines analyzed : 266 +2021-05-11 19:29:32.955 [Thread-1] INFO . - rules used : 59 +2021-05-11 19:29:32.955 [Thread-1] INFO . - issues detected : 0 +2021-05-11 19:29:32.955 [Thread-1] INFO . - time cost(sec) : 0 +2021-05-11 19:29:32.955 [Thread-1] INFO . - +2021-05-11 19:29:32.955 [Thread-2] INFO . - 五月 11, 2021 7:29:32 下午 com.huawei.secfinderj.override.HwPmd end +2021-05-11 19:29:32.955 [Thread-2] INFO . - 信息: SecFinder-J run successed! +2021-05-11 19:29:33.025 [CodeMars1] INFO . - start parse errorreport xml +2021-05-11 19:29:33.026 [CodeMars1] INFO . - parse xml time : 2 +2021-05-11 19:29:33.026 [CodeMars1] INFO . - end parse errorreport xml +2021-05-11 19:29:33.027 [main] INFO . - end collecting report. +2021-05-11 19:29:33.027 [main] INFO . - end analyzing. diff --git a/.idea/code-check/java/detect.txt b/.idea/code-check/java/detect.txt new file mode 100644 index 0000000000000000000000000000000000000000..0229ee19e9051be81cde403bf67a629839a33fdb --- /dev/null +++ b/.idea/code-check/java/detect.txt @@ -0,0 +1 @@ +D:/ohos/trunk/SOW-24/ohos-Scanner-Compat-Library-master/scanner/src/main/java/no/nordicsemi/android/support/v18/scanner/BluetoothScannerImplV5.java diff --git a/.idea/code-check/java/output.xml b/.idea/code-check/java/output.xml new file mode 100644 index 0000000000000000000000000000000000000000..8cf7ac8cd0e3e8f262172cde6cc1c04a3a429fc3 --- /dev/null +++ b/.idea/code-check/java/output.xml @@ -0,0 +1,9 @@ + + + + +1 +266 +0 +0 + diff --git a/.idea/code-check/java/ruleclasspath.txt b/.idea/code-check/java/ruleclasspath.txt new file mode 100644 index 0000000000000000000000000000000000000000..02ab4392ec8cef8562dbd257ef87b98f2bad6e6d --- /dev/null +++ b/.idea/code-check/java/ruleclasspath.txt @@ -0,0 +1 @@ +D:\soft\deveco\DevEco Studio 2.1.0.301\plugins\codecheck\lib\CodeMars\engines\SecFinder-J\rule\ diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000000000000000000000000000000000000..61a9130cd9669c3843e6445dfe1fee2d493869bc --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000000000000000000000000000000000000..d964c316c7d424bdbdd31ef2398357d13742d2e6 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,23 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000000000000000000000000000000000000..ba2e7443424bc5abb3b2c16dd9751cfceb69faed --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/markdown-navigator-enh.xml b/.idea/markdown-navigator-enh.xml new file mode 100644 index 0000000000000000000000000000000000000000..a8fcc84db3668cadd75348be61bda65a8fc5ea21 --- /dev/null +++ b/.idea/markdown-navigator-enh.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/markdown-navigator.xml b/.idea/markdown-navigator.xml new file mode 100644 index 0000000000000000000000000000000000000000..2aba66280be8440b9ca797afa0ac90fd06503ced --- /dev/null +++ b/.idea/markdown-navigator.xml @@ -0,0 +1,64 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000000000000000000000000000000000000..53f05c51c5d431697f23ce9abedc264ed3a5fa65 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,26 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/previewer/phone/phoneSettingConfig_1341911773.json b/.idea/previewer/phone/phoneSettingConfig_1341911773.json new file mode 100644 index 0000000000000000000000000000000000000000..f505e6b9ca3d41faf1f0a39a496db58b117fbd60 --- /dev/null +++ b/.idea/previewer/phone/phoneSettingConfig_1341911773.json @@ -0,0 +1,25 @@ +{ + "setting": { + "1.0.1": { + "Language": { + "args": { + "Language": "zh-CN" + } + } + } + }, + "frontend": { + "1.0.0": { + "Resolution": { + "args": { + "Resolution": "360*780" + } + }, + "DeviceType": { + "args": { + "DeviceType": "phone" + } + } + } + } +} \ No newline at end of file diff --git a/.idea/previewer/previewConfig.json b/.idea/previewer/previewConfig.json new file mode 100644 index 0000000000000000000000000000000000000000..29f1efdaec0a48216f012617082546b7918e7ce2 --- /dev/null +++ b/.idea/previewer/previewConfig.json @@ -0,0 +1,9 @@ +{ + "1.0.0": { + "LastPreviewDevice": { + "D:\\ohos\\trunk\\SOW-24\\ohos-Scanner-Compat-Library-master\\entry": [ + "phone" + ] + } + } +} \ No newline at end of file diff --git a/.idea/qaplug_profiles.xml b/.idea/qaplug_profiles.xml new file mode 100644 index 0000000000000000000000000000000000000000..6c583dfa8f3e7ef50535af521d43726b0f0303c3 --- /dev/null +++ b/.idea/qaplug_profiles.xml @@ -0,0 +1,183 @@ + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000000000000000000000000000000000000..35eb1ddfbbc029bcab630581847471d7f238ec53 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ChangeLog.md b/ChangeLog.md new file mode 100644 index 0000000000000000000000000000000000000000..e2e988d658b10742d8e5e6d86a69f86b38c392e4 --- /dev/null +++ b/ChangeLog.md @@ -0,0 +1,33 @@ +## 0.0.1-SNAPSHOT +* 鍙戝竷beta鐗堟湰 + +Version 1.4.3 +Filtering based on service data + +Version 1.4.2 +Crashing when disabling scanning after Bluetooth has been turned OFF. + +Version 1.4.1 +#58 - memory leak when using PendingIntent fixed. +Migrated to Android Studio 3.4.2, Gradle wrapper 5.1.1. +Improved publication script. + +Version 1.4.0 +Feature equal to 1.3.1, but migrated to Android Jetpack (androidX). + +Version 1.3.1 +#45 - removing a task from Recents is killing the background service. Not any more. Watch out for Doze mode! + +Version 1.3.0 +#33 - Scanning with PendingIntent supported (and emulated on older platforms using a background service). +#44 - SCAN_MODE_OPPORTUNISTIC falls back to SCAN_MODE_LOW_POWER on Lollipop and to power save settings on older devices. +#39 - fixed infinite scanning with #40. + +Version 1.2.0 +#32, #34, #36 - Fixed race conditions, supported worker thread, simplified code + +Version 1.1.0 +Support for Android Oreo added + +Version 1.0.0 +JUnit tests added \ No newline at end of file diff --git a/README.md b/README.md index ef3839a6e65472d346ee99e20ef53abf541f1691..3ad04bd24bef4a0ccdf3efc90a3f61386cb35be2 100644 --- a/README.md +++ b/README.md @@ -1,61 +1,51 @@ -# Android BLE Scanner Compat library +# Scanner-Compat-Library -[ ![Download](https://api.bintray.com/packages/nordic/android/no.nordicsemi.android.support.v18%3Ascanner/images/download.svg) ](https://bintray.com/nordic/android/no.nordicsemi.android.support.v18%3Ascanner/_latestVersion) +**鏈」鐩熀浜庡紑婧愰」鐩產ndroid-Scanner-Compat-Library 杩涜楦胯挋鍖栫殑绉绘鍜屽紑鍙戯紝鍙互閫氳繃椤圭洰鏍囩浠ュ強github鍦板潃锛锛夎拷韪埌鍘熷畨鍗撻」鐩増鏈** -The 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 (`ScanSetting#setLegacy(boolean)` -or `setPhy(int)`) is available only on Android Oreo or newer and such calls will be ignored on -older platforms where only legacy advertising packets on PHY LE 1M will be reported, -due to the Bluetooth chipset capabilities. +#### 椤圭洰浠嬬粛 +- 椤圭洰鍚嶇О锛氳摑鐗欏簱 +- 鎵灞炵郴鍒楋細楦胯挋鐨勭涓夋柟缁勪欢閫傞厤绉绘 +- 鍔熻兘锛氬叧浜庤摑鐗欑殑鎿嶄綔 +- 椤圭洰绉绘鐘舵侊細涓诲姛鑳藉畬鎴 +- 璋冪敤宸紓锛氭棤 +- 寮鍙戠増鏈細sdk5锛孌evEco Studio2.1 beta4 +- 椤圭洰浣滆呭拰缁存姢浜猴細璧垫棴涓 +- 鑱旂郴鏂瑰紡锛歾haoxudong073@chinasoftinc.com +- 鍘熼」鐩瓺oc鍦板潃锛 +- 鍩虹嚎鐗堟湰锛歍ag v1.4.3 -### Background scanning -`SCAN_MODE_LOW_POWER` or `SCAN_MODE_OPPORTUNISTIC` should be used when scanning in background. -Note, that newer Android versions will enforce using low power mode in background, even if another one has been set. -This library allows to emulate [scanning with PendingIntent](https://developer.android.com/reference/android/bluetooth/le/BluetoothLeScanner.html#startScan(java.util.List%3Candroid.bluetooth.le.ScanFilter%3E,%20android.bluetooth.le.ScanSettings,%20android.app.PendingIntent)) -on pre-Oreo devices by starting a background service that will scan with requested scan mode. -This is much less battery friendly than when the original method is used, but works and saves -a lot of development time if such feature should be implemented anyway. Please read below -for more details. +#### 鏁堟灉婕旂ず +![鏁堟灉婕旂ず] -## Usage - -The compat library may be found on jcenter repository. Add it to your project by adding the -following dependency: - -```Groovy -implementation 'no.nordicsemi.android.support.v18:scanner:1.4.2' +#### 瀹夎鏁欑▼ +1.鍦ㄩ」鐩牴鐩綍涓嬬殑build.gradle鏂囦欢涓坊鍔 ``` - -Projects not migrated to Android Jetpack should use version 1.3.1, which is feature-equal to 1.4.0. - -## API - -The 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: - -```java -BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner(); +allprojects { + repositories { + maven { + url 'https://s01.oss.sonatype.org/content/repositories/snapshots/' + } + } +} ``` - -You also need to change the packets for **ScanSettings**, **ScanFilter** and **ScanCallback** -classes to: - -```java -no.nordicsemi.android.support.v18.scanner +2.鍦╡ntry妯″潡涓嬬殑build.gradle鏂囦欢涓坊鍔犱緷璧栥 +``` +dependencies { + implementation('com.gitee.chinasoft_ohos:Scanner-Compat-Library:0.0.1-SNAPSHOT') + ...... +} ``` +鍦╯dk4锛孌evEco Studio2.1 beta2涓嬮」鐩彲鐩存帴杩愯 +濡傛棤娉曡繍琛岋紝鍒犻櫎椤圭洰.gradle,.idea,build,gradle,build.gradle鏂囦欢锛 +骞朵緷鎹嚜宸辩殑鐗堟湰鍒涘缓鏂伴」鐩紝灏嗘柊椤圭洰鐨勫搴旀枃浠跺鍒跺埌鏍圭洰褰曚笅 -## Sample +#### 浣跨敤璇存槑 -To start scanning use (example): +浣跨敤璇ュ簱闈炲父绠鍗曪紝鍙渶鏌ョ湅鎻愪緵鐨勭ず渚嬬殑婧愪唬鐮併 ```java - BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner(); + BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner(); ScanSettings settings = new ScanSettings.Builder() .setLegacy(false) .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) @@ -67,133 +57,24 @@ To start scanning use (example): scanner.startScan(filters, settings, scanCallback); ``` -to stop scanning use: -```java - BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner(); - scanner.stopScan(scanCallback); -``` +#### 娴嬭瘯淇℃伅 -### Scanning modes - -There are 4 scanning modes available in native [ScanSettings](https://developer.android.com/reference/android/bluetooth/le/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. -1. [SCAN_MODE_LOW_POWER](https://developer.android.com/reference/android/bluetooth/le/ScanSettings#SCAN_MODE_LOW_POWER) - -Perform Bluetooth LE scan in low power mode. This is the default scan mode as it consumes the least power. -The scanner will scan for 0.5 second and rest for 4.5 seconds. A Bluetooth LE device should advertise -very often (at least once per 100 ms) in order to be found with this mode, otherwise the scanning interval may miss some or even all -advertising events. This mode may be enforced if the scanning application is not in foreground. -2. [SCAN_MODE_BALANCED](https://developer.android.com/reference/android/bluetooth/le/ScanSettings#SCAN_MODE_BALANCED) - -Perform Bluetooth LE scan in balanced power mode. Scan results are returned at a rate that provides a -good trade-off between scan frequency and power consumption. The scanner will scan for 2 seconds followed -by 3 seconds of idle. -3. [SCAN_MODE_LOW_LATENCY](https://developer.android.com/reference/android/bluetooth/le/ScanSettings#SCAN_MODE_LOW_LATENCY) - -Scan using highest duty cycle. It's recommended to only use this mode when the application is running in the foreground. -4. [SCAN_MODE_OPPORTUNISTIC](https://developer.android.com/reference/android/bluetooth/le/ScanSettings#SCAN_MODE_OPPORTUNISTIC) - -A special Bluetooth LE scan mode. Applications using this scan mode will passively listen for other scan results -without starting BLE scans themselves. - -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 `Builder#setPowerSave(long,long)`. - -Opportunistic scanning is not possible to emulate and will fallback to `SCAN_MODE_LOW_POWER` on Lollipop and -power save settings on pre-Lollipop devices. That means that this library actually will initiate scanning -on its own. This may have impact on battery consumption and should be used with care. - -### Scan filters and batching - -Offloaded filtering is available on Lollipop or newer devices where -[BluetoothAdapter#isOffloadedFilteringSupported()](https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#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 -`Builder#useHardwareFilteringIfSupported(false)`. Keep in mind that, newer Android versions may prohibit -background scanning without native filters to save battery, so this method should be used with care. - -Android Scanner Compat Library may also emulate batching. To enable scan batching call `Builder#setScanDelay(interval)` -with an interval greater than 0. For intervals less 5 seconds the actual interval may vary. -If you want to get results in lower intervals, call `Builder#useHardwareBatchingIfSupported(false)`, which will -start a normal scan and report results in given interval. Emulated batching uses significantly more battery -than offloaded as it wakes CPU with every device found. - -### Scanning with Pending Intent - -Android 8.0 Oreo introduced [Background Execution Limits](https://developer.android.com/about/versions/oreo/background) -which made background running services short-lived. At the same time, to make background scanning possible, a new -[method](https://developer.android.com/reference/android/bluetooth/le/BluetoothLeScanner.html#startScan(java.util.List%3Candroid.bluetooth.le.ScanFilter%3E,%20android.bluetooth.le.ScanSettings,%20android.app.PendingIntent)) -was added to [BluetoothLeScanner](https://developer.android.com/reference/android/bluetooth/le/BluetoothLeScanner.html) -which allows registering a [PendingIntent](https://developer.android.com/reference/android/app/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 -`PendingIntent` must be created with an explicit Intent). - -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 -`scanner.startScan(filters, settings, context, pendingIntent)`, which will be scanning in -background with given settings and will send the given `PendingIntent` when a device -matching filter is found. To lower battery consumption it is recommended to set -`ScanSettings.SCAN_MODE_LOW_POWER` scanning mode and use filter, but even with those conditions fulfilled -**the battery consumption will be significantly higher than on Oreo+**. To stop scanning call -`scanner.stopScan(context, pendingIntent)` with -[the same](https://developer.android.com/reference/android/app/PendingIntent) intent in parameter. -The service will be stopped when the last scan was stopped. - -On Android Oreo or newer this library will use the native scanning mechanism. However, as it may also -emulate batching or apply filtering (when `useHardwareBatchingIfSupported` or `useHardwareFilteringIfSupported` -were called with parameter *false*) the library will register its own broadcast -receiver that will translate results from native to compat classes. - -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: +CodeCheck浠g爜娴嬭瘯鏃犲紓甯 -```java - Intent intent = new Intent(context, MyReceiver.class); // explicite intent - intent.setAction("com.example.ACTION_FOUND"); - intent.putExtra("some.extra", value); // optional - PendingIntent pendingIntent = PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT); - - BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner(); - ScanSettings settings = new ScanSettings.Builder() - .setScanMode(ScanSettings.SCAN_MODE_LOW_POWER) - .setReportDelay(10000) - .build(); - List filters = new ArrayList<>(); - filters.add(new ScanFilter.Builder().setServiceUuid(mUuid).build()); - scanner.startScan(filters, settings, context, pendingIntent); -``` - -Add your `MyRecever` to *AndroidManifest*, as the application context might have been released -and all broadcast receivers registered to it together with it. +CloudTest浠g爜娴嬭瘯鏃犲紓甯 -To stop scanning call: +鐏粧瀹夊叏鐥呮瘨瀹夊叏妫娴嬮氳繃 -```java - // To stop scanning use the same or an equal PendingIntent (check PendingIntent documentation) - Intent intent = new Intent(context, MyReceiver.class); - intent.setAction("com.example.ACTION_FOUND"); - PendingIntent pendingIntent = PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_CANCEL_CURRENT); - - BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner(); - scanner.stopScan(context, pendingIntent); -``` +褰撳墠鐗堟湰demo鍔熻兘涓庡畨鍗撳師缁勪欢鍩烘湰鏃犲樊寮 -**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 +娴嬭瘯鍛橈細浣欎附椋 -## Background scanning guidelines +#### 鐗堟湰杩唬 -To 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 `PendingIntent` and `SCAN_MODE_LOW_POWER` or `SCAN_MODE_OPPORTUNISTIC` -should be used, together with report delay set and filters used. -`useHardwareFilteringIfSupported` and `useHardwareBatchingIfSupported` should be set to *true* (default). +- v0.0.1_SNAPSHOT -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. +#### 鐗堟潈鍜岃鍙俊鎭 -## License + The Scanner Compat library is available under BSD 3-Clause license. See the LICENSE file for more info. -The Scanner Compat library is available under BSD 3-Clause license. See the LICENSE file for more info. \ No newline at end of file diff --git a/build.gradle b/build.gradle index 8da322c631295ac04d26e5ed4126a1165424ac37..0f37d1b371f1632aaf2af4a2814463596cc0ddf0 100644 --- a/build.gradle +++ b/build.gradle @@ -1,22 +1,37 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. +apply plugin: 'com.huawei.ohos.app' + +ohos { + compileSdkVersion 5 + defaultConfig { + compatibleSdkVersion 5 + } +} buildscript { repositories { - google() + maven { + url 'https://mirrors.huaweicloud.com/repository/maven/' + } + maven { + url 'https://developer.huawei.com/repo/' + } jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.5.2' - - classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4' - // NOTE: Do not place your application dependencies here; they belong - // in the individual module build.gradle files + classpath 'com.huawei.ohos:hap:2.4.2.5' + classpath 'com.huawei.ohos:decctest:1.0.0.6' } } allprojects { repositories { - google() + maven { + url 'https://mirrors.huaweicloud.com/repository/maven/' + } + maven { + url 'https://developer.huawei.com/repo/' + } jcenter() } -} \ No newline at end of file +} diff --git a/entry/.gitignore b/entry/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..796b96d1c402326528b4ba3c12ee9d92d0e212e9 --- /dev/null +++ b/entry/.gitignore @@ -0,0 +1 @@ +/build diff --git a/entry/build.gradle b/entry/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..18fad72ab26a49aad281ab3dea45b0cab0c36801 --- /dev/null +++ b/entry/build.gradle @@ -0,0 +1,14 @@ +apply plugin: 'com.huawei.ohos.hap' +ohos { + compileSdkVersion 5 + defaultConfig { + compatibleSdkVersion 5 + } + +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar', '*.har']) + testCompile 'junit:junit:4.12' + implementation project(':scanner') +} diff --git a/entry/src/main/config.json b/entry/src/main/config.json new file mode 100644 index 0000000000000000000000000000000000000000..838029d36775dce26637fc2d19042c434e718f44 --- /dev/null +++ b/entry/src/main/config.json @@ -0,0 +1,110 @@ +{ + "app": { + "bundleName": "com.example.myapplication", + "vendor": "example", + "version": { + "code": 1, + "name": "1.0" + }, + "apiVersion": { + "compatible": 5, + "target": 5 + } + }, + "deviceConfig": {}, + "module": { + "package": "com.example.myapplication", + "name": ".MyApplication", + "deviceType": [ + "tv" + ], + "distro": { + "deliveryWithInstall": true, + "moduleName": "entry", + "moduleType": "entry" + }, + "abilities": [ + { + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ], + "orientation": "unspecified", + "name": "com.example.myapplication.MainAbility", + "icon": "$media:icon", + "description": "$string:mainability_description", + "label": "MyApplication", + "type": "page", + "launchType": "standard" + }, + { + "name": "no.nordicsemi.android.support.v18.scanner.ScannerService", + "type": "service", + "visible": true + } + ], + "reqPermissions": [ + { + "name": "ohos.permission.USE_BLUETOOTH", + "reason": "浣跨敤钃濈墮鏉冮檺", + "usedScene": { + "ability": [ + "com.example.myapplication.slice.MainAbility", + "no.nordicsemi.android.support.v18.scanner.ScannerService" + ], + "when": "always" + } + }, + { + "name": "ohos.permission.DISCOVER_BLUETOOTH", + "reason": "鎼滅储钃濈墮鏉冮檺", + "usedScene": { + "ability": [ + "com.example.myapplication.slice.MainAbility", + "no.nordicsemi.android.support.v18.scanner.ScannerService" + ], + "when": "always" + } + }, + { + "name": "ohos.permission.LOCATION", + "reason": "鍏佽鍓嶅彴鑾峰彇鏉冮檺", + "usedScene": { + "ability": [ + "com.example.myapplication.slice.MainAbility", + "no.nordicsemi.android.support.v18.scanner.ScannerService" + ], + "when": "always" + } + }, + { + "name": "ohos.permission.LOCATION_IN_BACKGROUND", + "reason": "鍏佽鍚庡彴鑾峰彇鏉冮檺", + "usedScene": { + "ability": [ + "com.example.myapplication.slice.MainAbility", + "no.nordicsemi.android.support.v18.scanner.ScannerService" + ], + "when": "always" + } + }, + { + "name": "ohos.permission.GET_NETWORK_INFO", + "reason": "鍏佽搴旂敤鑾峰彇鏁版嵁缃戠粶淇℃伅", + "usedScene": { + "ability": [ + "com.example.myapplication.slice.MainAbility", + "no.nordicsemi.android.support.v18.scanner.ScannerService" + ], + "when": "always" + } + } + ] + } +} \ No newline at end of file diff --git a/entry/src/main/java/com/example/myapplication/HistoryBean.java b/entry/src/main/java/com/example/myapplication/HistoryBean.java new file mode 100644 index 0000000000000000000000000000000000000000..1c5894396a1fcaaf26565b635ae4764fa13abc70 --- /dev/null +++ b/entry/src/main/java/com/example/myapplication/HistoryBean.java @@ -0,0 +1,16 @@ +package com.example.myapplication; + +public class HistoryBean { + + private String name; + public HistoryBean(String name) { + this.name = name; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + +} diff --git a/entry/src/main/java/com/example/myapplication/HistoryItemProvider.java b/entry/src/main/java/com/example/myapplication/HistoryItemProvider.java new file mode 100644 index 0000000000000000000000000000000000000000..f573c855496a9a53a409ec88587d263e54dcd19b --- /dev/null +++ b/entry/src/main/java/com/example/myapplication/HistoryItemProvider.java @@ -0,0 +1,71 @@ +package com.example.myapplication; + +import no.nordicsemi.android.support.v18.scanner.ScanResult; +import ohos.aafwk.ability.AbilitySlice; +import ohos.agp.components.*; +import ohos.bluetooth.ble.BleScanResult; + +import java.util.ArrayList; +import java.util.List; + +public class HistoryItemProvider extends BaseItemProvider { + private List list; + private AbilitySlice slice; + private int layout; + + public HistoryItemProvider(List list, AbilitySlice slice, int layout) { + this.list = list; + this.slice = slice; + this.layout = layout; + } + + @Override + public int getCount() { + return list == null ? 0 : list.size(); + } + + public List getList() { + if (list == null) { + return new ArrayList<>(); + } + return list; + } + + @Override + public Object getItem(int i) { + if (list != null && i >= 0 && i < list.size()) { + return list.get(i); + } + return null; + } + + @Override + public long getItemId(int i) { + return i; + } + + @Override + public Component getComponent(int i, Component component, ComponentContainer componentContainer) { + final Component comp; + if (component == null) { + comp = LayoutScatter.getInstance(slice).parse(layout, null, false); + } else { + comp = component; + } + List list = getList(); + ScanResult historyBean = list.get(i); + BleScanResult bleScanResult = (BleScanResult) historyBean.getDevice(); + String str = bleScanResult.getPeripheralDevice().getDeviceName().toString(); + str = str.replace("Optional[", ""); + str = str.replace("]", ""); + Text text = (Text) comp.findComponentById(ResourceTable.Id_text); + if (str.equals("Optional.empty")) { + String strs = bleScanResult.getPeripheralDevice().getDeviceAddr().toString(); + text.setText(strs); + } else { + text.setText(str); + } + + return comp; + } +} \ No newline at end of file diff --git a/entry/src/main/java/com/example/myapplication/MainAbility.java b/entry/src/main/java/com/example/myapplication/MainAbility.java new file mode 100644 index 0000000000000000000000000000000000000000..d184331d82957d055552c81610186524a42ba1db --- /dev/null +++ b/entry/src/main/java/com/example/myapplication/MainAbility.java @@ -0,0 +1,15 @@ +package com.example.myapplication; + +import com.example.myapplication.slice.MainAbilitySlice; +import ohos.aafwk.ability.Ability; +import ohos.aafwk.content.Intent; +import ohos.aafwk.content.Operation; + +public class MainAbility extends Ability { + @Override + public void onStart(Intent intent) { + super.onStart(intent); + super.setMainRoute(MainAbilitySlice.class.getName()); + } + +} diff --git a/entry/src/main/java/com/example/myapplication/MyApplication.java b/entry/src/main/java/com/example/myapplication/MyApplication.java new file mode 100644 index 0000000000000000000000000000000000000000..33915e2cc691b0fa169fd0a13e2262ac3925bafd --- /dev/null +++ b/entry/src/main/java/com/example/myapplication/MyApplication.java @@ -0,0 +1,10 @@ +package com.example.myapplication; + +import ohos.aafwk.ability.AbilityPackage; + +public class MyApplication extends AbilityPackage { + @Override + public void onInitialize() { + super.onInitialize(); + } +} diff --git a/entry/src/main/java/com/example/myapplication/ScannerService.java b/entry/src/main/java/com/example/myapplication/ScannerService.java new file mode 100644 index 0000000000000000000000000000000000000000..62890b645bc40151a526c312bfd9f30a6fae125f --- /dev/null +++ b/entry/src/main/java/com/example/myapplication/ScannerService.java @@ -0,0 +1,129 @@ +package com.example.myapplication; + +import no.nordicsemi.android.support.v18.scanner.BluetoothLeScannerCompat; +import no.nordicsemi.android.support.v18.scanner.ScanCallback; +import no.nordicsemi.android.support.v18.scanner.ScanSettings; +import ohos.aafwk.ability.Ability; +import ohos.aafwk.content.Intent; +import ohos.app.Context; +import ohos.bluetooth.ble.BleScanFilter; +import ohos.event.intentagent.IntentAgent; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.logging.Handler; +import java.util.logging.LogRecord; + +public class ScannerService extends Ability { + + /* package */ static final String EXTRA_PENDING_INTENT = "ohos.permission.GET_NETWORK_INFO"; + /* package */ static final String EXTRA_FILTERS = "no.nordicsemi.android.support.v18.EXTRA_FILTERS"; + /* package */ static final String EXTRA_SETTINGS = "no.nordicsemi.android.support.v18.EXTRA_SETTINGS"; + /* package */ static final String EXTRA_START = "no.nordicsemi.android.support.v18.EXTRA_START"; + + private HashMap callbacks; + Handler handler; + private final Object LOCK = new Object(); + Context context = getContext(); + + @Override + protected void onStart(Intent intent) { + super.onStart(intent); + callbacks = new HashMap<>(); + handler = new Handler() { + @Override + public void publish(LogRecord logRecord) { + } + @Override + public void flush() { + } + @Override + public void close() throws SecurityException { + } + }; + + } + + @Override + public void onCommand(final Intent intent,final boolean restart,final int startId) { + super.onCommand(intent, restart, startId); +// final IntentAgent callbackIntent = intent.getSequenceableParam(EXTRA_PENDING_INTENT); + final boolean start = intent.getBooleanParam(EXTRA_START, false); + final boolean stop = !start; +// if (callbackIntent == null) { +// boolean shouldStop; +// synchronized (LOCK) { +// shouldStop = callbacks.isEmpty(); +// } +// +// if (shouldStop) +// stopScan(callbackIntent); +//// return 2; +// } + boolean knownCallback; + synchronized (LOCK) { +// knownCallback = callbacks.containsKey(callbackIntent); + } + +// if (start && !knownCallback) { +// final ArrayList filters = intent.getSequenceableArrayListParam(EXTRA_FILTERS); +// final ScanSettings settings = intent.getSequenceableParam(EXTRA_SETTINGS); +// startScan(filters != null ? filters : Collections.emptyList(), +// settings != null ? settings : new ScanSettings.Builder().build(), +// callbackIntent); +// } else if (stop && knownCallback) { +// stopScan(callbackIntent); +// } +// return 2; + } + + private void startScan(List bleScanFilters, ScanSettings scanSettings, IntentAgent callbackIntent) { +// final PendingIntentExecutor executor = +// new PendingIntentExecutor(callbackIntent, scanSettings,this); + synchronized (LOCK) { +// callbacks.put(callbackIntent, executor); + } + + try { + final BluetoothLeScannerCompat scannerCompat = BluetoothLeScannerCompat.getScanner(); + } catch (final Exception e) { + } + } + + private void stopScan(final IntentAgent callbackIntent) { + ScanCallback callback; + boolean shouldStop; + synchronized (LOCK) { + callback = callbacks.remove(callbackIntent); + shouldStop = callbacks.isEmpty(); + } + if (callback == null) + return; + try { + final BluetoothLeScannerCompat scannerCompat = BluetoothLeScannerCompat.getScanner(); + scannerCompat.stopScan(callback); + } catch (final Exception e) { + } + if (shouldStop) + stopScan(callbackIntent); + } + + @Override + protected void onStop() { + final BluetoothLeScannerCompat scannerCompat = BluetoothLeScannerCompat.getScanner(); + for (final ScanCallback callback : callbacks.values()) { + try { + scannerCompat.stopScan(callback); + } catch (final Exception e) { + // Ignore + } + } + callbacks.clear(); + callbacks = null; + handler = null; + super.onStop(); + super.onStop(); + } +} diff --git a/entry/src/main/java/com/example/myapplication/slice/MainAbilitySlice.java b/entry/src/main/java/com/example/myapplication/slice/MainAbilitySlice.java new file mode 100644 index 0000000000000000000000000000000000000000..42e5d26de37fd0b4cde369390585f28ae0423a42 --- /dev/null +++ b/entry/src/main/java/com/example/myapplication/slice/MainAbilitySlice.java @@ -0,0 +1,278 @@ +package com.example.myapplication.slice; + +import com.example.myapplication.HistoryItemProvider; +import com.example.myapplication.ResourceTable; +import no.nordicsemi.android.support.v18.scanner.*; +import ohos.aafwk.ability.AbilitySlice; +import ohos.aafwk.content.Intent; +import ohos.agp.components.*; +import ohos.agp.window.dialog.ToastDialog; +import ohos.app.Context; +import ohos.bluetooth.ble.BleScanFilter; +import ohos.bluetooth.ble.BleScanResult; +import ohos.bundle.IBundleManager; +import ohos.hiviewdfx.HiLog; +import ohos.hiviewdfx.HiLogLabel; + +import java.util.*; + +public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener { + + private Button btnStartScan, btnStopScan; + private RadioButton one, two, three, four; + private RadioContainer rabut; + private BluetoothLeScannerCompat scanner; + + private List beans = new ArrayList<>(); + Context context; + private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "MY_TAG"); + //璁剧疆钃濈墮鎵弿杩囨护鍣ㄩ泦鍚 + private List scanFilterList; + //璁剧疆钃濈墮鎵弿杩囨护鍣 + private ScanFilter.Builder scanFilterBuilder; + //璁剧疆钃濈墮鎵弿璁剧疆 + private ScanSettings.Builder scanSettingBuilder; + private ListContainer dialog; + private HistoryItemProvider historyItemProvider; + private boolean isBut = false; + private boolean isPD = false; +// private ToastDialog toastDialog; + + + @Override + public void onStart(Intent intent) { + super.onStart(intent); + super.setUIContent(ResourceTable.Layout_ability_main); + initView(); + initClick(); + String[] permission = {"ohos.permission.LOCATION", "ohos.permission.USE_BLUETOOTH" + , "ohos.permission.DISCOVER_BLUETOOTH", "ohos.permission.LOCATION_IN_BACKGROUND" + , "ohos.permission.GET_NETWORK_INFO"}; + requestPermissionsFromUser(permission, 0); + if (verifySelfPermission("ohos.permission.LOCATION_IN_BACKGROUND") != IBundleManager.PERMISSION_GRANTED) { + return; + } else { + return; + } + + } + + private void initView() { + dialog = (ListContainer) findComponentById(ResourceTable.Id_dialog); + btnStartScan = (Button) findComponentById(ResourceTable.Id_btn_start_scan); + btnStopScan = (Button) findComponentById(ResourceTable.Id_btn_stop_scan); + one = (RadioButton) findComponentById(ResourceTable.Id_one); + two = (RadioButton) findComponentById(ResourceTable.Id_two); + three = (RadioButton) findComponentById(ResourceTable.Id_three); + four = (RadioButton) findComponentById(ResourceTable.Id_four); + rabut = (RadioContainer) findComponentById(ResourceTable.Id_rabut); + scanner = BluetoothLeScannerCompat.getScanner(); + historyItemProvider = new HistoryItemProvider(beans, MainAbilitySlice.this, ResourceTable.Layout_item_layout); + dialog.setItemProvider(historyItemProvider); + rabut.mark(0); + } + + private void initClick() { + btnStartScan.setClickedListener(this); + btnStopScan.setClickedListener(this); + + rabut.setMarkChangedListener(new RadioContainer.CheckedStateChangedListener() { + @Override + public void onCheckedChanged(RadioContainer radioContainer, int i) { + if (i == 0) { + if (isBut == false) { + ScanSettings settings = new ScanSettings.Builder() + .setLegacy(false) + .setScanMode(ScanSettings.SCAN_MODE_LOW_POWER) + .setReportDelay(10000) + .setUseHardwareBatchingIfSupported(false) + .build(); + } else { + new ToastDialog(getContext()) + .setText("鎼滅储涓,璇风粨鏉熷綋鍓嶆悳绱") + .show(); + } + } else if (i == 1) { + if (isBut == false) { + ScanSettings settings = new ScanSettings.Builder() + .setLegacy(false) + .setScanMode(ScanSettings.SCAN_MODE_BALANCED) + .setReportDelay(10000) + .setUseHardwareBatchingIfSupported(false) + .build(); + } else { + new ToastDialog(getContext()) + .setText("鎼滅储涓,璇风粨鏉熷綋鍓嶆悳绱") + .show(); + } + } else if (i == 2) { + if (isBut == false) { + ScanSettings settings = new ScanSettings.Builder() + .setLegacy(false) + .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) + .setReportDelay(10000) + .setUseHardwareBatchingIfSupported(false) + .build(); + } else { + new ToastDialog(getContext()) + .setText("鎼滅储涓,璇风粨鏉熷綋鍓嶆悳绱") + .show(); + } + } else { + if (isBut == false) { + ScanSettings settings = new ScanSettings.Builder() + .setLegacy(false) + .setScanMode(ScanSettings.SCAN_MODE_OPPORTUNISTIC) + .setReportDelay(10000) + .setUseHardwareBatchingIfSupported(false) + .build(); + } else { + new ToastDialog(getContext()) + .setText("鎼滅储涓,璇风粨鏉熷綋鍓嶆悳绱") + .show(); + } + } + } + }); + } + + + @Override + public void onActive() { + super.onActive(); + } + + @Override + public void onForeground(Intent intent) { + super.onForeground(intent); + } + + private ScanSettings buildScanSettings() { + scanSettingBuilder = new ScanSettings.Builder(); + scanSettingBuilder.setUseHardwareBatchingIfSupported(false); + //璁剧疆钃濈墮LE鎵弿鐨勬壂鎻忔ā寮忋 + //浣跨敤鏈楂樺崰绌烘瘮杩涜鎵弿銆傚缓璁彧鍦ㄥ簲鐢ㄧ▼搴忓浜庢妯″紡鏃朵娇鐢ㄦ妯″紡鍦ㄥ墠鍙拌繍琛 + scanSettingBuilder.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY); + //璁剧疆钃濈墮LE鎵弿婊ゆ尝鍣ㄧ‖浠跺尮閰嶇殑鍖归厤妯″紡 + //鍦ㄤ富鍔ㄦā寮忎笅锛屽嵆浣夸俊鍙峰己搴﹁緝寮憋紝hw涔熶細鏇村揩鍦扮‘瀹氬尮閰.鍦ㄤ竴娈垫椂闂村唴寰堝皯鏈夌洰鍑/鍖归厤銆 + scanSettingBuilder.setMatchMode(ScanSettings.MATCH_MODE_AGGRESSIVE); + //璁剧疆钃濈墮LE鎵弿鐨勫洖璋冪被鍨 + //涓烘瘡涓涓尮閰嶈繃婊ゆ潯浠剁殑钃濈墮骞垮憡瑙﹀彂涓涓洖璋冦傚鏋滄病鏈夎繃婊ゅ櫒鏄椿鍔ㄧ殑锛屾墍鏈夌殑骞垮憡鍖呰鎶ュ憡 + scanSettingBuilder.setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES); + scanSettingBuilder.setReportDelay(10000); + scanSettingBuilder.setUseHardwareBatchingIfSupported(false); + return scanSettingBuilder.build(); + } + + @Override + public void onClick(Component component) { + switch (component.getId()) { + case ResourceTable.Id_btn_start_scan: + if (isBut == false) { + start(); + new ToastDialog(getContext()) + .setText("寮濮") + .show(); + } else { + new ToastDialog(getContext()) + .setText("鎼滅储涓") + .show(); + } + break; + case ResourceTable.Id_btn_stop_scan: + if (isBut == true) { + stop(); + new ToastDialog(getContext()) + .setText("缁撴潫") + .show(); + } else { + new ToastDialog(getContext()) + .setText("宸茬粨鏉") + .show(); + } + break; + } + } + + private void start() { + isBut = true; + List filters = new ArrayList<>(); + UUID uuid = UUID.randomUUID(); + scanner.startScan(this, null, buildScanSettings(), scanCallback); + scanner.startScan(filters, buildScanSettings(), this, null); + } + + private void stop() { + isBut = false; + scanner.stopScan(scanCallback); + beans.clear(); + getUITaskDispatcher().asyncDispatch(new Runnable() { + @Override + public void run() { + historyItemProvider.notifyDataChanged(); + } + }); + } + + private ScanCallback scanCallback = new ScanCallback() { + @Override + public void onScanResult(int callbackType, ScanResult result) { + super.onScanResult(callbackType, result); + System.out.println("LOGLIST" + "/**/" + "RESULTDEVICE-------------------------------" + result.getDevice()); + } + + + @Override + public void onBatchScanResults(List results) { + super.onBatchScanResults(results); + if (results.size() > 0) { + beans.clear(); + beans.addAll(results); + } + + getUITaskDispatcher().asyncDispatch(new Runnable() { + @Override + public void run() { + if (results.size() > 0) { + int size = results.size(); + } + } + }); + + for (ScanResult scanResult : results) { + + String deviceName = scanResult.getScanRecord().getDeviceName(); + if (deviceName != null && !deviceName.isEmpty()) { +// System.out.println("LOGLISTALL" + "/--/--/ " +// + "DEVICE =" + ((BleScanResult)scanResult.getDevice()).getPeripheralDevice().getDeviceAddr()+ ", " +// +"DEVICENAME ="+scanResult.getScanRecord().getDeviceName()+", " +// +"DATASTATUS ="+scanResult.getDataStatus()); + } + int size = results.size(); + System.out.println("LOGLISTALL" + "/**/" + "ALLSIZE = " + size); + } + getUITaskDispatcher().asyncDispatch(new Runnable() { + @Override + public void run() { + historyItemProvider.notifyDataChanged(); + } + }); + } + + @Override + public void onScanFailed(int errorCode) { + super.onScanFailed(errorCode); + System.out.println("onScanFailed"); + } + }; + + @Override + protected void onBackground() { + super.onBackground(); + } + + @Override + protected void onStop() { + super.onStop(); + } +} \ No newline at end of file diff --git a/entry/src/main/resources/base/element/string.json b/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..6becfe43fc3852c05cb8654d79518be7487f6947 --- /dev/null +++ b/entry/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "app_name", + "value": "MyApplication" + }, + { + "name": "mainability_description", + "value": "Java_Phone_Empty Feature Ability" + }, + { + "name": "HelloWorld", + "value": "Hello World" + } + ] +} \ No newline at end of file diff --git a/entry/src/main/resources/base/graphic/background_ability_main.xml b/entry/src/main/resources/base/graphic/background_ability_main.xml new file mode 100644 index 0000000000000000000000000000000000000000..c0c0a3df480fa387a452b9c40ca191cc918a3fc0 --- /dev/null +++ b/entry/src/main/resources/base/graphic/background_ability_main.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/entry/src/main/resources/base/graphic/background_list.xml b/entry/src/main/resources/base/graphic/background_list.xml new file mode 100644 index 0000000000000000000000000000000000000000..1629bd1a5ce8d90ff3c1a9c9ab543bb6079c7a0d --- /dev/null +++ b/entry/src/main/resources/base/graphic/background_list.xml @@ -0,0 +1,11 @@ + + + + + + + \ No newline at end of file diff --git a/entry/src/main/resources/base/layout/ability_main.xml b/entry/src/main/resources/base/layout/ability_main.xml new file mode 100644 index 0000000000000000000000000000000000000000..84da3f7f39bcfac4b040cb6dc5f0fa8c319e9bd5 --- /dev/null +++ b/entry/src/main/resources/base/layout/ability_main.xml @@ -0,0 +1,83 @@ + + + + + +