安卓蓝牙COM口

标签 android bluetooth

我花了一些时间研究 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/

相关文章:

python - 从自定义应用程序读取蓝牙低功耗数据到 ble dongle (csr8510)

bluetooth - ELI5 : Wireless iAP

不支持安卓蓝牙

Swift - 后台蓝牙写入特性

android - 如何对相机图像应用效果?

android - 带有 PARTIAL_WAKE_LOCK 的唤醒锁不工作

android - 非法状态异常 : Can't change tag of fragment was android:switcher now android:switcher

android - 如何知道Android中录音的长度

android - 在 Android 中处理解析推送通知

android - 连接到无法在 Samsung Galaxy Core Prime (Android 5.1.1) 上运行的 gatt 服务器