成功实现手机蓝牙控制智能小车机器人视频程序源代码AndroidWord文件下载.docx
- 文档编号:17438506
- 上传时间:2022-12-01
- 格式:DOCX
- 页数:16
- 大小:19.44KB
成功实现手机蓝牙控制智能小车机器人视频程序源代码AndroidWord文件下载.docx
《成功实现手机蓝牙控制智能小车机器人视频程序源代码AndroidWord文件下载.docx》由会员分享,可在线阅读,更多相关《成功实现手机蓝牙控制智能小车机器人视频程序源代码AndroidWord文件下载.docx(16页珍藏版)》请在冰豆网上搜索。
>
AbsoluteLayout
android:
id="
@+id/widget0"
layout_width="
fill_parent"
layout_height="
xmlns:
android="
Button
@+id/btnF"
100px"
60px"
text="
前进"
layout_x="
130px"
layout_y="
62px"
/Button>
@+id/btnL"
左转"
20px"
152px"
@+id/btnR"
右转"
240px"
@+id/btnB"
后退"
242px"
@+id/btnS"
停止"
/AbsoluteLayout>
这个布局文件的效果就是如视频中所示的手机操作界面。
然后是权限声明,这一步不能少,否则将无法使用安卓手机的蓝牙功能。
权限声明如下:
打开AndroidManifest.xml文件,修改代码如下:
manifestxmlns:
package="
com.ThinBTClient.www"
android:
versionCode="
1"
versionName="
uses-permissionandroid:
name="
android.permission.BLUETOOTH_ADMIN"
/>
<
android.permission.BLUETOOTH"
applicationandroid:
icon="
@drawable/icon"
label="
@string/app_name"
activityandroid:
.ThinBTClient"
intent-filter>
actionandroid:
android.intent.action.MAIN"
categoryandroid:
android.intent.category.LAUNCHER"
/intent-filter>
/activity>
/application>
/manifest>
其中红色、加粗部分就是要添加的权限声明。
然后编写Activity中的执行代码,这些代码的作用就是发送指令,控制小车的运动。
代码如下:
packagecom.ThinBTClient.www;
importandroid.app.Activity;
importandroid.os.Bundle;
importjava.io.IOException;
importjava.io.OutputStream;
importjava.util.UUID;
importandroid.bluetooth.BluetoothAdapter;
importandroid.bluetooth.BluetoothDevice;
importandroid.bluetooth.BluetoothSocket;
importandroid.content.DialogInterface;
importandroid.content.DialogInterface.OnClickListener;
importandroid.provider.ContactsContract.CommonDataKinds.Event;
importandroid.util.Log;
importandroid.view.MotionEvent;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.Toast;
publicclassThinBTClientextendsActivity{
privatestaticfinalStringTAG="
THINBTCLIENT"
;
privatestaticfinalbooleanD=true;
privateBluetoothAdaptermBluetoothAdapter=null;
privateBluetoothSocketbtSocket=null;
privateOutputStreamoutStream=null;
ButtonmButtonF;
ButtonmButtonB;
ButtonmButtonL;
ButtonmButtonR;
ButtonmButtonS;
privatestaticfinalUUIDMY_UUID=UUID.fromString("
00001101-0000-1000-8000-00805F9B34FB"
);
privatestaticStringaddress="
00:
11:
03:
21:
43"
//<
==要连接的蓝牙设备MAC地址
/**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//前进
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){
Log.e(TAG,"
ONRESUME:
Outputstreamcreationfailed."
e);
}
message="
msgBuffer=message.getBytes();
outStream.write(msgBuffer);
Exceptionduringwrite."
break;
caseMotionEvent.ACTION_UP:
0"
returnfalse;
});
//后退
mButtonB=(Button)findViewById(R.id.btnB);
mButtonB.setOnTouchListener(newButton.OnTouchListener(){
3"
//左转
mButtonL=(Button)findViewById(R.id.btnL);
mButtonL.setOnTouchListener(newButton.OnTouchListener(){
2"
//右转
mButtonR=(Button)findViewById(R.id.btnR);
mButtonR.setOnTouchListener(newButton.OnTouchListener(){
4"
//停止
mButtonS=(Button)findViewById(R.id.btnS);
mButtonS.setOnTouchListener(newButton.OnTouchListener(){
if(event.getAction()==MotionEvent.ACTION_DOWN)
Stringmessage="
byte[]msgBuffer=message.getBytes();
if(D)
+++ONCREATE+++"
mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter==null){
Toast.makeText(this,"
Bluetoothisnotavailable."
Toast.LENGTH_LONG).show();
finish();
return;
if(!
mBluetoothAdapter.isEnabled()){
PleaseenableyourBluetoothandre-runthisprogram."
+++DONEINONCREATE,GOTLOCALBTADAPTER+++"
publicvoidonStart(){
super.onStart();
if(D)Log.e(TAG,"
++ONSTART++"
publicvoidonResume(){
super.onResume();
if(D){
+ONRESUME+"
+ABOUTTOATTEMPTCLIENTCONNECT+"
BluetoothDevicedevice=mBluetoothAdapter.getRemoteDevice(address);
btSocket=device.createRfcommSocketToServiceRecord(MY_UUID);
Socketcreationfailed."
mBluetoothAdapter.cancelDiscovery();
btSocket.connect();
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 成功 实现 手机 蓝牙 控制 智能 小车 机器人 视频 程序 源代码 Android