WIFI小车APP
Posted 行稳方能走远
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WIFI小车APP相关的知识,希望对你有一定的参考价值。
对应这篇文章:433M射频遥控灯、震动感应灯、WIFI避障小车
代码概要
activity_contrl.xml
<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:background="@drawable/test"
tools:context=".ContrlActivity" >
<LinearLayout
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_marginRight="100dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/btnqian"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_weight="1"
android:background="@drawable/btn_back"
android:text="前进"
android:layout_margin="10dp"
android:textColor="#ffffff" />
<Button
android:id="@+id/btnhou"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_weight="1"
android:layout_margin="10dp"
android:background="@drawable/btn_back"
android:text="后退"
android:textColor="#ffffff" />
</LinearLayout>
<LinearLayout
android:layout_centerVertical="true"
android:layout_marginLeft="100dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/btnzuo"
android:layout_height="100dp"
android:layout_width="100dp"
android:layout_weight="1"
android:background="@drawable/btn_back"
android:text="左转"
android:layout_margin="10dp"
android:textColor="#ffffff" />
<Button
android:id="@+id/btnyou"
android:layout_height="100dp"
android:layout_width="100dp"
android:layout_weight="1"
android:layout_margin="10dp"
android:background="@drawable/btn_back"
android:text="右转"
android:textColor="#ffffff" />
</LinearLayout>
</RelativeLayout>
ContrlActivity.java
package com.gec.bluetoothcarlink;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
public class ContrlActivity extends Activity {
Button qian;
Button hou;
Button zuo;
Button you;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 璁剧疆鏃犳爣棰樻爮
requestWindowFeature(Window.FEATURE_NO_TITLE);
// 璁剧疆鍏ㄥ睆
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
// 淇濇寔灞忓箷甯镐寒
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_contrl);
initView();
}
void initView() {
qian = (Button) findViewById(R.id.btnqian);
hou = (Button) findViewById(R.id.btnhou);
zuo = (Button) findViewById(R.id.btnzuo);
you = (Button) findViewById(R.id.btnyou);
qian.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
new NetUtils("4").sendMessage();
break;
case MotionEvent.ACTION_UP: {
new NetUtils("s").sendMessage();
break;
}
}
return false;
}
});
hou.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
new NetUtils("5").sendMessage();
break;
case MotionEvent.ACTION_UP: {
new NetUtils("s").sendMessage();
break;
}
}
return false;
}
});
zuo.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
new NetUtils("6").sendMessage();
break;
case MotionEvent.ACTION_UP: {
new NetUtils("z").sendMessage();
break;
}
}
return false;
}
});
you.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
new NetUtils("7").sendMessage();
break;
case MotionEvent.ACTION_UP: {
new NetUtils("z").sendMessage();
break;
}
}
return false;
}
});
}
}
NetUtils.java
package com.gec.bluetoothcarlink;
import java.io.OutputStream;
import java.net.Socket;
import android.os.Handler;
public class NetUtils {
public String message;
public String reTurnMes;
public Handler handler;
public NetUtils(String message) {
this.message = message;
}
public NetUtils(String message, Handler handler) {
this.message = message;
this.handler = handler;
}
public void sendMessage() {
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
Socket client = new Socket("192.168.1.217", 8888);
//Socket client = new Socket(StartActivity.IP, Integer.parseInt(StartActivity.Port));
OutputStream out = client.getOutputStream();
out.write(message.getBytes());
out.close();
client.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
}
详细工程代码下载
https://download.csdn.net/download/zhuguanlin121/21023813?spm=1001.2014.3001.5501
以上是关于WIFI小车APP的主要内容,如果未能解决你的问题,请参考以下文章