蓝牙小车安卓软件开发.docx

上传人:b****1 文档编号:23264840 上传时间:2023-05-15 格式:DOCX 页数:24 大小:144.39KB
下载 相关 举报
蓝牙小车安卓软件开发.docx_第1页
第1页 / 共24页
蓝牙小车安卓软件开发.docx_第2页
第2页 / 共24页
蓝牙小车安卓软件开发.docx_第3页
第3页 / 共24页
蓝牙小车安卓软件开发.docx_第4页
第4页 / 共24页
蓝牙小车安卓软件开发.docx_第5页
第5页 / 共24页
点击查看更多>>
下载资源
资源描述

蓝牙小车安卓软件开发.docx

《蓝牙小车安卓软件开发.docx》由会员分享,可在线阅读,更多相关《蓝牙小车安卓软件开发.docx(24页珍藏版)》请在冰豆网上搜索。

蓝牙小车安卓软件开发.docx

蓝牙小车安卓软件开发

基于蓝牙串口小车的安卓软件开发

学校:

华南师范大学

学院:

物理与电信工程学院

 

作者:

黄世隆

学号:

20113100019

指导老师:

刘朝辉

摘要

使用的蓝牙小车是基于STC89C52RC单片机开发板,蓝牙模块是HC-06,模块端通过USB转TTL,通过接收安卓手机发来的16进制指令进行运动。

安卓软件控制小车的方式有按键控制以及重力感应控制两种。

其中按键方式是默认开启的,重力感应控制方式可以自由选择开启与关闭。

 

关键词:

蓝牙小车安卓软件重力感应

软件制作逻辑框图

方案特点

针对目前最流行的安卓智能手机开发的一款软件。

对于控件,采用了比较美观的ImageButton的按键,加了背景图片。

方案设计与讨论

由于需要使用到蓝牙,而模拟器没有蓝牙功能,所以测试需要一部安卓手机。

程序中我把蓝牙的MAC地址固定了,通用性不高,在以后改进。

软件设计的界面比较简陋。

手机发送的指令格式为四位16进制指令:

上:

1F00;

下:

2F00;

左:

3F00;

右:

4F00;

停止:

0000;

系统实现及原理分析

为布局中的每个按键建立一个监听器,为了实现能够手不离开屏幕使得小车能够持续行走的效果,控制小车的ImageButton控件采用的监听器为onTouchListener,该控件可以监听手按下屏幕与离开屏幕两种触发事件,手按下屏幕触发向输出流写行走指令,离开屏幕触发向输入流写停止指令。

其余普通的按键时间则采用onClickListener监听器,监听手离开屏幕的触发事件,实现手机蓝牙与小车蓝牙的连接、断开,重力感应控制的开启与关闭。

在程序中,为了实现成功与小车蓝牙建立连接,要先选用与之相对应的UUID(单片机的UUID为“00001101-0000-1000-8000-00805F9B34FB”)和蓝牙MAC地址(我做使用的为HC-06蓝牙模块,MAC地址为“98:

D3:

31:

B2:

18:

43”),并且要事先建立配对,获取本地蓝牙适配器,实现连接。

成功建立连接之后,采用的通信方式是socket通信。

重力感应控制则是获取手机重力感应的X、Y、Z值(即手机不同摆放方向,例如手机屏幕朝上、朝下、左侧转、右侧转等),判断X、Y、Z值代表的方向,然后往输出流写相应的指令达到控制的效果。

相应的重力控制开启关闭功能是定义一个布尔变量,使之是否能够往输入流写指令。

测量结果与误差分析

在测试过程中,发现有些手机正确配对后但无法连接小车,原因不明,可能与手机的硬件有关。

对于指令(四位十六进制数)的发送,一次只能发送两位十六进制数,所以需要连续发送两次。

重力感应控制功能,由于需要根据不断改变的重力感应值改变指令,单独开启线程发送指令比较复杂,所以直接把发送数据的程序放在了重力感应的监听器里面。

