单片机与接口技术课外制作-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() {

本文来源:https://www.bwwdw.com/article/gk6e.html

Top