1

I wrote this simple code in Java for Android to connect to KNX bus over Ethernet (WiFi). Unfortunately not working

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
 }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
protected void onStart() {

    super.onStart();
    /*Pokus pro spojeni*/
    setContentView(R.layout.activity_main); 
    Button actButton = (Button) findViewById(R.id.button1); 
    int a=1;
    actButton.setText("1234" + Integer.toString(a));
    new NewComm().execute("Sending.....");
    Log.i ("MyTag","Starting.....");
  }
}

final  class NewComm extends AsyncTask<String, Void, Void>{

@Override
protected Void doInBackground(String... text) {

            String Enable = "Enalbe";
    String Disable = "Disable";
    String Address = "1/5/217";
    final CEMI_Connection tunnel;
    try {
        /* Create a new connection using CEMI_Connection
         *  to KNXnet/IP interface (in this case the ip
         *  192.168.1.3), with the default port
        */

        System.out.println("vsm> Connecting.....");
        tunnel = new CEMI_Connection(
                new InetSocketAddress("192.168.0.110",
                        EIBNETIP_Constants.EIBNETIP_PORT_NUMBER),
                        new TunnellingConnectionType());

        /* Just saying that it connected. In case of an error,
         *              
         *              
         * this is not executed, so you know it was unable to connect
         */
        System.out.println("Connected.");
        /* Create the knx device (a port from a relay for example)             
         *   and declare it as a boolean device, and it's a Enable/Disable             
         *   device. In the API docs there are all kinds avaliable 
        */

        PointPDUXlator object = PDUXlatorList.getPointPDUXlator(
                PDUXlatorList.TYPE_BOOLEAN[0],
                PointPDUXlator_Boolean.DPT_ENABLE[0]);

        /* Declare that we are going to write some info in the "Address"
         *              *  and this info is writed in the string Enable. To disable just
         *                            *  change the variable             
         */  

        object.setServiceType(PointPDUXlator.A_GROUPVALUE_WRITE);            
        object.setASDUfromString(Enable);

        /* Create the message to be sent to device
         * The device KNX address was declared in the begining
        */ 

        CEMI_L_DATA message = new CEMI_L_DATA(
                CEMI_L_DATA.MC_L_DATAREQ,
                new EIB_Address(),
                new EIB_Address(Address),
                object.getAPDUByteArray());
        /* Send the message using the connection "tunnel"
        */            
        tunnel.sendFrame(message, CEMI_Connection.WAIT_FOR_CONFIRM);

        /* Print that the message was sent
         */
        System.out.println("Message Sent to " + Address + ": " + Enable);

        /* Disconnect from the device and print it to the screen             
         * If your device can handle more than 1 Tunneling connection you             
         * can let it connected, but in my case I use different devices             
         * to control it, so I always have to disconnect after doing
         * anything             
         */ 

        tunnel.disconnect(null);
        System.out.println("Disconnected.");
        }        catch (EICLException ex) {
                ex.printStackTrace();
                System.out.println("vsm> Connection exception! "+ex.getMessage());
                    }  // connection error
    return null;

}


}

This code can not run on my android mobile phone. If I run it the program failed with message "application stop working...." on my android mobile phone. But on PC the similar code (content of doInBackground() ) working good. Working good it mean that I am able to receive KNX connecting UDP packet on another computer.

On Android device I can send simple UDP packet over wifi - that mean wifi connection and basic code in Android Java is working.

Can anybody experienced examine my code and give me a advice where is the problem? Is part of code with calimero API using correct? It was vary helpful for me. Can Calimero api works in android OS? Does anybody have experiences with that (I think so, according to posts here in forum).

LogCat content:

12-01 07:48:03.452: E/dalvikvm(15234): Could not find class 'tuwien.auto.eicl.CEMI_Connection', referenced from method com.example.testapp.NewComm.doInBackground

12-01 07:48:03.452: W/dalvikvm(15234): VFY: unable to resolve new-instance 550 (Ltuwien/auto/eicl/CEMI_Connection;) in Lcom/example/testapp/NewComm;

12-01 07:48:03.452: D/dalvikvm(15234): VFY: replacing opcode 0x22 at 0x000e

12-01 07:48:03.452: W/dalvikvm(15234): VFY: unable to resolve exception class 555 
(Ltuwien/auto/eicl/util/EICLException;)

12-01 07:48:03.452: W/dalvikvm(15234): VFY: unable to find exception handler at addr 0x80

12-01 07:48:03.452: W/dalvikvm(15234): VFY:  rejected Lcom/example/testapp/NewComm;.doInBackground ([Ljava/lang/String;)Ljava/lang/Void;

12-01 07:48:03.452: W/dalvikvm(15234): VFY:  rejecting opcode 0x0d at 0x0080

12-01 07:48:03.452: W/dalvikvm(15234): VFY:  rejected Lcom/example/testapp/NewComm;.doInBackground ([Ljava/lang/String;)Ljava/lang/Void;

12-01 07:48:03.462: W/dalvikvm(15234): Verifier rejected class Lcom/example/testapp/NewComm;

12-01 07:48:03.462: D/AndroidRuntime(15234): Shutting down VM
12-01 07:48:03.462: W/dalvikvm(15234): threadid=1: thread exiting with uncaught exception (group=0x40a95228)

12-01 07:48:03.472: E/AndroidRuntime(15234): FATAL EXCEPTION: main

12-01 07:48:03.472: E/AndroidRuntime(15234): java.lang.VerifyError: com/example/testapp/NewComm

12-01 07:48:03.472: E/AndroidRuntime(15234):    at com.example.testapp.MainActivity.onStart(MainActivity.java:48)

12-01 07:48:03.472: E/AndroidRuntime(15234):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1195)

12-01 07:48:03.472: E/AndroidRuntime(15234):    at android.app.Activity.performStart(Activity.java:4548)

12-01 07:48:03.472: E/AndroidRuntime(15234):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2178)

12-01 07:48:03.472: E/AndroidRuntime(15234):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)

12-01 07:48:03.472: E/AndroidRuntime(15234):    at android.app.ActivityThread.access$600(ActivityThread.java:139)

12-01 07:48:03.472: E/AndroidRuntime(15234):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)

12-01 07:48:03.472: E/AndroidRuntime(15234):    at android.os.Handler.dispatchMessage(Handler.java:99)

12-01 07:48:03.472: E/AndroidRuntime(15234):    at android.os.Looper.loop(Looper.java:156)

12-01 07:48:03.472: E/AndroidRuntime(15234):    at android.app.ActivityThread.main(ActivityThread.java:4977)

12-01 07:48:03.472: E/AndroidRuntime(15234):    at java.lang.reflect.Method.invokeNative(Native Method)

12-01 07:48:03.472: E/AndroidRuntime(15234):    at java.lang.reflect.Method.invoke(Method.java:511)

12-01 07:48:03.472: E/AndroidRuntime(15234):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)

12-01 07:48:03.472: E/AndroidRuntime(15234):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)

12-01 07:48:03.472: E/AndroidRuntime(15234):    at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

1

同样的问题也发生在我身上。您需要检查您导入到项目中的 jar 文件,您可以从:YourProjectName(右键单击) -> 属性 -> Java BuildPath -> 排序和导出

于 2013-03-25T14:11:04.293 回答