参考文献

Android开发权威指南(第二版)李宁编著

附件

XML文件布局:

android="

xmlns:

tools="

android:

layout_width="match_parent"

android:

layout_height="match_parent"

android:

paddingBottom="@dimen/activity_vertical_margin"

android:

paddingLeft="@dimen/activity_horizontal_margin"

android:

paddingRight="@dimen/activity_horizontal_margin"

android:

paddingTop="@dimen/activity_vertical_margin"

android:

background="@drawable/ky"

tools:

context=".MainActivity">

android:

id="@+id/button2"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_alignBottom="@+id/button1"

android:

layout_toRightOf="@+id/imageButton1"

android:

text="断开连接"/>

android:

id="@+id/imageButton1"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_above="@+id/imageButton3"

android:

layout_centerHorizontal="true"

android:

layout_marginBottom="19dp"

android:

src="@drawable/up"/>

android:

id="@+id/imageButton4"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_above="@+id/imageButton5"

android:

layout_alignRight="@+id/button2"

android:

layout_marginBottom="24dp"

android:

src="@drawable/right"/>

android:

id="@+id/imageButton3"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_alignTop="@+id/imageButton4"

android:

layout_toLeftOf="@+id/button2"

android:

src="@drawable/stop"/>

android:

id="@+id/imageButton5"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_alignParentBottom="true"

android:

layout_marginBottom="39dp"

android:

layout_toRightOf="@+id/imageButton2"

android:

src="@drawable/down"/>

android:

id="@+id/imageButton2"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_below="@+id/imageButton1"

android:

layout_marginRight="18dp"

android:

layout_toLeftOf="@+id/imageButton3"

android:

src="@drawable/left"/>

android:

id="@+id/button1"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_alignLeft="@+id/imageButton2"

android:

text="建立连接"/>

android:

id="@+id/button3"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_below="@+id/button1"

android:

layout_marginTop="18dp"

android:

layout_toLeftOf="@+id/imageButton1"

android:

text="开启重力"/>

android:

id="@+id/button4"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_alignBaseline="@+id/button3"

android:

layout_alignBottom="@+id/button3"

android:

layout_alignRight="@+id/button2"

android:

text="关闭重力"/>

android:

id="@+id/editText1"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_alignRight="@+id/imageButton2"

android:

layout_below="@+id/imageButton4"

android:

ems="10">

源程序:

packagecom.example.car;

importjava.io.IOException;

importjava.io.InputStream;

importjava.io.OutputStream;

importjava.util.Iterator;

importjava.util.Set;

importjava.util.Timer;

importjava.util.TimerTask;

importjava.util.UUID;

importandroid.app.Activity;

importandroid.bluetooth.BluetoothAdapter;

importandroid.bluetooth.BluetoothDevice;

importandroid.bluetooth.BluetoothSocket;

importandroid.content.Intent;

importandroid.hardware.Sensor;

importandroid.hardware.SensorManager;

importandroid.os.Bundle;

importandroid.util.Log;

importandroid.view.Menu;

importandroid.view.MotionEvent;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.widget.Button;

importandroid.widget.TextView;

importandroid.view.View.OnClickListener;

importandroid.widget.Button;

importandroid.widget.EditText;

importandroid.widget.ImageButton;

importandroid.widget.Toast;

importandroid.hardware.SensorManager;

importandroid.hardware.Sensor;

importandroid.hardware.SensorEventListener;

importandroid.hardware.SensorEvent;

