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]

Android蓝牙遥控器APP关键代码 guihub项目的更多相关文章

  1. Android之打开闪光灯关键代码

    在AndroidManifest中注册相应的权限: <uses-permission android:name="android.permission.FLASHLIGHT" ...

  2. Android开发 开启闪光灯 关键代码

    在AndroidManifest中注册响应的权限: <uses-permission android:name="android.permission.FLASHLIGHT" ...

  3. 【转】蓝牙ble app开发(三) -- 抓包

    原文网址:http://blog.csdn.net/lckj686/article/details/43156617 关于android 蓝牙app开发抓包的重要性在 android 蓝牙ble ap ...

  4. 【转】android蓝牙开发---与蓝牙模块进行通信--不错

    原文网址:http://www.cnblogs.com/wenjiang/p/3200138.html 近半个月来一直在搞android蓝牙这方面,主要是项目需要与蓝牙模块进行通信.开头的进展很顺利, ...

  5. android蓝牙开发---与蓝牙模块进行通信

    近半个月来一直在搞android蓝牙这方面,主要是项目需要与蓝牙模块进行通信.开头的进展很顺利,但因为蓝牙模块不在我这里,所以只能用手机测试.一开头就发现手机的蓝牙不能用,为了证明这点,我刷了四次不同 ...

  6. Android蓝牙A2DP连接实现

    代码地址如下:http://www.demodashi.com/demo/14624.html 开发环境: 开发工具:Androidstudio 适配机型:honor8(Android6.0), 坚果 ...

  7. android 蓝牙开发---与蓝牙模块进行通讯 基于eclipse项目

      2017.10.20 之前参加一个大三学长的创业项目,做一个智能的车锁App,用到嵌入式等技术,App需要蓝牙.实时位置等技术,故查了几篇相关技术文章,以此参考!             //先说 ...

  8. 【转】【翻】Android Design Support Library 的 代码实验——几行代码,让你的 APP 变得花俏

    转自:http://mrfufufu.github.io/android/2015/07/01/Codelab_Android_Design_Support_Library.html [翻]Andro ...

  9. Android Design Support Library 的 代码实验——几行代码,让你的 APP 变得花俏

    原文:Codelab for Android Design Support Library used in I/O Rewind Bangkok session--Make your app fanc ...

随机推荐

  1. Keil MDK 5代码补全功能设置

    这段时间在用Keil5编程,经常会遇到在程序文件头部定义一个全局变量.在后面的编程过程中,经常会要用到这个变量,如果每次再打这个变量名会特别麻烦和浪费时间,我就想着Keil5有没有像vs软件一样的代码 ...

  2. 如何交叉编译openssl库?

    1. 获取源码 wget https://www.openssl.org/source/openssl-1.0.2s.tar.gz 2. 解压源码 tar xvf openssl-1.0.2s.tar ...

  3. PaddlePaddle实现线性回归

    在本次实验中我们将使用PaddlePaddle来搭建一个简单的线性回归模型,并利用这一模型预测你的储蓄(在某地区)可以购买多大面积的房子.并且在学习模型搭建的过程中,了解到机器学习的若干重要概念,掌握 ...

  4. LeetCode_58. Length of Last Word

    58. Length of Last Word Easy Given a string s consists of upper/lower-case alphabets and empty space ...

  5. (十四)用session和过滤器方法检验用户是否登录

    一.session方法 1.1 编写登录页面文件(index.html) <!doctype html> <html> <head> <title>测试 ...

  6. MongoDB集群之分片技术应用 —— 学习笔记

    课程链接:https://www.imooc.com/learn/501 一.什么是分片? 分片:将数据进行2拆分,将数据水平的分散到不同的服务器上. 二.为什么要分片? 架构上:读写均衡.去中心化 ...

  7. 车道线检测github集锦

    re1. github_lane_detection; end

  8. Docker - 在CentOS7中安装Docker

    在CentOS 7中安装Docker 1-确认系统信息 # cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) # uname - ...

  9. 海量无损高音质音乐文件分享180TB(持续更新)

    海量无损高音质音乐文件分享180TBWAV,flac,ape格式(持续更新),由于本人是音乐发烧爱好者,收集海量的无损音乐,已经分类好了,比较方便查找,但是本地没法存储,所有放在网盘中,并且我这边还会 ...

  10. VS2010 如何在调试的时候输入参数

    VS2010 如何在调试的时候输入参数 声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 很明显,好多算法程序实现的时候提供的例程都是需要在命令行中输入参数,比 ...