2010-12-09 2 views
2

J'ai essayé toutes les suggestions dans d'autres commentaires sans résultat et j'espère que quelqu'un peut m'aider. J'ai lutté avec ce problème pendant trois jours maintenant. Je suis certain que mes UUID sont corrects et je sais que l'accès bluetooth est activé dans le manifeste. J'essaie de connecter mon application android à un serveur python fonctionnant sous Fedora. Cela a fonctionné par intermittence et pas du tout pour le moment. Les exceptions Android que je reçois sont généralement le long de ces lignes .. Ceux-ci sont lancés lorsque btSocket.connect(); est exécuté dans le code ci-joint.android.bluetooth.BluetoothSocket ne peut pas se connecter

12-09 05:08:42.331: ERROR/BluetoothService(676): java.io.IOException: Service discovery failed 

ou

12-09 05:27:00.757: ERROR/BluetoothService(729): java.io.IOException: Service discovery failed 

Ceci est ma classe android bluetooth qui est censé prendre soin de tout. Le thread est démarré lorsque la classe d'application principale reçoit un message auquel la socket a été connectée. Ma classe bluetooth est basée sur http://www.anddev.org/viewtopic.php?p=35487#35487.

package spin.halo; 

import java.io.*; 
import java.lang.reflect.InvocationTargetException; 
import java.lang.reflect.Method; 
import java.util.UUID; 

import android.bluetooth.*; 
import android.os.Handler; 
import android.util.Log; 

public class BluetoothService extends Thread{ 

    private static final String TAG = "BluetoothService"; 
    private static final boolean D = true; 
    private BluetoothAdapter mBluetoothAdapter = null; 
    private BluetoothSocket btSocket = null; 
    private OutputStream outStream = null; 
    private InputStream inStream = null; 
    private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 

    private static String address; 

    private Handler appHandler; 

    public BluetoothService(Handler h) { 
     if (D) 
      Log.e(TAG, "+++ ON CREATE +++"); 

     appHandler = h; 

     mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
     if (mBluetoothAdapter == null) { 
      Log.e(TAG, "NO BT ADAPTER!"); 
      return; 
     } 

     if (!mBluetoothAdapter.isEnabled()) { 
      Log.e(TAG, "Bluetooth is not enabled!"); 
      return; 
     } 

     if (D) 
      Log.e(TAG, "+++ DONE IN ON CREATE, GOT LOCAL BT ADAPTER +++"); 
    } 

    public void connectToServer() { 
     connectToServer("60:33:4B:25:0D:37"); 
    } 

    public void connectToServer(String serverMacAddress) { 

     address = serverMacAddress; 
     // 
     if (D) { 
      Log.e(TAG, "+ ABOUT TO ATTEMPT CLIENT CONNECT +"); 
     } 

     BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); 
     Log.v(TAG, "REMOTE DEVICE: " + device.toString()); 

     try { 
      btSocket = device.createRfcommSocketToServiceRecord(MY_UUID); 
      Log.v(TAG, "SOCKET: " + btSocket.toString()); 
     } catch (Exception e) { 
      Log.e(TAG, "ON RESUME: Socket creation failed.", e); 
     } 

     /* Discovery may be going on, e.g., if you're running a 
     'scan for devices' search from your handset's Bluetooth 
     settings, so we call cancelDiscovery(). It doesn't hurt 
     to call it, but it might hurt not to... discovery is a 
     heavyweight process; you don't want it in progress when 
     a connection attempt is made.*/ 
     mBluetoothAdapter.cancelDiscovery(); 

     // Blocking connect, for a simple client nothing else can 
     // happen until a successful connection is made, so we 
     // don't care if it blocks. 

     try { 
      btSocket.connect(); 
      Log.e(TAG, "ON RESUME: BT connection established, data transfer link open."); 
      appHandler.sendMessage(appHandler.obtainMessage(ValidationApp.BT_CONNECTION_MADE, "")); 
     } catch (IOException e) { 
      try { 
       Log.e(TAG, "ON RESUME: Could not connect", e); 
       btSocket.close(); 
      } catch (IOException e2) { 
       Log.e(TAG, "ON RESUME: Unable to close socket during connection failure", e2); 
      } 
     } 

