小R科技-WIFI机器人网·机器人创意工作室

 找回密码
 立即注册
查看: 70258|回复: 60

Android手机控制智能小车机器人代码

[复制链接]
发表于 2012-3-12 20:49:49 | 显示全部楼层 |阅读模式
     目前主流的手机操作系统有塞班、安卓(Android)、Windows Mobile,对比了一下,首先,塞班是用C++写的,这么多门语言我唯独看到C++就头大···,放弃了···,Windows Moblie其实和之前发的电脑端程序基本是一样的,也就没什么意思了,最后决定选择目前正火的Android手机作为控制平台。
Android是个开源的应用,使用Java语言对其编程。于是这次的开发我选用Eclipse作为开发工具,用Java语言开发手机端的控制程序,原理就是通过获取蓝牙小车上的蓝牙模块的MAC地址,发起连接请求,并将数据通过Socket封包发送就OK了。
下面开始介绍Android手机端控制程序的编写:
首先打开Eclipse,当然之前的Java开发环境和安卓开发工具自己得先配置好,这里就不多说了,网上教程一大摞。
然后新建一个Android项目,修改布局文件main.xml,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<Button
android:id="@+id/btnF"
android:layout_width="100px"
android:layout_height="60px"
android:text="前进"
android:layout_x="130px"
android:layout_y="62px"
>
</Button>
<Button
android:id="@+id/btnL"
android:layout_width="100px"
android:layout_height="60px"
android:text="左转"
android:layout_x="20px"
android:layout_y="152px"
>
</Button>
<Button
android:id="@+id/btnR"
android:layout_width="100px"
android:layout_height="60px"
android:text="右转"
android:layout_x="240px"
android:layout_y="152px"
>
</Button>
<Button
android:id="@+id/btnB"
android:layout_width="100px"
android:layout_height="60px"
android:text="后退"
android:layout_x="130px"
android:layout_y="242px"
>
</Button>
<Button
android:id="@+id/btnS"
android:layout_width="100px"
android:layout_height="60px"
android:text="停止"
android:layout_x="130px"
android:layout_y="152px"
>
</Button>
</AbsoluteLayout>
这个布局文件的效果就是如视频中所示的手机操作界面。
然后是权限声明,这一步不能少,否则将无法使用安卓手机的蓝牙功能。
权限声明如下:
打开AndroidManifest.xml文件,修改代码如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ThinBTClient.www"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ThinBTClient"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
其中红色、加粗部分就是要添加的权限声明。
然后编写Activity中的执行代码,这些代码的作用就是发送指令,控制小车的运动。
代码如下:
package com.ThinBTClient.www;
import android.app.Activity;
import android.os.Bundle;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;
import android.app.Activity;

import android.bluetooth.BluetoothAdapter;

import android.bluetooth.BluetoothDevice;

import android.bluetooth.BluetoothSocket;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;

import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Event;

import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import android.widget.Button;
import android.widget.Toast;
public class ThinBTClient extends Activity {
private static final String TAG = "THINBTCLIENT";

private static final boolean D = true;
private BluetoothAdapter mBluetoothAdapter = null;
private BluetoothSocket btSocket = null;
private OutputStream outStream = null;
Button mButtonF;

Button mButtonB;
Button mButtonL;
Button mButtonR;
Button mButtonS;

private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

private static String address = "00:11:03:21:00:43"; // <==要连接的蓝牙设备MAC地址

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//前进
mButtonF=(Button)findViewById(R.id.btnF);
mButtonF.setOnTouchListener(new Button.OnTouchListener(){

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
String message;
byte[] msgBuffer;
int action = event.getAction();
switch(action)
{
case MotionEvent.ACTION_DOWN:
try {
outStream = btSocket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "ON RESUME: Output stream creation failed.", e);
}

message = "1";
msgBuffer = message.getBytes();
try {
outStream.write(msgBuffer);
} catch (IOException e) {
Log.e(TAG, "ON RESUME: Exception during write.", e);
}
break;

case MotionEvent.ACTION_UP:
try {
outStream = btSocket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "ON RESUME: Output stream creation failed.", e);
}

message = "0";
msgBuffer = message.getBytes();
try {
outStream.write(msgBuffer);
} catch (IOException e) {
Log.e(TAG, "ON RESUME: Exception during write.", e);
}
break;
}
return false;
}


});
//后退
mButtonB=(Button)findViewById(R.id.btnB);
mButtonB.setOnTouchListener(new Button.OnTouchListener(){


@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
String message;
byte[] msgBuffer;
int action = event.getAction();
switch(action)
{
case MotionEvent.ACTION_DOWN:
try {
outStream = btSocket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "ON RESUME: Output stream creation failed.", e);
}

message = "3";
msgBuffer = message.getBytes();
try {
outStream.write(msgBuffer);
} catch (IOException e) {
Log.e(TAG, "ON RESUME: Exception during write.", e);
}
break;

case MotionEvent.ACTION_UP:
try {
outStream = btSocket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "ON RESUME: Output stream creation failed.", e);
}

message = "0";
msgBuffer = message.getBytes();
try {
outStream.write(msgBuffer);
} catch (IOException e) {
Log.e(TAG, "ON RESUME: Exception during write.", e);
}
break;
}

