成功實(shí)現(xiàn)手機(jī)藍(lán)牙控制智能小車機(jī)器人!視頻+程序源代碼Android_第1頁(yè)
成功實(shí)現(xiàn)手機(jī)藍(lán)牙控制智能小車機(jī)器人!視頻+程序源代碼Android_第2頁(yè)
成功實(shí)現(xiàn)手機(jī)藍(lán)牙控制智能小車機(jī)器人!視頻+程序源代碼Android_第3頁(yè)
成功實(shí)現(xiàn)手機(jī)藍(lán)牙控制智能小車機(jī)器人!視頻+程序源代碼Android_第4頁(yè)
成功實(shí)現(xiàn)手機(jī)藍(lán)牙控制智能小車機(jī)器人!視頻+程序源代碼Android_第5頁(yè)
已閱讀5頁(yè),還剩7頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、但是通過(guò)電腦控制畢竟上次成功實(shí)現(xiàn)了通過(guò)筆記本電腦藍(lán)牙來(lái)控制智能小車機(jī)器人的運(yùn)動(dòng),不方便,于是乎本人打算將控制程序移植到手機(jī)上。目前主流的手機(jī)操作系統(tǒng)有塞班、安卓(Android)、WindowsMobile,對(duì)比了一下,首先,塞班是用C+寫的,這么多門語(yǔ)言我唯獨(dú)看到C+就頭大,放棄了;WindowsMoblie其實(shí)和之前發(fā)的電腦端程序基本是一樣的,也就沒(méi)什么意思了,最后決定選擇目前正火的Android手機(jī)作為控制平臺(tái)。Android是個(gè)開(kāi)源的應(yīng)用,使用Java語(yǔ)言對(duì)其編程。于是這次的開(kāi)發(fā)我選用Eclipse作為開(kāi)發(fā)工具,用Java語(yǔ)言開(kāi)發(fā)手機(jī)端的控制程序,由于之前對(duì)Android的藍(lán)牙通信這塊

2、涉及不多,一開(kāi)始感覺(jué)有點(diǎn)小茫然,而網(wǎng)上也少有這方面的例程,有少數(shù)人做出了類似的東西,但是只傳了個(gè)視頻裝X!雪特經(jīng)過(guò)幾天的研究,最終確定了手機(jī)藍(lán)牙通信其實(shí)就是Socket編程,再經(jīng)過(guò)一番編寫和調(diào)試,昨晚終于大功告成!這是視頻:下面開(kāi)始介紹Android手機(jī)端控制程序的編寫:首先打開(kāi)Eclipse,當(dāng)然之前的Java開(kāi)發(fā)環(huán)境和安卓開(kāi)發(fā)工具自己得先配置好,這里就不多說(shuō)了,網(wǎng)上教程一大摞。然后新建一個(gè)Android項(xiàng)目,修改布局文件main.xml,代碼如下:<?xmlversion="1.0"encoding="utf-8"?><Absolu

3、teLayoutandroid:id="+id/widget0"android:layout_width="fill_parent"android:layout_height="fill_parent"xmlns:android="Buttonandroid:id="+id/btnF"android:layout_width="100px”android:layout_height="60px"android:text="前進(jìn)"android:layout

4、_x="130px”android:layout_y="62px"></Button><Buttonandroid:id="+id/btnL"android:layout_width="100px"android:layout_height="60px"android:text="左轉(zhuǎn)"android:layout_x="20px"android:layout_y="152px"></Button><

5、;Buttonandroid:id="+id/btnR"android:layout_width="100px"android:layout_height="60px"android:text="右轉(zhuǎn)"android:layout_x="240px"android:layout_y="152px"></Button><Buttonandroid:id="+id/btnB"android:layout_width="100p

6、x"android:layout_height="60px"android:text="后退"android:layout_x="130px"android:layout_y="242px"></Button><Buttonandroid:id="+id/btnS"android:layout_width="100px"android:layout_height="60px"android:text="停止&qu

7、ot;android:layout_x="130px"android:layout_y="152px"></Button></AbsoluteLayout>這個(gè)布局文件的效果就是如視頻中所示的手機(jī)操作界面。然后是權(quán)限聲明,這一步不能少,否則將無(wú)法使用安卓手機(jī)的藍(lán)牙功能。權(quán)限聲明如下:打開(kāi)AndroidManifest.xml文件,修改代碼如下:<?xmlversion="1.0"encoding="utf-8"?><manifestxmlns:android=&quo

