Skip to content

Commit

Permalink
Merge branch 'release/v1.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
rouddy committed Jul 17, 2020
2 parents d1790ba + 4191c07 commit af8647e
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 106 deletions.
84 changes: 64 additions & 20 deletions algorigoble/build.gradle
Original file line number Diff line number Diff line change
@@ -1,30 +1,73 @@
apply plugin: 'com.android.library'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'com.novoda.bintray-release' // must be applied after your artifact generating plugin (eg. java / com.android.library)

publish {
userOrg = 'chagilhwan'
groupId = 'com.algorigo.algorigoble'
artifactId = 'AlgorigoBleLibrary'
publishVersion = '1.1.5'
desc = 'AlgorigoBleLibrary'
website = 'https://github.com/Algorigo/AlgorigoBleLibrary'
issueTracker = "${website}/issues"
repository = "${website}.git"
apply plugin: 'maven-publish'
apply plugin: 'com.jfrog.bintray'

version '1.2.0'
group 'com.algorigo.algorigoble'

publishing {
publications {
AlgorigoBle(MavenPublication) {
artifact("$buildDir/outputs/aar/algorigoble-release.aar")
groupId this.group
artifactId 'AlgorigoBleLibrary'
version this.version

pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')

// Iterate over the implementation dependencies (we don't want the test ones), adding a <dependency> node for each
configurations.implementation.allDependencies.each {
// Ensure dependencies such as fileTree are not included in the pom.
if (it.name != 'unspecified') {
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
}
}
}
}
}
}

bintray {
user = System.getenv('BINTRAY_USER')
key = System.getenv('BINTRAY_KEY')
publications = ['AlgorigoBle']
configurations = ['archives']
override = true
pkg {
repo = 'maven'
name = 'AlgorigoBle'
userOrg = 'chagilhwan'
description = "Algorigo Ble Library with rx"
publish = true
publicDownloadNumbers = true
licenses = ['Apache-2.0']
websiteUrl = 'https://github.com/Algorigo/AlgorigoBleLibrary'
vcsUrl = '${websiteUrl}.git'
dryRun = false
version {
name = this.version
desc = 'minor update'
released = new Date()
vcsTag = this.version
}
}
}