return false;
}


});
//左转
mButtonL=(Button)findViewById(R.id.btnL);
mButtonL.setOnTouchListener(new Button.OnTouchListener(){

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
String message;
byte[] msgBuffer;
int action = event.getAction();
switch(action)
{
case MotionEvent.ACTION_DOWN:
try {
outStream = btSocket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "ON RESUME: Output stream creation failed.", e);
}

message = "2";
msgBuffer = message.getBytes();
try {
outStream.write(msgBuffer);
} catch (IOException e) {
Log.e(TAG, "ON RESUME: Exception during write.", e);
}
break;

case MotionEvent.ACTION_UP:
try {
outStream = btSocket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "ON RESUME: Output stream creation failed.", e);
}

message = "0";
msgBuffer = message.getBytes();
try {
outStream.write(msgBuffer);
} catch (IOException e) {
Log.e(TAG, "ON RESUME: Exception during write.", e);
}
break;
}

return false;

}
});
//右转
mButtonR=(Button)findViewById(R.id.btnR);
mButtonR.setOnTouchListener(new Button.OnTouchListener(){

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
String message;
byte[] msgBuffer;
int action = event.getAction();
switch(action)
{
case MotionEvent.ACTION_DOWN:
try {
outStream = btSocket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "ON RESUME: Output stream creation failed.", e);
}

message = "4";
msgBuffer = message.getBytes();
try {
outStream.write(msgBuffer);
} catch (IOException e) {
Log.e(TAG, "ON RESUME: Exception during write.", e);
}
break;

case MotionEvent.ACTION_UP:
try {
outStream = btSocket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "ON RESUME: Output stream creation failed.", e);
}

message = "0";
msgBuffer = message.getBytes();
try {
outStream.write(msgBuffer);
} catch (IOException e) {
Log.e(TAG, "ON RESUME: Exception during write.", e);
}
break;
}

return false;

}


});

//停止
mButtonS=(Button)findViewById(R.id.btnS);
mButtonS.setOnTouchListener(new Button.OnTouchListener(){

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction()==MotionEvent.ACTION_DOWN)
try {
outStream = btSocket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "ON RESUME: Output stream creation failed.", e);
}

String message = "0";
byte[] msgBuffer = message.getBytes();
try {
outStream.write(msgBuffer);
} catch (IOException e) {
Log.e(TAG, "ON RESUME: Exception during write.", e);
}
return false;
}


});


if (D)
Log.e(TAG, "+++ ON CREATE +++");
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
Toast.makeText(this, "Bluetooth is not available.", Toast.LENGTH_LONG).show();
finish();
return;
}

if (!mBluetoothAdapter.isEnabled()) {
Toast.makeText(this, "Please enable your Bluetooth and re-run this program.", Toast.LENGTH_LONG).show();
finish();
return;
}

if (D)
Log.e(TAG, "+++ DONE IN ON CREATE, GOT LOCAL BT ADAPTER +++");
}

@Override
public void onStart() {
super.onStart();
if (D) Log.e(TAG, "++ ON START ++");
}

@Override
public void onResume() {
super.onResume();
if (D) {
Log.e(TAG, "+ ON RESUME +");
Log.e(TAG, "+ ABOUT TO ATTEMPT CLIENT CONNECT +");
}
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
try {
btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
Log.e(TAG, "ON RESUME: Socket creation failed.", e);
}
mBluetoothAdapter.cancelDiscovery();
try {
btSocket.connect();
Log.e(TAG, "ON RESUME: BT connection established, data transfer link open.");
} catch (IOException e) {
try {
btSocket.close();
} catch (IOException e2) {
Log .e(TAG,"ON RESUME: Unable to close socket during connection failure", e2);
}
}

// Create a data stream so we can talk to server.
if (D)
Log.e(TAG, "+ ABOUT TO SAY SOMETHING TO SERVER +");
/* try {
outStream = btSocket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "ON RESUME: Output stream creation failed.", e);
}

String message = "1";
byte[] msgBuffer = message.getBytes();
try {
outStream.write(msgBuffer);
} catch (IOException e) {
Log.e(TAG, "ON RESUME: Exception during write.", e);
}
*/
}

@Override
public void onPause() {
super.onPause();

if (D)
Log.e(TAG, "- ON PAUSE -");
if (outStream != null) {
try {
outStream.flush();
} catch (IOException e) {
Log.e(TAG, "ON PAUSE: Couldn't flush output stream.", e);
}
}

try {
btSocket.close();
} catch (IOException e2) {
Log.e(TAG, "ON PAUSE: Unable to close socket.", e2);
}
}

@Override
public void onStop() {
super.onStop();
if (D)Log.e(TAG, "-- ON STOP --");
}

