一、BluetoothSocket介绍
BluetoothSocket是Android中用于在蓝牙设备之间进行数据传输的一种Socket。它提供了一种类似于TCP/IP协议的数据传输方式,可以在两个设备之间建立一个虚拟的通信管道,使得两个设备之间可以进行数据的传输和接收。BluetoothSocket可以用于客户端和服务端之间的通信,通过BluetoothSocket可以实现数据的双向传输。在Android中,BluetoothSocket是通过BluetoothDevice的createRfcommSocketToServiceRecord()方法创建的。
具体使用方法可以参考以下代码:
// 通过MAC地址获取远程设备
BluetoothDevice targetDevice = bluetoothAdapter.getRemoteDevice("40:44:FD:A5:B3:22");
// 通过与服务端保持一致的uuid来创建射频socket
new Thread(new Runnable() {
@Override
public void run() {
try {
BluetoothSocket bluetoothSocket = targetDevice.createRfcommSocketToServiceRecord(UUID.fromString(UUIDString));
bluetoothSocket.connect();
mSocket = bluetoothSocket;
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
二、BluetoothSocket相关接口
IBluetoothSocketManager
API for Bluetooth Sockets service.
IBluetoothSocketManager代码位于:
packages/modules/Bluetooth/system/binder/android/bluetooth/IBluetoothSocketManager.aidl
IBluetoothSocketManager的定义:
interface IBluetoothSocketManager {}
IBluetoothSocketManager API:
ParcelFileDescriptor connectSocket(in BluetoothDevice device, int type, in @nullable ParcelUuid uuid, int port, int flag);
ParcelFileDescriptor createSocketChannel(int type, in @nullable String serviceName, in @nullable ParcelUuid uuid, int port, int flag);
void requestMaximumTxDataLength(in BluetoothDevice device);
三、BluetoothSocket相关类
BluetoothSocket
蓝牙套接口类似于TCP套接字: Socket和ServerSocket 。 在服务器端,使用BluetoothServerSocket创建侦听服务器套接字。 当连接被BluetoothServerSocket接受时,它将返回一个新的BluetoothSocket来管理连接。 在客户端,使用单个BluetoothSocket来启动传出连接并管理连接。
最常见的蓝牙套接字类型是RFCOMM,它是Android API支持的类型。 RFCOMM是一种通过蓝牙实现的面向连接的流媒体传输。 它也被称为串行端口配置文件(SPP)。
要创建一个BluetoothSocket ,用于连接到已知的设备,使用BluetoothDevice.createRfcommSocketToServiceRecord() 。 然后调用connect()尝试连接到远程设备。 此呼叫将阻塞,直到连接建立或连接失败。
要创建 BluetoothSocket作为服务器(或“主机”),请参阅 BluetoothServerSocket文档。
连接套接字后,无论是作为客户端启动还是作为服务器接受,都可以通过调用 getInputStream()和 getOutputStream()来打开IO流,以分别检索自动连接到套接字的对象 InputStream和 OutputStream 。
BluetoothSocket是线程安全的。 特别是, close()将始终立即中止正在进行的操作并关闭套接字。
BluetoothSocket是Android中用于在蓝牙设备之间进行数据传输的一种Socket。它提供了一种类似于TCP/IP协议的数据传输方式,可以在两个设备之间建立一个虚拟的通信管道,实现数据的传输和接收。在蓝牙通信中,BluetoothSocket通常用于客户端和服务端之间的数据传输。客户端通过BluetoothSocket连接到服务端,然后通过输入输出流进行数据的传输。
BluetoothSocket代码位于:
packages/modules/Bluetooth/framework/java/android/bluetooth/BluetoothSocket.java
BluetoothSocket的定义:
public final class BluetoothSocket implements Closeable {}
BluetoothSocket API:
void close():关闭此流并释放与其关联的所有系统资源。
void connect():尝试连接到远程设备。
int getConnectionType():获取底层连接的类型。
InputStream getInputStream():获取与此套接字关联的输入流。
int getMaxReceivePacketSize():获取底层传输的最大支持接收数据包大小。
int getMaxTransmitPacketSize():获取底层传输的最大支持传输数据包大小。
OutputStream getOutputStream():获取与此套接字关联的输出流。
BluetoothDevice getRemoteDevice():获取此套接字连接或连接到的远程设备。
boolean isConnected():获取此套接字的连接状态,即是否存在与远程设备的活动连接。
BluetoothServerSocket
BluetoothServerSocket是Android中用于创建蓝牙服务器的类。它允许设备在蓝牙网络上监听传入的连接请求,并在连接请求到达时创建BluetoothSocket对象。
BluetoothServerSocket代码位于:
packages/modules/Bluetooth/framework/java/android/bluetooth/BluetoothServerSocket.java
BluetoothServerSocket的定义:
public final class BluetoothServerSocket implements Closeable {}
BluetoothServerSocket API:
BluetoothSocket accept():阻塞直到连接建立。
BluetoothSocket accept(int timeout):阻塞直到建立连接,并超时。
void close():立即关闭此套接字,并释放所有关联的资源。
String toString():返回对象的字符串表示形式。
BluetoothSocketManagerBinder
BluetoothSocketManagerBinder实现了IBluetoothSocketManager接口,内部调用AdapterService实现connectSocket、createSocketChannel等功能。
BluetoothSocketManagerBinder代码位于:
packages/modules/Bluetooth/android/app/src/com/android/bluetooth/btservice/BluetoothSocketManagerBinder.java
BluetoothSocketManagerBinder的定义:
class BluetoothSocketManagerBinder extends IBluetoothSocketManager.Stub {}
BluetoothInputStream
Used to write to a Bluetooth socket.
用于写入蓝牙socket。
BluetoothInputStream代码位于:
packages/modules/Bluetooth/framework/java/android/bluetooth/BluetoothInputStream.java
BluetoothInputStream的定义
final class BluetoothInputStream extends InputStream {}
BluetoothOutputStream
Used to read from a Bluetooth socket.
用于从蓝牙socket读取。
BluetoothOutputStream代码位于:
packages/modules/Bluetooth/framework/java/android/bluetooth/BluetoothOutputStream.java
BluetoothOutputStream的定义:
final class BluetoothOutputStream extends OutputStream {
四、BluetoothSocket流程分析
BluetoothSocket connect流程分析
待更新
BluetoothSocket close流程分析
待更新