成功实现手机蓝牙控制智能小车机器人视频程序源代码Android.docx

上传人:b****5 文档编号:4504705 上传时间:2022-12-01 格式:DOCX 页数:16 大小:19.44KB
下载 相关 举报
成功实现手机蓝牙控制智能小车机器人视频程序源代码Android.docx_第1页
第1页 / 共16页
成功实现手机蓝牙控制智能小车机器人视频程序源代码Android.docx_第2页
第2页 / 共16页
成功实现手机蓝牙控制智能小车机器人视频程序源代码Android.docx_第3页
第3页 / 共16页
成功实现手机蓝牙控制智能小车机器人视频程序源代码Android.docx_第4页
第4页 / 共16页
成功实现手机蓝牙控制智能小车机器人视频程序源代码Android.docx_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

成功实现手机蓝牙控制智能小车机器人视频程序源代码Android.docx

《成功实现手机蓝牙控制智能小车机器人视频程序源代码Android.docx》由会员分享,可在线阅读,更多相关《成功实现手机蓝牙控制智能小车机器人视频程序源代码Android.docx(16页珍藏版)》请在冰豆网上搜索。

成功实现手机蓝牙控制智能小车机器人视频程序源代码Android.docx

成功实现手机蓝牙控制智能小车机器人视频程序源代码Android

上次成功实现了通过笔记本电脑蓝牙来控制智能小车机器人的运动,但是通过电脑控制毕竟不方便,于是乎~本人打算将控制程序移植到手机上。

目前主流的手机操作系统有塞班、安卓(Android)、WindowsMobile,对比了一下,首先,塞班是用C++写的,这么多门语言我唯独看到C++就头大···,放弃了···,WindowsMoblie其实和之前发的电脑端程序基本是一样的,也就没什么意思了,最后决定选择目前正火的Android手机作为控制平台。

Android是个开源的应用,使用Java语言对其编程。

于是这次的开发我选用Eclipse作为开发工具,用Java语言开发手机端的控制程序,由于之前对Android的蓝牙通信这块涉及不多,一开始感觉有点小茫然,而网上也少有这方面的例程,有少数人做出了类似的东西,但是只传了个视频装X!

雪特····

经过几天的研究,最终确定了手机蓝牙通信其实就是Socket编程,再经过一番编写和调试,昨晚终于大功告成!

这是视频:

 

下面开始介绍Android手机端控制程序的编写:

首先打开Eclipse,当然之前的Java开发环境和安卓开发工具自己得先配置好,这里就不多说了,网上教程一大摞。

然后新建一个Android项目,修改布局文件main.xml,代码如下:

xmlversion="1.0"encoding="utf-8"?

>

android:

id="@+id/widget0"

android:

layout_width="fill_parent"

android:

layout_height="fill_parent"

xmlns:

android="

>

android:

id="@+id/btnF"

android:

layout_width="100px"

android:

layout_height="60px"

android:

text="前进"

android:

layout_x="130px"

android:

layout_y="62px"

>

android:

id="@+id/btnL"

android:

layout_width="100px"

android:

layout_height="60px"

android:

text="左转"

android:

layout_x="20px"

android:

layout_y="152px"

>

android:

id="@+id/btnR"

android:

layout_width="100px"

android:

layout_height="60px"

android:

text="右转"

android:

layout_x="240px"

android:

layout_y="152px"

>

android:

id="@+id/btnB"

android:

layout_width="100px"

android:

layout_height="60px"

android:

text="后退"

android:

layout_x="130px"

android:

layout_y="242px"

>

android:

id="@+id/btnS"

android:

layout_width="100px"

android:

layout_height="60px"

android:

text="停止"

android:

layout_x="130px"

android:

layout_y="152px"

>

这个布局文件的效果就是如视频中所示的手机操作界面。

然后是权限声明,这一步不能少,否则将无法使用安卓手机的蓝牙功能。

权限声明如下:

打开AndroidManifest.xml文件,修改代码如下:

xmlversion="1.0"encoding="utf-8"?

>

android="

package="com.ThinBTClient.www"

android:

versionCode="1"

android:

versionName="1.0">

name="android.permission.BLUETOOTH_ADMIN"/>

name="android.permission.BLUETOOTH"/>

icon="@drawable/icon"android:

label="@string/app_name">

name=".ThinBTClient"

android:

label="@string/app_name">

name="android.intent.action.MAIN"/>