@Override
public void onDestroy() {
super.onDestroy();
if (D) Log.e(TAG, "--- ON DESTROY ---");
}
}
可以看到,在这个程序中我直接把小车蓝牙模块的MAC地址给写进去了,其实更合理一点应该让程序运行后搜索周围蓝牙设备,然后选择需要连接的设备,获取它的MAC地址再连接,但是我为了图省事,就直接把我的小车的蓝牙MAC给定死了(反正也没那么多小车让我遥控~~~),这样一打开程序,手机将自动连接智能小车。
好了,手机端的开发到此介绍完毕~希望能为需要者提供一点帮助。
下一步是打算增加红外避障功能、超声波测距功能,机械手臂,如果有机会,以后再安装上火控系统发射钻天猴~

再次鄙视光显摆却不肯分享技术的装X党!
由于需要源代码的朋友太多,故贴上源工程的下载地址,各位可以到这里下载:
http://download.csdn.net/source/3242996
如果对编程足够熟悉的话,可以自己建立工程并直接复制本人博客中的代码,效果是一样的。
版权所有:Liuviking,转载请注明出处,O(∩_∩)O谢谢~

2011/11/19备注:此控制程序属于第一版,尚有诸多缺陷,仅供参考。目前第二版的蓝牙控制程序已经发出,帖子地址为:http://hi.baidu.com/liuhuiviking/blog/item/392b715133158d928d5430df.html

机器人创意工作室     群号:145181710

回复

使用道具 举报

发表于 2016-3-13 13:46:20 | 显示全部楼层
弄得我都想自己也写一个了,虽然不会Java
回复 支持 反对

使用道具 举报

发表于 2016-2-14 20:51:12 | 显示全部楼层
值得学习!学习!
回复 支持 反对

使用道具 举报

发表于 2015-4-9 21:23:16 | 显示全部楼层
帅到无极限 发表于 2015-4-9 21:01
不好意思,楼主,我想做一个搜索蓝牙设备,在你这个发送指令的基础上,应该怎么做

我想保留你的发送指令的代码,但想弄一个按下一个按钮后命令系统去搜索蓝牙设备,可是我检验到但我不用你那个on Start()后面一系列的程序后,单击按钮,app会停止运行,这个问题应该怎么解决,恳请能得到楼主的回复。

下面这个是我一个普普通通的按键
search=(Button)findViewById(R.id.search);
            search.setOnTouchListener(new Button.OnTouchListener(){
               
                               

                                @Override
                                public boolean onTouch(View v, MotionEvent event) {
                                        // TODO Auto-generated method stub
                                       
                                        int action = event.getAction();
                                        switch(action)
                                        {
                                        case MotionEvent.ACTION_DOWN:
                                                //DisplayToast("套接字关闭失败!");
                                                BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
                                        if (!adapter.isEnabled()) {
                                            adapter.enable();
                                       //搜索蓝牙设备代码
                                        }
                                     
                                                break;
                                        }
                                        return false;}
            });
回复 支持 反对

使用道具 举报

发表于 2015-4-9 21:01:15 | 显示全部楼层
帅到无极限 发表于 2015-4-7 12:23
对喔,感谢楼主了楼主,祝楼主生活愉快

不好意思,楼主,我想做一个搜索蓝牙设备,在你这个发送指令的基础上,应该怎么做
回复 支持 反对

使用道具 举报

发表于 2015-4-7 12:23:03 | 显示全部楼层
liuviking 发表于 2015-4-7 12:20
松开后车总要停止呀,0就是停止

对喔,感谢楼主了楼主,祝楼主生活愉快
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-4-7 12:20:51 | 显示全部楼层
帅到无极限 发表于 2015-4-7 12:18
哦哦,懂了一些,还有一个问题拜托楼主不厌其烦地回答,为什么每次按钮松开后都要发送一个数据0,下位机 ...

松开后车总要停止呀,0就是停止
回复 支持 反对

使用道具 举报

发表于 2015-4-7 12:18:15 | 显示全部楼层
liuviking 发表于 2015-4-4 09:34
输出流用于发送数据。按下时发出1,松开后发送0

哦哦,懂了一些,还有一个问题拜托楼主不厌其烦地回答,为什么每次按钮松开后都要发送一个数据0,下位机接收后有什么用,谢谢
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-4-4 09:34:05 | 显示全部楼层
帅到无极限 发表于 2015-4-3 23:05
过程不是很了解当ACTION_Down时,获得输出流,并且设置message为1,而当ACTION_UP时,又得到一个输出流, ...

输出流用于发送数据。按下时发出1,松开后发送0
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

新品特惠推荐上一条 /2 下一条

QQ|QQ技术咨询1|QQ技术咨询2|商务合作微信1:xiaorgeek001|商务合作微信2:XiaoRGEEK|诚聘英才|Archiver|手机版|小R科技-WIFI机器人网·机器人创意工作室 ( 粤ICP备15000788号-6 )

GMT+8, 2025-6-16 21:13 , Processed in 1.138305 second(s), 20 queries .

Powered by XiaoR GEEK X3.4

© 2014-2021 XiaoR GEEK

快速回复 返回顶部 返回列表