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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

行稳方能走远

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值