android {
compileSdkVersion 28
compileSdkVersion 29

defaultConfig {
minSdkVersion 18
targetSdkVersion 28
targetSdkVersion 29
versionCode 1
versionName publish.publishVersion
versionName this.version

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand All @@ -46,7 +89,7 @@ dependencies {
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

implementation 'org.jetbrains.kotlin:kotlin-reflect:1.3.50'
implementation 'org.jetbrains.kotlin:kotlin-reflect:1.3.72'

//RxAndroidBle
implementation "com.polidea.rxandroidble2:rxandroidble:1.10.1"
Expand All @@ -61,11 +104,12 @@ dependencies {
implementation 'com.google.code.gson:gson:2.8.5'
}

repositories {
mavenCentral()
}

buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.novoda:bintray-release:0.9.1'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package com.algorigo.algorigoble

import android.bluetooth.BluetoothDevice

class BleScanFilter {
open class BleScanFilter {

internal var deviceName: String? = null
internal var deviceAddress: String? = null

fun isOk(device: BluetoothDevice?, rssi: Int, scanRecord: ByteArray?): Boolean {
open fun isOk(device: BluetoothDevice?, rssi: Int, scanRecord: ByteArray?): Boolean {
return (if (deviceName != null) deviceName.equals(device?.name) else true) &&
(if (deviceAddress != null) deviceAddress.equals(device?.address) else true)
}
Expand Down
141 changes: 63 additions & 78 deletions algorigoble/src/main/java/com/algorigo/algorigoble/impl/BleScanner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.bluetooth.BluetoothDevice
import android.bluetooth.le.ScanCallback
import android.bluetooth.le.ScanResult
import android.os.Build
import android.util.Log
import androidx.annotation.RequiresApi
import com.algorigo.algorigoble.BleScanFilter
import com.algorigo.algorigoble.BleScanSettings
Expand All @@ -19,108 +20,81 @@ import java.util.concurrent.TimeUnit

internal class BleScanner private constructor(private val bluetoothAdapter: BluetoothAdapter){

private abstract class FilteredLeScanCallback : BluetoothAdapter.LeScanCallback {

var scanFilters: Array<out BleScanFilter> = arrayOf()

override fun onLeScan(device: BluetoothDevice?, rssi: Int, scanRecord: ByteArray?) {
if (scanFilters.size == 0) {
onScanResult(device, rssi, scanRecord)
} else {
for (scanFilter in scanFilters) {
if (scanFilter.isOk(device, rssi, scanRecord)) {
onScanResult(device, rssi, scanRecord)
break
}
}
}
}

abstract fun onScanResult(device: BluetoothDevice?, rssi: Int, scanRecord: ByteArray?)
}

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
private lateinit var scanCallback : ScanCallback
private lateinit var leCallback : FilteredLeScanCallback

private val scanSubject = PublishSubject.create<BluetoothDevice>()

init {
initCallback()
}

private fun initCallback() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
initCallback21()
} else {
initCallback18()
}
}

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
private fun initCallback21() {
scanCallback = object : ScanCallback() {
override fun onScanResult(callbackType: Int, result: ScanResult?) {
super.onScanResult(callbackType, result)
result?.device?.let {
scanSubject.onNext(it)
private inner class BleScanCallback(val scanFilters: Array<out BleScanFilter>) : ScanCallback() {
override fun onScanResult(callbackType: Int, result: ScanResult?) {
super.onScanResult(callbackType, result)
result?.let {
if (isOk(it)) {
scanSubject.onNext(it.device)
}
}
}

override fun onBatchScanResults(results: MutableList<ScanResult>?) {
super.onBatchScanResults(results)
results?.let {
it.forEach {
override fun onBatchScanResults(results: MutableList<ScanResult>?) {
super.onBatchScanResults(results)
results?.let {
it.forEach {
if (isOk(it)) {
scanSubject.onNext(it.device)
}
}
}
}

override fun onScanFailed(errorCode: Int) {
super.onScanFailed(errorCode)
scanSubject.onError(IllegalStateException("onScanFailed:$errorCode"))
}
override fun onScanFailed(errorCode: Int) {
super.onScanFailed(errorCode)
scanSubject.onError(IllegalStateException("onScanFailed:$errorCode"))
}

private fun isOk(result: ScanResult): Boolean {
if (scanFilters.size == 0) return true
scanFilters.forEach { if (it.isOk(result.device, result.rssi, result.scanRecord?.bytes)) return true }
return false
}
}

private fun initCallback18() {
leCallback = object : FilteredLeScanCallback() {
override fun onScanResult(device: BluetoothDevice?, rssi: Int, scanRecord: ByteArray?) {
@Suppress("deprecation")
private inner class BleLeScanCallback(val scanFilters: Array<out BleScanFilter>) : BluetoothAdapter.LeScanCallback {
override fun onLeScan(device: BluetoothDevice?, rssi: Int, scanRecord: ByteArray?) {
if (isOk(device, rssi, scanRecord)) {
device?.let {
scanSubject.onNext(it)
}
}
}

private fun isOk(device: BluetoothDevice?, rssi: Int, scanRecord: ByteArray?): Boolean {
if (scanFilters.size == 0) return true
scanFilters.forEach { if (it.isOk(device, rssi, scanRecord)) return true }
return false
}
}

private val scanSubject = PublishSubject.create<BluetoothDevice>()

private fun startScanObservable(scanSettings: BleScanSettings, vararg scanFilters: BleScanFilter): Observable<BluetoothDevice> {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
startScanObservable21(scanSettings, *scanFilters)
} else {
startScanObservable18(scanSettings, *scanFilters)
}
}

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
private fun startScanObservable21(scanSettings: BleScanSettings, vararg scanFilters: BleScanFilter): Observable<BluetoothDevice> {
val scanCallback = BleScanCallback(scanFilters)
return scanSubject
.doOnSubscribe {
startScan(scanSettings, *scanFilters)
startScan21(scanCallback, scanSettings, *scanFilters)
}
.doFinally {
stopScan()
stopScan21(scanCallback)
}
}

private fun startScan(scanSettings: BleScanSettings, vararg scanFilters: BleScanFilter) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
startScan21(scanSettings, *scanFilters)
} else {
startScan18(scanSettings, *scanFilters)
}
}

private fun stopScan() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
stopScan21()
} else {
stopScan18()
}
}

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
private fun startScan21(bleScanSettings: BleScanSettings, vararg bleScanFilters: BleScanFilter) {
private fun startScan21(scanCallback: ScanCallback, bleScanSettings: BleScanSettings, vararg bleScanFilters: BleScanFilter) {
bluetoothAdapter.bluetoothLeScanner.startScan(
BleScanOptionsConverter.convertScanFilters(bleScanFilters),
BleScanOptionsConverter.convertScanSettings(bleScanSettings),
Expand All @@ -129,18 +103,29 @@ internal class BleScanner private constructor(private val bluetoothAdapter: Blue
}

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
private fun stopScan21() {
private fun stopScan21(scanCallback: ScanCallback) {
bluetoothAdapter.bluetoothLeScanner.stopScan(scanCallback)
}

@Suppress("deprecation")
private fun startScan18(bleScanSettings: BleScanSettings, vararg bleScanFilters: BleScanFilter) {
leCallback.scanFilters = bleScanFilters
private fun startScanObservable18(scanSettings: BleScanSettings, vararg scanFilters: BleScanFilter): Observable<BluetoothDevice> {
val leCallback = BleLeScanCallback(scanFilters)
return scanSubject
.doOnSubscribe {
startScan18(leCallback, scanSettings, *scanFilters)
}
.doFinally {
stopScan18(leCallback)
}
}

@Suppress("deprecation")
private fun startScan18(leCallback: BleLeScanCallback, bleScanSettings: BleScanSettings, vararg bleScanFilters: BleScanFilter) {
bluetoothAdapter.startLeScan(leCallback)
}

@Suppress("deprecation")
private fun stopScan18() {
private fun stopScan18(leCallback: BleLeScanCallback) {
bluetoothAdapter.stopLeScan(leCallback)
}

Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ android {
storePassword project.property("storePassword")
}
}
compileSdkVersion 28
compileSdkVersion 29
defaultConfig {
applicationId "com.algorigo.algorigoblelibrary"
minSdkVersion 18
targetSdkVersion 28
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Expand Down
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.3.50'
ext.kotlin_version = '1.3.72'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.2'
classpath 'com.android.tools.build:gradle:4.0.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
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
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Tue Aug 27 12:04:47 KST 2019
#Thu Jul 16 17:52:48 KST 2020
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip

0 comments on commit af8647e

Please sign in to comment.