2012-04-09 7 views
1

블루투스 헤드셋 연결 및 연결 끊김을 인식하고 로그 파일에 기록하는 작은 응용 프로그램을 만들고 싶습니다.블루투스 연결 감지 (안드로이드 SDK ICS)

블루투스 연결을 감지하기 위해 android SDK example을 사용하려고했지만 작동하지 않습니다. 내 응용 프로그램이로드 될 때 OnServiceConnected에 대한 호출이 있는데 그게 전부입니다. OnServiceConnected 또는 OnServiceDisconnected에 대한 다른 호출은 내가 블루투스 헤드셋으로 연결하거나 연결을 끊을 때 (나는 그보다 더 피곤했다).

모든 코드를 OnCreate 함수에 작성했습니다.

아마 안드로이드 4에 문제가 있습니까? 다른 건 없나요?

// 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 
    } 
}; 

답변

3

좋아, 찾았습니다. 여기

가 있습니다 :

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

당신의 대답은 여전히 ​​내가 대답 당신에게 안드로이드 매니페스트에 블루투스 권한을 추가 할 필요가 추가됩니다 맞습니다. '사용 권한 android : name = "android.permission.BLUETOOTH"/>' – alexm

+0

무엇이'action'입니까? – msysmilu

+0

'action'은'String action = intent.getAction();'입니다. – msysmilu