我有两个不同的蓝牙打印机。Bixolon SPP-R200 和富士通 FTP-628WSL110。我可以分别连接到它们(使用三星 Galaxy SII)打印、断开连接和重新连接就好了。但是,如果我关闭 Bixolon 并尝试与 Fujitsu 配对(之前未配对,Bixolon 仍然配对),那么在尝试连接到创建的套接字时它会失败。反过来也一样。
这是错误消息:
07-02 13:00:11.040: E/MyApp.BluetoothConnection(9380): Failed to connect to rfcomm socket.
07-02 13:00:11.040: E/MyApp.BluetoothConnection(9380): java.io.IOException: Service discovery failed
07-02 13:00:11.040: E/MyApp.BluetoothConnection(9380): at android.bluetooth.BluetoothSocket$SdpHelper.doSdp(BluetoothSocket.java:406)
07-02 13:00:11.040: E/MyApp.BluetoothConnection(9380): at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:217)
07-02 13:00:11.040: E/MyApp.BluetoothConnection(9380): at MyApp.BluetoothConnection.connect(BluetoothConnection.java:171)
07-02 13:00:11.040: E/MyApp.BluetoothConnection(9380): at MyApp.AbstractBluetoothPrinter.connect(AbstractBluetoothPrinter.java:34)
这是进行连接尝试的代码,在解释的情况下失败的行是 btSocket.connect(); - 例外见上文:
/** Is set in connect() */
private BluetoothSocket btSocket = null;
/** Is set prior to connect() */
private BluetoothSocket btDevice;
public boolean connect(){
try {
btSocket = btDevice.createRfcommSocketToServiceRecord("00001101-0000-1000-8000-00805F9B34FB");
if (btDevice.getName().startsWith("FTP")) {
//Special treatment for the fujitsu printer
SystemClock.sleep(1000);
}
} catch (Throwable e) {
LogCat.e(TAG, "Failed to create rfcomm socket.", e);
return false;
}
try {
// Stop Bluetooth discovery if it's going on
BluetoothHandler.cancelDiscovery();
// This fails under the described circumstances
btSocket.connect();
} catch (Throwable e) {
LogCat.e(TAG, "Failed to connect to rfcomm socket.", e);
return false;
}
// Obtain streams etc...
}
我使用相同的UUID 连接到两个设备(但一次只打开一个设备,它们不会同时打开),SDK API 中众所周知的 SPP UUID:
00001101-0000-1000-8000-00805F9B34FB
这让我想知道:难道我需要为每个设备使用不同的 UUID?如果是的话,有什么想法吗?