Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BlueFalcon example in Compose Multiplatform #116

Merged
merged 3 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions examples/ComposeMultiplatform-Example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
*.iml
.gradle
.idea
.DS_Store
build
captures
.externalNativeBuild
.cxx
local.properties
xcuserdata
iosBlueFalconExampleMP/iosBlueFalconExampleMP.xcodeproj/project.xcworkspace/contents.xcworkspacedata
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
plugins {
id("com.android.application")
kotlin("android")
id("org.jetbrains.compose")
}

android {
namespace = "com.example.bluefalconcomposemultiplatform.android"
compileSdk = 34
defaultConfig {
applicationId = "com.example.bluefalconcomposemultiplatform.android"
minSdk = 24
targetSdk = 34
versionCode = 1
versionName = "1.0"
}
buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.5.3"
}
packagingOptions {
resources {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
}
}
buildTypes {
getByName("release") {
isMinifyEnabled = false
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = "17"
}
}

dependencies {
implementation(project(":shared"))
implementation("androidx.compose.ui:ui:1.4.0")
implementation("androidx.compose.ui:ui-tooling:1.4.0")
implementation("androidx.compose.ui:ui-tooling-preview:1.4.0")
implementation("androidx.compose.foundation:foundation:1.4.0")
implementation("androidx.compose.material:material:1.4.0")
implementation("androidx.activity:activity-compose:1.7.0")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<uses-permission
android:name="android.permission.BLUETOOTH_ADVERTISE"
android:usesPermissionFlags="neverForLocation"
tools:targetApi="s" />
<uses-permission
android:name="android.permission.BLUETOOTH_SCAN"
android:usesPermissionFlags="neverForLocation"
tools:targetApi="s" />
<uses-permission
android:name="android.permission.BLUETOOTH_CONNECT"
android:usesPermissionFlags="neverForLocation"
tools:targetApi="s" />

<application
android:allowBackup="false"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.example.bluefalconcomposemultiplatform.android

import android.Manifest
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.ui.platform.LocalContext
import androidx.core.app.ActivityCompat
import com.example.bluefalconcomposemultiplatform.App
import com.example.bluefalconcomposemultiplatform.di.AppModule

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

if (needToAskForRuntimePermissions()) {
askForRuntimePermissions {
// Do something when permission granted
}
}

setContent {
App(
darkTheme = isSystemInDarkTheme(),
dynamicColor = false,
appModule = AppModule(LocalContext.current.applicationContext)
)
}
}

private fun askForRuntimePermissions(doWhenPermissionAcquired: ()-> Unit) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {

registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions ->

if (permissions[Manifest.permission.BLUETOOTH_SCAN] == true &&
permissions[Manifest.permission.BLUETOOTH_CONNECT] == true &&
permissions[Manifest.permission.ACCESS_FINE_LOCATION] == true
) {
doWhenPermissionAcquired()
} else {
Toast.makeText(this, "Cannot scan without permission.", Toast.LENGTH_LONG)
.show()
}
}.apply {
launch(
arrayOf(
Manifest.permission.BLUETOOTH_SCAN,
Manifest.permission.BLUETOOTH_CONNECT,
Manifest.permission.ACCESS_FINE_LOCATION
)
)
}
}
}

private fun needToAskForRuntimePermissions() =
Build.VERSION.SDK_INT >= Build.VERSION_CODES.S &&

((ActivityCompat.checkSelfPermission(
this,
Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED ||

ActivityCompat.checkSelfPermission(
this,
Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED ||

ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<style name="AppTheme" parent="android:Theme.Material.NoActionBar"/>
</resources>
17 changes: 17 additions & 0 deletions examples/ComposeMultiplatform-Example/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
buildscript {
dependencies {
// Add dependencies
}
}

plugins {
//trick: for the same plugin versions in all sub-modules
kotlin("multiplatform").apply(false)
id("com.android.application").apply(false)
id("com.android.library").apply(false)
id("org.jetbrains.compose").apply(false)
}

tasks.register("clean", Delete::class) {
delete(rootProject.buildDir)
}
20 changes: 20 additions & 0 deletions examples/ComposeMultiplatform-Example/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#Gradle
org.gradle.jvmargs=-Xmx2048M -Dfile.encoding=UTF-8 -Dkotlin.daemon.jvm.options\="-Xmx2048M"

#Kotlin
kotlin.code.style=official

#Android
android.useAndroidX=true
android.nonTransitiveRClass=true

#MPP
kotlin.mpp.enableCInteropCommonization=true
kotlin.mpp.androidSourceSetLayoutVersion=2

org.jetbrains.compose.experimental.uikit.enabled=true
kotlin.native.cacheKind=none

kotlin.version=1.9.10
agp.version=8.0.1
compose.version=1.5.1
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#Sun Jun 25 11:57:55 CEST 2023
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading
Loading