     // Create output stream 
     try { 
      outStream = btSocket.getOutputStream(); 
     } catch (IOException e) { 
      Log.e(TAG, "ON RESUME: Output stream creation failed.", e); 
     } 

     // Create input stream 
     try { 
      inStream = btSocket.getInputStream(); 
     } catch (IOException e) { 
      Log.e(TAG, "Input stream creation failed.", e); 
     } 
    } 

    public void write(String message) { 
     if(message.length() > 0) { 
      byte[] msgBuffer = message.getBytes(); 
      try { 
       outStream.write(msgBuffer); 
      } catch (IOException e) { 
       Log.e(TAG, "ON RESUME: Exception during write.", e); 
      } 
     } 
    } 

    public void run() { 
     LineNumberReader mLineReader = new LineNumberReader(new InputStreamReader(inStream)); 
     while(true) { 
      try { 
       String message = mLineReader.readLine(); 

       if(D) {Log.v(TAG, "Bluetooth says: " + message);} 
       Log.v(TAG, appHandler.obtainMessage(ValidationApp.BT_MESSAGE, message).toString()); 
       appHandler.sendMessage(appHandler.obtainMessage(ValidationApp.BT_MESSAGE, message)); 
      } catch (IOException e) { 
       Log.e(TAG, "startListen: ", e); 
      } 
     } 
    } 
} 

Les parties clés de mon code python sont ci-dessous. Je suis assez confiant sur ce code.

# pybluez library 
import bluetooth 

server_socket = bluetooth.BluetoothSocket(bluetooth.RFCOMM) 
client_sockets = [] 

server_socket.bind(("",bluetooth.PORT_ANY)) 
port = server_socket.getsockname()[1] 
uuid = "00001101-0000-1000-8000-00805F9B34FB" 

print "Listening for devices..." 

# advertise service 
server_socket.listen(1) 
bluetooth.advertise_service(server_socket, "Validation Host", 
    service_id = uuid, 
    service_classes = [ uuid, bluetooth.SERIAL_PORT_CLASS ], 
    profiles = [ bluetooth.SERIAL_PORT_PROFILE ], 
) 

# accept incoming connections 
client_sock, client_info = server_socket.accept() 
client_sockets.append(client_sock) 
print "Accepted Connection from ", client_info 

Merci d'avoir regardé.

Répondre

3

Votre code semble généralement bon, je suppose que vous venez de copier et coller à partir de quelques exemples.

Il y a un bug dans certains téléphones Android tels que le désir HTC, qui causent l'échec de la méthode device.createRfcommSocketToServiceRecord. Je suggère l'approche suivante:

1) Essayer de discuter entre deux ordinateurs Linux en utilisant des scripts pythong fournis à (vous savez où) par que vous vérifiez que votre configuration Linux fonctionne correctement.

2) Essayer d'établir une connexion de l'ordinateur à Android (utiliser android-bluetooth-chat-client-python) Sachez que la démo BluetoothChat par défaut est capable d'accepter la connexion uniquement au premier essai.

3) Essayez de vous connecter depuis un téléphone Android à l'ordinateur linux, mais spécifier manuellement le numéro de canal RFCOMM en utilisant le code suivant

// BUGBUG: Following code is not properly implemented on HTC DESIRE 
// mSocket =     device.createRfcommSocketToServiceRecord(UUID.fromString("6a462dc0-703a-4bf3-a80e-a473a6332c64")); 
// WORKAROUND: Connecting directly to RFCOMM channel 1 
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] { int.class }); 
mSocket = (BluetoothSocket) m.invoke(device, Integer.valueOf(1)); // 1==RFCOMM channel code 

Vous aurez besoin de savoir quel est votre numéro de canal RFCOMM en utilisant

# sdptool browse local 
+0

Vous êtes un saint! –

+0

si cela ne fonctionne pas, essayez "createInsecureRfcommSocket" au lieu de "createRfcommSocket". – vanomart

Questions connexes