android,kotlin2025. 1. 8. 10:49
android,kotlin2024. 6. 21. 14:55

[android] Android RS232 구현방법

 

gradle

dependencies {
    implementation 'com.github.mik3y:usb-serial-for-android:3.4.6'
}

 

manifest

<uses-feature android:name="android.hardware.usb.host" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.USB_PERMISSION" />

<application
    ...>
    <activity ...>
        ...
        <intent-filter>
            <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
        </intent-filter>
        <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
            android:resource="@xml/device_filter" />
    </activity>
</application>

 

필터

<resources>
    <usb-device vendor-id="0x2341" product-id="0x0043" />
</resources>

 

하드웨어에서 제공하는 키를 넣으면 된다.

vendor-id="0x2341" product-id="0x0043"

 

activity

 private lateinit var usbManager: UsbManager
    private var serialPort: UsbSerialPort? = null
    
     override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        usbManager = getSystemService(USB_SERVICE) as UsbManager
        setupSerialConnection()
    }
    
    private fun setupSerialConnection() {
        val availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(usbManager)
        if (availableDrivers.isEmpty()) {
            Log.d(TAG, "No USB devices found")
            return
        }

        val driver = availableDrivers[0]
        val connection = usbManager.openDevice(driver.device)
        if (connection == null) {
            Log.d(TAG, "Could not open connection")
            return
        }

        serialPort = driver.ports[0]
        serialPort?.open(connection)
        serialPort?.setParameters(9600, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE)

        val usbIoManager = SerialInputOutputManager(serialPort, object : SerialInputOutputManager.Listener {
            override fun onNewData(data: ByteArray) {
                runOnUiThread {
                    Log.d(TAG, "Received data: ${String(data)}")
                }
            }

            override fun onRunError(e: Exception) {
                Log.e(TAG, "Runner stopped.", e)
            }
        })
        Executors.newSingleThreadExecutor().submit(usbIoManager)

        // Example: Sending data
        val sendData = "하드웨어에 전해줄 명령어".toByteArray()
        serialPort?.write(sendData, 1000)
    }
    
      override fun onDestroy() {
        super.onDestroy()
        serialPort?.close()
    }

 

협약 된 명령어 넣어준다.

 val sendData = "하드웨어에 전해줄 명령어".toByteArray()

Posted by thdeodls85
android,kotlin2024. 6. 4. 15:39

[android] Could not parse metadata! Try bumping kotlinpoet and/or kotlinx-metadata version cohttp://m.squareup.moshi.kotlinpoet 해결방법

 

  kapt 'com.squareup.moshi:moshi-kotlin-codegen:1.14.0'
  
  ->
   kapt 'com.squareup.moshi:moshi-kotlin-codegen:1.15.1'
   
   변경처리한다.

 

 

[참조] https://github.com/square/moshi#codegen

 

GitHub - square/moshi: A modern JSON library for Kotlin and Java.

A modern JSON library for Kotlin and Java. Contribute to square/moshi development by creating an account on GitHub.

github.com

 

<dependency>
  <groupId>cohttp://m.squareup.moshi</groupId>
  <artifactId>moshi-kotlin</artifactId>
  <version>1.15.1</version>
</dependency>

Posted by thdeodls85
android,kotlin2024. 6. 3. 15:24

[android] None of the following candidates is applicable because of receiver type mismatch: public val NamedDomainObjectContainer<Configuration>.implementation: NamedDomainObjectProvider<Configuration> defined in org.gradle.kotlin.dsl 해결방법은?

 

내부 프로젝트 import 시킬려고 했더니...

 

Gradle Kotlin DSL 추천 해서 해봣더니.. 문법이 Groovy DSL 과 다르다.. 적용 시킬려면 제대로 된 문법을 넣으면 된다.

 

예전방식

// 예전방식
implementation project(':프로젝트명')

// dsl방식
implementation(project(":프로젝트명"))

 

 

Posted by thdeodls85
android,kotlin2024. 3. 19. 13:49

[android] 패키지가 잘못되어 앱이 설치되지 않았습니다 해결방법

 

debug 빌드해서 apk 로 발행해 줄려는데.. 설치가 되지 않는다..

 

그럴때는 싸인해서 apk 발행하면 된다.

Posted by thdeodls85
android,kotlin2023. 10. 23. 19:52

[kotlin]signingconfig with name not found android studio

 

다른 글에서 나와있는것처럼 했는데... 안된다..

 

그런데 너무 간단한거다...

 

signningConfigs 순서상 먼저 해주고 buildTypes 설정해야 "release" 찾을 수 있다. 

signingConfigs {
    create("release") {
        storePassword = ""
        keyAlias = ""
        keyPassword = ""
        storeFile = file("")
    }
}

하고나서

 buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )

            signingConfig = signingConfigs.getByName("release")
        }
    }
Posted by thdeodls85
android,kotlin2023. 10. 17. 14:38

[kotlin]Plugin [id: 'androidx.navigation.safeargs.kotlin'] was not found in any of the following sources 해결방법

 

safeargs 적용 시킬려고 봤더니..

 

[참조] https://jae-young.tistory.com/46

 

안드로이드(Android) - Navigation 사용법

[OverView] 이번에는 Navigation에 대해 알려드리겠습니다. Navigation는 iOS개발에서 사용하는 스토리보드와 흡사합니다. Fragment간의 플로우를 시각적으로 볼수 있다는 특징이 있어 앱 플로우를 보거나

jae-young.tistory.com

build.gradle  안에서 

plugins {
    id("com.android.application")
    id("org.jetbrains.kotlin.android")
    id("androidx.navigation.safeargs.kotlin")
}

dependencies {
    implementation("androidx.navigation:navigation-safe-args-gradle-plugin:2.7.4")
}

이렇게만 하면 될줄알았는데..

 

project.gradle 안에 넣어줘야 한다.

plugins {
    id("androidx.navigation.safeargs.kotlin") version "2.7.4" apply false
}

 

Posted by thdeodls85
android,kotlin2023. 10. 17. 13:52

[kotlin]Navigation action cannot be found from the current destination Destination 해결방법

 

navigation 쓰는데... destination 찾을수가 없단다..

 

분명 로직은 잘 된거라 생각했는데..

 

왜 그런가 했더니.. 연결을 잘못 해준거엿다..

 

내 경우를 설명들어..

 

tab 3개 있고... parent fragment 밑에 3개의 child fragment가 있는데.. 

 

child fragment -> child detail fragment로 이동시키기 위해 action을 했다... 그래서 위와같이 에러가 나왔다.

 

알고봤더니

 

parent 에서 child detail fragment 로 action 해야한다..

 

findNavController().currentDestination 

-> 디버깅 해보면 parent fragment로 되어있은걸 확인해볼 수 있다..
Posted by thdeodls85