Android Question Suddenly google code scanner (based on ML kit) not working

The ML kit based Google Scanner was working but no longer works. I haven't changed any thing in code. When the 'button' is tapped, brief black screen appears and app goes back to main page. No discernible error is shown. How to rectify it? This is the log: Is it got something to do with 'Transport backend 'cct' is not registered' ? Also what are all those strange com.hunantv.imgo.activity, com.tencent.qqlive, com.qiyi.video, com.hunantv.imgo.activity.inter, com.tencent.qqlivei18n, com.iqiyi.i18n, tv.danmaku.bili] ?
 
Fresh download and running of code too doesn't work. The complete log is below:

 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
com.hunantv.imgo.activity, com.tencent.qqlive, com.qiyi.video, com.hunantv.imgo.activity.inter, com.tencent.qqlivei18n, com.iqiyi.i18n, tv.danmaku.bili
You are seeing the system logs. It includes the output of all apps.

Change the scan method code to:
B4X:
Public Sub Scan (Formats As List) As ResumableSub
    Dim builder As JavaObject
    builder.InitializeNewInstance("com/google/mlkit/vision/codescanner/GmsBarcodeScannerOptions.Builder".Replace("/", "."), Null)
    Dim f(Formats.Size - 1) As Int
    For i = 1 To Formats.Size - 1
        f(i - 1) = Formats.Get(i)
    Next
    builder.RunMethod("setBarcodeFormats", Array(Formats.Get(0), f))
'    builder.RunMethod("enableAutoZoom", Null)
    Dim options As JavaObject = builder.RunMethod("build", Null)
    Dim scanning As JavaObject
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    Dim scanner As JavaObject = scanning.InitializeStatic("com/google/mlkit/vision/codescanner/GmsBarcodeScanning".Replace("/", ".")).RunMethod("getClient", Array(ctxt, options))
    Dim o As JavaObject = scanner.RunMethod("startScan", Null)
    Do While o.RunMethod("isComplete", Null).As(Boolean) = False
        Sleep(50)
    Loop
    Dim res As ScannerResult
    res.Initialize
    If o.RunMethod("isSuccessful", Null) Then
        res.Success = True
        res.Barcode = o.RunMethod("getResult", Null)
        res.Value = res.Barcode.RunMethod("getRawValue", Null)
    Else
        Log("error: " & o.RunMethod("getException", Null))
    End If
    Return res
End Sub

Post the filtered logs.
 
Upvote 0
Thanks for looking into the issue. The filtered Log after changing code:

 
Upvote 0
Upvote 0
If you have another device to test then try it.

Deleting Google Play Services cache might help. Also try to restart the device.
Interestingly, it works in older phone! First time it shows error as it apparently downloads 'Barcode UI module' and then it starts working.
Log:
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
Looks like a bug in the SDK itself. Try to change the package name and see if it starts working.
@Erel, I know that this is an old thread but nevertheless - might be a solution for the problem.

I have had the same problem and have tried everything from uninstalling the app, change the package name, clear the Google Play Services cache, change barcode_ui to barcode in the manifest, etc but just could not get the scanner to start. Always gave the error "scanner not found" (...or something like that). I came across some Java code on the web and added it as inline Java code that are called from the Initialize method in the B4A class GoogleCodeScanner. The inline Java code apparently requests GPS for an urgent install of the dependency.

My hack of it is VERY UNTIDY but it did the trick. After days of trying everything else the scanner is now working on first run of the B4A project. Perhaps you can add it to your class via Java Object or fix up my hack of the inline Java Code.

Project attached.

My hack:

Initialize of class GoogleCodeScanner:
Public Sub Initialize
    Dim m As JavaObject
    m = Me
    m.RunMethod("onScanButtonClicked", Null)

My hack of the inline Java Code:

Force an install via inline Java code:
#if Java

BA ba;

import com.google.android.gms.common.moduleinstall.InstallStatusListener;
import com.google.android.gms.common.moduleinstall.ModuleInstallStatusUpdate;
import com.google.android.gms.common.moduleinstall.ModuleInstallClient;
import com.google.android.gms.common.moduleinstall.ModuleInstall;
import com.google.android.gms.common.moduleinstall.ModuleInstallRequest;
import com.google.mlkit.vision.codescanner.GmsBarcodeScannerOptions;
import com.google.mlkit.vision.codescanner.GmsBarcodeScanning;
import com.google.mlkit.vision.codescanner.GmsBarcodeScanner;
import com.google.android.gms.common.api.OptionalModuleApi;

import com.google.android.gms.tasks.Task;

import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;

ModuleInstallClient moduleInstallClient;

public void onScanButtonClicked() {

    GmsBarcodeScannerOptions.Builder optionsBuilder = new GmsBarcodeScannerOptions.Builder();

    moduleInstallClient = ModuleInstall.getClient(ba.applicationContext);

    OptionalModuleApi optionalModuleApi = GmsBarcodeScanning.getClient(ba.applicationContext);
    moduleInstallClient
            .areModulesAvailable(optionalModuleApi);
   
    moduleInstall();
}

final class ModuleInstallProgressListener implements InstallStatusListener {
    @Override
    public void onInstallStatusUpdated(ModuleInstallStatusUpdate update) {
        ModuleInstallStatusUpdate.ProgressInfo progressInfo = update.getProgressInfo();
        // Progress info is only set when modules are in the progress of downloading.
        if (progressInfo != null) {
            int progress =
                    (int) (progressInfo.getBytesDownloaded() * 100 / progressInfo.getTotalBytesToDownload());
            // Set the progress for the progress bar.
            BA.Log("" + progress);
        }
        // Handle failure status maybe…

        // Unregister listener when there are no more install status updates.
        if (isTerminateState(update.getInstallState())) {

            moduleInstallClient.unregisterListener(this);
        }
    }

    public boolean isTerminateState(@ModuleInstallStatusUpdate.InstallState int state) {
        return  true;    //state == STATE_CANCELED || state == STATE_COMPLETED || state == STATE_FAILED;
    }
}

private void moduleInstall(){
    InstallStatusListener listener = new ModuleInstallProgressListener();

    OptionalModuleApi optionalModuleApi = GmsBarcodeScanning.getClient(ba.applicationContext);
    ModuleInstallRequest moduleInstallRequest =
            ModuleInstallRequest.newBuilder()
                    .addApi(optionalModuleApi)
                    // Add more API if you would like to request multiple optional modules
                    //.addApi(...)
                    // Set the listener if you need to monitor the download progress
                    .setListener(listener)
                    .build();

    moduleInstallClient.installModules(moduleInstallRequest)
            /*.addOnSuccessListener(
                    response -> {
                        if (response.areModulesAlreadyInstalled()) {
                            // Modules are already installed when the request is sent.
                            BA.Log("Modules are already installed when the request is sent.");
                        }
                    })
            .addOnFailureListener(
                    e -> {
                        // Handle failure...
                        BA.Log("" + e);
                    })*/;

}
#End If
 

Attachments

  • BarcodeScannerModified.zip
    12.7 KB · Views: 5
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…