Skip to content

Commit df4af90

Browse files
Merge pull request #45 from NordicPlayground/develop
Develop
2 parents 00969a3 + f796720 commit df4af90

File tree

22 files changed

+336
-304
lines changed

22 files changed

+336
-304
lines changed

app/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ dependencies {
5151

5252
implementation(libs.androidx.activity.compose)
5353

54+
implementation(libs.nordic.blek.scanner)
55+
5456
implementation(libs.androidx.hilt.navigation.compose)
5557

5658
implementation(libs.androidx.lifecycle.runtime.compose)

app/src/main/java/no/nordicsemi/android/common/test/main/page/BasicViewsPage.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ import no.nordicsemi.android.common.theme.NordicTheme
6363
import no.nordicsemi.android.common.theme.view.PagerViewItem
6464
import no.nordicsemi.android.common.theme.view.RssiIcon
6565
import no.nordicsemi.android.common.ui.scanner.main.DeviceListItem
66-
import no.nordicsemi.android.common.ui.scanner.model.DiscoveredBluetoothDevice
66+
import no.nordicsemi.android.kotlin.ble.core.ServerDevice
6767
import java.util.*
6868
import javax.inject.Inject
6969

@@ -86,7 +86,7 @@ class BasicPageViewModel @Inject constructor(
8686
private val savedStateHandle: SavedStateHandle,
8787
) : ViewModel() {
8888
// Initialize the selected device from the saved state handle.
89-
val device = savedStateHandle.getStateFlow<DiscoveredBluetoothDevice?>(DEVICE_KEY, null)
89+
val device = savedStateHandle.getStateFlow<ServerDevice?>(DEVICE_KEY, null)
9090

9191
init {
9292
navigator.resultFrom(Scanner)
@@ -111,11 +111,11 @@ class BasicPageViewModel @Inject constructor(
111111

112112
@SuppressLint("MissingPermission")
113113
data class DeviceInfo(
114-
private val device: DiscoveredBluetoothDevice?
114+
private val device: ServerDevice?
115115
) {
116116
val name = device?.name ?: "Unknown"
117117
val address = device?.address ?: "Unknown"
118-
val rssi = device?.rssi ?: 0
118+
val rssi = device?.highestRssi ?: 0
119119
}
120120

121121
@Composable

app/src/main/java/no/nordicsemi/android/common/test/scanner/Scanner.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ import no.nordicsemi.android.common.navigation.createDestination
3737
import no.nordicsemi.android.common.navigation.defineDestination
3838
import no.nordicsemi.android.common.navigation.viewmodel.SimpleNavigationViewModel
3939
import no.nordicsemi.android.common.ui.scanner.ScannerView
40-
import no.nordicsemi.android.common.ui.scanner.model.DiscoveredBluetoothDevice
40+
import no.nordicsemi.android.kotlin.ble.core.ServerDevice
4141

4242
/**
4343
* The scanner destination servers as an example of a destination that takes a parameter and returns
4444
* a result. The parameter is a [ParcelUuid] that is used to filter the devices that are displayed.
4545
* The result is a [DiscoveredBluetoothDevice] that was selected by the user.
4646
*/
47-
val Scanner = createDestination<ParcelUuid?, DiscoveredBluetoothDevice>("scanner")
47+
val Scanner = createDestination<ParcelUuid?, ServerDevice>("scanner")
4848

4949
val ScannerDestination = defineDestination(Scanner) {
5050
val vm: SimpleNavigationViewModel = hiltViewModel()

settings.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ dependencyResolutionManagement {
4949
}
5050
versionCatalogs {
5151
create("libs") {
52-
from("no.nordicsemi.android.gradle:version-catalog:1.4.7")
52+
from("no.nordicsemi.android.gradle:version-catalog:1.5.0")
5353
}
5454
}
5555
}

uilogger/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,7 @@ android {
5959
dependencies {
6060
implementation(project(":theme"))
6161

62+
implementation(libs.nordic.blek.core)
63+
6264
implementation(libs.nordic.log)
6365
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (c) 2022, Nordic Semiconductor
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without modification, are
6+
* permitted provided that the following conditions are met:
7+
*
8+
* 1. Redistributions of source code must retain the above copyright notice, this list of
9+
* conditions and the following disclaimer.
10+
*
11+
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
12+
* of conditions and the following disclaimer in the documentation and/or other materials
13+
* provided with the distribution.
14+
*
15+
* 3. Neither the name of the copyright holder nor the names of its contributors may be
16+
* used to endorse or promote products derived from this software without specific prior
17+
* written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22+
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23+
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
26+
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
27+
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29+
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
package no.nordicsemi.android.common.logger
33+
34+
import android.content.Context
35+
import android.content.Intent
36+
import android.net.Uri
37+
38+
private const val LOGGER_PACKAGE_NAME = "no.nordicsemi.android.log"
39+
private const val LOGGER_LINK = "https://play.google.com/store/apps/details?id=no.nordicsemi.android.log"
40+
41+
object LoggerLauncher {
42+
43+
/**
44+
* Opens the log session in nRF Logger app, or opens Google Play if the app is not installed.
45+
*/
46+
fun launch(context: Context, sessionUri: Uri? = null) {
47+
val packageManger = context.packageManager
48+
49+
if (packageManger.getLaunchIntentForPackage(LOGGER_PACKAGE_NAME) != null && sessionUri != null) {
50+
openLauncher(context, sessionUri)
51+
} else try {
52+
openGooglePlay(context)
53+
} catch (e: Exception) {
54+
e.printStackTrace() //Google Play not installed
55+
}
56+
}
57+
58+
private fun openLauncher(context: Context, sessionUri: Uri) {
59+
val intent = Intent(Intent.ACTION_VIEW, sessionUri)
60+
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
61+
intent.let { context.startActivity(intent) }
62+
}
63+
64+
private fun openGooglePlay(context: Context) {
65+
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(LOGGER_LINK))
66+
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
67+
context.startActivity(intent)
68+
}
69+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (c) 2022, Nordic Semiconductor
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without modification, are
6+
* permitted provided that the following conditions are met:
7+
*
8+
* 1. Redistributions of source code must retain the above copyright notice, this list of
9+
* conditions and the following disclaimer.
10+
*
11+
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
12+
* of conditions and the following disclaimer in the documentation and/or other materials
13+
* provided with the distribution.
14+
*
15+
* 3. Neither the name of the copyright holder nor the names of its contributors may be
16+
* used to endorse or promote products derived from this software without specific prior
17+
* written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22+
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23+
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
26+
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
27+
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29+
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
package no.nordicsemi.android.common.logger
33+
34+
import android.content.Context
35+
import android.util.Log
36+
import no.nordicsemi.android.kotlin.ble.core.logger.BlekLogger
37+
import no.nordicsemi.android.log.LogContract
38+
import no.nordicsemi.android.log.Logger
39+
40+
class NordicBlekLogger(
41+
private val context: Context,
42+
profile: String?,
43+
private val key: String,
44+
name: String?
45+
) : BlekLogger {
46+
private val logSession = Logger.newSession(context, profile, key, name)
47+
48+
override fun log(priority: Int, log: String) {
49+
Log.println(priority, key, log)
50+
Logger.log(logSession, LogContract.Log.Level.fromPriority(priority), log)
51+
}
52+
53+
/**
54+
* Opens the log session in nRF Logger app, or opens Google Play if the app is not installed.
55+
*/
56+
fun launch() {
57+
LoggerLauncher.launch(context, logSession?.sessionUri)
58+
}
59+
}

uilogger/src/main/java/no/nordicsemi/android/common/logger/NordicLogger.kt

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,11 @@ import android.net.Uri
3737
import dagger.assisted.Assisted
3838
import dagger.assisted.AssistedInject
3939
import dagger.hilt.android.qualifiers.ApplicationContext
40+
import no.nordicsemi.android.kotlin.ble.core.logger.BlekLogger
41+
import no.nordicsemi.android.log.LogContract
4042
import no.nordicsemi.android.log.Logger
4143
import no.nordicsemi.android.log.annotation.LogLevel
4244

43-
private const val LOGGER_PACKAGE_NAME = "no.nordicsemi.android.log"
44-
private const val LOGGER_LINK = "https://play.google.com/store/apps/details?id=no.nordicsemi.android.log"
45-
4645
/**
4746
* Creates a new instance of the logger
4847
*
@@ -57,63 +56,34 @@ class NordicLogger @AssistedInject constructor(
5756
@Assisted("profile") profile: String?,
5857
@Assisted("key") key: String,
5958
@Assisted("name") name: String?,
60-
) {
59+
) : BlekLogger {
6160
private val logSession = Logger.newSession(context, profile, key, name)
6261

6362
/**
6463
* Logs the given message with the given log level.
6564
*
6665
* If nRF Logger is not installed, this method does nothing.
6766
*/
68-
fun log(@LogLevel level: Int, message: String) {
69-
Logger.log(logSession, level, message)
67+
override fun log(@LogLevel priority: Int, log: String) {
68+
Logger.log(logSession, priority, log)
7069
}
7170

71+
7272
companion object {
7373

7474
/**
7575
* Opens the log session in nRF Logger app, or opens Google Play if the app is not installed.
7676
*/
7777
fun launch(context: Context, logger: NordicLogger? = null) {
78-
val packageManger = context.packageManager
79-
80-
// Make sure nRF Logger is installed.
81-
val intent = packageManger.getLaunchIntentForPackage(LOGGER_PACKAGE_NAME) ?: run {
82-
with (Intent(Intent.ACTION_VIEW, Uri.parse(LOGGER_LINK))) {
83-
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
84-
context.startActivity(this)
85-
}
86-
return@launch
87-
}
88-
89-
// Start nRF Logger and show the log session, or the main screen if session does not exist.
90-
with (logger?.logSession?.sessionUri?.let { Intent(Intent.ACTION_VIEW, it) } ?: intent) {
91-
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
92-
context.startActivity(this)
93-
}
78+
LoggerLauncher.launch(context, logger?.logSession?.sessionsUri)
9479
}
9580

9681
/**
9782
* Opens the log session in nRF Logger app, or opens Google Play if the app is not installed.
9883
*/
9984
fun launch(context: Context, sessionUri: Uri? = null) {
100-
val packageManger = context.packageManager
101-
102-
// Make sure nRF Logger is installed.
103-
val intent = packageManger.getLaunchIntentForPackage(LOGGER_PACKAGE_NAME) ?: run {
104-
with (Intent(Intent.ACTION_VIEW, Uri.parse(LOGGER_LINK))) {
105-
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
106-
context.startActivity(this)
107-
}
108-
return@launch
109-
}
110-
111-
// Start nRF Logger and show the log session, or the main screen if session does not exist.
112-
with (sessionUri?.let { Intent(Intent.ACTION_VIEW, it) } ?: intent) {
113-
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
114-
context.startActivity(this)
115-
}
85+
LoggerLauncher.launch(context, sessionUri)
11686
}
117-
11887
}
119-
}
88+
}
89+

uiscanner/build.gradle.kts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ dependencies {
6161
implementation(project(":theme"))
6262
implementation(project(":permission"))
6363

64+
implementation(libs.nordic.blek.scanner)
65+
implementation(libs.nordic.blek.core)
66+
6467
implementation(libs.accompanist.flowlayout)
6568

6669
implementation(libs.androidx.compose.material)
6770
implementation(libs.androidx.compose.material.iconsExtended)
68-
69-
implementation(libs.nordic.scanner)
7071
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (c) 2022, Nordic Semiconductor
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without modification, are
6+
* permitted provided that the following conditions are met:
7+
*
8+
* 1. Redistributions of source code must retain the above copyright notice, this list of
9+
* conditions and the following disclaimer.
10+
*
11+
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
12+
* of conditions and the following disclaimer in the documentation and/or other materials
13+
* provided with the distribution.
14+
*
15+
* 3. Neither the name of the copyright holder nor the names of its contributors may be
16+
* used to endorse or promote products derived from this software without specific prior
17+
* written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22+
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23+
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
26+
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
27+
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29+
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
package no.nordicsemi.android.common.ui.scanner
33+
34+
import android.content.Context
35+
import dagger.Module
36+
import dagger.Provides
37+
import dagger.hilt.InstallIn
38+
import dagger.hilt.android.qualifiers.ApplicationContext
39+
import dagger.hilt.components.SingletonComponent
40+
import no.nordicsemi.android.kotlin.ble.scanner.NordicScanner
41+
42+
@Module
43+
@InstallIn(SingletonComponent::class)
44+
internal class HiltModule {
45+
46+
@Provides
47+
fun providesScanner(
48+
@ApplicationContext
49+
context: Context
50+
): NordicScanner {
51+
return NordicScanner(context)
52+
}
53+
}

0 commit comments

Comments
 (0)