版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、word但是通过电脑控制毕竟上次成功实现了通过笔记本电脑蓝牙来控制智能小车机器人的运动, 不方便,于是乎本人打算将控制程序移植到手机上。目前主流的手机操作系统有塞班、安卓An droid、Win dows Mobile,比照了一下,首先,塞班是用C+写的,这么多门语言我唯独看到C+就头大,放弃了 ,Windows Moblie其实和之前发的电脑端程序根本是一样的,也就没什么意思了,最后决定选择目前正火的 An droid手机作为控制平台。An droid是个开源的应用,使用Java语言对其编程。于是这次的开发我选用Eclipse作为开发工具,用Java语言开发手机端的控制程序,由于之前对An
2、droid的蓝牙通信这块涉与不多,一开始感觉有点小茫然,而网上也少有这方面的例程,有少数人做出了类似的东西,但 是只传了个视频装X!雪特经过几天的研究,最终确定了手机蓝牙通信其实就是Socket编程,再经过一番编写和调试,昨晚终于大功告成!这是视频:下面开始介绍An droid手机端控制程序的编写:首先打开Eclipse,当然之前的Java开发环境和安卓开发工具自己得先配置好,这里就不多 说了,网上教程一大摞。然后新建一个 An droid项目,修改布局文件 mai n. xml,代码如下: 这个布局文件的效果就是如视频中所示的手机操作界面。然后是权限声明,这一步不能少,否如此将无法使用安卓手
3、机的蓝牙功能。 权限声明如下:打开AndroidManifest.xml 文件,修改代码如下: applicati on an droid:ic on=drawable/ic on an droid:label=stri ng/app_ name vactivity an droid: name=.Thi nBTClie ntan droid:label=stri ng/app_ namevcategory an droid: name=a ndroid.i nten t.category.LAUNCHER /其中红色、加粗局部就是要添加的权限声明。然后编写Activity中的执行代码,这些代
4、码的作用就是发送指令,控制小车的运动。代码如下:package .Th in BTClie nt.;import an droid.app.Activity;import an droid.os.B un dle;import java.i o.I OExcepti on;import java.io.OutputStream;import java.util.UUID;import an droid.app.Activity;import an droid.bluetooth.BluetoothAdapter;import an droid.bluetooth.BluetoothDevice
5、;import an droid.bluetooth.BluetoothSocket;import an droid.c onten t.Dialog In terface;import an droid.c onten t.Dialog In terface. On ClickListe ner;import an droid.os.B un dle;import an vider .Con tactsC on tractmon DataK in ds.Eve nt;import an droid.util.Log;import an droid.view.Moti on
6、Eve nt;import an droid.view.View;import an droid.widget.Butt on;import an droid.widget.Toast;public class Thi nBTClie nt exte nds Activity private static final String TAG = THINBTCLIENT;private static final boolean D = true;private BluetoothAdapter mBIuetoothAdapter = n ull;private BluetoothSocket b
7、tSocket = n ull;private OutputStream outStream = n ull;Button mButto nF;Button mButto nB;Button mButto nL;Button mButt onR;Button mButto nS;privatestaticfinalUUIDMY_UUIDUUID.fromStri ng(00001101-0000-1000-8000-00805F9B34FB);要连接的蓝牙设备MACprivate static String address = 00:11:03:21:00:43; / =地址/* Called
8、 when the activity is first created. */Overridepublic void on Create(B un dle savedI nsta nceState) super. on Create(saved In sta nceState);setC onten tView(R .l ayout.ma in);/前进mButto nF=(Butto n)fin dViewByld(R.id.bt nF);mButtonF .setOnTouchListener(new Button.OnTouchListener()Overridepublic boole
9、a n on Touch(View v, Moti onEvent eve nt) / TODO Auto-ge nerated method stubString message;byte msgBuffer;int action = even t.getAct ion();switch(act ion)case Motio nEven t.ACTION_DOWN:try outStream = btSocket.getOutputStream(); catch (IOExceptio n e) Log.e(TAG, ON RESUME: Output stream creation fai
10、led., e); message = 1;msgBuffer = message.getBytes();try outStream.write(msgBuffer); catch (IOExceptio n e) Log.e(TAG, ON RESUME: Exception during write., e);break;case Motio nEven t.ACTION_UP:try outStream = btSocket.getOutputStream(); catch (IOExcepti on e) e);Log.e(TAG, ON RESUME: Output stream c
11、reation failed.,message = 0;msgBuffer = message.getBytes();try outStream.write(msgBuffer); catch (IOExcepti on e) Log.e(TAG, ON RESUME: Exception during write., e);break;return false;);/后退mButto nB=(Butto n)fin dViewByld(R.id.b tn B);mButt on B.set On TouchListe ner(new Butt on .0 nTouchListe ner()O
12、verridepublic boolea n on Touch(View v, Moti onEvent eve nt) / TODO Auto-ge nerated method stubString message;byte msgBuffer;int action = even t.getAct ion();switch(act ion)case Motio nEven t.ACTION_DOWN:try outStream = btSocket.getOutputStream(); catch (lOExceptio n e) Log.e(TAG, ON RESUME: Output
13、stream creation failed., e); message = 3;msgBuffer = message.getBytes();try outStream.write(msgBuffer); catch (lOExceptio n e) Log.e(TAG, ON RESUME: Exception during write., e); break;case Motio nEven t.ACTION_UP:try outStream = btSocket.getOutputStream(); catch (lOExcepti on e) Log.e(TAG, ON RESUME
14、: Output stream creation failed.,e);message = 0;break;msgBuffer = message.getBytes();try outStream.write(msgBuffer); catch (lOExcepti on e) Log.e(TAG, ON RESUME: Exception during write., e);return false;);/左转mButto nL=(Butto n)fin dViewByld(R.id.bt nL);mButt on L.set On TouchListe ner(new Butt on .0
15、 nTouchListe ner()Overridepublic boolea n on Touch(View v, Moti onEvent eve nt) / TODO Auto-ge nerated method stubString message;byte msgBuffer;int action = even t.getAct ion();switch(act ion)case Motio nEven t.ACTION_DOWN:try outStream = btSocket.getOutputStream(); catch (lOExceptio n e) Log.e(TAG,
16、 ON RESUME: Output stream creation failed., e);message = 2;msgBuffer = message.getBytes();try outStream.write(msgBuffer); catch (lOExceptio n e) Log.e(TAG, ON RESUME: Exception during write., e);break;case Motio nEven t.ACTION_UP:try outStream = btSocket.getOutputStream(); catch (IOExcepti on e) e);
17、Log.e(TAG, ON RESUME: Output stream creation failed.,message = 0;msgBuffer = message.getBytes();try outStream.write(msgBuffer); catch (IOExcepti on e) Log.e(TAG, ON RESUME: Exception during write., e);break;return false;);/右转mButto nR=(Butto n)fin dViewByld(R.id.bt nR);mButt on R.set On TouchListe n
18、er(new Butt on .O nTouchListe ner()Overridepublic boolea n on Touch(View v, Moti onEvent eve nt) / TODO Auto-ge nerated method stubString message;byte msgBuffer;int action = even t.getAct ion();switch(act ion)case Motio nEven t.ACTION_DOWN:try outStream = btSocket.getOutputStream(); catch (IOExcepti
19、o n e) Log.e(TAG, ON RESUME: Output stream creation failed., e);message = 4;msgBuffer = message.getBytes();try outStream.write(msgBuffer); catch (lOExceptio n e) Log.e(TAG, ON RESUME: Exception during write., e);break;case Motio nEven t.ACTION_UP:try outStream = btSocket.getOutputStream(); catch (IO
20、Excepti on e) e);Log.e(TAG, ON RESUME: Output stream creation failed.,message = 0;msgBuffer = message.getBytes();try outStream.write(msgBuffer); catch (IOExcepti on e) Log.e(TAG, ON RESUME: Exception during write., e);break;return false;);停止mButto nS=(Butto n)fin dViewByld(R.id.b tn S);mButt on S.se
21、t On TouchListe ner(new Butt on .O nTouchListe ner()Overridepublic boolea n on Touch(View v, Moti onEvent eve nt) / TODO Auto-ge nerated method stubif(eve nt.getActio n( )=Motio nEven t.ACTION_DOWN)try outStream = btSocket.getOutputStream(); catch (IOExcepti on e) Log.e(TAG, ON RESUME: Output stream
22、 creation failed.,e);String message = 0;byte msgBuffer = message.getBytes();try outStream.write(msgBuffer); catch (IOExcepti on e) Log.e(TAG, ON RESUME: Exception during write., e);return false;);if (D)Log.e(TAG, + ON CREATE +);mBluetoothAdapter = BluetoothAdapter .getDefaultAdapter();if (mBluetooth
23、Adapter = n ull) Toast.makeText(this,Bluetoothisnotavailable.,Toast 丄 ENGTH_LONG).show();fin ish();return;if (MBluetoothAdapter .isE nabled() Toast.makeText(this, Please enable your Bluetooth and re-run this program., Toast.LENGTH_LONG).show();fin ish();return;if (D)Log.e(TAG, + DONE IN ON CREATE, G
24、OT LOCAL BT ADAPTER +);Overridepublic void on Start() super. on Start();if (D) Log.e(TAG, + ON START +);Overridepublic void on Resume。super. on Resume();if (D) Log.e(TAG, + ON RESUME +);Log.e(TAG, + ABOUT TO ATTEMPT CLIENT CONNECT +);BluetoothDevice device = mBIuetoothAdapter .getRemoteDevice(addres
25、s);try btSocket = device.createRfmSocketToServiceRecord(MY_UUID); catch (IOExceptio n e) Log.e(TAG, ON RESUME: Socket creation failed., e);mBluetoothAdapter .cancelDiscovery();try btSocket.c onn ect();Log.e(TAG, ON RESUME: BT co nn ection established, data tran sfer link ope n.); catch (IOExceptio n
26、 e) try btSocket.close(); catch (lOException e2) Log .e(TAG,ON RESUME: Un able to close socket during conn ecti on failure, e2);/ Create a data stream so we can talk to server .if (D)Log.e(TAG, + ABOUT TO SAY SOMETHING TO SERVER +);/* try outStream = btSocket.getOutputStream(); catch (IOExceptio n e) Log.e(TAG, ON RESUME: Output stream creation failed., e);String message = 1;byte
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年办公室装修工程协议
- 创业家的伦理道德与企业社会责任考核试卷
- 《虚拟偶像粉丝群体的情感劳动研究》
- 《空间理论视域下《时间中的孩子》中的人物困境研究》
- 内陆养殖借势资源禀赋优化农业产业链的驱动力考核试卷
- 制鞋企业市场渠道策略考核试卷
- 2024年定制音响系统安装服务协议
- 《商业银行适当性义务研究》
- 2024至2030年中国铁路货车检修翻转机行业投资前景及策略咨询研究报告
- 2024至2030年中国转速校验台数据监测研究报告
- (零模)徐州市2024~2025学年上学期高三期中考试 英语试卷(含答案)
- 动脉瘤栓塞术术后护理
- 四川公安基础知识模拟5
- 英语KET官方样题Test1- Test 2
- 财务管理考试试题及答案
- 【课件】第七单元能源的合理利用与开发新版教材单元分析-九年级化学人教版(2024)上册
- 2024年认证行业法律法规及认证基础知识 CCAA年度确认 试题与答案
- 水库除险加固工程实施方案
- 5.1平行与垂直(进阶练习)2024-2025学年人教版数学四年级上册
- 8《安全记心上》第1课时 教学设计-2024-2025学年道德与法治三年级上册统编版
- 2024年人教版小学六年级数学(上册)期中考卷附答案
评论
0/150
提交评论