我用Java for Android编写了这段简单的代码来连接到KNX bus over Ethernet (WiFi)。不幸的是不能工作
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;
}
}这段代码不能在我的android手机上运行。如果我运行它,程序会失败,并显示消息"application stop working....“在我的android手机上。但在PC上,类似的代码( doInBackground()的内容)运行良好。工作良好意味着我可以在另一台计算机上接收到KNX连接UDP数据包。
在Android设备上,我可以通过wifi发送简单的UDP数据包-这意味着wifi连接和Android Java中的基本代码正在工作。
有经验的人能检查我的代码并给我建议吗?问题出在哪里?使用calimero API的部分代码是否正确?这对我很有帮助。Calimero api能在android操作系统中工作吗?有没有人有这方面的经验(根据论坛上的帖子,我想是的)。
LogCat内容:
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)发布于 2013-03-25 22:11:04
同样的问题也发生在我身上。您需要检查导入到项目中的jar文件,可以从以下位置执行此操作:YourProjectName(右键单击) -> Properties -> Java BuildPath -> Order -> export
https://stackoverflow.com/questions/13657256
复制相似问题