单片机与接口技术课外制作Android蓝牙遥控小车编程参考_第1页
单片机与接口技术课外制作Android蓝牙遥控小车编程参考_第2页
单片机与接口技术课外制作Android蓝牙遥控小车编程参考_第3页
单片机与接口技术课外制作Android蓝牙遥控小车编程参考_第4页
单片机与接口技术课外制作Android蓝牙遥控小车编程参考_第5页
已阅读5页,还剩21页未读 继续免费阅读

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

PAGE2/26《单片机与接口技术》课外制作(课程设计)编程参考(1)Android蓝牙遥控程序设计邹立杰广州大学华软软件学院软件工程系2014。5.22ConfigurationtheDevelopmentEnvironment:Downloadandinstall/configurationJavaSDKSEYoucanseekmethodsfrominternet.specially,youneedconfigurationthesystemenvironmentvariable“path".DownloadandInstallAndroidDevelopmentIDEDownloadthepackage:adt-bundle—windows—x86_64-20140321.zipfromtheURL:HYPERLINK”http:///sdk/index。html”http:///sdk/index.htmldownloadwebpage:Unpackagedthepackageadt—bundle—windows—x86_64-20140321。ziptoadirectoryonyourharddisk。 Ok,yourandroiddevelopmentenvironmentisok!CtreateaVirtualDeviceforYourAndroidApplication:starteclipseapplicationbydoubleclicktheexecutablefile:Theapplicationwindow:Selectthemenuitemasbelow:TheappeartheAVDmanagerdialog:Clickthe“New”button:FilltheAVDnameasyoulikename;Selectadevice;SelectaTarget;SelectaCPU/ABI;Selectaskin;Anydefaultitemyoucanselect。Thereferenceselectasbelow:Whenthebelowdialogappeared,youeAVDiscreatedok。StarttheAVD:Don’tclosetheAVD,KeeptheAVDalived!DesignYourCarBlue—teethRemoteProgram(1)CreateanAndroidApplicationProject:Fillthedialogitems:theclickthe“Next”buttonClickthe“Next”Button:Clickthe“Next”buttonAndcontinuetoclickthe“Next”button:Click“Finish”button,yourandroidapplicationprojectiscreated!ProjectFilesStruchure:ModifytheAndroidManifest。xmlfile:<manifestxmlns:android="http:///apk/res/android"package=”com。example.blue"android:versionCode="1"android:versionName="1.0"〉<uses—sdkandroid:minSdkVersion="8"android:targetSdkVersion=”15"/〉 <uses-permissionandroid:name="android.permission。BLUETOOTH_ADMIN”/> <uses-permissionandroid:name=”android。permission。BLUETOOTH"/>〈applicationandroid:icon=”@drawable/ic_launcher”android:label=”@string/app_name”android:theme=”@style/AppTheme”〉<activityandroid:name=".MainActivity”android:label="@string/title_activity_main"><intent—filter>〈actionandroid:name="ent.action.MAIN”/><categoryandroid:name="ent.category.LAUNCHER”/〉〈/intent—filter〉〈/activity></application>〈/manifest〉Add2sentencestotheAndroidManifest.xmlfile。Modifythelayoutfileactivity_main.xml〈?xmlversion="1.0"encoding="utf-8”?〉〈AbsoluteLayoutandroid:id=”@+id/widget0”android:layout_width="fill_parent"android:layout_height=”fill_parent"xmlns:android=”/apk/res/android"android:background=”@drawable/sise”〉<Buttonandroid:id="@+id/btnF"android:layout_width="88dp”android:layout_height=”wrap_content"android:layout_x=”124dp”android:layout_y=”58dp”android:text="向前"/><Buttonandroid:id=”@+id/btnS”android:layout_width="88dp"android:layout_height=”wrap_content"android:layout_x=”124dp"android:layout_y=”140dp"android:text="停止"/>〈Buttonandroid:id="@+id/btnB”android:layout_width="88dp"android:layout_height="wrap_content"android:layout_x="124dp”android:layout_y=”222dp”android:text="后退”/>〈Buttonandroid:id=”@+id/btnR"android:layout_width="88dp”android:layout_height="wrap_content”android:layout_x="227dp”android:layout_y="140dp”android:text=”右转”/>〈Buttonandroid:id="@+id/btnL"android:layout_width="88dp"android:layout_height=”wrap_content”android:layout_x=”23dp”android:layout_y=”140dp”android:text=”左转”/〉〈/AbsoluteLayout〉Modifytheresurcefilestrings。xml〈resources><stringname="app_name”>蓝牙小车遥控—华软学院</string>〈stringname=”hello_world"〉Helloworld!</string〉<stringname=”menu_settings”〉Settings</string>〈stringname="title_activity_main">蓝牙小车遥控-华软学院</string>〈/resources>ModifytheMainActivityfileMainActivity。javapackagecom。example。blue;importandroid.os。Bundle;importandroid。app。Activity;importandroid.view.Menu;importjava。io.IOException;importjava.io.OutputStream;importjava.util。UUID;importandroid。bluetooth。BluetoothAdapter;importandroid。bluetooth。BluetoothDevice;importandroid.bluetooth.BluetoothSocket;importandroid.view.Gravity;importandroid.view.MotionEvent;importandroid.view。View;importandroid。widget.Button;importandroid。widget。Toast;importcom。example。blue.R;publicclassMainActivityextendsActivity{privateBluetoothAdaptermBluetoothAdapter=null;privateBluetoothSocketbtSocket=null;privateOutputStreamoutStream=null;ButtonmButtonF;ButtonmButtonB;ButtonmButtonL;ButtonmButtonR;ButtonmButtonS;privatestaticfinalUUIDMY_UUID=UUID。fromString("00001101—0000-1000-8000—00805F9B34FB”);privatestaticStringaddress="20:14:04:15:12:46”;//蓝牙设备MAC地址@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//前进mButtonF=(Button)findViewById(R。id.btnF);mButtonF.setOnTouchListener(newButton。OnTouchListener(){ publicbooleanonTouch(Viewv,MotionEventevent){ //TODOAuto—generatedmethodstub Stringmessage; byte[]msgBuffer; intaction=event.getAction(); switch(action) { caseMotionEvent.ACTION_DOWN: try{ outStream=btSocket.getOutputStream(); }catch(IOExceptione){ } message="1”; msgBuffer=message.getBytes(); try{ outStream.write(msgBuffer); }catch(IOExceptione){} break; caseMotionEvent。ACTION_UP: try{ outStream=btSocket.getOutputStream(); }catch(IOExceptione){} message=”0”; msgBuffer=message。getBytes(); try{ outStream。write(msgBuffer); }catch(IOExceptione){} break; } returnfalse; } });//后退mButtonB=(Button)findViewById(R.id。btnB);mButtonB.setOnTouchListener(newButton。OnTouchListener(){ publicbooleanonTouch(Viewv,MotionEventevent){ //TODOAuto-generatedmethodstub Stringmessage; byte[]msgBuffer; intaction=event.getAction(); switch(action) { caseMotionEvent.ACTION_DOWN: try{ outStream=btSocket.getOutputStream(); }catch(IOExceptione){} message=”3"; msgBuffer=message。getBytes(); try{ outStream。write(msgBuffer); }catch(IOExceptione){} break; caseMotionEvent。ACTION_UP: try{ outStream=btSocket。getOutputStream(); }catch(IOExceptione){} message=”0"; msgBuffer=message.getBytes(); try{ outStream.write(msgBuffer); }catch(IOExceptione){} break; } returnfalse; }});//左转mButtonL=(Button)findViewById(R.id。btnL);mButtonL。setOnTouchListener(newButton.OnTouchListener(){ publicbooleanonTouch(Viewv,MotionEventevent){ //TODOAuto-generatedmethodstub Stringmessage; byte[]msgBuffer; intaction=event.getAction(); switch(action) { caseMotionEvent.ACTION_DOWN: try{ outStream=btSocket。getOutputStream(); }catch(IOExceptione){} message="2"; msgBuffer=message.getBytes(); try{ outStream.write(msgBuffer); }catch(IOExceptione){} break; caseMotionEvent.ACTION_UP: try{ outStream=btSocket。getOutputStream(); }catch(IOExceptione){} message=”0"; msgBuffer=message。getBytes(); try{ outStream。write(msgBuffer); }catch(IOExceptione){} break; } returnfalse; }});//右转mButtonR=(Button)findViewById(R.id.btnR);mButtonR.setOnTouchListener(newButton.OnTouchListener(){ publicbooleanonTouch(Viewv,MotionEventevent){ //TODOAuto-generatedmethodstub Stringmessage; byte[]msgBuffer; intaction=event.getAction(); switch(action) { caseMotionEvent。ACTION_DOWN: try{ outStream=btSocket。getOutputStream(); }catch(IOExceptione){} message=”4"; msgBuffer=message。getBytes(); try{ outStream.write(msgBuffer); }catch(IOExceptione){} break; caseMotionEvent。ACTION_UP: try{ outStream=btSocket.getOutputStream(); }catch(IOExceptione){} message="0”; msgBuffer=message.getBytes(); try{ outStream.write(msgBuffer); }catch(IOExceptione){} break; } returnfalse; }});//停止mButtonS=(Button)findViewById(R。id.btnS);mButtonS.setOnTouchListener(newButton。OnTouchListener(){ publicbooleanonTouch(Viewv,MotionEventevent){ //TODOAuto—generatedmethodstub if(event.getAction()==MotionEvent.ACTION_DOWN) try{ outStream=btSocket.getOutputStream();}catch(IOExceptione){}Stringmessage="0";byte[]msgBuffer=message。getBytes();try{ outStream。write(msgBuffer);}catch(IOExceptione){}returnfalse; }});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;}}publicvoidonStart(){super.onStart();}@OverridepublicvoidonResume(){super.onResume();DisplayToast(”正在尝试连接智能小车,请稍后····”);BluetoothDevicedevice=mBluetoothAdapter.getRemoteDevice(address);try{ btSocket=device.createRfcommSocketToServiceRecord(MY_UUID);}catch(IOExceptione){DisplayToast(”套接字创建失败!");}DisplayToast("成功连接智能小车!可以开始操控了~~~");mBluetoothAdapter.cancelDiscovery();try{ btSocket。connect(); DisplayToast(”连接成功建立,数据连接打开!”);}catch(IOExceptione){

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论