久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 4189|回復(fù): 3
打印 上一主題 下一主題
收起左側(cè)

基于手機(jī)藍(lán)牙控制小車51單片機(jī)手機(jī)程序

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:113175 發(fā)表于 2016-4-9 21:22 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
下面開始介紹Android手機(jī)端控制程序的編寫:
首先打開Eclipse,當(dāng)然之前的Java開發(fā)環(huán)境和安卓開發(fā)工具自己得先配置好,這里就不多說了,網(wǎng)上教程一大摞。
然后新建一個(gè)Android項(xiàng)目,修改布局文件main.xml,代碼如下:
[html] view plain copy
<?xml version="1.0" encoding="utf-8"?>  
<AbsoluteLayout  
android:id="@+id/widget0"  
android:layout_width="fill_parent"  
android:layout_height="fill_parent"  
xmlns:android="http://schemas.android.com/apk/res/android"  
>  
<Button  
android:id="@+id/btnF"  
android:layout_width="100px"  
android:layout_height="60px"  
android:text="前進(jìn)"  
android:layout_x="130px"  
android:layout_y="62px"  
>  
</Button>  
<Button  
android:id="@+id/btnL"  
android:layout_width="100px"  
android:layout_height="60px"  
android:text="左轉(zhuǎn)"  
android:layout_x="20px"  
android:layout_y="152px"  
>  
</Button>  
<Button  
android:id="@+id/btnR"  
android:layout_width="100px"  
android:layout_height="60px"  
android:text="右轉(zhuǎn)"  
android:layout_x="240px"  
android:layout_y="152px"  
>  
</Button>  
<Button  
android:id="@+id/btnB"  
android:layout_width="100px"  
android:layout_height="60px"  
android:text="后退"  
android:layout_x="130px"  
android:layout_y="242px"  
>  
</Button>  
<Button  
android:id="@+id/btnS"  
android:layout_width="100px"  
android:layout_height="60px"  
android:text="停止"  
android:layout_x="130px"  
android:layout_y="152px"  
>  
</Button>  
</AbsoluteLayout>
這個(gè)布局文件的效果就是如視頻中所示的手機(jī)操作界面。
然后是權(quán)限聲明,這一步不能少,否則將無法使用安卓手機(jī)的藍(lán)牙功能。
權(quán)限聲明如下:
打開AndroidManifest.xml文件,修改代碼如下:
[html] view plain copy
<?xml version="1.0" encoding="utf-8"?>  
<manifest xmlns:android="http://schemas.android.com/apk/res/android"  
package="com.ThinBTClient.www"  
android:versionCode="1"  
android:versionName="1.0">  

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />  
<uses-permission android:name="android.permission.BLUETOOTH" />  
<application android:icon="@drawable/icon" android:label="@string/app_name">  
<activity android:name=".ThinBTClient"  
android:label="@string/app_name">  
<intent-filter>  
<action android:name="android.intent.action.MAIN" />  
<category android:name="android.intent.category.LAUNCHER" />  
</intent-filter>  
</activity>  

</application>  
</manifest>
其中紅色、加粗部分就是要添加的權(quán)限聲明。
然后編寫Activity中的執(zhí)行代碼,這些代碼的作用就是發(fā)送指令,控制小車的運(yùn)動(dòng)。
代碼如下:
[java] view plain copy
package com.ThinBTClient.www;  

import android.app.Activity;  
import android.os.Bundle;  
import java.io.IOException;  

import java.io.OutputStream;  
import java.util.UUID;  

import android.app.Activity;  

import android.bluetooth.BluetoothAdapter;  

import android.bluetooth.BluetoothDevice;  

import android.bluetooth.BluetoothSocket;  
import android.content.DialogInterface;  
import android.content.DialogInterface.OnClickListener;  

import android.os.Bundle;  
import android.provider.ContactsContract.CommonDataKinds.Event;  

import android.util.Log;  
import android.view.MotionEvent;  
import android.view.View;  

