CodingDevs / thermal_printer

MIT License
8 stars 10 forks source link

lateinit property bluetoothService has not been initialized #10

Open linussorg opened 4 months ago

linussorg commented 4 months ago

lateinit property bluetoothService has not been initialized

Whenever I try to use a Bluetooth printer (weirdly USB as well), I get this error message. I can confirm that the package worked with version 1.0.5 on this same device before. Bluetooth is enabled in the device settings.

In this case, this is what the output looks like when the scan() method is called.

E/MethodChannel#com.codingdevs.thermal_printer(15934): Failed to handle method call
E/MethodChannel#com.codingdevs.thermal_printer(15934): kotlin.UninitializedPropertyAccessException: lateinit property bluetoothService has not been initialized
E/MethodChannel#com.codingdevs.thermal_printer(15934):  at com.codingdevs.thermal_printer.ThermalPrinterPlugin.verifyIsBluetoothIsOn(ThermalPrinterPlugin.kt:307)
E/MethodChannel#com.codingdevs.thermal_printer(15934):  at com.codingdevs.thermal_printer.ThermalPrinterPlugin.onMethodCall(ThermalPrinterPlugin.kt:212)
E/MethodChannel#com.codingdevs.thermal_printer(15934):  at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:267)
E/MethodChannel#com.codingdevs.thermal_printer(15934):  at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295)
E/MethodChannel#com.codingdevs.thermal_printer(15934):  at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:322)
E/MethodChannel#com.codingdevs.thermal_printer(15934):  at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
E/MethodChannel#com.codingdevs.thermal_printer(15934):  at android.os.Handler.handleCallback(Handler.java:959)
E/MethodChannel#com.codingdevs.thermal_printer(15934):  at android.os.Handler.dispatchMessage(Handler.java:100)
E/MethodChannel#com.codingdevs.thermal_printer(15934):  at android.os.Looper.loopOnce(Looper.java:232)
E/MethodChannel#com.codingdevs.thermal_printer(15934):  at android.os.Looper.loop(Looper.java:317)
E/MethodChannel#com.codingdevs.thermal_printer(15934):  at android.app.ActivityThread.main(ActivityThread.java:8516)
E/MethodChannel#com.codingdevs.thermal_printer(15934):  at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#com.codingdevs.thermal_printer(15934):  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
E/MethodChannel#com.codingdevs.thermal_printer(15934):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:878)
E/flutter (15934): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(error, lateinit property bluetoothService has not been initialized, null, kotlin.UninitializedPropertyAccessException: lateinit property bluetoothService has not been initialized
E/flutter (15934):  at com.codingdevs.thermal_printer.ThermalPrinterPlugin.verifyIsBluetoothIsOn(ThermalPrinterPlugin.kt:307)
E/flutter (15934):  at com.codingdevs.thermal_printer.ThermalPrinterPlugin.onMethodCall(ThermalPrinterPlugin.kt:212)
E/flutter (15934):  at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:267)
E/flutter (15934):  at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295)
E/flutter (15934):  at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:322)
E/flutter (15934):  at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
E/flutter (15934):  at android.os.Handler.handleCallback(Handler.java:959)
E/flutter (15934):  at android.os.Handler.dispatchMessage(Handler.java:100)
E/flutter (15934):  at android.os.Looper.loopOnce(Looper.java:232)
E/flutter (15934):  at android.os.Looper.loop(Looper.java:317)
E/flutter (15934):  at android.app.ActivityThread.main(ActivityThread.java:8516)
E/flutter (15934):  at java.lang.reflect.Method.invoke(Native Method)
E/flutter (15934):  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
E/flutter (15934):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:878)
E/flutter (15934): )
E/flutter (15934): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:651:7)
E/flutter (15934): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:322:18)
E/flutter (15934): <asynchronous suspension>
E/flutter (15934): 
raudinm commented 4 months ago

I'm getting the same error, with SDK version 34

Flutter version: 3.19.0 Dart version: 3.3.0

