name="android.intent.category.LAUNCHER"/>
其中红色、加粗部分就是要添加的权限声明。
然后编写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.OnClickListener;
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.makeText(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();