蓝牙门禁Android客户端
先来了解下Android传统蓝牙连接的大致简单的流程:
其中涉及到几个类依次来介绍,废话不多说,下面是从Android4.4开发指南蓝牙所用到的类的截图:
第一个类BluetoothAdapter:
注意两点:
1这是一个继承子Object的final类,不能进行继承。
2在系统为4.2及以下可以调用静态方法getDefaultAdapter()获取设备本地适配器;在系统为4.3及以上版本调用BluetoothManager的
getAdapter()
类中其他重要方法:获取已经配对的设备--BluetoothDevices的set集合,开始发现设备--bool,创建侦听的RFCOMM安全/非安全通道
第二个类为BluetoothDevice:
远程蓝牙设备,可以通过UUID创建出BluetoothSocket(蓝牙套接字接口)对象,可以进行连接操作。
详细说明下createRfcommSocketToServiceRecord方法:
创建一个RFCOMM蓝牙套接字准备开始一个安全的传出连接到远程设备。返回的是BluetoothSocket对象
注意点:如果连接蓝牙串行板,尝试使用著名的UUID-00001101-0000-1000-8000-00805F9B34FB(一般固定的)然而若是一个Android对等体请使用自己生成的UUID
第三,四个类BluetoothSocket与BluetoothServerSocket
首先看下BluetoothSocket介绍:
蓝牙套接字接口类似tcp套接字(Socket与ServerSocket);
在服务端方面,使用一个BluetoothServerSocket来创建一个侦听的服务端套接字。当一个连接被BluetoothServerSocket接受,它将返回一个新的BluetoothSocket来管理连接;在客户端,使用单个BluetoothSocket来启动传出连接和管理连接。
最常见的蓝牙套接字类型是RFCOMM,这是Android API支持的类型。 RFCOMM是面向连接的,通过蓝牙的流传输。 它也称为串行端口配置文件(SPP)。
使用 BluetoothDevice.createRfcommSocketToServiceRecord()去创建一个BluetoothSocket连接一个已知的设备,然后通过他回调connect()与远程设备建立一个连接。
一旦套接字已连接,无论是连接为客户端还是连接为服务端,通过调用getInputStream()与getOutputStream()来分别检索InputStream对象,这些对象分别自动连接到套接字。
BluetoothSocket是线程安全的,另外,close() 方法将立即终止正在进行的操作和关闭套接字。
再来看下BluetoothServerSocket类:
其中有两个重载方法,一个可设置超时连接,方法阻塞,直到建立连接
返回值为BluetoothSocket对象可以管理连接,数据共享交互
第五个类为BluetoothClass:描述蓝牙设备的一般特征和功能,暂时用不到。
下面来介绍下具体连接蓝牙功能的代码实现:
思路:如果将蓝牙连接与数据通信部分放在Activity中,那么假如退出Activity,套接字也会随着activity关闭,而且每打开一次Activity又 要原样来一遍,又不稳定。有没有一中可以一直在后台运行的东西,可控制性的去管理它呢?
通过服务与广播机制来实现Activity与Service通信,Service启动方式有两种,一种是通过bindService(),另一种是通过startService(),
这两种启动方式的区别大家去清楚,bindService方式的服务随着调用者消亡而死亡;startService方式创建一次就会存在,除非 stopself()方法或者进程死亡。两种方式都可以实现与Activity交互,显然采用第二种方式更稳定,Activity与Service的通信采用广播机制 清晰简单。
原理介绍下:
下面贴下Demo代码:
1需要声明权限,android:exported指的是能否与其他程序交互,不能被访问则为false.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.likego.gobackbluetooth"> <!--声明蓝牙权限!-->
<uses-permission android:name="android.permission.BLUETOOTH" /> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <service
android:name=".BlueToothService"
android:exported="false"></service>
</application> </manifest>
2.界面代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查找发现设备"
android:id="@+id/lookBtn"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送数据"
android:id="@+id/sendBtn"
android:layout_below="@+id/lookBtn"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="清空数据"
android:id="@+id/clearBtn"
android:layout_below="@+id/sendBtn"
android:layout_alignParentStart="true" android:layout_alignParentEnd="true" /> <EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:id="@+id/editText"
android:text="返回结果:"
android:singleLine="false"
android:scrollbars="vertical"
android:editable="false"
android:focusable="false"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_below="@+id/clearBtn"
android:layout_alignParentBottom="true"
android:gravity="top" />
</RelativeLayout>
3.Activity代码-发现与查找设备,连接设备,发送数据等。
4.SerVice代码
蓝牙门禁Android客户端的更多相关文章
- 离线人脸识别门禁考勤——Android设备端APK及源码免费下载
适用场景:门禁场景的应用,适合安装在Android系统的门口机.闸机头.Pad等设备上. 主要功能:人员注册.人脸识别开门.考勤打卡.门禁权限管理.识别记录查询等. 预览效果: PC端 设备端1 设备 ...
- 使用calabash测试开源中国Android客户端
Calabash-android是支持android的UI自动化测试框架,前面已经介绍过<中文Win7下成功安装calabash-android步骤>,这篇博文尝试测试一个真实应用:开源中 ...
- 大华门禁SDK二次开发(二)-SignalR应用
经过与大华技术支持的沟通,门禁服务程序已经开发好了,可以正常接收门禁开关事件,可以发送开门命令.基于项目实时性要求,这里使用SignalR实现门禁状态.控制命令的实时传送. 几种场景需求 根据Sign ...
- 【Arduino】开发入门【十】Arduino蓝牙模块与Android实现通信
[Arduino]开发入门[十]蓝牙模块 首先show一下新入手的蓝牙模块 蓝牙参数特点 1.蓝牙核心模块使用HC-06从模块,引出接口包括VCC,GND,TXD,RXD,预留LED状态输出脚,单片机 ...
- 利用nexus5伪造一张门禁卡
0×00 前言 我租住的杭州一个老小区一年前出现了所谓的“出租房杀人事件”,事件过后民警叔叔们给小区的每个单元都装上了门禁,所有住户都需要在物业处登记,物业的工作人员会让你提供身份证或者公交卡用来注册 ...
- Android客户端和服务器端数据交互
网上有很多例子来演示Android客户端和服务器端数据如何实现交互不过这些例子大多比较繁杂,对于初学者来说这是不利的,现在介绍几种代码简单.逻辑清晰的交互例子,本篇博客介绍第四种: 一.服务器端: 代 ...
- appium 自动化测试之知乎Android客户端
appium是一个开源框架,相对来说还不算很稳定.转载请注明出处!!!! 前些日子,配置好了appium测试环境,至于环境怎么搭建,参考:http://www.cnblogs.com/tobecraz ...
- 仿优酷Android客户端图片左右滑动(自动滑动)
最终效果: 页面布局main.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayou ...
- 【原创】轻量级即时通讯技术MobileIMSDK:Android客户端开发指南
申明:MobileIMSDK 目前为个人维护的原创开源工程,现陆续整理了一些资料,希望对需要的人有用.如需与作者交流,见文章底签名处,互相学习. MobileIMSDK开源工程的代码托管地址请进入 G ...
随机推荐
- LPC1788定时器使用
#ifndef __TIM_H_ #define __TIM_H_ #include "common.h" extern u8 tim1_mr0_flag; void tim0_c ...
- ymodem协议c实现(转)
源:ymodem协议c实现 /****************************************Copyright (c)******************************** ...
- iOS8推送消息的快速回复处理
http://blog.csdn.net/yujianxiang666/article/details/35260135 iOS8拥有了全新的通知中心,有全新的通知机制.当屏幕顶部收到推送时只需要往下 ...
- 解决tomcat运行报错java.lang.UnsatisfiedLinkError: apache-tomcat-7.0.37\bin\tcnative-1.dll:Can load AMD 64
http://www.apache.org/dist/tomcat/tomcat-connectors/native/ 到该地址下下载一个tomcat-native-1.2.2-win32-bin压缩 ...
- cocos2d动作讲解
从本章开始,我们开始讲解cocos2d-x库的动作(Action).游戏的世界是一个动态的世界:无论是主角精灵还是NPC精灵都处于不断的运动当中,甚至是背景中漂流的树叶,随风而动的小草.这些明显的或者 ...
- JDBC executeBatch 抛出异常停止
进行批量更新的时候发现: addBatch(sql); executeBatch 抛出异常后,剩余的sql没有更新,即出现异常之前的都入库了,异常之后即使有可执行sql都不会执行. 百度资料后了解:这 ...
- Spring MVC之RequestMapping
第一部分.概述 /**映射URL到控制器类或处理程序*/@Target({ElementType.METHOD, ElementType.TYPE})@Retention(RetentionPolic ...
- 【动态规划】Gym - 101102A - Coins
Hasan and Bahosain want to buy a new video game, they want to share the expenses. Hasan has a set of ...
- simplexml 对xml的增删改操作
simplexml 是php 处理xml 文件的一个方法,另一个是dom 处理,这里只说simplexml .目前php 处理xml 用的比较多,比较成熟的还是dom .但dom 在速度和代码量上还是 ...
- 会话Cookie及session的关系(Cookie & Session)
会话Cookie及session的关系(Cookie & Session) 在通常的使用中,我们只知道session信息是存放在服务器端,而cookie是存放在客户端.但服务器如何使用sess ...