package com.car.demo;
import java.io.IOException;
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.graphics.ColorMatrixColorFilter;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
/**
*
* @author Penny
* 2017年3月13日 10:46:43
* 藍牙玩具小車項目;
* Android端 APP
* 主要功能:
* 1 連接小車藍牙模塊,
* 2 並能對小車 進行 前后左右 方向控制。
*/
/**--可發送字段
case 0x31: P1=0xfe;beep=0;break; //接受到1,第一個LED亮
case 0x32: P1=0xfd;beep=0;break; //接受到2,第二個LED亮
case 0x33: P1=0xfb;beep=0;break; //接受到3,第三個LED亮
case 0x34: P1=0xf7;beep=0;break; //接受到4,第四個LED亮
case 0x35: P1=0xef;beep=0;break; //接受到5,第五個LED亮
case 0x36: P1=0xdf;beep=0;break; //接受到6,第六個LED亮
case 0x37: P1=0xbf;beep=0;break; //接受到7,第七個LED亮
case 0x38: P1=0x7f;beep=0;break; //接受到8,第八個LED亮
*/
public class CarControl extends Activity{
private final static String MY_UUID = "00001101-0000-1000-8000-00805F9B34FB"; //SPP服務UUID號
private final static String ADDRESS ="98:D3:35:00:D0:5C"; //藍牙模塊 mac number
BluetoothDevice _device = null; //藍牙設備
BluetoothSocket _socket = null; //藍牙通信socket
private BluetoothAdapter _bluetooth = null; //獲取本地藍牙適配器,即藍牙設備
private OutputStream os=null;
// private final static Handler handler =new Handler();//消息延時處理!!!!此方法存在內存泄漏
// static TimerTask task =null;
TextView msg = null;
/**
* 以上兩種方式比較簡單,但是需要很多的圖片和布局文件,如果項目中的圖片按鈕比較多,那就很浪費資源。第三種方式使用矩陣顏色濾鏡。
顏色過濾矩陣是一個4x5的矩陣,四行分別是紅色通道值,綠色通道值,藍色通道值和alpha通道值。五列分別是對應通道的紅色值,綠色值,藍色值,alpha值和偏移量。
RGB和Alpha的終值計算方法如下:
Red通道終值= a[0] * srcR + a[1] * srcG + a[2] * srcB + a[3] * srcA + a[4]
Green通道終值= a[5] * srcR + a[6] * srcG + a[7] * srcB + a[8] * srcA + a[9]
Blue通道終值= a[10] * srcR + a[11] * srcG + a[12] * srcB + a[13] * srcA + a[14]
Alpha通道終值= a[15] * srcR + a[16] * srcG + a[17] * srcB + a[18] * srcA + a[19]
備注:
srcR為原圖Red通道值,srcG為原圖Green通道值,srcB為原圖Blue通道值,srcA為原圖Alpha通道值。
每個通道的源值和終值都在0到255的范圍內。即使計算結果大於255或小於0,值都將被限制在0到255的范圍內。
*/
/**
* 按鈕被按下
*/
private final static float[] BUTTON_PRESSED = new float[] {
2.0f, 0, 0, 0, -50,
0, 2.0f, 0, 0, -50,
0, 0, 2.0f, 0, -50,
0, 0, 0, 0.4f, 0 };
/**
* 按鈕恢復原狀
*/
private final static float[] BUTTON_RELEASED = new float[] {
1, 0, 0, 0, 0,
0, 1, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, 1, 0 };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
/**
* 界面按鍵獲取
*/
setContentView(R.layout.ctl);
ImageButton up = (ImageButton) findViewById(R.id.up);
ImageButton down = (ImageButton) findViewById(R.id.down);
ImageButton left = (ImageButton) findViewById(R.id.left);
ImageButton right = (ImageButton) findViewById(R.id.right);
ImageButton stop = (ImageButton) findViewById(R.id.stop);
ImageButton conn = (ImageButton) findViewById(R.id.conn);
ImageButton close = (ImageButton) findViewById(R.id.close);
ImageButton btnA = (ImageButton) findViewById(R.id.a);
ImageButton btnB = (ImageButton) findViewById(R.id.b);
_bluetooth= BluetoothAdapter.getDefaultAdapter();
if (_bluetooth != null){
// Toast.makeText(this,"已發現:\n"+ _bluetooth.getName()+"\n"+_bluetooth.getAddress()+"\n設備!", Toast.LENGTH_SHORT).show();
delayMsg("已發現:"+ _bluetooth.getName()+","+_bluetooth.getAddress()+"設備!", 3000);//3秒后執行清空信息提示
}else{
if(!_bluetooth.isEnabled())
return;
}
// 設置設備可以被搜索
new Thread(){
public void run(){
if(!_bluetooth.isEnabled()){
_bluetooth.enable();
}
}
}.start();
delayMsg("請點擊藍牙連接按鈕!", 3000);//3秒后執行清空信息提示
/**
*連接按鍵
*/
conn.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
v.getBackground().setColorFilter(new ColorMatrixColorFilter(BUTTON_PRESSED));
v.setBackgroundDrawable(v.getBackground());
}else if(event.getAction() == MotionEvent.ACTION_UP) {
v.getBackground().setColorFilter(new ColorMatrixColorFilter(BUTTON_RELEASED));
v.setBackgroundDrawable(v.getBackground());
}
//-----------------------准備連接-------------------
if(_bluetooth!=null&&_socket==null){
_device = _bluetooth.getRemoteDevice(ADDRESS); //由Mac 地址獲取小車藍牙設備
try {
_socket = _device.createInsecureRfcommSocketToServiceRecord(UUID.fromString(MY_UUID));
} catch (IOException e) {
// Toast.makeText(CarControl.this, "藍牙模塊獲取Socket失敗!", Toast.LENGTH_SHORT).show();
// msg.setText("藍牙模塊獲取Socket失敗!");
delayMsg("藍牙模塊獲取Socket失敗!", 3000);
}
}
//---------------連接------------------------------
try {
if(_socket!=null&&!_socket.isConnected())
_socket.connect();
if(_socket.isConnected()&&os==null)
os= _socket.getOutputStream();
// Toast.makeText(CarControl.this, "socket成功連接", Toast.LENGTH_SHORT).show();
delayMsg("socket成功連接", 3000);
} catch (IOException e) {
Toast.makeText(CarControl.this, "socket連接失敗,程序已退出!", Toast.LENGTH_SHORT).show();
// delayMsg("socket連接失敗!程序已退出", 3000);
try {
if(_socket!=null)
_socket.close();
_socket=null;
if(_bluetooth!=null)
_bluetooth.disable();
_bluetooth=null;
finish();
} catch (IOException e1) {
// Toast.makeText(CarControl.this, "socket關閉失敗!程序已退出", Toast.LENGTH_SHORT).show();
// delayMsg("socket連接失敗!程序已退出", 3000);
}
}
return false;
}
});
/**
* 關閉按鍵
*/
close.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
v.getBackground().setColorFilter(new ColorMatrixColorFilter(BUTTON_PRESSED));
v.setBackgroundDrawable(v.getBackground());
}else if(event.getAction() == MotionEvent.ACTION_UP) {
v.getBackground().setColorFilter(new ColorMatrixColorFilter(BUTTON_RELEASED));
v.setBackgroundDrawable(v.getBackground());
}
finish();
if(_bluetooth!=null){
if(_bluetooth.isDiscovering()){
_bluetooth.cancelDiscovery();
}
if(_bluetooth.isEnabled()){
_bluetooth.disable();
_bluetooth=null;
}
}
if(_socket!=null){
try {
_socket.close();
_socket=null;
} catch (IOException e) {
}finally{
if(os!=null){
try {
os.close();
} catch (IOException e) {
}
}
}
}
return false;
}
});
/**
* 上下左右 1234
*/
//向上方向鍵 id=1
up.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
v.getBackground().setColorFilter(new ColorMatrixColorFilter(BUTTON_PRESSED));
v.setBackgroundDrawable(v.getBackground());
}else if(event.getAction() == MotionEvent.ACTION_UP) {
v.getBackground().setColorFilter(new ColorMatrixColorFilter(BUTTON_RELEASED));
v.setBackgroundDrawable(v.getBackground());
}
try {
if(_socket!=null&&_socket.isConnected()&&os!=null){
byte[] code1= new byte[]{0x33};
os.write(code1);
// Toast.makeText(CarControl.this, "前進指令(1)!", Toast.LENGTH_SHORT).show();
delayMsg("↑前進↑", 1000);
}else{
// Toast.makeText(CarControl.this, "沒有連接上小車藍牙", Toast.LENGTH_SHORT).show();
// msg.setText("沒有連接上小車藍牙!");
delayMsg("沒有連接上小車藍牙!", 2000);
}
} catch (IOException e) {
// Toast.makeText(CarControl.this, "前進指令(1)發送失敗!", Toast.LENGTH_SHORT).show();
delayMsg("前進指令(1)發送失敗!", 2000);
}
return false;
}
});
//向下方向鍵id=3
down.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
v.getBackground().setColorFilter(new ColorMatrixColorFilter(BUTTON_PRESSED));
v.setBackgroundDrawable(v.getBackground());
}else if(event.getAction() == MotionEvent.ACTION_UP) {
v.getBackground().setColorFilter(new ColorMatrixColorFilter(BUTTON_RELEASED));
v.setBackgroundDrawable(v.getBackground());
}
try {
if(_socket!=null&&_socket.isConnected()&&os!=null){
byte[] code3= new byte[]{0x34};
os.write(code3);
// Toast.makeText(CarControl.this, "后退指令(3)!", Toast.LENGTH_SHORT).show();
delayMsg("↓后退↓", 1000);
}else{
// Toast.makeText(CarControl.this, "沒有連接上小車藍牙", Toast.LENGTH_SHORT).show();
delayMsg("沒有連接上小車藍牙!", 2000);
}
} catch (IOException e) {
// Toast.makeText(CarControl.this, "后退指令(3)發送失敗!", Toast.LENGTH_SHORT).show();
delayMsg("后退指令(3)發送失敗!", 2000);
}
return false;
}
});
//向左方向鍵 id=4
left.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
v.getBackground().setColorFilter(new ColorMatrixColorFilter(BUTTON_PRESSED));
v.setBackgroundDrawable(v.getBackground());
}else if(event.getAction() == MotionEvent.ACTION_UP) {
v.getBackground().setColorFilter(new ColorMatrixColorFilter(BUTTON_RELEASED));
v.setBackgroundDrawable(v.getBackground());
}
try {
if(_socket!=null&&_socket.isConnected()&&os!=null){
byte[] code4= new byte[]{0x31};
os.write(code4);
// Toast.makeText(CarControl.this, "左轉指令(4)!", Toast.LENGTH_SHORT).show();
delayMsg("←左轉←", 1000);
}else{
// Toast.makeText(CarControl.this, "沒有連接上小車藍牙", Toast.LENGTH_SHORT).show();
delayMsg("沒有連接上小車藍牙!", 2000);
}
} catch (IOException e) {
// Toast.makeText(CarControl.this, "左轉指令(4)發送失敗!", Toast.LENGTH_SHORT).show();
delayMsg("左轉指令(4)發送失敗!", 2000);
}
return false;
}
});
//向右方向鍵 id=2
right.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
v.getBackground().setColorFilter(new ColorMatrixColorFilter(BUTTON_PRESSED));
v.setBackgroundDrawable(v.getBackground());
}else if(event.getAction() == MotionEvent.ACTION_UP) {
v.getBackground().setColorFilter(new ColorMatrixColorFilter(BUTTON_RELEASED));
v.setBackgroundDrawable(v.getBackground());
}
try {
if(_socket!=null&&_socket.isConnected()&&os!=null){
byte[] code2= new byte[]{0x32};
os.write(code2);
// Toast.makeText(CarControl.this, "右轉指令(2)!", Toast.LENGTH_SHORT).show();
delayMsg("→右轉→", 1000);
}else{
// Toast.makeText(CarControl.this, "沒有連接上小車藍牙!", Toast.LENGTH_SHORT).show();
delayMsg("沒有連接上小車藍牙!", 2000);
}
} catch (IOException e) {
// Toast.makeText(CarControl.this, "右轉指令(2)發送失敗!", Toast.LENGTH_SHORT).show();
delayMsg("右轉指令(2)發送失敗!", 2000);
}
return false;
}
});
//stop
stop.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
v.getBackground().setColorFilter(new ColorMatrixColorFilter(BUTTON_PRESSED));
v.setBackgroundDrawable(v.getBackground());
}else if(event.getAction() == MotionEvent.ACTION_UP) {
v.getBackground().setColorFilter(new ColorMatrixColorFilter(BUTTON_RELEASED));
v.setBackgroundDrawable(v.getBackground());
}
try {
if(_socket!=null&&_socket.isConnected()&&os!=null){
byte[] code2= new byte[]{0x35};
os.write(code2);
// Toast.makeText(CarControl.this, "右轉指令(2)!", Toast.LENGTH_SHORT).show();
delayMsg("A", 1000);
}else{
// Toast.makeText(CarControl.this, "沒有連接上小車藍牙!", Toast.LENGTH_SHORT).show();
delayMsg("沒有連接上小車藍牙!", 2000);
}
} catch (IOException e) {
// Toast.makeText(CarControl.this, "右轉指令(2)發送失敗!", Toast.LENGTH_SHORT).show();
delayMsg("A發送失敗!", 2000);
}
return false;
}
});
/***
* A-B 功能鍵
*/
//A
btnA.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
v.getBackground().setColorFilter(new ColorMatrixColorFilter(BUTTON_PRESSED));
v.setBackgroundDrawable(v.getBackground());
}else if(event.getAction() == MotionEvent.ACTION_UP) {
v.getBackground().setColorFilter(new ColorMatrixColorFilter(BUTTON_RELEASED));
v.setBackgroundDrawable(v.getBackground());
}
try {
if(_socket!=null&&_socket.isConnected()&&os!=null){
byte[] code2= new byte[]{0x36};
os.write(code2);
// Toast.makeText(CarControl.this, "右轉指令(2)!", Toast.LENGTH_SHORT).show();
delayMsg("A", 1000);
}else{
// Toast.makeText(CarControl.this, "沒有連接上小車藍牙!", Toast.LENGTH_SHORT).show();
delayMsg("沒有連接上小車藍牙!", 2000);
}
} catch (IOException e) {
// Toast.makeText(CarControl.this, "右轉指令(2)發送失敗!", Toast.LENGTH_SHORT).show();
delayMsg("A發送失敗!", 2000);
}
return false;
}
});
//B
btnB.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
v.getBackground().setColorFilter(new ColorMatrixColorFilter(BUTTON_PRESSED));
v.setBackgroundDrawable(v.getBackground());
}else if(event.getAction() == MotionEvent.ACTION_UP) {
v.getBackground().setColorFilter(new ColorMatrixColorFilter(BUTTON_RELEASED));
v.setBackgroundDrawable(v.getBackground());
}
try {
if(_socket!=null&&_socket.isConnected()&&os!=null){
byte[] code2= new byte[]{0x37};
os.write(code2);
// Toast.makeText(CarControl.this, "右轉指令(2)!", Toast.LENGTH_SHORT).show();
delayMsg("B", 1000);
}else{
// Toast.makeText(CarControl.this, "沒有連接上小車藍牙!", Toast.LENGTH_SHORT).show();
delayMsg("沒有連接上小車藍牙!", 2000);
}
} catch (IOException e) {
// Toast.makeText(CarControl.this, "右轉指令(2)發送失敗!", Toast.LENGTH_SHORT).show();
delayMsg("B發送失敗!", 2000);
}
return false;
}
});
}
private void delayMsg(final String msgtext,int times){
msg=(TextView) findViewById(R.id.ctlmsg);
msg.setText(msgtext);
}
}
開源地址[https://github.com/Himi7362/bluecar]