8、t;package="com.ThinBTClient.www"android:versionCode="1"android:versionName="1.0"><uses-permissionandroid:name="android.permission.BLUETOOTH_ADMIN"/><uses-permissionandroid:name="android.permission.BLUETOOTH"/><applicationandroid:ico

9、n="drawable/icon"android:label="string/app_name"><activityandroid:name=".ThinBTClient"android:label="string/app_name"><intent-filter><actionandroid:name="ent.action.MAIN"/><categoryandroid:name="ent.

10、category.LAUNCHER"/></intent-filter></activity></application></manifest>其中紅色、加粗部分就是要添加的權(quán)限聲明。然后編寫Activity中的執(zhí)行代碼,這些代碼的作用就是發(fā)送指令,控制小車的運(yùn)動(dòng)。代碼如下:packagecom.ThinBTClient.www;importandroid.app.Activity;importandroid.os.Bundle;importjava.io.IOException;importjava.io.OutputStrea

11、m;importjava.util.UUID;importandroid.app.Activity;importandroid.bluetooth.BluetoothAdapter;importandroid.bluetooth.BluetoothDevice;importandroid.bluetooth.BluetoothSocket;importandroid.content.DialogInterface;importandroid.content.DialogInterface.OnClickListener;importandroid.os.Bundle;importandroid

12、.provider.ContactsContract.CommonDataKinds.Event;importandroid.util.Log;importandroid.view.MotionEvent;importandroid.view.View;importandroid.widget.Button;importandroid.widget.Toast;publicclassThinBTClientextendsActivityprivatestaticfinalStringTAG="THINBTCLIENT"privatestaticfinalbooleanD=t

13、rue;privateBluetoothAdaptermBluetoothAdapter=null;privateBluetoothSocketbtSocket=null;privateOutputStreamoutStream=null;ButtonmButtonF;ButtonmButtonB;ButtonmButtonL;ButtonmButtonR;ButtonmButtonS;privatestaticfinalUUIDMY_UUID=UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");privatest

14、aticStringaddress="00:11:03:21:00:43"/<=要連接的藍(lán)牙設(shè)備MAC地址/*Calledwhentheactivityisfirstcreated.*/OverridepublicvoidonCreate(BundlesavedInstanceState)super.onCreate(savedInstanceState);setContentView(R.layout.main);前進(jìn)mButtonF=(Button)findViewById(R.id.btnF);mButtonF.setOnTouchListener(newBut

15、ton.OnTouchListener()OverridepublicbooleanonTouch(Viewv,MotionEventevent)/TODOAuto-generatedmethodstubStringmessage;bytemsgBuffer;intaction=event.getAction();switch(action)caseMotionEvent.ACTION_DOWN:tryoutStream=btSocket.getOutputStream();catch(IOExceptione)Log.e(TAG,"ONRESUME:Outputstreamcrea

16、tionfailed.",e);message="1"msgBuffer=message.getBytes();tryoutStream.write(msgBuffer);catch(IOExceptione)Log.e(TAG,"ONRESUME:Exceptionduringwrite.",e);break;caseMotionEvent.ACTION_UP:tryoutStream=btSocket.getOutputStream();catch(IOExceptione)Log.e(TAG,"ONRESUME:Outputst

17、reamcreationfailed.",e);message="0"msgBuffer=message.getBytes();tryoutStream.write(msgBuffer);catch(IOExceptione)Log.e(TAG,"ONRESUME:Exceptionduringwrite.",e);break;returnfalse;);后退mButtonB=(Button)findViewById(R.id.btnB);mButtonB.setOnTouchListener(newButton.OnTouchListener

18、()OverridepublicbooleanonTouch(Viewv,MotionEventevent)/TODOAuto-generatedmethodstubStringmessage;bytemsgBuffer;intaction=event.getAction();switch(action)caseMotionEvent.ACTION_DOWN:tryoutStream=btSocket.getOutputStream();catch(IOExceptione)Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",e

19、);message="3"msgBuffer=message.getBytes();tryoutStream.write(msgBuffer);catch(IOExceptione)Log.e(TAG,"ONRESUME:Exceptionduringwrite.",e);break;caseMotionEvent.ACTION_UP:tryoutStream=btSocket.getOutputStream();catch(IOExceptione)Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.

20、",e);message="0"msgBuffer=message.getBytes();try(outStream.write(msgBuffer);catch(IOExceptione)Log.e(TAG,"ONRESUME:Exceptionduringwrite.",e);break;returnfalse;);左轉(zhuǎn)mButtonL=(Button)findViewById(R.id.btnL);mButtonL.setOnTouchListener(newButton.OnTouchListener()Overridepublicbo

21、oleanonTouch(Viewv,MotionEventevent)/TODOAuto-generatedmethodstubStringmessage;bytemsgBuffer;intaction=event.getAction();switch(action)caseMotionEvent.ACTION_DOWN:tryoutStream=btSocket.getOutputStream();catch(IOExceptione)Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",e);message="2&

22、quot;msgBuffer=message.getBytes();tryoutStream.write(msgBuffer);Log.e(TAG,"ONRESUME:Exceptionduringwrite.",e);break;caseMotionEvent.ACTION_UP:tryoutStream=btSocket.getOutputStream();e);catch(IOExceptione)Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",message="0"msgBuf

