Android: alterne entre os dispositivos SPP Bluetooth
Eu tenho duas impressoras Bluetooth diferentes. Bixolon SPP-R200 e Fujitsu FTP-628WSL110. Eu posso conectar a cada um deles separadamente (usando um Samsung Galaxy SII) imprimir, desconectar e reconectar muito bem. No entanto, se eu desligar o Bixolon e tentar emparelhar com o Fujitsu (anteriormente desemparelhado, o Bixolon ainda está emparelhado), ele falhará ao tentar conectar-se ao soquete criado. Mesmo o contrário.
Aqui está a mensagem de erro:
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)
Aqui está o código, que faz a tentativa de conexão, a linha que falha sob as circunstâncias explicadas é btSocket.connect (); - exceção veja acima:
/** 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...
}
Estou usando omesmo UUID para se conectarambos dispositivos (mas apenas um dispositivo é ligado de cada vez, eles nunca são ligados ao mesmo tempo), o conhecido SPU UUID da API do SDK:
00001101-0000-1000-8000-00805F9B34FB
O que me faz pensar: Será que preciso de um UUID diferente para cada dispositivo? Se sim alguma ideia que?