80,472
社区成员




package com.cy.smartcar;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Calendar;
import java.util.UUID;
import com.cy.smartcar.DeviceListActivity;
import com.cy.smartcar.MainActivity;
import com.cy.smartcar.R;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Switch;
public class MainActivity extends Activity implements OnClickListener {
private final int REQUEST_CONNECT_DEVICE = 1;
private BluetoothAdapter _bluetooth = BluetoothAdapter.getDefaultAdapter();// 获取本地蓝牙适配器
static BluetoothSocket _socket = null; // 蓝牙通信socket
BluetoothDevice _device = null; // 蓝牙设备
boolean _discoveryFinished = false;
boolean bRun = true;// 用于控制线程开关的变量
boolean bThread = false;// 用于控制线程开关的变量
private InputStream iss; // 输入流,用来接收蓝牙数据
Calendar cal = Calendar.getInstance();
final int DIALOG_TIME = 0; // 设置对话框id
boolean sceneTrue = false;
boolean offTrue = true;
private final int REQUEST_SCENE = 2;
private final static String MY_UUID = "00001101-0000-1000-8000-00805F9B34FB"; // SPP服务UUID号
private String smsg = ""; // 显示用数据缓存
private int smsgInt = 0;
int speed;
TextView tstate;
TextView tspeed;
TextView deviceName;
Switch connect;
Button search;
Button up;
Button down;
Button tleft;
Button tright;
Button stop;
Button fast;
Button slow;
Button exit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//setContentView(R.layout.activity_main);
System.out.println("----------------");
// 如果打开本地蓝牙设备不成功,提示信息,结束程序
if (_bluetooth == null) {
Toast.makeText(this, "无法打开手机蓝牙,请确认手机是否有蓝牙功能!", Toast.LENGTH_LONG)
.show();
finish();
return;
}
ButtonInit();
/*
if(_bluetooth.isEnabled()){
connect.setChecked(true);
Toast.makeText(this, "蓝牙已打开", Toast.LENGTH_LONG)
.show();
}else{
connect.setChecked(false);
}*/
}
/**
* 执行所有主界面控键对应的操作
*/
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
System.out.println("========" + view.getId());
switch (view.getId()) {
// ==========================连接蓝牙设备==============================
case R.id.search:
if (_bluetooth.isEnabled() == false) {// 如果蓝牙服务不可用则提示
Toast.makeText(this, "请打开蓝牙", Toast.LENGTH_SHORT).show();
return;
}
connect.setChecked(true);
// 如果未连接设备则打开DeviceListActivity进行设备搜索
if (_socket == null) {
Intent intent = new Intent(this, DeviceListActivity.class);
startActivityForResult(intent, REQUEST_CONNECT_DEVICE);
// startActivity(intent);
} else {// 关闭连接socket
try {
iss.close();
_socket.close();
_socket = null;
bRun = false;
connect.setText("连接");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
// ==========================前进==============================
case R.id.up:
sceneTrue = false;
if (_socket != null) {
try {
OutputStream os = _socket.getOutputStream();
os.write(2);
tstate.setText("小车状态:前进");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
Toast.makeText(this, "请先连接智能小车", Toast.LENGTH_SHORT).show();
}
break;
// ==========================后退==============================
case R.id.down:
sceneTrue = false;
if (_socket != null) {
try {
OutputStream os = _socket.getOutputStream();
os.write(4);
tstate.setText("小车状态:后退");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
Toast.makeText(this, "请先连接智能小车", Toast.LENGTH_SHORT).show();
}
break;
// ==========================左转==============================
case R.id.tleft:
sceneTrue = false;
if (_socket != null) {
try {
OutputStream os = _socket.getOutputStream();
os.write(5);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
Toast.makeText(this, "请先连接智能小车", Toast.LENGTH_SHORT).show();
}
break;
// ==========================右转==============================
case R.id.tright:
sceneTrue = false;
if (_socket != null) {
try {
OutputStream os = _socket.getOutputStream();
os.write(7);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
Toast.makeText(this, "请先连接智能小车", Toast.LENGTH_SHORT).show();
}
break;
// ==========================刹车==============================
case R.id.stop:
sceneTrue = false;
if (_socket != null) {
try {
OutputStream os = _socket.getOutputStream();
os.write(8);
tstate.setText("小车状态:停止");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
Toast.makeText(this, "请先连接智能小车", Toast.LENGTH_SHORT).show();
}
break;
// ==========================加速==============================
case R.id.fast:
sceneTrue = false;
if (_socket != null) {
try {
OutputStream os = _socket.getOutputStream();
os.write(1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
Toast.makeText(this, "请先连接智能小车", Toast.LENGTH_SHORT).show();
}
break;
// ==========================减速==============================
case R.id.slow:
sceneTrue = false;
if (_socket != null) {
try {
OutputStream os = _socket.getOutputStream();
os.write(3);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
Toast.makeText(this, "请先连接智能小车", Toast.LENGTH_SHORT).show();
}
break;
// ==========================退出==============================
case R.id.exit:
finish();
break;
}
}
private void ButtonInit() {
// TODO Auto-generated method stub
deviceName = (TextView) this.findViewById(R.id.main_device_name);
tstate = (TextView) this.findViewById(R.id.tstate);
connect = (Switch) this.findViewById(R.id.connect);
search = (Button) this.findViewById(R.id.search);
up = (Button) this.findViewById(R.id.up);
down = (Button) this.findViewById(R.id.down);
tleft = (Button) this.findViewById(R.id.tleft);
tright = (Button) this.findViewById(R.id.tright);
stop = (Button) this.findViewById(R.id.stop);
fast = (Button) this.findViewById(R.id.fast);
slow = (Button) this.findViewById(R.id.slow);
exit = (Button) this.findViewById(R.id.exit);
// deviceName.setOnClickListener(this);
//connect.setOnClickListener(this);
OnCheckedChangeListener listener=new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
if(_bluetooth.enable()){//打开蓝牙
Toast.makeText(MainActivity.this, "打开蓝牙中...", Toast.LENGTH_SHORT).show();
connect.setChecked(true);
}else{
Toast.makeText(MainActivity.this, "打开失败...", Toast.LENGTH_SHORT).show();
connect.setChecked(false);
}
} else {
_bluetooth.disable();// 关闭蓝牙
Toast.makeText(MainActivity.this, "关闭蓝牙中...", Toast.LENGTH_SHORT).show();
connect.setChecked(false);
}
}
};
connect.setOnCheckedChangeListener(listener);
search.setOnClickListener(this);
up.setOnClickListener(this);
down.setOnClickListener(this);
tleft.setOnClickListener(this);
tright.setOnClickListener(this);
stop.setOnClickListener(this);
fast.setOnClickListener(this);
slow.setOnClickListener(this);
exit.setOnClickListener(this);
}
}
长度不够,底部一些确认无影响代码就没贴进来了。写的很烂,求轻喷