import android.widget.Button;  
import android.widget.Toast;  
public class ThinBTClient extends Activity {  
private static final String TAG = "THINBTCLIENT";  

private static final boolean D = true;  

private BluetoothAdapter mBluetoothAdapter = null;  

private BluetoothSocket btSocket = null;  

private OutputStream outStream = null;  
Button mButtonF;  

Button mButtonB;  
Button mButtonL;  
Button mButtonR;  
Button mButtonS;  

private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");  


private static String address = "00:11:03:21:00:43"; // <==要連接的藍(lán)牙設(shè)備MAC地址  


/** Called when the activity is first created. */  

@Override  

public void onCreate(Bundle savedInstanceState) {  

super.onCreate(savedInstanceState);  
setContentView(R.layout.main);  

//前進(jìn)  
mButtonF=(Button)findViewById(R.id.btnF);  
mButtonF.setOnTouchListener(new Button.OnTouchListener(){  

@Override  
public boolean onTouch(View v, MotionEvent event) {  
// TODO Auto-generated method stub  
String message;  
byte[] msgBuffer;  
int action = event.getAction();  
switch(action)  
{  
case MotionEvent.ACTION_DOWN:  
try {  
outStream = btSocket.getOutputStream();  

} catch (IOException e) {  
Log.e(TAG, "ON RESUME: Output stream creation failed.", e);  
}  


message = "1";  

msgBuffer = message.getBytes();  

try {  
outStream.write(msgBuffer);  

} catch (IOException e) {  
Log.e(TAG, "ON RESUME: Exception during write.", e);  
}  
break;  

case MotionEvent.ACTION_UP:  
try {  
outStream = btSocket.getOutputStream();  

} catch (IOException e) {  
Log.e(TAG, "ON RESUME: Output stream creation failed.", e);  
}  


message = "0";  

msgBuffer = message.getBytes();  

try {  
outStream.write(msgBuffer);  

} catch (IOException e) {  
Log.e(TAG, "ON RESUME: Exception during write.", e);  
}  
break;  
}  
return false;  
}  


});  
//后退  
mButtonB=(Button)findViewById(R.id.btnB);  
mButtonB.setOnTouchListener(new Button.OnTouchListener(){  


@Override  
public boolean onTouch(View v, MotionEvent event) {  
// TODO Auto-generated method stub  
String message;  
byte[] msgBuffer;  
int action = event.getAction();  
switch(action)  
{  
case MotionEvent.ACTION_DOWN:  
try {  
outStream = btSocket.getOutputStream();  

} catch (IOException e) {  
Log.e(TAG, "ON RESUME: Output stream creation failed.", e);  
}  


message = "3";  

msgBuffer = message.getBytes();  

try {  
outStream.write(msgBuffer);  

} catch (IOException e) {  
Log.e(TAG, "ON RESUME: Exception during write.", e);  
}  
break;  

case MotionEvent.ACTION_UP:  
try {  
outStream = btSocket.getOutputStream();  

} catch (IOException e) {  
Log.e(TAG, "ON RESUME: Output stream creation failed.", e);  
}  


message = "0";  

msgBuffer = message.getBytes();  

try {  
outStream.write(msgBuffer);  

} catch (IOException e) {  
Log.e(TAG, "ON RESUME: Exception during write.", e);  
}  
break;  
}  

return false;  
}  


});  
//左轉(zhuǎn)  
mButtonL=(Button)findViewById(R.id.btnL);  
mButtonL.setOnTouchListener(new Button.OnTouchListener(){  

@Override  
public boolean onTouch(View v, MotionEvent event) {  
// TODO Auto-generated method stub  
String message;  
byte[] msgBuffer;  
int action = event.getAction();  
switch(action)  
{  
case MotionEvent.ACTION_DOWN:  
try {  
outStream = btSocket.getOutputStream();  

} catch (IOException e) {  
Log.e(TAG, "ON RESUME: Output stream creation failed.", e);  
}  


message = "2";  

msgBuffer = message.getBytes();  

try {  
outStream.write(msgBuffer);  

} catch (IOException e) {  
Log.e(TAG, "ON RESUME: Exception during write.", e);  
}  
break;  

case MotionEvent.ACTION_UP:  
try {  
outStream = btSocket.getOutputStream();  

} catch (IOException e) {  
Log.e(TAG, "ON RESUME: Output stream creation failed.", e);  
}  


message = "0";  

msgBuffer = message.getBytes();  

try {  
outStream.write(msgBuffer);  

} catch (IOException e) {  
Log.e(TAG, "ON RESUME: Exception during write.", e);  
}  
break;  
}  

return false;  

}  
});  
//右轉(zhuǎn)  
mButtonR=(Button)findViewById(R.id.btnR);  
mButtonR.setOnTouchListener(new Button.OnTouchListener(){  

@Override  
public boolean onTouch(View v, MotionEvent event) {  
// TODO Auto-generated method stub  
String message;  
byte[] msgBuffer;  
int action = event.getAction();  
switch(action)  
{  
case MotionEvent.ACTION_DOWN:  
try {  
outStream = btSocket.getOutputStream();  

} catch (IOException e) {  
Log.e(TAG, "ON RESUME: Output stream creation failed.", e);  
}  


message = "4";  

msgBuffer = message.getBytes();  

try {  
outStream.write(msgBuffer);  

} catch (IOException e) {  
Log.e(TAG, "ON RESUME: Exception during write.", e);  
}  
break;  

case MotionEvent.ACTION_UP:  
try {  
outStream = btSocket.getOutputStream();  

} catch (IOException e) {  
Log.e(TAG, "ON RESUME: Output stream creation failed.", e);  
}  


message = "0";  

msgBuffer = message.getBytes();  

try {  
outStream.write(msgBuffer);  

} catch (IOException e) {  
Log.e(TAG, "ON RESUME: Exception during write.", e);  
}  
break;  
}  

return false;  

}  


});  

//停止  
mButtonS=(Button)findViewById(R.id.btnS);  
mButtonS.setOnTouchListener(new Button.OnTouchListener(){  

@Override  
public boolean onTouch(View v, MotionEvent event) {  
// TODO Auto-generated method stub  
if(event.getAction()==MotionEvent.ACTION_DOWN)  
try {  
outStream = btSocket.getOutputStream();  

} catch (IOException e) {  
Log.e(TAG, "ON RESUME: Output stream creation failed.", e);  
}  


String message = "0";  

byte[] msgBuffer = message.getBytes();  

try {  
outStream.write(msgBuffer);  

} catch (IOException e) {  
Log.e(TAG, "ON RESUME: Exception during write.", e);  
}  
return false;  
}  


});  

if (D)  
Log.e(TAG, "+++ ON CREATE +++");  
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();  

if (mBluetoothAdapter == null) {  
Toast.makeText(this, "Bluetooth is not available.", Toast.LENGTH_LONG).show();  
finish();  
return;  
}  


if (!mBluetoothAdapter.isEnabled()) {  
Toast.makeText(this, "Please enable your Bluetooth and re-run this program.", Toast.LENGTH_LONG).show();  
finish();  
return;  

}  


if (D)  
Log.e(TAG, "+++ DONE IN ON CREATE, GOT LOCAL BT ADAPTER +++");  

}  


@Override  

public void onStart() {  

super.onStart();  

if (D) Log.e(TAG, "++ ON START ++");  
}  


@Override  

public void onResume() {  

super.onResume();  
if (D) {  
Log.e(TAG, "+ ON RESUME +");  
Log.e(TAG, "+ ABOUT TO ATTEMPT CLIENT CONNECT +");  

}  

BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);  

try {  

btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);  

} catch (IOException e) {  

Log.e(TAG, "ON RESUME: Socket creation failed.", e);  

}  
mBluetoothAdapter.cancelDiscovery();  
try {  

btSocket.connect();  

Log.e(TAG, "ON RESUME: BT connection established, data transfer link open.");  

} catch (IOException e) {  

try {  
btSocket.close();  

} catch (IOException e2) {  

Log .e(TAG,"ON RESUME: Unable to close socket during connection 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 (IOException e) {
Log.e(TAG, "ON RESUME: Output stream creation failed.", e);
}


String message = "1";

byte[] msgBuffer = message.getBytes();

try {
outStream.write(msgBuffer);

} catch (IOException e) {
Log.e(TAG, "ON RESUME: Exception during write.", e);
}
*/  

}  


@Override  

public void onPause() {  

super.onPause();  


if (D)  
Log.e(TAG, "- ON PAUSE -");  
if (outStream != null) {  
try {  
outStream.flush();  
} catch (IOException e) {  
Log.e(TAG, "ON PAUSE: Couldn't flush output stream.", e);  
}  

}  


try {  
btSocket.close();  
} catch (IOException e2) {  
Log.e(TAG, "ON PAUSE: Unable to close socket.", e2);  
}  

}  


@Override  

public void onStop() {  

super.onStop();  

if (D)Log.e(TAG, "-- ON STOP --");  

}  


@Override  

public void onDestroy() {  

super.onDestroy();  

if (D) Log.e(TAG, "--- ON DESTROY ---");  

}  

}

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏2 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:1 發(fā)表于 2016-4-9 21:49 | 只看該作者
這個(gè)好像不完整吧還達(dá)不到加100分的條件 最好傳個(gè)壓縮包 51單片機(jī)程序呢?
回復(fù)

