




版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
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)牙小車(chē)遙控—華軟學(xué)院</string>〈stringname=”hello_world"〉Helloworld!</string〉<stringname=”menu_settings”〉Settings</string>〈stringname="title_activity_main">藍(lán)牙小車(chē)遙控-華軟學(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)打開(kāi)藍(lán)牙!”,Toast.LENGTH_LONG).show();finish();return;}if(!mBluetoothAdapter.isEnabled()){Toast。makeText(this,"請(qǐng)打開(kāi)藍(lán)牙并重新運(yùn)行程序!”,Toast。LENGTH_LONG).show();finish();return;}}publicvoidonStart(){super.onStart();}@OverridepublicvoidonResume(){super.onResume();DisplayToast(”正在嘗試連接智能小車(chē),請(qǐng)稍后····”);BluetoothDevicedevice=mBluetoothAdapter.getRemoteDevice(address);try{ btSocket=device.createRfcommSocketToServiceRecord(MY_UUID);}catch(IOExceptione){DisplayToast(”套接字創(chuàng)建失敗!");}DisplayToast("成功連接智能小車(chē)!可以開(kāi)始操控了~~~");mBluetoothAdapter.cancelDiscovery();try{ btSocket。connect(); DisplayToast(”連接成功建立,數(shù)據(jù)連接打開(kāi)!”);}catch(IOExceptione){
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 睡眠呼吸暫停癥的治療與護(hù)理
- 2025年校園安全管理報(bào)告:智慧校園環(huán)境下的校園安全設(shè)施維護(hù)
- 人民教育網(wǎng)中醫(yī)課件
- 車(chē)工工藝與技能訓(xùn)練(第二版)課件:工藝路線的制訂
- 糖尿病飲食的護(hù)理
- 馬克杯手繪藝術(shù)基礎(chǔ)教學(xué)
- 直腸癌患者術(shù)后的護(hù)理
- 膿毒血癥護(hù)理查房-圖文
- 偏癱疾病健康宣教要點(diǎn)
- 語(yǔ)文學(xué)科闖關(guān)課件設(shè)計(jì)大綱
- 4、《通向金融王國(guó)的自由之路》
- 煤礦機(jī)電和運(yùn)輸安全管理
- 大學(xué)生職業(yè)素養(yǎng)(高職)全套教學(xué)課件
- 涉密內(nèi)網(wǎng)分級(jí)保護(hù)設(shè)計(jì)方案
- 日本旅游路線展示課件
- 木地板培訓(xùn)資料大全
- 康養(yǎng)旅游概念及市場(chǎng)現(xiàn)狀分析
- 99版-干部履歷表-A4打印
- 人教版六年級(jí)上冊(cè)數(shù)學(xué)(新插圖) 倒數(shù)的認(rèn)識(shí) 教學(xué)課件
- CJJ 36-2016 城鎮(zhèn)道路養(yǎng)護(hù)技術(shù)規(guī)范
- 中華傳統(tǒng)文化之文學(xué)瑰寶學(xué)習(xí)通超星課后章節(jié)答案期末考試題庫(kù)2023年
評(píng)論
0/150
提交評(píng)論