2010-03-25 18 views
2

Je veux utiliser Android 2.1 pour me connecter à un périphérique Bluetooth externe, qui offre un port SPP pour moi. Dans ce cas, il s'agit d'une unité GPS externe. Lorsque j'essaie de me connecter, je ne peux pas connecter une socket établie en mode "client". Ensuite, si je tente de configurer un socket (étant dans le rôle de serveur), pour recevoir du texte à partir de mon PC, tout fonctionne très bien. L'ordinateur peut se connecter en tant que client au socket sur le téléphone via SPP à l'aide de l'UUID SSP ou d'un UUID aléatoire. Donc, le problème n'est pas que j'utilise le mauvais UUID.SPP Socket createRfcommSocketToServiceRecord ne se connecte pas

Mais l'inverse (par exemple l'appel de la connexion sur la socket client établie) createRfcommSocketToServiceRecord(UUID uuid)) ne fonctionne tout simplement pas. Malheureusement, j'ai n'a pas le temps d'inspecter le problème plus loin. Ce serait grand si quelqu'un pouvait me pointer du bon côté. Téléphone: Samsung Galaxy Spica I-5700 Android 2.1 Dans la partie suivante du fichier journal doit être le problème.

Greets PhilDev

post-scriptum Je serai présent pendant les heures de bureau.

Voici le fichier journal:


03-21 03:10:52.020: DEBUG/BluetoothSocket.cpp(4643): initSocketFromFdNative 
03-21 03:10:52.025: DEBUG/BluetoothSocket(4643): connect 
03-21 03:10:52.025: DEBUG/BluetoothSocket(4643): doSdp 
03-21 03:10:52.050: DEBUG/ADAPTER(2132): create_device(01:00:00:7F:B5:B3) 
03-21 03:10:52.050: DEBUG/ADAPTER(2132): adapter_create_device(01:00:00:7F:B5:B3) 
03-21 03:10:52.055: DEBUG/DEVICE(2132): Creating device [address = 01:00:00:7F:B5:B3] /org/bluez/2132/hci0/dev_01_00_00_7F_B5_B3 [name = ] 
03-21 03:10:52.055: DEBUG/DEVICE(2132): btd_device_ref(0x10c18): ref=1 
03-21 03:10:52.065: INFO/BluetoothEventLoop.cpp(1914): event_filter: Received signal org.bluez.Adapter:DeviceCreated from /org/bluez/2132/hci0 
03-21 03:10:52.065: INFO/BluetoothService.cpp(1914): ... Object Path = /org/bluez/2132/hci0/dev_01_00_00_7F_B5_B3 
03-21 03:10:52.065: INFO/BluetoothService.cpp(1914): ... Pattern = 00001101-0000-1000-8000-00805f9b34fb, strlen = 36 
03-21 03:10:52.070: DEBUG/DEVICE(2132): *************DiscoverServices******** 
03-21 03:10:52.070: INFO/DTUN_HCID(2132): dtun_client_get_remote_svc_channel: starting discovery on (uuid16=0x0011) 
03-21 03:10:52.070: INFO/DTUN_HCID(2132): bdaddr=01:00:00:7F:B5:B3 
03-21 03:10:52.070: INFO/DTUN_CLNT(2132):  Client calling DTUN_METHOD_DM_GET_REMOTE_SERVICE_CHANNEL (id 4) 
03-21 03:10:52.070: INFO/(2106): DTUN_ReceiveCtrlMsg: [DTUN] Received message [BTLIF_DTUN_METHOD_CALL] 4354 
03-21 03:10:52.070: INFO/(2106): handle_method_call: handle_method_call :: received DTUN_METHOD_DM_GET_REMOTE_SERVICE_CHANNEL (id 4), len 134 
03-21 03:10:52.075: ERROR/BTLD(2106): ****************search UUID = 1101*********** 
03-21 03:10:52.075: INFO//system/bin/btld(2103):   btapp_dm_GetRemoteServiceChannel() 
03-21 03:10:52.120: DEBUG/BluetoothService(1914): updateDeviceServiceChannelCache(01:00:00:7F:B5:B3) 
03-21 03:10:52.120: DEBUG/BluetoothEventLoop(1914): ClassValue: null for remote device: 01:00:00:7F:B5:B3 is null 
03-21 03:10:52.120: INFO/BluetoothEventLoop.cpp(1914): event_filter: Received signal org.bluez.Adapter:PropertyChanged from /org/bluez/2132/hci0 
03-21 03:10:52.305: WARN/BTLD(2106): bta_dm_check_av:0 
03-21 03:10:56.395: DEBUG/WifiService(1914): ACTION_BATTERY_CHANGED pluggedType: 2 
03-21 03:10:57.440: WARN/BTLD(2106): SDP - Rcvd conn cnf with error: 0x4 CID 0x43 
03-21 03:10:57.440: INFO/BTL-IFS(2106): send_ctrl_msg: [BTL_IFS CTRL] send BTLIF_DTUN_SIGNAL_EVT (CTRL) 13 pbytes (hdl 10) 
03-21 03:10:57.445: INFO/DTUN_CLNT(2132): dtun-rx signal [DTUN_SIG_DM_RMT_SERVICE_CHANNEL] (id 42) len 15 
03-21 03:10:57.445: INFO/DTUN_HCID(2132): dtun_dm_sig_rmt_service_channel: success=1, service=00000000 
03-21 03:10:57.445: ERROR/DTUN_HCID(2132): discovery unsuccessful! 

