内力网站建设,0元开店0元铺货无加盟费开网店,网站做百度地图怎么做呢,资源网址有哪些这学期有个课设#xff0c;我们组我负责一个手机APP的开发#xff0c;虽然刚开始说要实现什么智能导航#xff0c;类似高德地图那种#xff0c;但最后阉割的只剩一个Socket通信了#xff0c;因为之前没有接触过#xff08;可能之后也不会再接触#xff09;#xff0c;记…这学期有个课设我们组我负责一个手机APP的开发虽然刚开始说要实现什么智能导航类似高德地图那种但最后阉割的只剩一个Socket通信了因为之前没有接触过可能之后也不会再接触记录一下开发中遇到的问题。
1.开发工具Andriod Studio,主要功能实现与开发板的Socket通信具体是手机端收到板子消息按钮变色或者文本框显示对应文字手机端也会发送消息给板子让板子跳转用户操作界面整个就是一个简单的前端逻辑不需要手机端处理。手机端只负责显示。 2.实现效果
3.一些关键问题和收获 1andriod studio 的使用 感觉还是比较复杂的前期的一些环境配置有点忘了按照提示做就可以主要是手机运行环境的一些配置然后之后在打包程序安装在手机上出了点问题需要密钥按网上指导就可以。 对自带模板的灵活使用as自带很多模板比如我开发的这个丑陋的app就是在bottom Navigation Activity这个现成模板的基础上进行开发的。 之前as排版的Xml文件下载手机上就会变乱。后面不知不觉就好了注意这个问题。 2Socket通信 这个是这个项目的一个主要工作其中遇到的第一个问题怎么创建Socket通信
class ConnectThread extends Thread {public void run() {try {//新建Socketsocket new Socket(192.168.4.1, 8086);System.out.println(创建Socket成功);} catch (IOException e) {e.printStackTrace();}}}Socket的收发信息注意Socket的收发信息或者说对网络的操作不能直接在主线程进行一般都需要开子线程。 Socket的收信息
final byte[] buffer new byte[1024];//创建接收缓冲区try {inputStream socket.getInputStream();System.out.println(Socket接收数据成功);} catch (IOException e) {e.printStackTrace();}int len 0;//数据读出来并且返回数据的长try {len inputStream.read(buffer);System.out.println(接收数据大小 len);} catch (IOException e) {e.printStackTrace();}//BUFFER 内容转为字符串String recvStr new String(buffer, 0, len);Socket的发信息 //控制子线程开始public void buttonFunc() {// 在主线程中需要发送数据的代码片断前插入// 阻塞主线程,使子线程按照“同步”的方式执行try {System.out.println(线程开始);new Thread(sendthread).start(); // 线程启动new Thread(sendthread).join(); // 线程加入执行队列,主线程被阻塞,等待子线程执行完毕} catch (Exception e) {e.printStackTrace();System.out.println(e.toString());}}// 发送数据子线程final Runnable sendthread new Runnable() {Overridepublic void run() {try {System.out.println(发送数据的子线程执行中);try {try {//输出流初始化outputStream socket.getOutputStream();System.out.println(outputStream创建成功);} catch (IOException e) {e.printStackTrace();}try {//写入数据outputStream.write(sendbuf);outputStream.flush();System.out.println(数据写入成功);} catch (IOException e) {e.printStackTrace();}} catch (Exception e) {e.printStackTrace();}System.out.println(数据发送成功子线程执行完毕);} catch (Exception e) {e.printStackTrace();System.out.println(子线程: e.toString());}}};socket的数据解析及数据格式 收信息需要先转换为string类型发信息如果要发16进制byte数组直接写成16进制的形式。
源码 美食城
package com.example.parkingassitant2.ui.home;import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;import com.example.parkingassitant2.databinding.FragmentHomeBinding;import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;public class HomeFragment extends Fragment {private FragmentHomeBinding binding;//发送数据保存byte[] sendbuf {(byte) 0xaa, (byte) 0xaa,0x03, (byte) 0x12,0x03,0x18, (byte) 0xaa};Socket socket null; //定义socketOutputStream outputStream null; //定义输出流发送public View onCreateView(NonNull LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {HomeViewModel homeViewModel new ViewModelProvider(this).get(HomeViewModel.class);binding FragmentHomeBinding.inflate(inflater, container, false);View root binding.getRoot();ConnectThread ct;ct new ConnectThread();ct.start();//江底捞final Button jiangdilao binding.xiaocaiyuan;//湖底捞按钮final Button hudilao binding.A2;//河底捞按钮final Button hedilao binding.B1;//海底捞按钮final Button haidilao binding.B2;//KFC按钮final Button KFC binding.C1;//小菜园按钮final Button xiaocaiyuan binding.C2;//万达影城按钮final Button wanda binding.D1;//星巴克按钮final Button starbuck binding.D2;//江底捞按钮监听函数jiangdilao.setOnClickListener(new View.OnClickListener() {Overridepublic void onClick(View v) {//不同按钮对应不同的数据流sendbuf new byte[]{(byte) 0xaa, (byte) 0xaa, 0x03, (byte) 0x12, 0x03, 0x18, (byte) 0xaa};buttonFunc();Toast.makeText(getContext(), 预约成功请更新停车场信息, Toast.LENGTH_SHORT).show();}});//湖底捞按钮监听函数hudilao.setOnClickListener(new View.OnClickListener() {Overridepublic void onClick(View v) {//不同按钮对应不同的数据流sendbuf new byte[]{(byte) 0xaa, (byte) 0xaa, 0x03, (byte) 0x21, 0x03, 0x27, (byte) 0xaa};buttonFunc();Toast.makeText(getContext(), 预约成功请更新停车场信息, Toast.LENGTH_SHORT).show();}});//河底捞按钮监听函数hedilao.setOnClickListener(new View.OnClickListener() {Overridepublic void onClick(View v) {//不同按钮对应不同的数据流sendbuf new byte[]{(byte) 0xaa, (byte) 0xaa, 0x03, (byte) 0x22, 0x03, 0x28, (byte) 0xaa};buttonFunc();Toast.makeText(getContext(), 预约成功请更新停车场信息, Toast.LENGTH_SHORT).show();}});//海底捞按钮监听函数haidilao.setOnClickListener(new View.OnClickListener() {Overridepublic void onClick(View v) {//不同按钮对应不同的数据流sendbuf new byte[]{(byte) 0xaa, (byte) 0xaa, 0x03, (byte) 0x11, 0x03, 0x17, (byte) 0xaa};buttonFunc();Toast.makeText(getContext(), 预约成功请更新停车场信息, Toast.LENGTH_SHORT).show();}});//小菜园按钮监听函数xiaocaiyuan.setOnClickListener(new View.OnClickListener() {Overridepublic void onClick(View v) {//不同按钮对应不同的数据流sendbuf new byte[]{(byte) 0xaa, (byte) 0xaa, 0x03, (byte) 0x13, 0x03, 0x19, (byte) 0xaa};buttonFunc();Toast.makeText(getContext(), 预约成功请更新停车场信息, Toast.LENGTH_SHORT).show();}});//星巴克按钮监听函数starbuck.setOnClickListener(new View.OnClickListener() {Overridepublic void onClick(View v) {//不同按钮对应不同的数据流sendbuf new byte[]{(byte) 0xaa, (byte) 0xaa, 0x03, (byte) 0x14, 0x03, 0x1a, (byte) 0xaa};buttonFunc();Toast.makeText(getContext(), 预约成功请更新停车场信息, Toast.LENGTH_SHORT).show();}});//肯德基按钮监听函数KFC.setOnClickListener(new View.OnClickListener() {Overridepublic void onClick(View v) {//不同按钮对应不同的数据流sendbuf new byte[]{(byte) 0xaa, (byte) 0xaa, 0x03, (byte) 0x23, 0x03, 0x29, (byte) 0xaa};buttonFunc();Toast.makeText(getContext(), 预约成功请更新停车场信息, Toast.LENGTH_SHORT).show();}});//电影院按钮监听函数wanda.setOnClickListener(new View.OnClickListener() {Overridepublic void onClick(View v) {//不同按钮对应不同的数据流sendbuf new byte[]{(byte) 0xaa, (byte) 0xaa, 0x03, (byte) 0x24, 0x03, 0x2a, (byte) 0xaa};buttonFunc();Toast.makeText(getContext(), 预约成功请更新停车场信息, Toast.LENGTH_SHORT).show();}});return root;}class ConnectThread extends Thread {public void run() {try {//两个不同IP地址。网络助手和板子//socket new Socket(192.168.71.1, 8086);//新建Socketsocket new Socket(192.168.4.1, 8086);System.out.println(创建Socket成功);} catch (IOException e) {e.printStackTrace();}}}SuppressLint(SuspiciousIndentation)Overridepublic void onDestroyView() {super.onDestroyView();binding null;try {if(socket ! null) {socket.close();}} catch (IOException e) {e.printStackTrace();}}//控制子线程开始public void buttonFunc() {// 在主线程中需要发送数据的代码片断前插入// 阻塞主线程,使子线程按照“同步”的方式执行try {System.out.println(线程开始);new Thread(sendthread).start(); // 线程启动new Thread(sendthread).join(); // 线程加入执行队列,主线程被阻塞,等待子线程执行完毕} catch (Exception e) {e.printStackTrace();System.out.println(e.toString());}}// 发送数据子线程final Runnable sendthread new Runnable() {Overridepublic void run() {try {System.out.println(发送数据的子线程执行中);try {try {//输出流初始化outputStream socket.getOutputStream();System.out.println(outputStream创建成功);} catch (IOException e) {e.printStackTrace();}try {//写入数据outputStream.write(sendbuf);outputStream.flush();System.out.println(数据写入成功);} catch (IOException e) {e.printStackTrace();}} catch (Exception e) {e.printStackTrace();}System.out.println(数据发送成功子线程执行完毕);} catch (Exception e) {e.printStackTrace();System.out.println(子线程: e.toString());}}};
}停车场
package com.example.parkingassitant2.ui.dashboard;import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;import com.example.parkingassitant2.databinding.FragmentDashboardBinding;import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;public class DashboardFragment extends Fragment {private FragmentDashboardBinding binding;Socket socket null; //定义socketOutputStream outputStream null; //定义输入流接收public View onCreateView(NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {DashboardViewModel dashboardViewModel new ViewModelProvider(this).get(DashboardViewModel.class);binding FragmentDashboardBinding.inflate(inflater, container, false);View root binding.getRoot();ConnectThread ct;ct new ConnectThread();ct.start();return root;}class ConnectThread extends Thread{InputStream inputStreamnull; //定义输入流接收final Button A1 binding.A1;final Button A2 binding.A2;final Button D1 binding.D1;public void run(){System.out.println(Thread.currentThread().getName(): Hello);try {//socket new Socket(192.168.71.1, 8086);socket new Socket(192.168.4.1, 8086);binding.textView5.setText(连接成功);System.out.println(创建Socket成功);} catch (IOException e) {System.out.println(创建Socket失败);e.printStackTrace();}try{while (true){final byte[] buffer new byte[1024];//创建接收缓冲区try {inputStream socket.getInputStream();System.out.println(Socket接收数据成功);} catch (IOException e) {e.printStackTrace();}int len 0;//数据读出来并且返回数据的长try {len inputStream.read(buffer);System.out.println(接收数据大小 len);} catch (IOException e) {e.printStackTrace();}//BUFFER 内容转为字符串String recvStr new String(buffer, 0, len);//找到读取开始处一般为0int stadex recvStr.indexOf(aaaa);System.out.println(解析数据开始位置 stadex);int check_3,check_4,check_5,check_6;while(stadex len){stadex recvStr.indexOf(aaaa,stadex);int funcType_sta stadex4;int funcType_end stadex6;if(funcType_end len || funcType_sta len){break;}//位置3是操作类型 02 是显示车牌预约停车场String pos3 recvStr.substring(funcType_sta,funcType_end);// check_3 Integer.parseInt(pos3);stadex funcType_end;//停车预约功能中位置4是车牌号String pos4 recvStr.substring(stadex,stadex2);//check_4 Integer.parseInt(pos4);stadex 2;//停车预约功能中位置5是目的地String pos5 recvStr.substring(stadex,stadex2);//check_5 Integer.parseInt(pos5);stadex 2;String pos6 recvStr.substring(stadex,stadex2);//check_6 Integer.parseInt(pos6);stadex 2;//if( check_6 ! check_3check_4check_5) break;stadex 2;System.out.println(解析数据包 aaaa pos3 pos4 pos5 pos6 aa);System.out.println(操作类型指示值: pos3);switch(pos3){case 01: {System.out.println(操作类型为欢迎车牌进站);String showText null;switch (pos4) {case 01:showText 欢迎京A00001;break;case 02:showText 欢迎青ASB520;break;case 03:showText 欢迎皖A66666;break;case 04:showText 欢迎宁E88888;break;default:break;}binding.textView5.setText(showText);break;}case 04: {System.out.println(操作类型为停车预约);String showText null;switch (pos4) {case 01:showText 京A00001;break;case 02:showText 青ASB520;break;case 03:showText 皖A66666;break;case 04:showText 宁E88888;break;default:break;}String address null;switch (pos5){case 11:address 海底捞;break;case 12:address 江底捞;break;case 13:address 小菜园;break;case 14:address 星巴克;break;case 21:address 湖底捞;break;case 22:address 河底捞;break;case 23:address 肯德基;break;case 24:address 电影院;break;case a1:address A1;break;case a2:address A2;break;case d1:address D1;break;default:break;}binding.textView5.setText(showText车主预约车位 address);break;}case 03: {System.out.println(操作类型为车位状态变更);switch (pos4) {case a1: {if (pos5.equals(00)) {binding.A1.setBackgroundColor(Color.parseColor(#00FF00));} else if (pos5.equals(01)) {binding.A1.setBackgroundColor(Color.parseColor(#FF0000));} else if (pos5.equals(02)) {binding.A1.setBackgroundColor(Color.parseColor(#FFFF00));}break;}case a2: {System.out.println(pos5: );System.out.println(pos5);if (pos5.equals(00)) {binding.A2.setBackgroundColor(Color.parseColor(#00FF00));} else if (pos5.equals(01)) {binding.A2.setBackgroundColor(Color.parseColor(#FF0000));} else if (pos5.equals(02)) {binding.A2.setBackgroundColor(Color.parseColor(#FFFF00));}break;}case d1: {if (pos5.equals(00)) {binding.D1.setBackgroundColor(Color.parseColor(#00FF00));} else if (pos5.equals(01)) {binding.D1.setBackgroundColor(Color.parseColor(#FF0000));} else if (pos5.equals(02)) {binding.D1.setBackgroundColor(Color.parseColor(#FFFF00));}break;}default:break;}}default:break;}}}} catch (Exception ex) {ex.printStackTrace();}}}SuppressLint(SuspiciousIndentation)Overridepublic void onDestroyView() {super.onDestroyView();try {if(socket ! null) {socket.close();}} catch (IOException e) {e.printStackTrace();}binding null;}
}