1.摘要
Android手机间通过蓝牙方式进行通信,有两种常见的方式,一种是socket方式,另一种是通过GATT Server(Android 5.0以后)通信。
(1)socket方式最为简单,但是很多低功耗的蓝牙设备,如单片机上的蓝牙模块可能不支持
(2)GATT方式相对比较复杂
(3)socket、GATT均为C/S(client-server)模式
本文基于两种通信方式,进行详细展开,并推荐了开源项目,建议配合学习。
关键词
(1)Bluetooth
蓝牙(Bluetooth):蓝牙,是一种支持设备短距离通信(一般10m内)的无线电技术,能在包括移动电话、PDA、无线耳机、笔记本电脑、相关外设等众多设备之间进行无线信息交换。利用“蓝牙”技术,能够有效地简化移动通信终端设备之间的通信,也能够成功地简化设备与因特网Internet之间的通信,从而使数据传输变得更加迅速高效,为无线通信拓宽道路。
(2) UUID
UUID(Universally Unique Identifier):用于标识蓝牙服务以及通讯特征访问属性,不同的蓝牙服务和属性使用不同的访问方法。
(3)服务UUID
服务UUID(Service UUID):不同的服务(Service)应该有不同的编号(UUID),用以区分不同的服务(Service)。
(4)特征值UUID
特征值UUID(Characteristic UUID):特性(Characteristic) 是依附于某个服务(Service)的
(5)属性(Property)
(5.1)Read: 读属性
Read: 读属性,具有这个属性的特性是可读的,也就是说这个属性允许手机来读取一些信息。手机可以发送指令来读取某个具有读属性UUID的信息。
(5.2)Notify: 通知属性
Notify: 通知属性, 具有这个属性的特性是可以发送通知的,也就是说具有这个属性的特性(Characteristic)可以主动发送信息给手机。
(5.3)Write: 写属性
Write: 写属性, 具有这个属性的特性是可以接收写入数据的。通常手机发送数据给蓝模块就是通过这个属性完成的。这个属性在Write 完成后,会发送写入完成结果的反馈给手机,然后手机再可以写入下一包或处理后续业务,这个属性在写入一包数据后,需要等待应用层返回写入结果,速度比较慢。
(5.4)WriteWithout Response:写属性
WriteWithout Response:写属性,从字面意思上看,只是写,不需要返回写的结果,这个属性的特点是不需要应用层返回,完全依靠协议层完成,速度快,但是写入速度超过协议处理速度的时候,会丢包。
(6) GATT
GATT(Generic Attribute Profile):中文名叫通用属性协议,它定义了services和characteristic两种东西来完成低功耗蓝牙设备之间的数据传输。它是建立在通用数据协议Attribute Protocol (ATT),之上的,ATT把services和characteristic以及相关的数据保存在一张简单的查找表中,该表使用16-bit的id作为索引。
(7)profile
profile可以理解为一种规范,一个标准的通信协议,它存在于从机中。蓝牙组织规定了一些标准的profile,例如 HID OVER GATT ,防丢器 ,心率计等。每个profile中会包含多个service,每个service代表从机的一种能力。
2. Bluetooth Socket
以该项目demo为例介绍
蓝牙端口监听接口和TCP端口类似:Socket和ServerSocket类。在服务器端,使用BluetoothServerSocket类来创建一个 监听服务端口。当一个连接被BluetoothServerSocket所接受,它会返回一个新的BluetoothSocket来管理该连接。在客户 端,使用一个单独的BluetoothSocket类去初始化一个外接连接和管理该连接。
最通常使用的蓝牙端口是RFCOMM,它是被Android API支持的类型。RFCOMM是一个面向连接,通过蓝牙模块进行的数据流传输方式,它也被称为串行端口规范(Serial Port Profile,SPP)。
为了创建一个BluetoothSocket去连接到一个已知设备,使用方法 BluetoothDevice.createRfcommSocketToServiceRecord()。然后调用connect()方法去尝试一个 面向远程设备的连接。这个调用将被阻塞指导一个连接已经建立或者该链接失效。
为了创建一个BluetoothSocket作为服务端(或者“主机”),查看BluetoothServerSocket文档。
每当该端口连接成功,无论它初始化为客户端,或者被接受作为服务器端,通过getInputStream()和getOutputStream()来打开IO流,从而获得各自的InputStream和OutputStream对象
BluetoothSocket类线程安全。特别的,close()方法总会马上放弃外界操作并关闭服务器端口。
注意
1.需要BLUETOOTH权限。
2.此处BluetoothSocket使用uuid:00001101-0000-1000-8000-00805F9B34FB
2.1 Server
private static final String UUIDString = "00001101-0000-1000-8000-00805F9B34FB";
//开启服务器
private class ServerThread extends Thread {
@Override
public void run() {
try {
/* 创建一个蓝牙服务器
* 参数分别:服务器名称、UUID */
mServerSocket = bluetoothAdapter.listenUsingRfcommWithServiceRecord(PROTOCOL_SCHEME_RFCOMM, UUID.fromString(UUIDString));
while (true){
Log.d("server", "wait cilent connect...");
Message msg = new Message();
msg.obj = "请稍候,正在等待客户端的连接...";
msg.what = WAITING_FOR_CLIENT;
linkDetectedHandler.sendMessage(msg);
/* 接受客户端的连接请求 */
BluetoothSocket socket = mServerSocket.accept();
socketMap.put(socket.getRemoteDevice().getAddress(), socket);
// remoteDeviceMap.put(socket.getRemoteDevice().getAddress(),socket.getRemoteDevice());
Log.d("server", "accept success !");
Message msg2 = new Message();
String info = "客户端已经连接上!可以发送信息。";
msg2.obj = info;
msg.what = CONNECTED_CLIENT;
linkDetectedHandler.sendMessage(msg2);
//启动接受数据
ReadThread mreadThread = new ReadThread(socket.getRemoteDevice().getAddress());
readThreadMap.put(socket.getRemoteDevice().getAddress(),mreadThread);
mreadThread.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.2 Client
//开启客户端
private class ClientThread extends Thread {
private String remoteAddress;
public ClientThread(String remoteAddress) {
this.remoteAddress = remoteAddress;
}
@Override
public void run() {
try {
//创建一个Socket连接:只需要服务器在注册时的UUID号
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(remoteAddress);
BluetoothSocket socket = device.createRfcommSocketToServiceRecord(UUID.fromString(UUIDString));
//连接
Message msg2 = new Message();
msg2.obj = "请稍候,正在连接服务器:" + remoteAddress;
msg2.what = IS_CONNECTING_SERVER;
linkDetectedHandler.sendMessage(msg2);
socket.connect();
socketMap.put(remoteAddress, socket);
Message msg = new Message();
msg.obj = remoteAddress;
msg.what = CONNECTED_SERVER;
linkDetectedHandler.sendMessage(msg);
//启动接受数据
ReadThread mreadThread = new ReadThread(remoteAddress);
readThreadMap.put(remoteAddress,mreadThread);
mreadThread.start();
} catch (IOException e) {
e.printStackTrace();
socketMap.remove(remoteAddress);
Log.e("connect", e.getMessage(), e);
Message msg = new Message();
msg.obj = "连接服务端异常!断开连接重新试一试。"+e.getMessage();
msg.what = CONNECT_SERVER_ERROR;
linkDetectedHandler.sendMessage(msg);
}
}
}
3. Bluetooth GATT
以该项目demo为例介绍
注意
1.GATT提供了notifyCharacteristicChanged方法去响应客户端的请求值,响应值(characteristic.value)
2.uuid经过本人测试仅支持以下情况,网文中提及手机端可能不支持自定义uuid。
val BLE_SERVICE_UUID = UUID.fromString("00001802-0000-1000-8000-00805f9b34fb")
val BLE_WRITE_UUID = UUID.fromString("00002a02-0000-1000-8000-00805f9b34fb")
val BLE_READ_UUID = UUID.fromString("00002a03-0000-1000-8000-00805f9b34fb")
val BLE_DESC_UUID = UUID.fromString("00002a04-0000-1000-8000-00805f9b34fb")
3.1 Server
private fun setupServer() {
val gattService = BluetoothGattService(Constants.BLE_SERVICE_UUID, BluetoothGattService.SERVICE_TYPE_PRIMARY)
val characteristicRead = BluetoothGattCharacteristic(Constants.BLE_READ_UUID, BluetoothGattCharacteristic.PROPERTY_READ, BluetoothGattCharacteristic.PERMISSION_READ)
val descriptor = BluetoothGattDescriptor(Constants.BLE_DESC_UUID, BluetoothGattCharacteristic.PERMISSION_WRITE)
characteristicRead.addDescriptor(descriptor)
gattService.addCharacteristic(characteristicRead)
val characteristicWrite = BluetoothGattCharacteristic(Constants.BLE_WRITE_UUID, BluetoothGattCharacteristic.PROPERTY_WRITE or
BluetoothGattCharacteristic.PROPERTY_READ or BluetoothGattCharacteristic.PROPERTY_NOTIFY,
BluetoothGattCharacteristic.PERMISSION_WRITE)
gattService.addCharacteristic(characteristicWrite)
Log.d("wenpd", "startGattServer:stagattServicetus=$gattService")
mGattServer.addService(gattService)
}
3.2 Client
private class GattClientCallback extends BluetoothGattCallback {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
if (status == BluetoothGatt.GATT_FAILURE) {
disconnectGattServer();
return;
} else if (status != BluetoothGatt.GATT_SUCCESS) {
disconnectGattServer();
return;
}
if (newState == BluetoothProfile.STATE_CONNECTED) {
mConnected = true;
gatt.discoverServices();
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
disconnectGattServer();
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
Log.d(TAG, "onServicesDiscovered status:" + status);
if (status != BluetoothGatt.GATT_SUCCESS) {
return;
}
BluetoothGattService service = gatt.getService(Constants.SERVICE_UUID);
BluetoothGattCharacteristic characteristic = service.getCharacteristic(Constants.CHARACTERISTIC_UUID);
characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
mInitialized = gatt.setCharacteristicNotification(characteristic, true);
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
byte[] messageBytes = characteristic.getValue();
/*for(int i = 0, j = messageBytes.length -1; i < j; ++i, --j) {
byte temp = messageBytes[i];
messageBytes[i] = messageBytes[j];
messageBytes[j] = temp;
}*/
String messageString = new String(messageBytes, StandardCharsets.UTF_8);
Log.d(TAG,"Received message: " + messageString);
setReceivedData(messageString);
}
}
附录
常用uuid
00001000-0000-1000-8000-00805F9B34FB Service Discovery Server Service
00001001-0000-1000-8000-00805F9B34FB Browse Group Descriptor Service
00001002-0000-1000-8000-00805F9B34FB Public Browse Group Service
#蓝牙串口服务
00001101-0000-1000-8000-00805F9B34FB Serial Port Service
#使用PPP服务类的局域网访问
00001102-0000-1000-8000-00805F9B34FB LAN Access Using PPP Service
#拨号网络服务
00001103-0000-1000-8000-00805F9B34FB Dialup Networking Service
#信息同步服务
00001104-0000-1000-8000-00805F9B34FB IrMC Sync Service
00001105-0000-1000-8000-00805F9B34FB SDP_OBEX Object Push Service
#文件传输服务
00001106-0000-1000-8000-00805F9B34FB OBEX File Transfer Service
00001107-0000-1000-8000-00805F9B34FB IrMC Sync Command Service
00001108-0000-1000-8000-00805F9B34FB SDP_Headset Service
00001109-0000-1000-8000-00805F9B34FB Cordless Telephony Service
0000110A-0000-1000-8000-00805F9B34FB SDP_Audio Source Service
0000110B-0000-1000-8000-00805F9B34FB SDP_Audio Sink Service
0000110C-0000-1000-8000-00805F9B34FB SDP_AV Remote Control Target Service
0000110D-0000-1000-8000-00805F9B34FB SDP_Advanced Audio Distribution Service
0000110E-0000-1000-8000-00805F9B34FB SDP_AV Remote Control Service
0000110F-0000-1000-8000-00805F9B34FB Video Conferencing Service
00001110-0000-1000-8000-00805F9B34FB Intercom Service
#蓝牙传真服务
00001111-0000-1000-8000-00805F9B34FB Fax Service
00001112-0000-1000-8000-00805F9B34FB Headset Audio Gateway Service
00001113-0000-1000-8000-00805F9B34FB WAP Service
00001114-0000-1000-8000-00805F9B34FB WAP Client Service
#个人局域网服务
00001115-0000-1000-8000-00805F9B34FB PANU Service
#个人局域网服务
00001116-0000-1000-8000-00805F9B34FB NAP Service
#个人局域网服务
00001117-0000-1000-8000-00805F9B34FB GN Service
00001118-0000-1000-8000-00805F9B34FB Direct Printing Service
00001119-0000-1000-8000-00805F9B34FB Reference Printing Service
0000111A-0000-1000-8000-00805F9B34FB Imaging Service
0000111B-0000-1000-8000-00805F9B34FB Imaging ResponderService
0000111C-0000-1000-8000-00805F9B34FB Imaging Automatic Archive Service
0000111D-0000-1000-8000-00805F9B34FB ImagingReferenceObjectsService
0000111E-0000-1000-8000-00805F9B34FB SDP_Hands free Service
0000111F-0000-1000-8000-00805F9B34FB Hands free Audio Gateway Service
00001120-0000-1000-8000-00805F9B34FB Direct Printing Reference Objects Service
00001121-0000-1000-8000-00805F9B34FB Reflected UI Service
00001122-0000-1000-8000-00805F9B34FB Basic Pringing Service
00001123-0000-1000-8000-00805F9B34FB Printing Status Service
#人机输入服务
00001124-0000-1000-8000-00805F9B34FB HumanInterface Device Service
00001125-0000-1000-8000-00805F9B34FB Hardcopy Cable Replacement Service
#蓝牙打印服务
00001126-0000-1000-8000-00805F9B34FB HCR Print Service
00001127-0000-1000-8000-00805F9B34FB HCR Scan Service
00001128-0000-1000-8000-00805F9B34FB CommonI SDN Access Service
00001129-0000-1000-8000-00805F9B34FB Video Conferencing GW Service
0000112A-0000-1000-8000-00805F9B34FB UDIMT Service
0000112B-0000-1000-8000-00805F9B34FB UDITA Service
0000112C-0000-1000-8000-00805F9B34FB Audio Video Service
0000112D-0000-1000-8000-00805F9B34FB SIM Access Service
00001200-0000-1000-8000-00805F9B34FB PnP Information Service
00001201-0000-1000-8000-00805F9B34FB Generic Networking Service
00001202-0000-1000-8000-00805F9B34FB Generic File Transfer Service
00001203-0000-1000-8000-00805F9B34FB Generic Audio Service
00001204-0000-1000-8000-00805F9B34FB Generic Telephony Service
==============================================================================================
Sample Services
0000180d-0000-1000-8000-00805f9b34fb Heart Rate Service
0000180a-0000-1000-8000-00805f9b34fb Device Information Service
Sample Characteristics.
00002a37-0000-1000-8000-00805f9b34fb Heart Rate Measurement
00002a29-0000-1000-8000-00805f9b34fb Manufacturer Name String
GATT Services
00001800-0000-1000-8000-00805f9b34fb GenericAccess
00001801-0000-1000-8000-00805f9b34fb GenericAttribute
GATT Declarations
00002800-0000-1000-8000-00805f9b34fb Primary Service
00002801-0000-1000-8000-00805f9b34fb Secondary Service
00002802-0000-1000-8000-00805f9b34fb Include
00002803-0000-1000-8000-00805f9b34fb Characteristic
GATT Descriptors
00002900-0000-1000-8000-00805f9b34fb Characteristic Extended Properties
00002901-0000-1000-8000-00805f9b34fb Characteristic User Description
00002902-0000-1000-8000-00805f9b34fb Client Characteristic Configuration
00002903-0000-1000-8000-00805f9b34fb Server Characteristic Configuration
00002904-0000-1000-8000-00805f9b34fb Characteristic Presentation Format
00002905-0000-1000-8000-00805f9b34fb Characteristic Aggregate Format
00002906-0000-1000-8000-00805f9b34fb Valid Range
00002907-0000-1000-8000-00805f9b34fb External Report Reference Descriptor
00002908-0000-1000-8000-00805f9b34fb Report Reference Descriptor
GATT Characteristics
00002a00-0000-1000-8000-00805f9b34fb Device Name
00002a01-0000-1000-8000-00805f9b34fb Appearance
00002a02-0000-1000-8000-00805f9b34fb Peripheral Privacy Flag
00002a03-0000-1000-8000-00805f9b34fb Reconnection Address
00002a04-0000-1000-8000-00805f9b34fb PPCP
00002a05-0000-1000-8000-00805f9b34fb Service Changed
GATT Service UUIDs
00001802-0000-1000-8000-00805f9b34fb Immediate Alert
00001803-0000-1000-8000-00805f9b34fb Link Loss
00001804-0000-1000-8000-00805f9b34fb Tx Power
00001805-0000-1000-8000-00805f9b34fb Current Time Service
00001806-0000-1000-8000-00805f9b34fb Reference Time Update Service
00001807-0000-1000-8000-00805f9b34fb Next DST Change Service
00001808-0000-1000-8000-00805f9b34fb Glucose
00001809-0000-1000-8000-00805f9b34fb Health Thermometer
0000180a-0000-1000-8000-00805f9b34fb Device Information
0000180b-0000-1000-8000-00805f9b34fb Network Availability
0000180d-0000-1000-8000-00805f9b34fb Heart Rate
0000180e-0000-1000-8000-00805f9b34fb Phone Alert Status Service
0000180f-0000-1000-8000-00805f9b34fb Battery Service
00001810-0000-1000-8000-00805f9b34fb Blood Pressure
00001811-0000-1000-8000-00805f9b34fb Alert Notification Service
00001812-0000-1000-8000-00805f9b34fb Human Interface Device
00001813-0000-1000-8000-00805f9b34fb Scan Parameters
00001814-0000-1000-8000-00805f9b34fb Running Speed and Cadence
00001816-0000-1000-8000-00805f9b34fb Cycling Speed and Cadence
00001818-0000-1000-8000-00805f9b34fb Cycling Power
00001819-0000-1000-8000-00805f9b34fb Location and Navigation
GATT Characteristic UUIDs
00002a06-0000-1000-8000-00805f9b34fb Alert Level
00002a07-0000-1000-8000-00805f9b34fb Tx Power Level
00002a08-0000-1000-8000-00805f9b34fb Date Time
00002a09-0000-1000-8000-00805f9b34fb Day of Week
00002a0a-0000-1000-8000-00805f9b34fb Day Date Time
00002a0c-0000-1000-8000-00805f9b34fb Exact Time 256
00002a0d-0000-1000-8000-00805f9b34fb DST Offset
00002a0e-0000-1000-8000-00805f9b34fb Time Zone
00002a0f-0000-1000-8000-00805f9b34fb Local Time Information
00002a11-0000-1000-8000-00805f9b34fb Time with DST
00002a12-0000-1000-8000-00805f9b34fb Time Accuracy
00002a13-0000-1000-8000-00805f9b34fb Time Source
00002a14-0000-1000-8000-00805f9b34fb Reference Time Information
00002a16-0000-1000-8000-00805f9b34fb Time Update Control Point
00002a17-0000-1000-8000-00805f9b34fb Time Update State
00002a18-0000-1000-8000-00805f9b34fb Glucose Measurement
00002a19-0000-1000-8000-00805f9b34fb Battery Level
00002a1c-0000-1000-8000-00805f9b34fb Temperature Measurement
00002a1d-0000-1000-8000-00805f9b34fb Temperature Type
00002a1e-0000-1000-8000-00805f9b34fb Intermediate Temperature
00002a21-0000-1000-8000-00805f9b34fb Measurement Interval
00002a22-0000-1000-8000-00805f9b34fb Boot Keyboard Input Report
00002a23-0000-1000-8000-00805f9b34fb System ID
00002a24-0000-1000-8000-00805f9b34fb Model Number String
00002a25-0000-1000-8000-00805f9b34fb Serial Number String
00002a26-0000-1000-8000-00805f9b34fb Firmware Revision String
00002a27-0000-1000-8000-00805f9b34fb Hardware Revision String
00002a28-0000-1000-8000-00805f9b34fb Software Revision String
00002a29-0000-1000-8000-00805f9b34fb Manufacturer Name String
00002a2a-0000-1000-8000-00805f9b34fb IEEE 11073-20601 Regulatory Certification Data List
00002a2b-0000-1000-8000-00805f9b34fb Current Time
00002a31-0000-1000-8000-00805f9b34fb Scan Refresh
00002a32-0000-1000-8000-00805f9b34fb Boot Keyboard Output Report
00002a33-0000-1000-8000-00805f9b34fb Boot Mouse Input Report
00002a34-0000-1000-8000-00805f9b34fb Glucose Measurement Context
00002a35-0000-1000-8000-00805f9b34fb Blood Pressure Measurement
00002a36-0000-1000-8000-00805f9b34fb Intermediate Cuff Pressure
00002a37-0000-1000-8000-00805f9b34fb Heart Rate Measurement
00002a38-0000-1000-8000-00805f9b34fb Body Sensor Location
00002a39-0000-1000-8000-00805f9b34fb Heart Rate Control Point
00002a3e-0000-1000-8000-00805f9b34fb Network Availability
00002a3f-0000-1000-8000-00805f9b34fb Alert Status
00002a40-0000-1000-8000-00805f9b34fb Ringer Control Point
00002a41-0000-1000-8000-00805f9b34fb Ringer Setting
00002a42-0000-1000-8000-00805f9b34fb Alert Category ID Bit Mask
00002a43-0000-1000-8000-00805f9b34fb Alert Category ID
00002a44-0000-1000-8000-00805f9b34fb Alert Notification Control Point
00002a45-0000-1000-8000-00805f9b34fb Unread Alert Status
00002a46-0000-1000-8000-00805f9b34fb New Alert
00002a47-0000-1000-8000-00805f9b34fb Supported New Alert Category
00002a48-0000-1000-8000-00805f9b34fb Supported Unread Alert Category
00002a49-0000-1000-8000-00805f9b34fb Blood Pressure Feature
00002a4a-0000-1000-8000-00805f9b34fb HID Information
00002a4b-0000-1000-8000-00805f9b34fb Report Map
00002a4c-0000-1000-8000-00805f9b34fb HID Control Point
00002a4d-0000-1000-8000-00805f9b34fb Report
00002a4e-0000-1000-8000-00805f9b34fb Protocol Mode
00002a4f-0000-1000-8000-00805f9b34fb Scan Interval Window
00002a50-0000-1000-8000-00805f9b34fb PnP ID
00002a51-0000-1000-8000-00805f9b34fb Glucose Feature
00002a52-0000-1000-8000-00805f9b34fb Record Access Control Point
00002a53-0000-1000-8000-00805f9b34fb RSC Measurement
00002a54-0000-1000-8000-00805f9b34fb RSC Feature
00002a55-0000-1000-8000-00805f9b34fb SC Control Point
00002a5b-0000-1000-8000-00805f9b34fb CSC Measurement
00002a5c-0000-1000-8000-00805f9b34fb CSC Feature
00002a5d-0000-1000-8000-00805f9b34fb Sensor Location
00002a63-0000-1000-8000-00805f9b34fb Cycling Power Measurement
00002a64-0000-1000-8000-00805f9b34fb Cycling Power Vector
00002a65-0000-1000-8000-00805f9b34fb Cycling Power Feature
00002a66-0000-1000-8000-00805f9b34fb Cycling Power Control Point
00002a67-0000-1000-8000-00805f9b34fb Location and Speed
00002a68-0000-1000-8000-00805f9b34fb Navigation
00002a69-0000-1000-8000-00805f9b34fb Position Quality
00002a6a-0000-1000-8000-00805f9b34fb LN Feature
00002a6b-0000-1000-8000-00805f9b34fb LN Control Point"