单片机与接口技术课外制作-Android蓝牙遥控小车编程参考
更新时间:2023-05-14 09:58:01 阅读量: 实用文档 文档下载
- 单片机原理及接口技术推荐度:
- 相关推荐
《单片机与接口技术》课外制作(课程设计)编程参考(1)
Android蓝牙遥控程序设计
邹立杰
广州大学华软软件学院 软件工程系
2014.5.22
1. Configuration the Development Environment:
(1) Download and install/configuration Java SDK SE
You can seek methods from internet .specially,you need configuration the system environment variable “path”. (2) Download and Install Android Development IDE
Download the package:
adt-bundle-windows-x86_64-20140321.zip from the URL:
/sdk/index.html
download web page:
(3) Unpackaged the package
adt-bundle-windows-x86_64-20140321.zip to a directory on your hard disk.
Ok, your android development environment is ok!
2. Ctreate a Virtual Device for Your Android Application:
(1) start eclipse application by double click the
executable file:
The application window:
Select the menu item as below:
The appear the AVD manager dialog:
Click the “New”button:
Fill the AVD name as you like name; Select a device; Select a Target; Select a CPU/ABI; Select a skin;
Any default item you can select. The reference select as below:
When the below dialog appeared ,youe AVD is created ok.
Start the AVD:
Don’t close the AVD , Keep the AVD alived !
3. Design Your Car Blue-teeth Remote Program
(1)
Create an Android Application Project:
Fill the dialog items:
the click the “Next” button
Click the “Next” Button:
Click the “Next” button
And continue to click the “Next” button:
Click “Finish” button,your android application project is created!
Project Files Struchure:
(2)
Modify the AndroidManifest.xml file:
<manifest xmlns:android="/apk/res/android" package="com.example.blue" android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <uses-permission android:name="android.permission.BLUETOOTH" /> <application
android:icon="@drawable/ic_launcher" android:label="@string/app_name"
android:theme="@style/AppTheme" > <activity
android:name=".MainActivity"
android:label="@string/title_activity_main" > <intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="UNCHER" /> </intent-filter> </activity> </application> </manifest>
Add 2 sentences to the AndroidManifest.xml file.
(3)
Modify the layout file activity_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="/apk/res/android" android:background="@drawable/sise" >
<Button
android:id="@+id/btnF"
android:layout_width="88dp"
android:layout_height="wrap_content" android:layout_x="124dp" android:layout_y="58dp" android:text="向前" />
<Button
android:id="@+id/btnS"
android:layout_width="88dp"
android:layout_height="wrap_content" android:layout_x="124dp" android:layout_y="140dp" android:text="停止" /> <Button
android:id="@+id/btnB"
android:layout_width="88dp"
android:layout_height="wrap_content" android:layout_x="124dp" android:layout_y="222dp" android:text="后退" /> <Button
android:id="@+id/btnR"
android:layout_width="88dp"
android:layout_height="wrap_content" android:layout_x="227dp" android:layout_y="140dp" android:text="右转" /> <Button
android:id="@+id/btnL"
android:layout_width="88dp"
android:layout_height="wrap_content" android:layout_x="23dp" android:layout_y="140dp" android:text="左转" /> </AbsoluteLayout>
(4) Modify the resurce file strings.xml
<resources>
<string name="app_name">蓝牙小车遥控-华软学院</string> <string name="hello_world">Hello world!</string> <string name="menu_settings">Settings</string>
<string name="title_activity_main">蓝牙小车遥控-华软学院</string> </resources>
(5) Modify the Main Activity file MainActivity.java
package com.example.blue;
import android.os.Bundle; import android.app.Activity; import android.view.Menu; import java.io.IOException; import java.io.OutputStream; import java.util.UUID;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; import android.view.Gravity;
import android.view.MotionEvent; import android.view.View;
import android.widget.Button; import android.widget.Toast; import com.example.blue.R;
public class MainActivity extends Activity {
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 = "20:14:04:15:12:46"; // 蓝牙设备MAC地址 @Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(yout.activity_main);
//前进
mButtonF=(Button)findViewById(R.id.btnF);
mButtonF.setOnTouchListener(new Button.OnTouchListener(){ 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) { } message = "1"; msgBuffer = message.getBytes(); try { outStream.write(msgBuffer); } catch (IOException e) { } break; case MotionEvent.ACTION_UP: try { outStream = btSocket.getOutputStream(); } catch (IOException e) { } message = "0"; msgBuffer = message.getBytes(); try { outStream.write(msgBuffer); } catch (IOException e) { } break; } return false; } });
//后退
mButtonB=(Button)findViewById(R.id.btnB);
mButtonB.setOnTouchListener(new Button.OnTouchListener(){ 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) { } message = "3"; msgBuffer = message.getBytes(); try { outStream.write(msgBuffer); } catch (IOException e) { } break; case MotionEvent.ACTION_UP: try { outStream = btSocket.getOutputStream(); } catch (IOException e) { } message = "0"; msgBuffer = message.getBytes();
try { outStream.write(msgBuffer); } catch (IOException e) { } break; } return false; } });
//左转
mButtonL=(Button)findViewById(R.id.btnL);
mButtonL.setOnTouchListener(new Button.OnTouchListener(){ 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) { } message = "2"; msgBuffer = message.getBytes(); try { outStream.write(msgBuffer); } catch (IOException e) { } break; case MotionEvent.ACTION_UP: try { outStream = btSocket.getOutputStream(); } catch (IOException e) { } message = "0"; msgBuffer = message.getBytes(); try { outStream.write(msgBuffer); } catch (IOException e) { } break; } return false; } });
//右转
mButtonR=(Button)findViewById(R.id.btnR);
mButtonR.setOnTouchListener(new Button.OnTouchListener(){ 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) { } message = "4"; msgBuffer = message.getBytes(); try { outStream.write(msgBuffer); } catch (IOException e) { } break; case MotionEvent.ACTION_UP: try { outStream = btSocket.getOutputStream(); } catch (IOException e) { } message = "0"; msgBuffer = message.getBytes(); try { outStream.write(msgBuffer); } catch (IOException e) { } break; } return false; } });
//停止
mButtonS=(Button)findViewById(R.id.btnS);
mButtonS.setOnTouchListener(new Button.OnTouchListener(){ 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) { } String message = "0";
byte[] msgBuffer = message.getBytes(); try {
outStream.write(msgBuffer);
} catch (IOException e) { } return false; } });
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (mBluetoothAdapter == null) { Toast.makeText(this, "蓝牙设备不可用,请打开蓝牙!", Toast.LENGTH_LONG).show(); finish(); return; }
if (!mBluetoothAdapter.isEnabled()) {
Toast.makeText(this, "请打开蓝牙并重新运行程序!", Toast.LENGTH_LONG).show(); finish(); return; } }
public void onStart() {
正在阅读:
单片机与接口技术课外制作-Android蓝牙遥控小车编程参考05-14
2015电大现代产权法律制度专题(教学考一体化)答案必过01-27
腹膜透析病历首页-最终版05-27
07无机化学试卷A05-15
风电场远动与数据网设备介绍及检查项目.07-02
科学是什么作文250字07-14
长沙理工大学保研综合测评文件12-10
中班数学《10以内的数物对应》03-08
2015年信息组原试题A卷01-02
- 教学能力大赛决赛获奖-教学实施报告-(完整图文版)
- 互联网+数据中心行业分析报告
- 2017上海杨浦区高三一模数学试题及答案
- 招商部差旅接待管理制度(4-25)
- 学生游玩安全注意事项
- 学生信息管理系统(文档模板供参考)
- 叉车门架有限元分析及系统设计
- 2014帮助残疾人志愿者服务情况记录
- 叶绿体中色素的提取和分离实验
- 中国食物成分表2020年最新权威完整改进版
- 推动国土资源领域生态文明建设
- 给水管道冲洗和消毒记录
- 计算机软件专业自我评价
- 高中数学必修1-5知识点归纳
- 2018-2022年中国第五代移动通信技术(5G)产业深度分析及发展前景研究报告发展趋势(目录)
- 生产车间巡查制度
- 2018版中国光热发电行业深度研究报告目录
- (通用)2019年中考数学总复习 第一章 第四节 数的开方与二次根式课件
- 2017_2018学年高中语文第二单元第4课说数课件粤教版
- 上市新药Lumateperone(卢美哌隆)合成检索总结报告
- 蓝牙
- 技术课
- 单片机
- 小车
- 遥控
- 接口
- Android
- 编程
- 参考
- 制作
- rhel6.4无人值守安装
- 仓库管理系统详细设计
- 第二章 发电机运行
- 2009-2014年上半年湖北省食品制造业行业经营状况分析年报
- word中如何任意插入页码
- 河北省普通中小学校管理规定
- 掘锚一体化实现煤巷快速掘进的几点思考
- 六年级下册信息技术教案
- 物流设备生产制造项目可行性报告
- 立白广告策划书1
- 企业家应当具备的素质
- 【讲练通】2014年高中历史课时提升训练 第五单元 第1课 宗教改革的历史背景(人教版 选修1)
- 新型薄管板废热锅炉的应用
- 现在进行时态练习题 一
- 福建省2012年度春季省级机关公开遴选公务员考试公文写作题(作答参考)
- 河北省永年县第二中学2015-2016学年高二12月月考数学(理)试题 Word版含答案
- 建筑给排水工程的施工质量控制
- 第15讲 函数的综合应用
- 姜旭平 网络营销 第四章
- 金融英语 chapter 6