Android蓝牙遥控器APP关键代码 guihub项目
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项目的更多相关文章
- Android之打开闪光灯关键代码
在AndroidManifest中注册相应的权限: <uses-permission android:name="android.permission.FLASHLIGHT" ...
- Android开发 开启闪光灯 关键代码
在AndroidManifest中注册响应的权限: <uses-permission android:name="android.permission.FLASHLIGHT" ...
- 【转】蓝牙ble app开发(三) -- 抓包
原文网址:http://blog.csdn.net/lckj686/article/details/43156617 关于android 蓝牙app开发抓包的重要性在 android 蓝牙ble ap ...
- 【转】android蓝牙开发---与蓝牙模块进行通信--不错
原文网址:http://www.cnblogs.com/wenjiang/p/3200138.html 近半个月来一直在搞android蓝牙这方面,主要是项目需要与蓝牙模块进行通信.开头的进展很顺利, ...
- android蓝牙开发---与蓝牙模块进行通信
近半个月来一直在搞android蓝牙这方面,主要是项目需要与蓝牙模块进行通信.开头的进展很顺利,但因为蓝牙模块不在我这里,所以只能用手机测试.一开头就发现手机的蓝牙不能用,为了证明这点,我刷了四次不同 ...
- Android蓝牙A2DP连接实现
代码地址如下:http://www.demodashi.com/demo/14624.html 开发环境: 开发工具:Androidstudio 适配机型:honor8(Android6.0), 坚果 ...
- android 蓝牙开发---与蓝牙模块进行通讯 基于eclipse项目
2017.10.20 之前参加一个大三学长的创业项目,做一个智能的车锁App,用到嵌入式等技术,App需要蓝牙.实时位置等技术,故查了几篇相关技术文章,以此参考! //先说 ...
- 【转】【翻】Android Design Support Library 的 代码实验——几行代码,让你的 APP 变得花俏
转自:http://mrfufufu.github.io/android/2015/07/01/Codelab_Android_Design_Support_Library.html [翻]Andro ...
- Android Design Support Library 的 代码实验——几行代码,让你的 APP 变得花俏
原文:Codelab for Android Design Support Library used in I/O Rewind Bangkok session--Make your app fanc ...
随机推荐
- 单硬盘根分区扩容(非LVM)
单用户模式(内核参数末尾加single)救援模式(用光盘启动,选第三个,rescue installed system) 救援模式有什么作用: 1可以更改root密码:2恢复硬盘.文件系统操作:3系统 ...
- udf提权小结
00x1 首先判断mysql版本, mysql版本 < 5.2 , UDF导出到系统目录c:/windows/system32/ mysql版本 > 5.2 ,UDF导出到安装路径MySQ ...
- CYLTabBarController的简单使用
#pragma mark- 登录成功跳转至主页 -(void)jumpToMainVC { [UIApplication sharedApplication].statusBarStyle = UIS ...
- jenkins:构建机器为windows,部署机器为linux
备份老的jar包 d=`date +"%Y%m%d-%H%M%S"` cd /home/eccore/app/uat -SNAPSHOT.jar steward-api--SNAP ...
- Win10利用CodeBlocks搭建Objective-C开发环境(二)
工程文件已经建好:但此时会发现main.m文件为灰色,且无法点击,此时需右键点击main.m文件,在option选项中勾选 compile file和 link file选项. 设置完成后,双击mai ...
- 在C/C++中常用的符号
C++中&和*的用法一直是非常让人头疼的难点,课本博客上讲这些知识点一般都是分开讲其用法的,没有详细的总结,导致我在这方面的知识结构格外混乱,在网上找到了一篇英文文章简单总结了这两个符号的一些 ...
- 《C语言程序设计教程》学习笔记
<C语言程序设计教程>--朱鸣华.刘旭麟等 第一章 C语言概述 1.C语言的特点: 1)兼具高级.低级语言的双重能力(C语言允许直接访问物理地址,能够进行位操作,能实现汇编语言的大部分功能 ...
- OneNote2016代码高亮插件的安装与使用
OneNote2016代码高亮插件的安装与使用 使用效果 我觉得CSDN和博客园上面的许多讲解都不是很清晰,最后还是我自己弄好的.这里分享一下: 第一步要确认自己OneNote的版本是32位的还是64 ...
- UIPath工具取得多个文件的方法
下图是取得某个路径下的多个文件的做法.取得Excel文件的第一个sheet页[workBook.GetSheets(0)]
- .net和ASP.net,c#的区别
.NET.C#和ASP.NET三者之间的区别如下: 一.什么是.NET?.NET是微软公司下的一个开发平台,.NET核心就是.NET Framwork(.NET框架)是.NET程序开发和运行的环境,在 ...