Search code examples
androidkotlinonvif

Android - How to implement external library with a login form


I'm learning to develop android app using kotlin. I have an app written using web technologies and I want to port it on android. I've created a simple layout where I have some input field that are needed to connect to the IP cam I need to display.

I've found this repo on github and I'm trying to implement it in my app but I don't know how to proceed.

How I can correctly implement the library I need and how I pass the data from the layout to the main file of the app so I can connect when the button is clicked? I want to create something similar to the screen of the repository I'm using to connect to the ip cam.

I've added the gradle plugin in my build.gradle file

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

android {
    compileSdk 31

    defaultConfig {
        applicationId "com.example.onvif_camview"
        minSdk 19
        targetSdk 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'com.squareup.okhttp3:okhttp:3.10.0'
    implementation 'com.rvirin.onvif:onvifcamera:1.1.6'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

In my MainActivity.kt file I have added the needed code. but I get some error notice Unresolved reference: OnvifDevice

package com.example.onvif_camview

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        currentDevice = OnvifDevice("IP_ADDRESS:PORT", "login", "pwd")
        currentDevice.listener = this
        currentDevice.getDeviceInformation()
    }
    // Called by the SDK each time a request is performed on the camera, when the result is parsed
    override fun requestPerformed(response: OnvifResponse) {
        Log.d("ONVIF", "Request ${response.request.type} performed.")
        Log.d("ONVIF","Succeeded: ${response.success},
                message: ${response.parsingUIMessage}")

        if (response.request.type == GetDeviceInformation) {
            currentDevice.getProfiles()

        } else if (response.request.type == GetProfiles) {
            currentDevice.getStreamURI()

        } else if (response.request.type == GetStreamURI) {
            Log.d("ONVIF", "Stream URI retrieved: ${currentDevice.rtspURI}")
        }
    }

}

in my layout I have this code

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Username"
        android:inputType="textPersonName"
        android:text="Username"
        tools:layout_editor_absoluteX="91dp"
        tools:layout_editor_absoluteY="189dp" />

    <EditText
        android:id="@+id/IP_ADDRESS:PORT"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="IP_ADDRESS"
        android:inputType="textPersonName"
        android:text="Indirizzo IP"
        tools:layout_editor_absoluteX="91dp"
        tools:layout_editor_absoluteY="129dp" />

    <EditText
        android:id="@+id/pwd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Password"
        android:inputType="textPersonName"
        android:text="Password"
        tools:layout_editor_absoluteX="92dp"
        tools:layout_editor_absoluteY="252dp" />

    <Button
        android:id="@+id/button"
        android:layout_width="209dp"
        android:layout_height="43dp"
        android:text="CONNETTI"
        tools:layout_editor_absoluteX="93dp"
        tools:layout_editor_absoluteY="314dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

Solution

  • place cursor somewhere in the middle of OnvifDevice(... and some hint should appear allowing importing dependency. you can press Alt+Enter for autoimport then

    also you haven't implemented needed interface OnvifListener and didn't declared onvifDevice anywhere...

    class MainActivity : AppCompatActivity(), OnvifListener {
    
        lateinit onvifDevice:OnvifDevice
    
        override fun onCreate(savedInstanceState: Bundle?) {
            ... rest of code
    

    it should also be imported as above