Version 1 of AndroWish: create "borg broadcast ..." following Java Code

Updated 2019-04-12 06:55:53 by oehhar

HaO 2019-04-08: Aim of this page is to show the translation of Java code to AndroWish borg broadcast... I have no idea about Java. This page may be evident to Java/Android insiders. For me, it was 3 days fiddelling and 2 E-Mails from Christian (thanks).

Application

The application was to receive Barcode data from a Honeywell EDA50k0 with Android 7.1.1. The bar code scanner is activated by a scan button and returns data to the application on a succesful decode.

Send Broadcast

The first functionality is to send a broadcast.

The Java code is as follows:

    private static final String ACTION_CLAIM_SCANNER =
        "com.honeywell.aidc.action.ACTION_CLAIM_SCANNER";
    private static final String EXTRA_PROFILE =
        "com.honeywell.aidc.extra.EXTRA_PROFILE";
    private static final String EXTRA_PROPERTIES =
        "com.honeywell.aidc.extra.EXTRA_PROPERTIES";
    private static final String ACTION_BARCODE_DATA =
        "com.honeywell.sample.action.BARCODE_DATA";
    ...
    private void claimScanner() {
        Bundle properties = new Bundle();
        properties.putBoolean("DPR_DATA_INTENT", true);
        properties.putString("DPR_DATA_INTENT_ACTION",
            ACTION_BARCODE_DATA);
        sendBroadcast(new Intent(ACTION_CLAIM_SCANNER)
            .putExtra(EXTRA_PROFILE, "MyProfile1")
            .putExtra(EXTRA_PROPERTIES, properties)
            );
    }

The important command is "sendBroadcast". The corresponding TCL borg command is:

borg broadcast send action ?uri type categories arguments?

with the following parameters:

  • action: the action string. In this case: "com.honeywell.aidc.action.ACTION_CLAIM_SCANNER"
  • uri, type, categories: additional action parameters. In this case, they are all empty (was written in the device documentation).
  • arguments: discussed below

Arguments

The argument is documented within the documentation of "borg activity" as follows:

arguments are key-value pairs where the values are mapped to Java strings by default. If the key is a 2-element list made up of a data type indicator (int, byte, short, char, long, float, double, Uri) followed by the key, the value is converted to that data type. See below for some examples of this command.

Within Java, this is the part of the "sendBroadcast" with ".putsExtra(argkey, argval)".

If "argVal" is not of Java types string, int, byte, short, char, long, float, double, Uri, it is not supported.

We have the following commands in the example:

String argument

    private static final String EXTRA_PROFILE =
        "com.honeywell.aidc.extra.EXTRA_PROFILE";
    ...
            .putExtra(EXTRA_PROFILE, "MyProfile1")

In this case, "MyProfile1" is a string. This may be implemented by an argument list of: "com.honeywell.aidc.extra.EXTRA_PROFILE" "MyProfile1".

Bundle argument

    private static final String EXTRA_PROPERTIES =
        "com.honeywell.aidc.extra.EXTRA_PROPERTIES";
    private static final String ACTION_BARCODE_DATA =
        "com.honeywell.sample.action.BARCODE_DATA";
    ...
        Bundle properties = new Bundle();
        properties.putBoolean("DPR_DATA_INTENT", true);
        properties.putString("DPR_DATA_INTENT_ACTION",
            ACTION_BARCODE_DATA);
        ...
            .putExtra(EXTRA_PROPERTIES, properties)

The variable "properties" is of type "Bundle". This is not supported by AndroWish. Christian sees the possibility to directly include Java with TCLJBlend requiring the current trunk.

I decided not to include this parameter and work around it by configuration.

Resulting Broadcast code

The final broadcast call is as follows:

borg broadcast send com.honeywell.aidc.action.ACTION_CLAIM_SCANNER {} {} {}\
        [list com.honeywell.aidc.extra.EXTRA_PROFILE MyProfile1]

The following message gets written to the system log:

04-08 10:07:40.671  2877  2877 I DCS-IntentApi: onReceive, action: com.honeywell.aidc.action.ACTION_CLAIM_SCANNER
...
04-08 10:07:40.674  2877  2877 I DCS-IntentApi: setProfile: MyProfile1

When the broadcast is executed (data scanned), the following message is written to the Android log:

04-08 10:14:16.154  1698  2860 E ActivityManager: Sending non-protected broadcast com.honeywell.aidc.extra.EXTRA_PROPERTIES from system 2877:

Receive Broadcast

The second function is to receive data from a broadcast sent by the bar code scanner, e.g. the bar code data when scanned. It is composed of two parts, the setup and the called routine. Both are present in Java and TCL.

Setup Broadcast

The java code is as follows:

    private static final String ACTION_BARCODE_DATA =
        "com.honeywell.sample.action.BARCODE_DATA";

    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver(barcodeDataReceiver,
            new IntentFilter(ACTION_BARCODE_DATA));
    }

The key command within Java is "registerReceiver(barcodeDataReceiver, new IntentFilter(ACTION_BARCODE_DATA));". The intent name and handling function is passed.

The corresponding AndroWish borg command is:

borg broadcast register action cmd

The parameters are:

  • action: the Android action string. This is passed to "IntentFilter" and may also be seen in the Android log after: "ActivityManager: Sending non-protected broadcast".
  • cmd: a TCL command (or prefix) which is called when the broadcast is received.

In this case, the command is:

borg broadcast register com.honeywell.sample.action.BARCODE_DATA callback

When registered, the Android log contains this line:

04-08 10:14:16.171  9041  9041 V AndroWish: nativeBroadcastCallback: -1,com.honeywell.aidc.action.ACTION_CLAIM_SCANNER,null,null,null,[Ljava.lang.String;@22cd7a8

Broadcast callback

The java code is as follows:

    private BroadcastReceiver barcodeDataReceiver
        = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (ACTION_BARCODE_DATA.equals(intent.getAction())) {
                int version = intent.getIntExtra("version", 0);
                String data = intent.getStringExtra("data");
                ... (some additional values here)
            }
        }
    };

The TCL callback function is called with the following parameters:

proc callback {CodeIn ActionIn URIIn TypeIn CategoryIn lDataIn} {
    ...
}

In my case, the parameters are as follows:

  • CodeIn: Status code I suppose. Value is "-1".
  • ActionIn: The action string "com.honeywell.sample.action.BARCODE_DATA"
  • URIIn, TypeIn, CategoryIn: probably the parameters already given on registration. All empty strings in my case.
  • lDataIn: contains all data fields given by the Java intent.get<Type>Extra as an alternating list of key and value. My example is: "version 1 data {Scanned code data}".

TCL looks so easy compared to Java...

Unregister

When Android stops the application, the scanner claim should be stopped and also the bradcast notification:

borg broadcast unregister com.honeywell.sample.action.BARCODE_DATA

After a discussion on clt "Re: Androwish: when fires <<Terminating>> virtual event" started 2019-04-10:

  • when moving away from foreground, virtual event "<<WillEnterBackground>>" is called.
  • when pressing the "X" in the task view, wm protocol . WM_DELETE_WINDOW cmd" is called.
  • when the application is whiped-away from the task view, no information is given.
  • The <<Terminating>> virtual event is never called

The virtual event "<<DidEnterForeground>>" is called when the application is actively put into foreground but not on startup. Thus, scanning is activated on program startup and on virtual event "<<DidEnterForeground>>".

Scanning is stopped on virtual event "<<WillEnterBackground>>".