單片機(jī)與接口技術(shù)課外制作Android藍(lán)牙遙控小車編程參考_第1頁
單片機(jī)與接口技術(shù)課外制作Android藍(lán)牙遙控小車編程參考_第2頁
單片機(jī)與接口技術(shù)課外制作Android藍(lán)牙遙控小車編程參考_第3頁
單片機(jī)與接口技術(shù)課外制作Android藍(lán)牙遙控小車編程參考_第4頁
單片機(jī)與接口技術(shù)課外制作Android藍(lán)牙遙控小車編程參考_第5頁
已閱讀5頁,還剩21頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

PAGE2/26《單片機(jī)與接口技術(shù)》課外制作(課程設(shè)計(jì))編程參考(1)Android藍(lán)牙遙控程序設(shè)計(jì)鄒立杰廣州大學(xué)華軟軟件學(xué)院軟件工程系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〉A(chǔ)dd2sentencestotheAndroidManifest.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=”右轉(zhuǎn)”/>〈Buttonandroid:id="@+id/btnL"android:layout_width="88dp"android:layout_height=”wrap_content”android:layout_x=”23dp”android:layout_y=”140dp”android:text=”左轉(zhuǎn)”/〉〈/AbsoluteLayout〉Modifytheresurcefilestrings。xml〈resources><stringname="app_name”>藍(lán)牙小車遙控—華軟學(xué)院</string>〈stringname=”hello_world"〉Helloworld!</string〉<stringname=”menu_settings”〉Settings</string>〈stringname="title_activity_main">藍(lán)牙小車遙控-華軟學(xué)院</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”;//藍(lán)牙設(shè)備MAC地址@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//前進(jìn)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; }});//左轉(zhuǎn)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; }});//右轉(zhuǎn)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,"藍(lán)牙設(shè)備不可用,請(qǐng)打開藍(lán)牙!”,Toast.LENGTH_LONG).show();finish();return;}if(!mBluetoothAdapter.isEnabled()){Toast。makeText(this,"請(qǐng)打開藍(lán)牙并重新運(yùn)行程序!”,Toast。LENGTH_LONG).show();finish();return;}}publicvoidonStart(){super.onStart();}@OverridepublicvoidonResume(){super.onResume();DisplayToast(”正在嘗試連接智能小車,請(qǐng)稍后····”);BluetoothDevicedevice=mBluetoothAdapter.getRemoteDevice(address);try{ btSocket=device.createRfcommSocketToServiceRecord(MY_UUID);}catch(IOExceptione){DisplayToast(”套接字創(chuàng)建失??!");}DisplayToast("成功連接智能小車!可以開始操控了~~~");mBluetoothAdapter.cancelDiscovery();try{ btSocket。connect(); DisplayToast(”連接成功建立,數(shù)據(jù)連接打開!”);}catch(IOExceptione){

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論