使用道具 舉報(bào)

板凳
ID:105247 發(fā)表于 2016-5-21 00:24 | 只看該作者
樓主要是發(fā)個(gè)工程包太漂亮了,不過學(xué)習(xí)了
回復(fù)

使用道具 舉報(bào)

地板
ID:100515 發(fā)表于 2017-3-20 18:26 | 只看該作者
額,這個(gè)是從別人那里copy過來的吧.....  
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 成人国产精品久久久 | 天天综合日日夜夜 | 在线播放亚洲 | 久久久久亚洲精品中文字幕 | 精品精品 | 精品日韩欧美一区二区 | 欧美成人h版在线观看 | 国产偷录视频叫床高潮对白 | 亚洲精品久久久久久久久久久久久 | 亚洲91精品 | 99久久免费精品国产免费高清 | 国产精品区一区二区三区 | 中文字幕成人av | 91电影 | 国产成都精品91一区二区三 | 9191在线播放 | 久久aⅴ乱码一区二区三区 亚洲欧美综合精品另类天天更新 | 日韩最新网址 | 亚洲国产精品一区二区三区 | 成年人在线观看 | 日韩欧美在线观看视频 | 91在线精品视频 | 国产精品国产精品国产专区不片 | 中国一级特黄真人毛片免费观看 | 欧美激情第一区 | 亚洲国产日本 | 亚洲一区在线播放 | 日朝毛片 | 日韩在线小视频 | 91福利网 | 成人国产午夜在线观看 | 精品真实国产乱文在线 | 欧美做暖暖视频 | 中文字幕 视频一区 | 国产视频一二三区 | 色999视频| 二区中文字幕 | 狠狠躁躁夜夜躁波多野结依 | 色啪网 | 国产成人高清视频 | 国产成人网 |