Android的Bluetooth串口输出其实是通过OutputStream发送的,发送到连接的设备上。下面是一个简单的例子,可以在Android设备上创建一个Bluetooth连接并发送数据:
// 获取BluetoothAdapter BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// 获取已配对的设备
Set
// 如果找到设备,则创建连接并打开OutputStream发送数据 if (device != null) { // 获取BluetoothSocket BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuid);
// 连接BluetoothSocket
socket.connect();
// 获取OutputStream
OutputStream outputStream = socket.getOutputStream();
// 发送数据
outputStream.write("Hello, world!".getBytes());
// 关闭OutputStream和BluetoothSocket
outputStream.close();
socket.close();
} else { // 没有找到设备 }