E/MethodChannel#com.codingdevs.thermal_printer(22675): Failed to handle method call E/MethodChannel#com.codingdevs.thermal_printer(22675): kotlin.UninitializedPropertyAccessException: lateinit property bluetoothService has not been initialized E/MethodChannel#com.codingdevs.thermal_printer(22675): at com.codingdevs.thermal_printer.ThermalPrinterPlugin.verifyIsBluetoothIsOn(ThermalPrinterPlugin.kt:307) E/MethodChannel#com.codingdevs.thermal_printer(22675): at com.codingdevs.thermal_printer.ThermalPrinterPlugin.onMethodCall(ThermalPrinterPlugin.kt:221) E/MethodChannel#com.codingdevs.thermal_printer(22675): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:267) E/MethodChannel#com.codingdevs.thermal_printer(22675): at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:292) E/MethodChannel#com.codingdevs.thermal_printer(22675): at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:319) E/MethodChannel#com.codingdevs.thermal_printer(22675): at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12) E/MethodChannel#com.codingdevs.thermal_printer(22675): at android.os.Handler.handleCallback(Handler.java:958) E/MethodChannel#com.codingdevs.thermal_printer(22675): at android.os.Handler.dispatchMessage(Handler.java:99) E/MethodChannel#com.codingdevs.thermal_printer(22675): at android.os.Looper.loopOnce(Looper.java:205) E/MethodChannel#com.codingdevs.thermal_printer(22675): at android.os.Looper.loop(Looper.java:294) E/MethodChannel#com.codingdevs.thermal_printer(22675): at android.app.ActivityThread.main(ActivityThread.java:8177) E/MethodChannel#com.codingdevs.thermal_printer(22675): at java.lang.reflect.Method.invoke(Native Method) E/MethodChannel#com.codingdevs.thermal_printer(22675): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552) E/MethodChannel#com.codingdevs.thermal_printer(22675): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971) I/flutter (22675): ----------------FIREBASE CRASHLYTICS---------------- I/flutter (22675): PlatformException(error, lateinit property bluetoothService has not been initialized, null, kotlin.UninitializedPropertyAccessException: lateinit property bluetoothService has not been initialized I/flutter (22675): at com.codingdevs.thermal_printer.ThermalPrinterPlugin.verifyIsBluetoothIsOn(ThermalPrinterPlugin.kt:307) I/flutter (22675): at com.codingdevs.thermal_printer.ThermalPrinterPlugin.onMethodCall(ThermalPrinterPlugin.kt:221) I/flutter (22675): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:267) I/flutter (22675): at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:292) I/flutter (22675): at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:319) I/flutter (22675): at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12) I/flutter (22675): at android.os.Handler.handleCallback(Handler.java:958) I/flutter (22675): at android.os.Handler.dispatchMessage(Handler.java:99) I/flutter (22675): at android.os.Looper.loopOnce(Looper.java:205) I/flutter (22675): at android.os. I/flutter (22675): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:651:7) I/flutter (22675): #1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:334:18) I/flutter (22675): I/flutter (22675): ----------------------------------------------------

linussorg commented 3 months ago

Possible issue

I discovered that the issue has something to do with the Android target SDK version. Because of previous issues, I had a segment inside my android/build.gradle that sets the compileSdkVersion and targetSdkVersion for every submodule (See the // RELEVANT SECTION START/END comments):

subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
    afterEvaluate { project ->
        if (it.hasProperty('android')) {

            if (it.android.namespace == null) {
                def manifest = new XmlSlurper().parse(file(it.android.sourceSets.main.manifest.srcFile))
                def packageName = manifest.@package.text()
                println("Setting ${packageName} as android namespace")
                android.namespace = packageName
            }

            def javaVersion = JavaVersion.VERSION_17
            android {
                // RELEVANT SECTION START
                def androidApiVersion = 34
                compileSdkVersion androidApiVersion
                defaultConfig {
                    targetSdkVersion androidApiVersion
                }
                // RELEVANT SECTION END
                compileOptions {
                    sourceCompatibility javaVersion
                    targetCompatibility javaVersion
                }
                tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
                    kotlinOptions {
                        jvmTarget = javaVersion.toString()
                    }
                }
                println("Setting java version to ${javaVersion.toString()} which is $javaVersion")
                println("Setting compileSdkVersion and targetSdkVersion to $androidApiVersion")
            }
        }
    }

    project.evaluationDependsOn(':app')
}

