2012-04-09 5 views
1

Ich möchte eine kleine App bauen, die Bluetooth-Headset Verbindung und Trennung erkennen und in die Protokolldatei schreiben.Bluetooth-Verbindung erkennen (Android SDK ICS)

Ich habe versucht, android SDK example für die Erkennung der Bluetooth-Verbindung zu verwenden, aber es funktioniert nicht. Wenn meine Anwendung geladen wird, bekomme ich einen Anruf bei OnServiceConnected und das war's. keine anderen Aufrufe von OnServiceConnected oder OnServiceDisconnected, wenn ich die Verbindung über Bluetooth-Headsets herstelle oder abtrenne (ich habe mehr als eine müde).

Ich schrieb den gesamten Code in der OnCreate-Funktion.

Vielleicht ist das Problem mit Android 4? Oder ist es etwas anderes?

// Get the default adapter 
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 

// Establish connection to the proxy. 
mBluetoothAdapter.getProfileProxy(this.getApplicationContext(), mProfileListener, BluetoothProfile.HEADSET); 

private BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() { 
    public void onServiceConnected(int profile, BluetoothProfile proxy) { 
     // Write to log - OnServiceConnected 
    } 
    public void onServiceDisconnected(int profile) { 
     // Write to log - OnServiceDisconnected 
    } 
}; 

Antwort

3

Ok, gefunden.

Hier ist es:

getApplicationContext().registerReceiver(receiver, 
        new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED)); 
getApplicationContext().registerReceiver(receiver, 
        new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED)); 

private final BroadcastReceiver mReceiver = new BroadcastReceiver() 
    { 
     @Override 
     public void onReceive(Context context, Intent intent) 
     { 
      if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) 
      { 
       // LOG - Connected 
      } 
      else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) 
      { 
       // LOG - Disconnected 
      } 
     } 
    } 
+1

Ihre Antwort richtig ist nach wie vor, das ich Sie Ihre Antwort auf die Notwendigkeit fügen Sie die Bluetooth-Berechtigung für das Android-Manifest hinzuzufügen. '' – alexm

+0

was ist 'aktion'? – msysmilu

+0

'action' ist' String action = intent.getAction(); ' – msysmilu