package android.lynx.BluetoothCar;
import java.io.IOException;
import java.io.InputStream;
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.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class AndroidBluetoothCarActivity extends Activity implements CarPositionSetable{
private static final String TAG = "BluetoothTest";
private static final boolean D = true;
private BluetoothAdapter mBluetoothAdapter = null;
private BluetoothDevice device = null;
private BluetoothSocket btSocket = null;
private OutputStream outStream = null;
Button mButtonF;
Button mButtonB;
Button mButtonL;
Button mButtonR;
Button mButtonS;
ToggleButton JoyStickSwitcher;
VirtualJoyStickView JoyStick;
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); //这条是蓝牙串口通用的UUID,不要更改
private static String address = "00:19:5D:EE:2E:A5"; // <==要连接的蓝牙设备MAC地址
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//前进
mButtonF=(Button)findViewById(R.id.btnF);
mButtonF.setOnTouchListener(new Button.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
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 = "CMF";
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 = "CS";
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) {
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 = "CMB";
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 = "CS";
msgBuffer = message.getBytes();
try {
outStream.write(msgBuffer);
} catch (IOException e) {
Log.e(TAG, "ON RESUME: Exception during write.", e);
}
break;
}
return false;
}
});
//左转
mButtonL=(Button)findViewById(R.id.btnL);
mButtonL.setOnTouchListener(new Button.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
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 = "CTL";
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 = "CTL";
msgBuffer = message.getBytes();
try {
outStream.write(msgBuffer);
} catch (IOException e) {
Log.e(TAG, "ON RESUME: Exception during write.", e);
}
break;
}
return false;
}
});
//右转
mButtonR=(Button)findViewById(R.id.btnR);
mButtonR.setOnTouchListener(new Button.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
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 = "CTR";
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 = "CTR";
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) {
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 = "CR";
byte[] msgBuffer = message.getBytes();
try {
outStream.write(msgBuffer);
} catch (IOException e) {
Log.e(TAG, "ON RESUME: Exception during write.", e);
}
return false;
}
});
JoyStick = (VirtualJoyStickView)findViewById(R.id.JoyStick);
JoyStick.setPositionSetable(this);
JoyStickSwitcher=(ToggleButton)findViewById(R.id.JoyStickSwitcher);
JoyStickSwitcher.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if(isChecked){
mButtonF.setEnabled(false);
mButtonB.setEnabled(false);
mButtonS.setEnabled(false);
mButtonL.setEnabled(false);
mButtonR.setEnabled(false);
JoyStick.ThreadCreate();
}else{
mButtonF.setEnabled(true);
mButtonB.setEnabled(true);
mButtonS.setEnabled(true);
mButtonL.setEnabled(true);
mButtonR.setEnabled(true);
JoyStick.ThreadDestory();
}
}
});
if(D)
Log.e(TAG, "+++ ON CREATE +++");
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter == null)
{
Toast.makeText(this, "Bluetooth is not available.", Toast.LENGTH_LONG).show();
fi
没有合适的资源?快使用搜索试试~ 我知道了~
android触屏摇杆控制蓝牙车工程文件
共50个文件
class:32个
java:5个
xml:3个
5星 · 超过95%的资源 需积分: 9 107 下载量 100 浏览量
2012-01-10
16:10:50
上传
评论 2
收藏 122KB RAR 举报
温馨提示
文件里是android触屏摇杆控制蓝牙车的工程文件,关联博客为http://blog.csdn.net/lynx2/article/details/7190982
资源推荐
资源详情
资源评论
收起资源包目录
AndroidBluetoothCar.rar (50个子文件)
AndroidBluetoothCar
AndroidManifest.xml 897B
res
layout
main.xml 3KB
drawable-ldpi
icon.png 6KB
drawable-mdpi
icon.png 7KB
drawable-hdpi
icon.png 12KB
values
strings.xml 197B
proguard.cfg 1KB
src
android
lynx
BluetoothCar
StickPositionGetable.java 433B
VirtualJoyStickView.java 5KB
AndroidBluetoothCarActivity.java 13KB
CarPositionSetable.java 604B
bin
classes
android
lynx
BluetoothCar
CarPositionSetable.class 251B
AndroidBluetoothCarActivity$5.class 2KB
StickPositionGetable.class 183B
AndroidBluetoothCarActivity$3.class 2KB
R.class 580B
R$layout.class 415B
R$attr.class 361B
AndroidBluetoothCarActivity$6.class 2KB
R$id.class 922B
AndroidBluetoothCarActivity$1.class 2KB
R$string.class 448B
AndroidBluetoothCarActivity$4.class 2KB
VirtualJoyStickView.class 5KB
AndroidBluetoothCarActivity$2.class 2KB
R$drawable.class 421B
AndroidBluetoothCarActivity.class 9KB
res
AndroidBluetoothCar.apk 30KB
android
lynx
BluetoothCar
CarPositionSetable.class 251B
AndroidBluetoothCarActivity$5.class 2KB
StickPositionGetable.class 183B
AndroidBluetoothCarActivity$3.class 2KB
R.class 580B
R$layout.class 415B
R$attr.class 361B
AndroidBluetoothCarActivity$6.class 2KB
R$id.class 922B
AndroidBluetoothCarActivity$1.class 2KB
R$string.class 448B
AndroidBluetoothCarActivity$4.class 2KB
VirtualJoyStickView.class 5KB
AndroidBluetoothCarActivity$2.class 2KB
R$drawable.class 421B
AndroidBluetoothCarActivity.class 9KB
classes.dex 16KB
resources.ap_ 21KB
.classpath 288B
assets
project.properties 360B
.project 855B
gen
android
lynx
BluetoothCar
R.java 2KB
共 50 条
- 1
猞猁
- 粉丝: 140
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页