蓝牙门禁Android客户端
先来了解下Android传统蓝牙连接的大致简单的流程:
其中涉及到几个类依次来介绍,废话不多说,下面是从Android4.4开发指南蓝牙所用到的类的截图:
第一个类BluetoothAdapter:
注意两点:
1这是一个继承子Object的final类,不能进行继承。
2在系统为4.2及以下可以调用静态方法getDefaultAdapter()获取设备本地适配器;在系统为4.3及以上版本调用BluetoothManager的
getAdapter()
类中其他重要方法:获取已经配对的设备--BluetoothDevices的set集合,开始发现设备--bool,创建侦听的RFCOMM安全/非安全通道
远程蓝牙设备,可以通过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代码