androiddelphidelphi-2010firemonkey-fm3

Delphi 10.2 Tokyo Android onActivityResult Sunmi Code Scanning


I need to integrate Code Scanning into the application (https://docs.sunmi.com/htmls/Scan%20code%20driver.html) The application was made in Delphi 10.2 Tokyo

When I press the button, I use this code and the scaner opens and reads the code

procedure TfSkenKarta.Button1Click(Sender: TObject);
  var
   Intent: JIntent;
   ReqCode : Integer;
begin
ReqCode := SCAN_REQUEST_CODE;
FMessageSubscriptionID := TMessageManager.DefaultManager.SubscribeToMessage(TMessageResultNotification, HandleActivityMessage);

Intent := TJIntent.Create;
Intent.setPackage(StringToJString('com.sunmi.sunmiqrcodescanner')); 
Intent.setClassName(StringToJString('com.sunmi.sunmiqrcodescanner'),StringToJString('com.sunmi.sunmiqrcodescanner.activity.ScanActivity'));
Intent.putExtra(StringToJString('PLAY_SOUND'), true);
TAndroidHelper.Activity.startActivityForResult(Intent, SCAN_REQUEST_CODE);
end;

I'm using this data retrieval

procedure TfSkenKarta.HandleActivityMessage(const Sender: TObject; const M: TMessage);
begin
   if M is TMessageResultNotification then
    OnActivityResult(
      TMessageResultNotification(M).RequestCode,
      TMessageResultNotification(M).ResultCode,
      TMessageResultNotification(M).Value);
end;


function TfSkenKarta.OnActivityResult(RequestCode, ResultCode: Integer;
  Data: JIntent): Boolean;
var ret:JArrayList;
    LScanContent, LScanFormat: string;
begin
    Result := False;
   TMessageManager.DefaultManager.Unsubscribe(TMessageResultNotification, FMessageSubscriptionID);
   FMessageSubscriptionID := 0;

 if RequestCode = SCAN_REQUEST_CODE then
  begin
   if ResultCode = TJActivity.JavaClass.RESULT_OK then
    begin
      Result := True;
      if Assigned(Data) then
      begin
        LScanContent := JStringToString(Data.getStringExtra(StringToJString('VALUE')));
        LScanFormat := JStringToString(Data.getStringExtra(StringToJString('TYPE')));
        TThread.Synchronize(nil,
          procedure
          begin
             ShowmessageToast('tu sam TThreadTThread  :LScanContent:' +LScanContent + ' LScanFormat:: ' +  LScanFormat, TJToast.JavaClass.LENGTH_LONG);
          end
        );
      end;
    end

  end;

  end;

java example

/**

    * 

    *Creating a Intent at where you want start scanner, calling the scanner by startActiityForResult();

    */

    Intent intent = new Intent("com.summi.scan");

    intent.setPackage("com.sunmi.sunmiqrcodescanner");



    /**

    * The method is the same function as above 

    *Intent intent = new Intent("com.summi.scan");

    *intent.setClassName("com.sunmi.sunmiqrcodescanner", "com.sunmi.sunmiqrcodescanner.activity.ScanActivity");

    */


    /**


    //there is also some options item about the scanner module, you can transfer parameters to control some settings, each item has a defaut status,transform parameter is not necessary,

    intent.putExtra("CURRENT_PPI", 0X0003);//The current preview resolution ,PPI_1920_1080 = 0X0001;PPI_1280_720 = 0X0002;PPI_BEST = 0X0003;

    intent.putExtra("PLAY_SOUND", true);// Prompt tone after scanning  ,default true

    intent.putExtra("PLAY_VIBRATE", false);//vibrate after scanning,default false,only support M1 right now.

    intent.putExtra("IDENTIFY_INVERSE_QR_CODE", true);//Whether to identify inverse code

    intent.putExtra("IDENTIFY_MORE_CODE", false);// Whether to identify several code,default false        

    intent.putExtra("IS_SHOW_SETTING", true);// Wether display set up button  at the top-right corner,default true

    intent.putExtra("IS_SHOW_ALBUM", true);// Wether display album,default true

    */

    startActivityForResult(intent, START_SCAN);

/////

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                if (requestCode == 1 && data != null) {
                        Bundle bundle = data.getExtras();
                        ArrayList<HashMap<String, String>> result = (ArrayList<HashMap<String, String>>) bundle
                                        .getSerializable("data");

                        Iterator<HashMap<String, String>> it = result.iterator();

                        while (it.hasNext()) {
                                HashMap<String, String> hashMap = it.next();

                                Log.i("sunmi", hashMap.get("TYPE"));//this is the type of the code 
                                Log.i("sunmi", hashMap.get("VALUE"));//this is the result of the code 

                        }

                }
                super.onActivityResult(requestCode, resultCode, data);
        }

but I can not read the scanned code anyway. I'm getting my empty data back


Solution

  • Given the Java example, I've come up with the following:

    uses
      Androidapi.JNI.GraphicsContentViewText, Androidapi.JNI.JavaTypes, Androidapi.JNI.Os, Androidapi.Helpers;
    
    ...
    
    var
      LBundle: JBundle;
      LMap: JMap;
      LIterator: JIterator;
      LType, LValue: string;
    begin
      LBundle := Data.getExtras;
      LIterator := TJArrayList.Wrap(LBundle.getSerializable(StringToJString('data'))).iterator;
      while LIterator.hasNext do
      begin
        LMap := TJMap.Wrap(JObjectToID(LIterator.next));
        LType := JStringToString(TJString.Wrap(JObjectToID(LMap.get(StringToJString('TYPE')))));
        LValue := JStringToString(TJString.Wrap(JObjectToID(LMap.get(StringToJString('VALUE')))));
      end;
    end;
    

    Unfortunately, since I don't have a Sunmi I'm unable to test it, so please bear that in mind