2012-04-25 11 views
6

Estoy tratando de entender el trabajo cliente-servidor bluetooth. Donde Android es cliente. PC es servidor.Bluetooth remoto

Encontré code. Esto funcionó con una galaxia de Samsung 10.1 (Android 3.2), pero no funciona con un samsung galaxy s (android 2.3.3) y htc wildfire s (android 2.2).

MY_UUID = UUID.fromString("04c6093b-0000-1000-8000-00805f9b34fb"); 

04-25 18:32:37.023: D/BluetoothCommandService(13665): setState() 1 -> 2 
04-25 18:32:37.033: I/BluetoothCommandService(13665): BEGIN mConnectThread 
04-25 18:32:37.063: E/BluetoothService.cpp(102): stopDiscoveryNative: D-Bus error in StopDiscovery: org.bluez.Error.Failed (Invalid discovery session) 
04-25 18:32:37.103: E/BluetoothCommandService(13665): Unable to start Service Discovery 
04-25 18:32:37.103: E/BluetoothCommandService(13665): java.io.IOException: Unable to start Service Discovery 
04-25 18:32:37.103: E/BluetoothCommandService(13665): at android.bluetooth.BluetoothSocket$SdpHelper.doSdp(BluetoothSocket.java:367) 
04-25 18:32:37.103: E/BluetoothCommandService(13665): at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:201) 
04-25 18:32:37.103: E/BluetoothCommandService(13665): at com.luugiathuy.apps.remotebluetooth.BluetoothCommandService$ConnectThread.run(BluetoothCommandService.java:258) 
04-25 18:32:37.103: D/BluetoothCommandService(13665): setState() 2 -> 1 

Respuesta

6

El dispositivo falla por el descubrimiento de servicios (eso creo).

  1. He leído acerca de los problemas de BT con numerosos dispositivos Samsung y dispositivos HTC, pero aquellos en los que específicamente se utiliza el perfil L2CAP/HID. Soln: se puede usar SPP o RFCOMM si está utilizando L2CAP

  2. Si está utilizando uno de los anteriores mencionados en solución, entonces trate de usar con UUID estándar

SPP 00001101-0000-1000- 8000-00805F9B34FB

RFCOMM 00000003-0000-1000-8000-00805F9B34FB

Editar

Alternativamente, usted puede tratar de usar la reflexión para obtener su conexión de socket algo así como el método de abajo

private static BluetoothSocket createBluetoothSocket(
     int type, int fd, boolean auth, boolean encrypt, String address, int port){ 
    try { 
     Constructor<BluetoothSocket> constructor = BluetoothSocket.class.getDeclaredConstructor(
       int.class, int.class,boolean.class,boolean.class,String.class, int.class); 
     constructor.setAccessible(true); 
     BluetoothSocket clientSocket = (BluetoothSocket) 
      constructor.newInstance(type,fd,auth,encrypt,address,port); 
     return clientSocket; 
    }catch (Exception e) { return null; } 
} 
+0

no funciona, puede tener más opciones? – alezhka

+0

está trabajando un android 2.3, pero no un 2.2 – alezhka

+0

¿Tiene algún error nuevo o problema similar ... y funciona con todos los dispositivos 2.3 que tiene –

2

Prueba esto:

static int sdk = Integer.parseInt(Build.VERSION.SDK); 

private static final UUID MY_UUID = 
    UUID.fromString((sdk<=8||sdk>=11)?"04c6093b-0000-1000-8000-00805f9b34fb":"00001101-0000-1000-8000-00805F9B34FB"); 

Se ha trabajado para mí :)
probado en 2.1 a 3.3 y 4.0.4 versiones de Android.

+0

It ha funcionado para mí, también. ¿¿Pero por qué?? – Hugo

+0

Creo que se debe a que los teléfonos inteligentes más nuevos no usan el mismo protocolo Bluetooth que los anteriores, por lo que su UUID también ha cambiado. – nonozor