onbon bx06 api for Android

Build Status Codacy Badge

Chinese

This document describes how to create a project of onbon.bx06 api in Android Studio IDE.

Test target of github source is BX-6Q2. Test information always be showed at fixed position (96,0) with fixed size (32,32).

Sample

dependency files

JAR - onbon bx06 api Java Libraries (7/10)

AAR - onbon bx06 api on Android devcie Libraries (Android only)

project configuration

build.gradle

allprojects {
    repositories {
        jcenter()
        flatDir {
            dirs 'libs'
        }
    }
}

app configuration

build.gradle

android {
    ...
    defaultConfig {
        ...
        project.ext.set("archivesBaseName", "bx6g.mobiledemo-" + defaultConfig.versionName);
    }
    dexOptions {
        preDexLibraries = false
        additionalParameters =["--core-library"]
    }

}

dependencies {
    ...
    compile files('libs/bx06.message-0.5.0-SNAPSHOT.jar')
    compile files('libs/bx06-0.5.0-SNAPSHOT.jar')
    compile files('libs/log4j-1.2.14.jar')
    compile files('libs/simple-xml-2.7.1.jar')
    compile files('libs/uia.comm-0.2.1.jar')
    compile files('libs/uia.message-0.5.1.jar')
    compile files('libs/uia.utils-0.1.2.jar')
    compile(name:'java.awt4a-0.1-release', ext:'aar')
    ...
}

AndroidManifest.xml

<application
    android:name="onbon.bx06.mobiledemo.MainApplication"
    ...>
</application>
<uses-permission android:name="android.permission.INTERNET" />

MainApplication.java

Implementation of class which defines at android:name in AndroidManifest.xml.

package onbon.bx06.mobiledemo;

import android.app.Application;

import j2a.awt.AwtEnv;
import onbon.bx06.Bx6GEnv;

public class MainApplication extends Application {

    private boolean initial;

    @Override
    public void onCreate() {
        super.onCreate();

        try {
            // java.awt for android initial
            AwtEnv.link(this);                          // link application and AWT
            AwtEnv.configPaintAntiAliasFlag(true);      // setup anti-alias flag.

            // BX6G API initial
            Bx6GEnv.initial();

            this.initial = true;
        }
        catch (Exception ex) {
            this.initial = false;
        }
    }
}

Develop

Screen operation

Because socket client can’t be run on UI thread, all operations execute to the screen need to be run in a new thread.

new Thread(new Runnable() {
    public void run() {
        // operate to screen
    }
}).start();

UI Update

After executing to the screen on non UI thread, use runOnUiThread to callback to UI thread.

runOnUiThread(new Runnable() {
    public void run() {
        // update UI
    }
});