Solution

There seems to currently be a problem with targeting SDK version 34. Here's my new configuration:

compileSdkVersion 34
defaultConfig {
    if (project.name != "thermal_printer") {
        targetSdkVersion 34
    } else {
        targetSdkVersion 33
    }
}

Conclusion

The Target SDK version of the thermal_printer package should not be set to 34, while the Compile SDK Version can.

Does that fix it for you as well, @raudinm ? And @rahuldevgarg, please take a look at it. Thank you!

raudinm commented 3 months ago

Hey @linussorg, thanks for sharing!, I tried to implement your solution, and after enabling bluetooth permissions for SDK version 34, I got this error:

E/MethodChannel#com.codingdevs.thermal_printer(11180): Failed to handle method call E/MethodChannel#com.codingdevs.thermal_printer(11180): java.lang.NullPointerException E/MethodChannel#com.codingdevs.thermal_printer(11180): at com.codingdevs.thermal_printer.ThermalPrinterPlugin.checkPermissions(ThermalPrinterPlugin.kt:384) E/MethodChannel#com.codingdevs.thermal_printer(11180): at com.codingdevs.thermal_printer.ThermalPrinterPlugin.verifyIsBluetoothIsOn(ThermalPrinterPlugin.kt:306) E/MethodChannel#com.codingdevs.thermal_printer(11180): at com.codingdevs.thermal_printer.ThermalPrinterPlugin.onMethodCall(ThermalPrinterPlugin.kt:221) E/MethodChannel#com.codingdevs.thermal_printer(11180): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:267) E/MethodChannel#com.codingdevs.thermal_printer(11180): at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:292) E/MethodChannel#com.codingdevs.thermal_printer(11180): at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:319) E/MethodChannel#com.codingdevs.thermal_printer(11180): at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12) E/MethodChannel#com.codingdevs.thermal_printer(11180): at android.os.Handler.handleCallback(Handler.java:958) E/MethodChannel#com.codingdevs.thermal_printer(11180): at android.os.Handler.dispatchMessage(Handler.java:99) E/MethodChannel#com.codingdevs.thermal_printer(11180): at android.os.Looper.loopOnce(Looper.java:205) E/MethodChannel#com.codingdevs.thermal_printer(11180): at android.os.Looper.loop(Looper.java:294) E/MethodChannel#com.codingdevs.thermal_printer(11180): at android.app.ActivityThread.main(ActivityThread.java:8177) E/MethodChannel#com.codingdevs.thermal_printer(11180): at java.lang.reflect.Method.invoke(Native Method) E/MethodChannel#com.codingdevs.thermal_printer(11180): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552) E/MethodChannel#com.codingdevs.thermal_printer(11180): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971) I/flutter (11180): ----------------FIREBASE CRASHLYTICS---------------- I/flutter (11180): PlatformException(error, null, null, java.lang.NullPointerException I/flutter (11180): at com.codingdevs.thermal_printer.ThermalPrinterPlugin.checkPermissions(ThermalPrinterPlugin.kt:384) I/flutter (11180): at com.codingdevs.thermal_printer.ThermalPrinterPlugin.verifyIsBluetoothIsOn(ThermalPrinterPlugin.kt:306) I/flutter (11180): at com.codingdevs.thermal_printer.ThermalPrinterPlugin.onMethodCall(ThermalPrinterPlugin.kt:221) I/flutter (11180): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:267) I/flutter (11180): at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:292) I/flutter (11180): at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:319) I/flutter (11180): at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12) I/flutter (11180): at android.os.Handler.handleCallback(Handler.java:958) I/flutter (11180): at android.os.Handler.dispatchMessage(Handler.java:99) I/flutter (11180): at android.os.Looper.loopOnce(Looper.java:205) I/flutter (11180): at android.os.Looper.loop(Looper.java:294 I/flutter (11180): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:651:7) I/flutter (11180): #1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:334:18) I/flutter (11180): I/flutter (11180): ----------------------------------------------------