package de.phil_dev.android.BT; 

import java.io.BufferedInputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.util.UUID; 

import android.app.Activity; 
import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothClass; 
import android.bluetooth.BluetoothDevice; 
import android.bluetooth.BluetoothServerSocket; 
import android.bluetooth.BluetoothSocket; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.Toast; 

public class ThinBTClient extends Activity { 

    private static final String TAG = "THINBTCLIENT"; 
    private static final boolean D = true; 
    private BluetoothAdapter mBluetoothAdapter = null; 
    private BluetoothSocket btSocket = null; 
    private BufferedInputStream inStream = null; 
    private BluetoothServerSocket myServerSocket; 
    private ConnectThread myConnection; 
    private ServerThread myServer; 
    // Well known SPP UUID (will *probably* map to 
    // RFCOMM channel 1 (default) if not in use); 
    // see comments in onResume(). 
    private static final UUID MY_UUID = UUID 
      .fromString("00001101-0000-1000-8000-00805F9B34FB"); 


    // .fromString("94f39d29-7d6d-437d-973b-fba39e49d4ee"); 

    // ==> hardcode your slaves MAC address here <== 
    // PC 
    // private static String address = "00:09:DD:50:86:A0"; 

    // GPS 
    private static String address = "00:0B:0D:8E:D4:33"; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     if (D) 
      Log.e(TAG, "+++ ON CREATE +++"); 

     mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
     if (mBluetoothAdapter == null) { 
      Toast.makeText(this, "Bluetooth is not available.", 
        Toast.LENGTH_LONG).show(); 
      finish(); 
      return; 
     } 

     if (!mBluetoothAdapter.isEnabled()) { 
      Toast.makeText(this, 
        "Please enable your BT and re-run this program.", 
        Toast.LENGTH_LONG).show(); 
      finish(); 
      return; 
     } 

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

    @Override 
    public void onStart() { 
     super.onStart(); 
     if (D) 
      Log.e(TAG, "++ ON START ++"); 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 

     if (D) { 
      Log.e(TAG, "+ ON RESUME +"); 
      Log.e(TAG, "+ ABOUT TO ATTEMPT CLIENT CONNECT +"); 
     } 

     // Make the phone discoverable 

     // When this returns, it will 'know' about the server, 
     // via it's MAC address. 
     // mBluetoothAdapter.startDiscovery(); 
     BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); 
     Log.e(TAG, device.getName() + " connected"); 


//  myServer = new ServerThread(); 
//  myServer.start(); 