23、fer=message.getBytes();tryoutStream.write(msgBuffer);catch(IOExceptione)Log.e(TAG,"ONRESUME:Exceptionduringwrite.",e);break;returnfalse;);右轉(zhuǎn)mButtonR=(Button)findViewById(R.id.btnR);mButtonR.setOnTouchListener(newButton.OnTouchListener()OverridepublicbooleanonTouch(Viewv,MotionEventevent)/T

24、ODOAuto-generatedmethodstubStringmessage;bytemsgBuffer;intaction=event.getAction();switch(action)caseMotionEvent.ACTION_DOWN:tryoutStream=btSocket.getOutputStream();catch(lOExceptione)Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",e);message="4"msgBuffer=message.getBytes();tryo

25、utStream.write(msgBuffer);catch(IOExceptione)Log.e(TAG,"ONRESUME:Exceptionduringwrite.",e);break;caseMotionEvent.ACTION_UP:tryoutStream=btSocket.getOutputStream();e);catch(IOExceptione)Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",message="0"msgBuffer=message.getByte

26、s();tryoutStream.write(msgBuffer);catch(IOExceptione)Log.e(TAG,"ONRESUME:Exceptionduringwrite.",e);break;returnfalse;);/停止mButtonS=(Button)findViewById(R.id.btnS);mButtonS.setOnTouchListener(newButton.OnTouchListener()OverridepublicbooleanonTouch(Viewv,MotionEventevent)/TODOAuto-generatedm

27、ethodstubif(event.getAction()=MotionEvent.ACTION_DOWN)tryoutStream=btSocket.getOutputStream();catch(IOExceptione)Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",e);Stringmessage="0"bytemsgBuffer=message.getBytes();tryoutStream.write(msgBuffer);catch(IOExceptione)Log.e(TAG,"

28、ONRESUME:Exceptionduringwrite.",e);returnfalse;);if(D)Log.e(TAG,"+ONCREATE+”);mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();if(mBluetoothAdapter=null)Toast.makeText(this,"Bluetoothisnotavailable.",Toast.LENGTH_LONG).show();finish();return;if(!mBluetoothAdapter.isEnabled(

29、)Toast.makeText(this,"PleaseenableyourBluetoothandre-runthisprogram.",Toast.LENGTH_LONG).show();finish();return;if(D)Log.e(TAG,"+DONEINONCREATE,GOTLOCALBTADAPTER+”);OverridepublicvoidonStart()super.onStart();if(D)Log.e(TAG,"+ONSTART+");OverridepublicvoidonResume()super.onRes

30、ume();if(D)Log.e(TAG,"+ONRESUME+");Log.e(TAG,"+ABOUTTOATTEMPTCLIENTCONNECT+");BluetoothDevicedevice=mBluetoothAdapter.getRemoteDevice(address);trybtSocket=device.createRfcommSocketToServiceRecord(MY_UUID);catch(IOExceptione)Log.e(TAG,"ONRESUME:Socketcreationfailed.",e);

31、mBluetoothAdapter.cancelDiscovery();trybtSocket.connect();Log.e(TAG,"ONRESUME:BTconnectionestablished,datatransferlinkopen.");try(btSocket.close();catch(lOExceptione2)Log.e(TAG,"ONRESUME:Unabletoclosesocketduringconnectionfailure",e2);/Createadatastreamsowecantalktoserver.if(D)Log.e(TAG,"+ABOUTTOSAYSOMETHI

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 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ì)用戶上傳內(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)論