【网学网提醒】:网学会员,鉴于大家对Android手机蓝牙控制十分关注,会员在此为大家搜集整理了“Android手机蓝牙控制”一文,供大家参考学习!
Android手机端控制
程序的编写:
首先打开Eclipse,当然之前的Java开发环境和安卓开发工具自己得先配置好,这里就不多说了,网上教程一大摞。
然后新建一个Android项目,修改布局文件main.xml,代码如下:
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="schemas.android/apk/res/android"
>
这个布局文件的效果就是如视频中所示的手机操作界面。
然后是权限声明,这一步不能少,否则将无法使用安卓手机的蓝牙功能。
权限声明如下:
打开AndroidManifest.xml文件,修改代码如下:
package="com.ThinBTClient.www"
android:versionCode="1"
android:versionName="1.0">
android:label="@string/app_name">
其中红色、加粗部分就是要添加的权限声明。
然后编写Activity中的执行代码,这些代码的作用就是发送指令,控制小车的运动。
代码如下:
packagecom.ThinBTClient.www;
importandroid.app.Activity;
importandroid.os.Bundle;
importjava.io.IOException;
importjava.io.OutputStream;
importjava.util.UUID;
importandroid.app.Activity;
importandroid.bluetooth.BluetoothAdapter;
importandroid.bluetooth.BluetoothDevice;
importandroid.bluetooth.BluetoothSocket;
importandroid.content.DialogInterface;
importandroid.content.DialogInterface.OnClickListe
ner;
importandroid.os.Bundle;
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:00:43";//<==要连接的蓝牙设备MAC地址
/**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//前进
mButtonF=(Button)findViewById(R.id.btnF);
mButtonF.setOnTouchListener(newButton.OnTouchListener(){
@Override
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="1";
msgBuffer=message.getBytes();
try{
outStream.write(msgBuffer);
}catch(IOExceptione){
Log.e(TAG,"ONRESUME:Exceptionduringwrite.",e);
}
break;
caseMotionEvent.ACTION_UP:
try{
outStream=btSocket.getOutputStream();
}catch(IOExceptione){
Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",e);
}
message="0";
msgBuffer=message.getBytes();
try{
outStream.write(msgBuffer);
}catch(IOExceptione){
Log.e(TAG,"ONRESUME:Exceptionduringwrite.",e);
}
break;
}
returnfalse;
}
});
//后退
mButtonB=(Button)findViewById(R.id.btnB);
mButtonB.setOnTouchListener(newButton.OnTouchListener(){
@Override
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="3";
msgBuffer=message.getBytes();
try{
outStream.write(msgBuffer);
}catch(IOExceptione){
Log.e(TAG,"ONRESUME:Exceptionduringwrite.",e);
}
break;
caseMotionEvent.ACTION_UP:
try{
outStream=btSocket.getOutputStream();
}catch(IOExceptione){
Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",e);
}
message="0";
msgBuffer=message.getBytes();
try{
outStream.write(msgBuffer);
}catch
(IOExceptione){
Log.e(TAG,"ONRESUME:Exceptionduringwrite.",e);
}
break;
}
returnfalse;
}
});
//左转
mButtonL=(Button)findViewById(R.id.btnL);
mButtonL.setOnTouchListener(newButton.OnTouchListener(){
@Override
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="2";
msgBuffer=message.getBytes();
try{
outStream.write(msgBuffer);
}catch(IOExceptione){
Log.e(TAG,"ONRESUME:Exceptionduringwrite.",e);
}
break;
caseMotionEvent.ACTION_UP:
try{
outStream=btSocket.getOutputStream();
}catch(IOExceptione){
Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",e);
}
message="0";
msgBuffer=message.getBytes();
try{
outStream.write(msgBuffer);
}catch(IOExceptione){
Log.e(TAG,"ONRESUME:Exceptionduringwrite.",e);
}
break;
}
returnfalse;
}
});
//右转
mButtonR=(Button)findViewById(R.id.btnR);
mButtonR.setOnTouchListener(newButton.OnTouchListener(){
@Override
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="4";
msgBuffer=message.getBytes();
try{
outStream.write(msgBuffer);
}catch(IOExceptione){
Log.e(TAG,"ONRESUME:Exceptionduringwrite.",e);
}
break;
caseMotionEvent.ACTION_UP:
try{
outStream=btSocket.getOutputStream();
}catch(IOExceptione){
Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",e);
}
message="0";
msgBuffer=message.getBytes();
try{
outStream.write(msgBuffer);
}catch(IOExceptione){
Log.e(TAG,"ONRESUME:Exceptionduringwrite.",e);
}
break;
}
returnfalse;
}
});
//停止
mButtonS=(Button)findViewById(R.id.btnS);
mButtonS.setOnTouchListener(newButton.OnTouchListener(){
@Override
publicbooleanonTouch(Viewv,MotionEventevent){
//TODOAuto-generatedmethodstub
if(event.getAction()==MotionEvent.ACTION_DOWN)
try{
outStream=btSocket.getOutputStream();
}catch(IOExceptione){
Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",e);
}
Stringmessage="0";
byte[]msgBuffer=message.getBytes();
try{
outStream.write(msgBuffer);
}catch(IOExceptione){
Log.e(TAG,"ONRESUME:Exceptionduringwrite.",e);
}
returnfalse;
}
});
if(D)
Log.e(TAG,"+++ONCREATE+++");
mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter==null){
Toast.makeT
ext(this,"Bluetoothisnotavailable.",Toast.LENGTH_LONG).show();
finish();
return;
}
if(!mBluetoothAdapter.isEnabled()){
Toast.makeText(this,"PleaseenableyourBluetoothandre-runthisprogram.",Toast.LENGTH_LONG).show();
finish();
return;
}
if(D)
Log.e(TAG,"+++DONEINONCREATE,GOTLOCALBTADAPTER+++");
}
@Override
publicvoidonStart(){
super.onStart();
if(D)Log.e(TAG,"++ONSTART++");
}
@Override
publicvoidonResume(){
super.onResume();
if(D){
Log.e(TAG,"+ONRESUME+");
Log.e(TAG,"+ABOUTTOATTEMPTCLIENTCONNECT+");
}
BluetoothDevicedevice=mBluetoothAdapter.getRemoteDevice(address);
try{
btSocket=device.createRfcommSocketToServiceRecord(MY_UUID);
}catch(IOExceptione){
Log.e(TAG,"ONRESUME:Socketcreationfailed.",e);
}
mBluetoothAdapter.cancelDiscovery();
try{
btSocket.connect();
Log.e(TAG,"ONRESUME:BTconnectionestablished,datatransferlinkopen.");
}catch(IOExceptione){
try{
btSocket.close();
}catch(IOExceptione2){
Log.e(TAG,"ONRESUME:Unabletoclosesocketduringconnectionfailure",e2);
}
}
//Createadatastreamsowecantalktoserver.
if(D)
Log.e(TAG,"+ABOUTTOSAYSOMETHINGTOSERVER+");
/*try{
outStream=btSocket.getOutputStream();
}catch(IOExceptione){
Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",e);
}
Stringmessage="1";
byte[]msgBuffer=message.getBytes();
try{
outStream.write(msgBuffer);
}catch(IOExceptione){
Log.e(TAG,"ONRESUME:Exceptionduringwrite.",e);
}
*/
}
@Override
publicvoidonPause(){
super.onPause();
if(D)
Log.e(TAG,"-ONPAUSE-");
if(outStream!=null){
try{
outStream.flush();
}catch(IOExceptione){
Log.e(TAG,"ONPAUSE:Couldn'tflushoutputstream.",e);
}
}
try{
btSocket.close();
}catch(IOExceptione2){
Log.e(TAG,"ONPAUSE:Unabletoclosesocket.",e2);
}
}
@Override
publicvoidonStop(){
super.onStop();
if(D)Log.e(TAG,"--ONSTOP--");
}
@Override
publicvoidonDestroy(){
super.onDestroy();
if(D)Log.e(TAG,"---ONDESTROY---");
}
}
在这个程序中蓝牙模块的MAC地址给写进去了,其实更合理一点应该让程序运行后搜索周围蓝牙设备,然后选择需要连接的设备,获取它的MAC地址再连接。