     myConnection = new ConnectThread(device); 
     myConnection.start(); 

    } 

    @Override 
    public void onPause() { 
     super.onPause(); 

     if (D) 
      Log.e(TAG, "- ON PAUSE -"); 

     try { 
      btSocket.close(); 
     } catch (IOException e2) { 
      Log.e(TAG, "ON PAUSE: Unable to close socket.", e2); 
     } 
    } 

    @Override 
    public void onStop() { 
     super.onStop(); 
     if (D) 
      Log.e(TAG, "-- ON STOP --"); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     if (D) 
      Log.e(TAG, "--- ON DESTROY ---"); 
    } 

    private class ServerThread extends Thread { 
     private final BluetoothServerSocket myServSocket; 

     public ServerThread() { 
      BluetoothServerSocket tmp = null; 

      // create listening socket 
      try { 
       tmp = mBluetoothAdapter 
         .listenUsingRfcommWithServiceRecord(
           "myServer", MY_UUID); 
      } catch (IOException e) { 
       Log.e(TAG, "Server establishing failed"); 
      } 

      myServSocket = tmp; 
     } 

     public void run() { 
      Log.e(TAG, "Beginn waiting for connection"); 
      BluetoothSocket connectSocket = null; 

      InputStream inStream = null; 
      byte[] buffer = new byte[1024]; 
      int bytes; 

      while (true) { 
       try { 
        connectSocket = myServSocket.accept(); 
       } catch (IOException e) { 
        Log.e(TAG, "Connection failed"); 
        break; 
       } 

       Log.e(TAG, "ALL THE WAY AROUND"); 
       try { 
        connectSocket = connectSocket.getRemoteDevice() 
          .createRfcommSocketToServiceRecord(MY_UUID); 
        connectSocket.connect(); 
       } catch (IOException e1) { 
        Log.e(TAG, "DIDNT WORK"); 
       } 

       // handle Connection 
       try { 
        inStream = connectSocket.getInputStream(); 
        while (true) { 
         try { 
          bytes = inStream.read(buffer); 
          Log.e(TAG, "Received: " + buffer.toString()); 
         } catch (IOException e3) { 
          Log.e(TAG, "disconnected"); 
          break; 
         } 
        } 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
        break; 
       } 

      } 
     } 

     void cancel() { 

     } 
    } 

    private class ConnectThread extends Thread { 

     private final BluetoothSocket mySocket; 
     private final BluetoothDevice myDevice; 

     public ConnectThread(BluetoothDevice device) { 
      myDevice = device; 
      BluetoothSocket tmp = null; 

      try { 
       tmp = device.createRfcommSocketToServiceRecord(MY_UUID); 
      } catch (IOException e) { 
       Log.e(TAG, "CONNECTION IN THREAD DIDNT WORK"); 
      } 
      mySocket = tmp; 
     } 

     public void run() { 
      Log.e(TAG, "STARTING TO CONNECT THE SOCKET"); 
      setName("My Connection Thread"); 
      InputStream inStream = null; 
      boolean run = false; 
      //mBluetoothAdapter.cancelDiscovery(); 

      try { 
       mySocket.connect(); 
       run = true; 
      } catch (IOException e) { 
       run = false; 
       Log.e(TAG, this.getName() 
         + ": CONN DIDNT WORK, Try closing socket"); 
       try { 
        mySocket.close(); 
       } catch (IOException e1) { 
        Log.e(TAG, this.getName() + ": COULD CLOSE SOCKET", e1); 

        this.destroy(); 
       } 
      } 

      synchronized (ThinBTClient.this) { 
       myConnection = null; 
      } 

      byte[] buffer = new byte[1024]; 
      int bytes; 
      // handle Connection 
      try { 
       inStream = mySocket.getInputStream(); 
       while (run) { 

        try { 
         bytes = inStream.read(buffer); 
         Log.e(TAG, "Received: " + buffer.toString()); 
        } catch (IOException e3) { 
         Log.e(TAG, "disconnected"); 
        } 
       } 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      // starting connected thread (handling there in and output 
     } 

     public void cancel() { 
      try { 
       mySocket.close(); 
      } catch (IOException e) { 
       Log.e(TAG, this.getName() + " SOCKET NOT CLOSED"); 
      } 
     } 

    } 

} 

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="de.phil_dev.android.BT" android:versionCode="1" 
    android:versionName="1.0"> 
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 
    <uses-permission android:name="android.permission.BLUETOOTH" /> 
    <uses-permission android:name="android.permission.INTERNET"/> 

    <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true"> 
     <activity android:name=".ThinBTClient" android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

    </application> 
    <uses-sdk android:minSdkVersion="7" /> 

</manifest> 
+0

Avez-vous déjà trouvé une solution à votre problème? – Jack

Répondre

1

Je trouve exactement ce problème se produit lorsque l'un des appareils a le Bluetooth éteint.

1

J'ai un problème similaire sur HTC Droid.

Mine obtient un peu plus loin si et connect() réussit mais je suis incapable de recevoir autant qu'un seul octet sur le socket InputStream.

Why can't HTC Droid running OTA 2.1 communicate with RFCOMM?

Je pense que nous avons atteint une limite dans le HTC a ainsi mis en œuvre Bluetooth sur leur téléphone. Notez que les fabricants, tels que HTC, doivent écrire des pilotes pour que Android OS puisse communiquer avec leur matériel propriétaire. HTC semble avoir publié 2.0/2.1 sans support de pilote complet.