publicclassMainActivityextendsActivity{

ImageButtonup;

ImageButtondown;

ImageButtonleft;

ImageButtonright;

ImageButtonstop;

Buttonfind;

Buttonclose;

Buttonop;

Buttoncl;

EditTexttv;

privateSensorManagermsensormanager;

privateSensormsensor;

privatefloatx,y,z;

booleanb=false;

privatestaticfinalUUIDuuid=UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

privateBluetoothAdapteropen=null;

BluetoothSocketsocket=null;//用于数据传输的socket

publicConnectedThreadthread=null;//连接蓝牙设备线程

privateOutputStreamos;

privatestaticfinalStringTAG="错误";

@Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

tv=(EditText)findViewById(R.id.editText1);

up=(ImageButton)findViewById(R.id.imageButton1);

up.setOnTouchListener(newImageButton.OnTouchListener(){

publicbooleanonTouch(Viewv,MotionEvente){

switch(e.getAction()){

caseMotionEvent.ACTION_DOWN:

try{

os=socket.getOutputStream();

os.write((byte)0x1f);

os.write((byte)0x00);

}

catch(IOExceptionee){

}

break;

caseMotionEvent.ACTION_UP:

try{

os=socket.getOutputStream();

os.write((byte)0x00);

os.write((byte)0x00);

}

catch(IOExceptionee){

}

break;

}

returntrue;

}

});

down=(ImageButton)findViewById(R.id.imageButton5);

down.setOnTouchListener(newImageButton.OnTouchListener(){

publicbooleanonTouch(Viewv,MotionEvente){

switch(e.getAction()){

caseMotionEvent.ACTION_DOWN:

try{

os=socket.getOutputStream();

os.write((byte)0x2f);

os.write((byte)0x00);

}

catch(IOExceptionee){

}

break;

caseMotionEvent.ACTION_UP:

try{

os=socket.getOutputStream();

os.write((byte)0x00);

os.write((byte)0x00);

}

catch(IOExceptionee){

}

break;

}

returntrue;

}

});

left=(ImageButton)findViewById(R.id.imageButton2);

left.setOnTouchListener(newImageButton.OnTouchListener(){

publicbooleanonTouch(Viewv,MotionEvente){

switch(e.getAction()){

caseMotionEvent.ACTION_DOWN:

try{

os=socket.getOutputStream();

os.write((byte)0x3f);

os.write((byte)0x00);

}

catch(IOExceptionee){

}

break;

caseMotionEvent.ACTION_UP:

try{

os=socket.getOutputStream();

os.write((byte)0x00);

os.write((byte)0x00);

}

catch(IOExceptionee){

}

break;

}

returntrue;

}

});

right=(ImageButton)findViewById(R.id.imageButton4);

right.setOnTouchListener(newImageButton.OnTouchListener(){

publicbooleanonTouch(Viewv,MotionEvente){

switch(e.getAction()){

caseMotionEvent.ACTION_DOWN:

try{

os=socket.getOutputStream();

os.write((byte)0x4f);

os.write((byte)0x00);

}

catch(IOExceptionee){

}

break;

caseMotionEvent.ACTION_UP:

try{

os=socket.getOutputStream();

os.write((byte)0x00);

os.write((byte)0x00);

}

catch(IOExceptionee){

}

break;

}

returntrue;

}

});

stop=(ImageButton)findViewById(R.id.imageButton3);

stop.setOnTouchListener(newImageButton.OnTouchListener(){

publicbooleanonTouch(Viewv,MotionEvente){

switch(e.getAction()){

caseMotionEvent.ACTION_DOWN:

try{

os=socket.getOutputStream();

os.write((byte)0x00);

os.write((byte)0x00);

}

catch(IOExceptionee){

}

break;

caseMotionEvent.ACTION_UP:

try{

os=socket.getOutputStream();

os.write((byte)0x00);

os.write((byte)0x00);

}

catch(IOExceptionee){

}

break;

}

returntrue;

}

});

find=(Button)findViewById(R.id.button1);

close=(Button)findViewById(R.id.button2);

find.setOnClickListener(newOnClickListener(){

publicvoidonClick(Viewv){

open=BluetoothAdapter.getDefaultAdapter();

if(!

op

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 农林牧渔 > 畜牧兽医

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1