我花了一些时间研究 Android 与蓝牙设备通信的能力,这些蓝牙设备旨在通过 PC 上的蓝牙 COM 端口进行通信。我一直无法找到明确的答案,所以我想我会在这里问。我想确保这在 Android 上是可能的。
我是蓝牙通信的新手,但到目前为止我所做的研究让我想到了 RFCOMM,它听起来有点像我想要的。不幸的是,我仍然无法确认这是否真的可行。
如有任何帮助/资源,我们将不胜感激。
最佳答案
是的,Android 可以连接到 PC 上的蓝牙 COM 端口。我目前正在开发这样的应用程序。这是一个代码示例(需要在 Manifest.xml 文件中设置蓝牙权限):
<uses-permission android:name="android.permission.BLUETOOTH" />
Java:
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter == null) {
// Device does not support Bluetooth
finish(); //exit
}
if (!adapter.isEnabled()) {
//make sure the device's bluetooth is enabled
Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetooth, REQUEST_ENABLE_BT);
}
final UUID SERIAL_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); //UUID for serial connection
mac = "00:15:83:3D:0A:57"; //my laptop's mac adress
device = adapter.getRemoteDevice(mac); //get remote device by mac, we assume these two devices are already paired
// Get a BluetoothSocket to connect with the given BluetoothDevice
BluetoothSocket socket = null;
OutputStream out = null;
try {
socket = device.createRfcommSocketToServiceRecord(SERIAL_UUID);
} catch (IOException e) {}
try {
socket.connect();
out = socket.getOutputStream();
//now you can use out to send output via out.write
} catch (IOException e) {}
关于安卓蓝牙COM口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6565144/