2012-07-18 2 views
0

BT 장치 연결/연결 해제 용 브로드 캐스트 수신기를 등록하는 서비스를 시작하고 있습니다. 그것은 잘 작동하는 것 같습니다. 그러나 문제가 발생했습니다. BT 헤드셋이 이미 연결되어 있는지 감지해야 현재 상태를 알 수 있습니다. 나는 아래의 코드를 사용하지만, 그것 일하지 않는 것 같습니다 :서비스를 시작할 때 BT 헤드셋/헤드폰을 감지하는 방법

// ... 
// get BluetoothAdapter 
BluetoothAdapter ba = BluetoothAdapter.getDefaultAdapter(); 
// to tell if BT headset is connected 
boolean isBtHeadsetOn = false; 

// if device supports BT 
if (ba != null) { 
    // get list of paired devices 
    Set<BluetoothDevice> devices = ba.getBondedDevices(); 

    if (devices != null) { 
     // loop through devices 
     for (BluetoothDevice device : devices) { 
      // get device class 
      BluetoothClass bc = device.getBluetoothClass(); 
      if (bc == null) continue; 
      int devClass = bc.getDeviceClass(); 

      // check if device is handsfree, headphones or headset 
      if (devClass == BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE || devClass == BluetoothClass.Device.AUDIO_VIDEO_HEADPHONES || devClass == BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET) { 
       // yes, it is 
       isBtHeadsetOn = true; 
       break; 
      } 
     } 
    } 
} 

// now I got my result in variable isBtHeadsetOn 
// ... 

내가 안드로이드 2.1 이상 코드를 개발하고이 서비스 #에서 onCreate에 위치를(). 도움 주셔서 감사합니다.

+0

android 2.2 이상에서 해결 방법이 있습니다. 그렇지 않으면 운이 없어집니다. –

답변

0

이 코드는 본드 장치 만 제공하지만 연결 상태는 제공하지 않습니다. 이것은 휴대 전화가 기억하는 블루투스 기기를 의미하지만, 연결되거나 연결되지 않을 수 있습니다. 당신이 당신이 실제로 연결 상태를 알 수있는 몇 가지 방법이있는 BluetoothHeadset 개체를 얻을 수있는 핸즈프리 알고있는 경우

<receiver android:name=".receiver.BTReceiver"> 
     <intent-filter> 
      <action android:name="android.bluetooth.adapter.action.STATE_CHANGED" /> 
      <action android:name="android.bluetooth.device.action.ACL_CONNECTED" /> 
      <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" /> 
      <action android:name="android.bluetooth.device.action.BOND_STATE_CHANGED" /> 
     </intent-filter> 

:

는 장치의 상태에 변화를 포착해야 상태를 알 수있는 일반적으로

: http://developer.android.com/reference/android/bluetooth/BluetoothHeadset.html

관련 문제