name="android.intent.category.LAUNCHER"/>

其中红色、加粗部分就是要添加的权限声明。

然后编写Activity中的执行代码,这些代码的作用就是发送指令,控制小车的运动。

代码如下:

packagecom.ThinBTClient.www;

importandroid.app.Activity;

importandroid.os.Bundle;

importjava.io.IOException;

importjava.io.OutputStream;

importjava.util.UUID;

importandroid.app.Activity;

importandroid.bluetooth.BluetoothAdapter;

importandroid.bluetooth.BluetoothDevice;

importandroid.bluetooth.BluetoothSocket;

importandroid.content.DialogInterface;

importandroid.content.DialogInterface.OnClickListener;

importandroid.os.Bundle;

importandroid.provider.ContactsContract.CommonDataKinds.Event;

importandroid.util.Log;

importandroid.view.MotionEvent;

importandroid.view.View;

importandroid.widget.Button;

importandroid.widget.Toast;

publicclassThinBTClientextendsActivity{

privatestaticfinalStringTAG="THINBTCLIENT";

privatestaticfinalbooleanD=true;

privateBluetoothAdaptermBluetoothAdapter=null;

privateBluetoothSocketbtSocket=null;

privateOutputStreamoutStream=null;

ButtonmButtonF;

ButtonmButtonB;

ButtonmButtonL;

ButtonmButtonR;

ButtonmButtonS;

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

privatestaticStringaddress="00:

11:

03:

21:

00:

43";//<==要连接的蓝牙设备MAC地址

/**Calledwhentheactivityisfirstcreated.*/

@Override

publicvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

//前进

mButtonF=(Button)findViewById(R.id.btnF);

mButtonF.setOnTouchListener(newButton.OnTouchListener(){

@Override

publicbooleanonTouch(Viewv,MotionEventevent){

//TODOAuto-generatedmethodstub

Stringmessage;

byte[]msgBuffer;

intaction=event.getAction();

switch(action)

{

caseMotionEvent.ACTION_DOWN:

try{

outStream=btSocket.getOutputStream();

}catch(IOExceptione){

Log.e(TAG,"ONRESUME:

Outputstreamcreationfailed.",e);

}

message="1";

msgBuffer=message.getBytes();

try{

outStream.write(msgBuffer);

}catch(IOExceptione){

Log.e(TAG,"ONRESUME:

Exceptionduringwrite.",e);

}

break;

caseMotionEvent.ACTION_UP:

try{

outStream=btSocket.getOutputStream();

}catch(IOExceptione){

Log.e(TAG,"ONRESUME:

Outputstreamcreationfailed.",e);

}

message="0";

msgBuffer=message.getBytes();

try{

outStream.write(msgBuffer);

}catch(IOExceptione){

Log.e(TAG,"ONRESUME:

Exceptionduringwrite.",e);

}

break;

}

returnfalse;

}

});

//后退

mButtonB=(Button)findViewById(R.id.btnB);

mButtonB.setOnTouchListener(newButton.OnTouchListener(){

@Override

publicbooleanonTouch(Viewv,MotionEventevent){

//TODOAuto-generatedmethodstub

Stringmessage;

byte[]msgBuffer;

intaction=event.getAction();

switch(action)

{

caseMotionEvent.ACTION_DOWN:

try{

outStream=btSocket.getOutputStream();

}catch(IOExceptione){

Log.e(TAG,"ONRESUME:

Outputstreamcreationfailed.",e);

}

message="3";

msgBuffer=message.getBytes();

try{

outStream.write(msgBuffer);

}catch(IOExceptione){

Log.e(TAG,"ONRESUME:

Exceptionduringwrite.",e);

}

break;

caseMotionEvent.ACTION_UP:

try{

outStream=btSocket.getOutputStream();

}catch(IOExceptione){

Log.e(TAG,"ONRESUME:

Outputstreamcreationfailed.",e);

}

message="0";

msgBuffer=message.getBytes();

try{

outStream.write(msgBuffer);

}catch(IOExceptione){

Log.e(TAG,"ONRESUME:

Exceptionduringwrite.",e);

}

break;

}

returnfalse;

}

});

//左转

mButtonL=(Button)findViewById(R.id.btnL);

mButtonL.setOnTouchListener(newButton.OnTouchListener(){

@Override

publicbooleanonTouch(Viewv,MotionEventevent){

//TODOAuto-generatedmethodstub

Stringmessage;

byte[]msgBuffer;

intaction=event.getAction();

switch(action)

{

caseMotionEvent.ACTION_DOWN:

try{

outStream=btSocket.getOutputStream();

}catch(IOExceptione){

Log.e(TAG,"ONRESUME:

Outputstreamcreationfailed.",e);

}

message="2";

msgBuffer=message.getBytes();

try{

outStream.write(msgBuffer);

}catch(IOExceptione){

Log.e(TAG,"ONRESUME:

Exceptionduringwrite.",e);

}

break;

caseMotionEvent.ACTION_UP:

try{

outStream=btSocket.getOutputStream();

}catch(IOExceptione){

Log.e(TAG,"ONRESUME:

Outputstreamcreationfailed.",e);

}

message="0";

msgBuffer=message.getBytes();

try{

outStream.write(msgBuffer);

}catch(IOExceptione){

Log.e(TAG,"ONRESUME:

Exceptionduringwrite.",e);

}

break;

}

returnfalse;

}

});

//右转

mButtonR=(Button)findViewById(R.id.btnR);

mButtonR.setOnTouchListener(newButton.OnTouchListener(){

@Override

publicbooleanonTouch(Viewv,MotionEventevent){

//TODOAuto-generatedmethodstub

Stringmessage;

byte[]msgBuffer;

intaction=event.getAction();

switch(action)

{

caseMotionEvent.ACTION_DOWN:

try{

outStream=btSocket.getOutputStream();

}catch(IOExceptione){

Log.e(TAG,"ONRESUME:

Outputstreamcreationfailed.",e);

}

message="4";

msgBuffer=message.getBytes();

try{

outStream.write(msgBuffer);

}catch(IOExceptione){

Log.e(TAG,"ONRESUME:

Exceptionduringwrite.",e);

}

break;

caseMotionEvent.ACTION_UP:

try{

outStream=btSocket.getOutputStream();

}catch(IOExceptione){

Log.e(TAG,"ONRESUME:

Outputstreamcreationfailed.",e);

}

message="0";

msgBuffer=message.getBytes();

try{

outStream.write(msgBuffer);

}catch(IOExceptione){

Log.e(TAG,"ONRESUME:

Exceptionduringwrite.",e);

}

break;

}

returnfalse;

}

});

//停止

mButtonS=(Button)findViewById(R.id.btnS);

mButtonS.setOnTouchListener(newButton.OnTouchListener(){

@Override

publicbooleanonTouch(Viewv,MotionEventevent){

//TODOAuto-generatedmethodstub

if(event.getAction()==MotionEvent.ACTION_DOWN)

try{

outStream=btSocket.getOutputStream();

}catch(IOExceptione){

Log.e(TAG,"ONRESUME:

Outputstreamcreationfailed.",e);

}

Stringmessage="0";

byte[]msgBuffer=message.getBytes();

try{

outStream.write(msgBuffer);

}catch(IOExceptione){

Log.e(TAG,"ONRESUME:

Exceptionduringwrite.",e);

}

returnfalse;

}

});

if(D)

Log.e(TAG,"+++ONCREATE+++");

mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();

if(mBluetoothAdapter==null){

Toast.makeText(this,"Bluetoothisnotavailable.",Toast.LENGTH_LONG).show();

finish();

return;

}

if(!

mBluetoothAdapter.isEnabled()){

Toast.makeText(this,"PleaseenableyourBluetoothandre-runthisprogram.",Toast.LENGTH_LONG).show();

finish();

return;

}

if(D)

Log.e(TAG,"+++DONEINONCREATE,GOTLOCALBTADAPTER+++");

}

@Override

publicvoidonStart(){

super.onStart();

if(D)Log.e(TAG,"++ONSTART++");

}

@Override

publicvoidonResume(){

super.onResume();

if(D){

Log.e(TAG,"+ONRESUME+");

Log.e(TAG,"+ABOUTTOATTEMPTCLIENTCONNECT+");

}

BluetoothDevicedevice=mBluetoothAdapter.getRemoteDevice(address);

try{

btSocket=device.createRfcommSocketToServiceRecord(MY_UUID);

}catch(IOExceptione){

Log.e(TAG,"ONRESUME:

Socketcreationfailed.",e);

}

mBluetoothAdapter.cancelDiscovery();

try{

btSocket.connect();

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

当前位置:首页 > 高中教育 > 高中教育

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

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