安卓蓝牙遥控小车 手机端程序
手机与单片机蓝牙模块连接后,通过手机端程序向蓝牙发送信息控制小车的运动,这里是手机端程序
关键代码:
package gjz.bluetooth;
import gjz.bluetooth.R;
import gjz.bluetooth.Bluetooth.ServerOrCilent;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.UUID;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CompoundButton.OnCheckedChangeListener;
@SuppressLint("HandlerLeak")
public class chatActivity extends Activity implements OnItemClickListener,OnClickListener, OnLongClickListener{
Handler handler=new Handler()
{
public void handleMessage(Message msg)
{
if(msg.what==1)
{
Toast.makeText(getApplicationContext(),"连接成功,可以开始控制小车", 1).show();
}
if(msg.what==0)
{
Toast.makeText(getApplicationContext(),"连接失败,原因:"+msg.obj, 1).show();
}
}
};
private ArrayList list;
Context mContext;
ToggleButton start_stop;//开始暂停按钮
SeekBar speedadjust;//速度调节按钮
Button ahead;;
Button back;;
Button left;
Button right;
public static final String PROTOCOL_SCHEME_L2CAP = "btl2cap";
public static final String PROTOCOL_SCHEME_RFCOMM = "btspp";
public static final String PROTOCOL_SCHEME_BT_OBEX = "btgoep";
public static final String PROTOCOL_SCHEME_TCP_OBEX = "tcpobex";
private BluetoothServerSocket mserverSocket = null;// 服务端socket
private clientThread clientConnectThread = null;// 客户端现成
private BluetoothSocket socket = null;// 蓝牙socket
private BluetoothDevice device = null;// 蓝牙设备socket
private readThread mreadThread = null;// 读消息线程
// 获取当前蓝牙适配器,即蓝牙设备
private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
private void init() {
list = new ArrayList();
start_stop=(ToggleButton)findViewById(R.id.start_stop);//开始暂停按钮
speedadjust=(SeekBar)findViewById(R.id.speedadjust);//速度调节按钮
Button ahead=(Button)findViewById(R.id.ahead);
Button back=(Button)findViewById(R.id.back);
Button left=(Button)findViewById(R.id.left);
Button right=(Button)findViewById(R.id.right);
//单击事件
ahead.setOnClickListener(this);
back.setOnClickListener(this);
left.setOnClickListener(this);
right.setOnClickListener(this);
//长按事件
ahead.setOnLongClickListener(this);
back.setOnLongClickListener(this);
left.setOnLongClickListener(this);
right.setOnLongClickListener(this);
start_stop.setOnCheckedChangeListen er(new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(isChecked)
{
sendMessage("1");
}
else
{
sendMessage("6");
}
}
});
}
String address = Bluetooth.BlueToothAddress;
if(!address.equals("null"))
{// 如果获取地址不为空
device = mBluetoothAdapter.getRemoteDevice(address);
clientConnectThread = new clientThread();
clientConnectThread.start();
Bluetooth.isOpen = true;
}
else
{
Toast.makeText(mContext,"地址为空!",Toast.LENGTH_SHORT)
}
//开启客户端线程
private class clientThread extends Thread {
public void run() {
try {
// 创建一个Socket连接:只需要服务器在注册时的UUID号
// 必须要有一个UUID号码,如果获取,可以百度
// socket = device.createRfcommSocketToServ iceRecord(BluetoothProtocols.OBEX_OBJECT_PUSH_PROTOCOL_UUID);
socket = device.createRfcommSocketToServ iceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
//连接,一下的Message是android中的
Log.e("蓝牙地址",device.getAddress());
socket.connect();
Message msg=new Message();
msg.what=1;
handler.sendMessage(msg);
}
catch (IOException e)
{
Log.e("connect","", e);
Message msg=new Message();
msg.obj=e.getMessage();
msg.what=0;
handler.sendMessage(msg);
}
}
};
private void shutdownClient() {
new Thread() {
public void run() {
if(clientConnectThread!=null)
{
clientConnectThread.interrupt();
clientConnectThread= null;
}
if(mreadThread != null)
{
mreadThread.interrupt();
mreadThread = null;
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
socket = null;
}
};
}.start();
}
//发送数据
private void sendMessageHandle(String msg)
{
if (socket == null)
{
Toast.makeText(mContext, "没有连接", Toast.LENGTH_SHORT)
.show();
return;
}
try {
// 输入输出流
OutputStream os = socket.getOutputStream();
os.write(msg.getBytes());
}catch (IOException e) {
e.printStackTrace();
}
list.add(new deviceListItem(msg, false));
}
//读取数据
try {
mmInStream = socket.getInputStream();// 获得输入流
} catch (IOException e1) {
e1.printStackTrace();
}
String s = new String(buf_data);
Message msg = new Message();
msg.obj = s;
msg.what = 1;
LinkDetectedHandler.sendMessage(msg);
mmInStream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
{// 如果是客户端
}
Bluetooth.serviceOrCilent = ServerOrCilent.NONE;
public class SiriListItem {
//本类实现在arraylist中显示信息
String message;
boolean isSiri;
public SiriListItem(String msg, boolean siri) {
message = msg;
isSiri = siri;
}
}