




版權(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年驅(qū)鼠器項(xiàng)目建議書
- 中學(xué)化學(xué)教學(xué)設(shè)計(jì)知到課后答案智慧樹章節(jié)測(cè)試答案2025年春魯東大學(xué)
- 2017-2018學(xué)年人教A版高中數(shù)學(xué)選修2-3檢測(cè)第一章計(jì)數(shù)原理單元質(zhì)量評(píng)估(一)
- 高考物理課標(biāo)版一輪復(fù)習(xí)考點(diǎn)規(guī)范練7牛頓第一定律牛頓第三定律
- 2024-2025學(xué)習(xí)筆記歷史(部編版)選必一第四單元 第12課 近代西方民族國家與國際法的發(fā)展
- 淺析打葉復(fù)烤企業(yè)的設(shè)備點(diǎn)檢工作
- 心理契約對(duì)建造師的項(xiàng)目公民行為的影響關(guān)系研究
- 西格列汀聯(lián)合二甲雙胍在2型糖尿病患者中的有效性和安全性以及對(duì)炎癥和氧化應(yīng)激的影響的研究
- 中國婚姻制度的嬗變歷程探索
- 新診斷2型糖尿病患者神經(jīng)電生理異常特點(diǎn)及相關(guān)因素研究
- ISO22000:2018體系之食品安全管理手冊(cè)含程序文件匯編2022版
- DZ∕T 0214-2020 礦產(chǎn)地質(zhì)勘查規(guī)范 銅、鉛、鋅、銀、鎳、鉬(正式版)
- QCSG1204009-2015電力監(jiān)控系統(tǒng)安全防護(hù)技術(shù)規(guī)范
- (正式版)SHT 3158-2024 石油化工管殼式余熱鍋爐
- 新生兒傳統(tǒng)四病篩查
- GB/T 41666.4-2024地下無壓排水管網(wǎng)非開挖修復(fù)用塑料管道系統(tǒng)第4部分:原位固化內(nèi)襯法
- 小班社會(huì)《多喝水身體好》課件
- 2024年單招必備時(shí)事政治200題(含答案)
- 人教版小學(xué)數(shù)學(xué)三年級(jí)下冊(cè)全冊(cè)同步練習(xí)(含答案)
- 綠色守護(hù)者PPT模板
- 人工智能時(shí)代的就業(yè)與職業(yè)發(fā)展
評(píng)論
0/150
提交評(píng)論