这是我在这里发表的第一篇文章,如果我把它放在错误的部分,请原谅我。 对于我们的学校项目,我想通过Android平板电脑打开/关闭一扇门。为了与我们的门的ecu连接,我有一个来自avisaro的can-wlan模块。
问题是我不是编程Android应用程序的专家,我对此一无所知。
从一个前项目我得到了这个UDP THread(UDP_Verbindung ja va) 有人可以帮我把UDP线程与布局结合起来,你可以在这里看到吗?
CAN数据字节输出应如下所示:
按钮“Öffnen”(打开),13。byte:{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0}
按钮“Schließen”(关闭),14.byte:{0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0, 0,0,0,0,0,0,0,0,0,0,0}
按钮“STOP”,15。byte:{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0}
Progressbar(“Heckklappengeschwindigkeit”),16字节,值:50 ... 100
UDP_Verbindung.java:
public class UDP_Verbindung implements Runnable{
private static final String TAGUDP = "UDP";
final int UDP_Target_Port = 12345;
private Thread threadUDP = null;
private boolean isrunning = false;
private DatagramSocket udpSocket = null;
byte[] buffin = new byte[28];
byte[] buffout = {0, 0, 0, 0, 0, 0, 8, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
DatagramPacket in = new DatagramPacket(buffin,buffin.length);
DatagramPacket out = new DatagramPacket(buffout,buffout.length);
int CAN_ID;
int CAN_Length;
private int udpStatus = 0;
private Handler uiHandler = null;
private int sendFlag = 0;
String ip = "168.192.0.74";
InetAddress addr = intToInetAddress(ipStringToInt( "192.168.0.74"));
public int receive = 1;
private Set<String> listenToIds;
public UDP_Verbindung(Handler uiHandler) {
this.uiHandler = uiHandler;
listenToIds = Collections.<String>emptySet();
}
public void setMessages(Set<String> messages){
listenToIds = messages;
Log.d(TAGUDP, "New Message IDs to listen to: ");
for(String strId: listenToIds) {
Log.d(TAGUDP, strId);
}
}
@Override
public void run() {
try
{
Log.d(TAGUDP,"Verbindungsaufbau");
udpSocket = new DatagramSocket(UDP_Target_Port);
udpSocket.setSoTimeout( 3000 );
} catch (SocketException ex)
{
ex.printStackTrace();
Log.d(TAGUDP,"Verbindungsaufbau fehlgeschlagen");
this.stop_service();
}
udpStatus = 1;
Message msg1 = new Message();
Bundle b1 = new Bundle();
msg1.what = 1;
b1.putInt("udpStatus", udpStatus);
msg1.setData(b1);
uiHandler.sendMessage(msg1);
while (isrunning)
{
try
{
udpSocket.receive(in);
//Log.d(TAGUDP,"Data received:");
}
catch (IOException ex)
{
Log.d(TAGUDP,"hier??");
}
InetAddress senderIP = in.getAddress();
int senderPort = in.getPort();
//Log.d(TAGUDP, senderIP.toString() + ":" + Integer.toString(senderPort) );
buffin = in.getData();//---------------------Einlesen des Inhalts einer CAN Nachricht
/*Log.d(TAGUDP, "LOsgeths:");
for (int o = 0; o < 28; o++){
Log.d(TAGUDP, buffin[o] + " ");
}*/
//Log.d(TAGUDP, senderIP.toString() + ":" + Integer.toString(senderPort) );
CAN_ID = buffin[8] + buffin[9]*256;//----------Ermitteln der CAN ID einer CAN Nachricht
String CAN_ID_hex = Integer.toHexString(CAN_ID);
if(listenToIds.contains(CAN_ID_hex)){
Log.d(TAGUDP, "Neue CAN Nachricht mit der ID " + CAN_ID_hex);
CAN_Length = buffin[6] & 15;//---------------Ermitteln der Länge einer CAN Nachricht
//Log.d(TAGUDP,"ID: " + CAN_ID + " ");
byte[] canData = ne
w byte[CAN_Length];
for (int l = 0; l < CAN_Length; l++){
canData[l] = buffin[12+l];
//Log.d(TAGUDP, buffin[12+l] + " ");
//Log.d(TAGUDP, Integer.toString(byteToUnsignedInt(buffin[12+l])));
}
Message msg = new Message();
Bundle b = new Bundle();
msg.what = 2;
b.putString("CAN_ID", CAN_ID_hex);
b.putByteArray("CAN_DATA", canData);
b.putInt("CAN_LENGTH", CAN_Length);
msg.setData(b);
uiHandler.sendMessage(msg);
}
}
}
public static int byteToUnsignedInt(byte b) {
return (0x00 << 24 | b & 0xff);
}
public void start_Service(){
Log.d(TAGUDP, "Start Thread");
this.threadUDP = new Thread(this);
isrunning = true;
threadUDP.start();
}
public void stop_service() {
Log.d(TAGUDP, "Trenne Verbindung");
Log.d(TAGUDP, "Warte bis Thread durchgelaufen");
isrunning = false;
while(true) {
try {
if (threadUDP != null) {
threadUDP.join(500);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
}
Log.d(TAGUDP, "Thread durchgelaufen");
if (udpSocket != null)
{
Log.d(TAGUDP, "Schließe Socket");
udpSocket.close();
Log.d(TAGUDP, "geschlossen");
}
threadUDP = null;
Log.d(TAGUDP, "UDP Thread geschlossen");
udpStatus = 0;
Message msg = new Message();
Bundle b = new Bundle();
msg.what = 1;
b.putInt("udpStatus", udpStatus);
msg.setData(b);
uiHandler.sendMessage(msg);
}
public void setsendFlagValue(int flag) {
this.sendFlag = flag;
}
public int getsendFlagValue() {
return this.sendFlag;
}
public static int ipStringToInt(String str) {
int result = 0;
String[] array = str.split("\\.");
if (array.length != 4) return 0;
try {
result = Integer.parseInt(array[3]);
result = (result << 8) + Integer.parseInt(array[2]);
result = (result << 8) + Integer.parseInt(array[1]);
result = (result << 8) + Integer.parseInt(array[0]);
} catch (NumberFormatException e) {
return 0;
}
return result;
}
public static InetAddress intToInetAddress(int hostAddress) {
InetAddress inetAddress;
byte[] addressBytes = { (byte)(0xff & hostAddress),
(byte)(0xff & (hostAddress >> 8)),
(byte)(0xff & (hostAddress >> 16)),
(byte)(0xff & (hostAddress >> 24)) };
try {
inetAddress = InetAddress.getByAddress(addressBytes);
} catch(UnknownHostException e) {
return null;
}
return inetAddress;
}
}
我的布局: layout
代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="de.yargeldi.heckklappenapp.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Öffnen"
android:id="@+id/button"
android:layout_marginTop="19dp"
android:textSize="30sp"
android:layout_below="@+id/imageView"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Schließen"
android:id="@+id/button2"
android:textSize="30sp"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="STOP"
android:id="@+id/button3"
android:textSize="30sp"
android:layout_below="@+id/button2"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/hochschule" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Heckklappengeschwindigkeit:"
android:id="@+id/textView"
android:textSize="20sp"
android:layout_below="@+id/button3"
android:layout_centerHorizontal="true" />
<SeekBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/seekBar1"
android:layout_below="@+id/textView"
android:layout_marginTop="25dp"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:max="50"
android:progressTint="#FF0000"
android:thumbTint="#0000CD"
android:indeterminateTint="#0000CD"
android:foregroundTint="#0000CD"
android:backgroundTint="#0000CD" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/textView1"
android:text="50%-100%:"
android:layout_below="@+id/seekBar1"
android:layout_alignStart="@+id/textView2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Heckklappenöffnungswinkel:"
android:id="@+id/text2"
android:layout_marginTop="49dp"
android:layout_below="@+id/seekBar1"
android:layout_alignStart="@+id/textView3" />
</RelativeLayout>
我试图完成它,我知道所有的错误,因为我是一个绝对的初学者,我不知道如何使用命令。 如果有人能证明我必须做得对,我会非常高兴。
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.Set;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button openbutton,closebutton,stopbutton;
Handler handler;
Thread thread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/////////////////////Button Finder//////////////////////
openbutton=(Button)findViewById(R.id.buttonopen);
closebutton=(Button)findViewById(R.id.buttonclose);
stopbutton=(Button)findViewById(R.id.buttonstop);
/////////////////////Button Listener///////////////////////
openbutton.setOnClickListener(openbuttonListener);
closebutton.setOnClickListener(closebuttonListener);
stopbutton.setOnClickListener(stopbuttonListener);
thread= new Thread(new UDP_Verbindung(handler));
thread.start();
}
}
public Button.OnClickListener openbuttonListener= new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
buffout[12]=1;
new sendThread().execute();
}
};
public Button.OnClickListener closebuttonListener= new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
buffout[13]=1;
new sendThread().execute();
}
};
public Button.OnClickListener stopbuttonListener= new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
buffout[14]=1;
new sendThread().execute();
}
};
public class sendThread extends AsyncTask<String, Integer, Boolean>{
@Override
protected Boolean doInBackground(String... arg0) {
try {
inet_addr = addr;
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DatagramPacket out = new DatagramPacket(buffout, buffout.length, inet_addr, UDP_Target_Port);
try {
socket= new DatagramSocket();
socket.send(out);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
class UDP_Verbindung implements Runnable{
private static final String TAGUDP = "UDP";
final int UDP_Target_Port = 12345;
private Thread threadUDP = null;
private boolean isrunning = false;
private DatagramSocket udpSocket = null;
byte[] buffin = new byte[28];
byte[] buffout = {0, 0, 0, 0, 0, 0, 8, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
DatagramPacket in = new DatagramPacket(buffin,buffin.length);
DatagramPacket out = new DatagramPacket(buffout,buffout.length);
int CAN_ID;
int CAN_Length;
private int udpStatus = 0;
private Handler uiHandler = null;
private int sendFlag = 0;
String ip = "168.192.0.74";
InetAddress addr = intToInetAddress(ipStringToInt( "192.168.0.74"));
public int receive = 1;
private Set<String> listenToIds;
public UDP_Verbindung(Handler uiHandler) {
this.uiHandler = uiHandler;
listenToIds = Collections.<String>emptySet();
}
public void setMessages(Set<String> messages){
listenToIds = messages;
Log.d(TAGUDP, "New Message IDs to listen to: ");
for(String strId: listenToIds) {
Log.d(TAGUDP, strId);
}
}
@Override
public void run() {
try
{
Log.d(TAGUDP,"Verbindungsaufbau");
udpSocket = new DatagramSocket(UDP_Target_Port);
udpSocket.setSoTimeout( 3000 );
} catch (SocketException ex)
{
ex.printStackTrace();
Log.d(TAGUDP,"Verbindungsaufbau fehlgeschlagen");
this.stop_service();
}
udpStatus = 1;
Message msg1 = new Message();
Bundle b1 = new Bundle();
msg1.what = 1;
b1.putInt("udpStatus", udpStatus);
msg1.setData(b1);
uiHandler.sendMessage(msg1);
while (isrunning)
{
try
{
udpSocket.receive(in);
//Log.d(TAGUDP,"Data received:");
}
catch (IOException ex)
{
Log.d(TAGUDP,"hier??");
}
InetAddress senderIP = in.getAddress();
int senderPort = in.getPort();
//Log.d(TAGUDP, senderIP.toString() + ":" + Integer.toString(senderPort) );
buffin = in.getData();//---------------------Einlesen des Inhalts einer CAN Nachricht
/*Log.d(TAGUDP, "LOsgeths:");
for (int o = 0; o < 28; o++){
Log.d(TAGUDP, buffin[o] + " ");
}*/
//Log.d(TAGUDP, senderIP.toString() + ":" + Integer.toString(senderPort) );
CAN_ID = buffin[8] + buffin[9]*256;//----------Ermitteln der CAN ID einer CAN Nachricht
String CAN_ID_hex = Integer.toHexString(CAN_ID);
if(listenToIds.contains(CAN_ID_hex)){
Log.d(TAGUDP, "Neue CAN Nachricht mit der ID " + CAN_ID_hex);
CAN_Length = buffin[6] & 15;//---------------Ermitteln der Länge einer CAN Nachricht
//Log.d(TAGUDP,"ID: " + CAN_ID + " ");
byte[] canData = new byte[CAN_Length];
for (int l = 0; l < CAN_Length; l++){
canData[l] = buffin[12+l];
//Log.d(TAGUDP, buffin[12+l] + " ");
//Log.d(TAGUDP, Integer.toString(byteToUnsignedInt(buffin[12+l])));
}
Message msg = new Message();
Bundle b = new Bundle();
msg.what = 2;
b.putString("CAN_ID", CAN_ID_hex);
b.putByteArray("CAN_DATA", canData);
b.putInt("CAN_LENGTH", CAN_Length);
msg.setData(b);
uiHandler.sendMessage(msg);
}
}
}
public static int byteToUnsignedInt(byte b) {
return (0x00 << 24 | b & 0xff);
}
public void start_Service(){
Log.d(TAGUDP, "Start Thread");
this.threadUDP = new Thread(this);
isrunning = true;
threadUDP.start();
}
public void stop_service() {
Log.d(TAGUDP, "Trenne Verbindung");
Log.d(TAGUDP, "Warte bis Thread durchgelaufen");
isrunning = false;
while(true) {
try {
if (threadUDP != null) {
threadUDP.join(500);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
}
Log.d(TAGUDP, "Thread durchgelaufen");
if (udpSocket != null)
{
Log.d(TAGUDP, "Schließe Socket");
udpSocket.close();
Log.d(TAGUDP, "geschlossen");
}
threadUDP = null;
Log.d(TAGUDP, "UDP Thread geschlossen");
udpStatus = 0;
Message msg = new Message();
Bundle b = new Bundle();
msg.what = 1;
b.putInt("udpStatus", udpStatus);
msg.setData(b);
uiHandler.sendMessage(msg);
}
public void setsendFlagValue(int flag) {
this.sendFlag = flag;
}
public int getsendFlagValue() {
return this.sendFlag;
}
public static int ipStringToInt(String str) {
int result = 0;
String[] array = str.split("\\.");
if (array.length != 4) return 0;
try {
result = Integer.parseInt(array[3]);
result = (result << 8) + Integer.parseInt(array[2]);
result = (result << 8) + Integer.parseInt(array[1]);
result = (result << 8) + Integer.parseInt(array[0]);
} catch (NumberFormatException e) {
return 0;
}
return result;
}
public static InetAddress intToInetAddress(int hostAddress) {
InetAddress inetAddress;
byte[] addressBytes = { (byte)(0xff & hostAddress),
(byte)(0xff & (hostAddress >> 8)),
(byte)(0xff & (hostAddress >> 16)),
(byte)(0xff & (hostAddress >> 24)) };
try {
inetAddress = InetAddress.getByAddress(addressBytes);
} catch(UnknownHostException e) {
return null;
}
return inetAddress;
}
}