Android蓝牙COM端口

时间:2011-07-03 19:42:30

标签: android bluetooth

我花了一些时间研究Android与蓝牙设备通信的能力,蓝牙设备旨在通过PC上的蓝牙COM端口进行通信。我一直无法找到明确的答案,所以我想我会在这里问。我想确保Android可以实现这一点。

我是蓝牙通信的新手,但到目前为止我所做的研究让我看到了RFCOMM,这听起来有点像我想要的。不幸的是,我仍然无法确认这是否可能。

非常感谢任何帮助/资源。

1 个答案:

答案 0 :(得分:13)

是的,Android可以连接到PC上的蓝牙COM端口。我目前正在开发这样的应用程序。这是一个代码示例(Ite需要在Manifest.xml文件中设置蓝牙权限):

<uses-permission android:name="android.permission.BLUETOOTH" />

爪哇:

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) {}