2012-03-13 2 views
52

BT를 켜고 끌 때 사용하는 버튼이있는 앱이 있습니다. 거기에 다음 코드가 있습니다.BluetoothAdapter에 대한 상태 변경 감지 중?

public void buttonFlip(View view) { 
    flipBT(); 
    buttonText(view); 
} 

public void buttonText(View view) { 
    Button buttonText = (Button) findViewById(R.id.button1); 
    if (mBluetoothAdapter.isEnabled() || (mBluetoothAdapter.a)) { 
     buttonText.setText(R.string.bluetooth_on); 
    } else { 
     buttonText.setText(R.string.bluetooth_off); 
    } 
} 

private void flipBT() { 
    if (mBluetoothAdapter.isEnabled()) { 
     mBluetoothAdapter.disable();  
    } else { 
     mBluetoothAdapter.enable(); 
    } 
} 

나는 BT 상태를 화나게하고 UI를 업데이트해야하는, - 버튼을 호출 버튼 플립를 호출하고 있습니다. 그러나 내가 가지고있는 문제는 BT가 켜지는 데 몇 초가 걸리는데,이 초 동안에는 BT 상태가 활성화되어 있지 않아 버튼에 2 초 후에 켜지더라도 Bluetooth가 꺼져 있다고 말합니다.

나는 BluetoothAdapter 안드로이드 문서에서 STATE_CONNECTING 상수를 찾았지 만 ... 나는 그것을 사용하는 방법, 초보자라는 것을 모릅니다.

그래서, 내가있어 두 가지 질문 :

  1. 동적으로 BT 상태 (예 : 버튼 또는 이미지로) UI 요소를 묶을 수있는 방법이 있나요 그 때 BT 상태 변경 때문에, 버튼도 바뀔거야?
  2. 그렇지 않으면 버튼을 눌러 정확한 상태를 얻고 싶습니다 (BT가 켜져 있다고하더라도 2 초 후에 만 ​​연결되므로 연결을 원한다고 말하고 싶습니다). 어떻게해야합니까?
+0

당신은 BT가 회전을 시작하면, 다음을 변경하는 부울 상태를 확인 부울을 사용하고 true로 설정할 수 없습니다 단추? –

+0

이 AOSP 코드를보고 블루투스 켜기/끄기 변경 사항을 추적하는 방법에 대한 느낌을 얻으십시오 : http://androidxref.com/5.1.0_r1/xref/frameworks/base/services/core/java/com/ android/server/BluetoothManagerService.java –

답변

149

당신은 BluetoothAdapter의 상태의 변경 사항을 청취하기 위해 BroadcastReceiver을 등록 할 것입니다 :에서 개인 인스턴스 변수로

당신의 Activity (또는 별도의 클래스 파일 ... 둘 중 하나를의) 선호이 당신의 Activity 그에 따라 Button의 텍스트를 변경하는 방법 setButtonText(String text)를 구현하고 있다고 가정

private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     final String action = intent.getAction(); 

     if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) { 
      final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 
               BluetoothAdapter.ERROR); 
      switch (state) { 
      case BluetoothAdapter.STATE_OFF: 
       setButtonText("Bluetooth off"); 
       break; 
      case BluetoothAdapter.STATE_TURNING_OFF: 
       setButtonText("Turning Bluetooth off..."); 
       break; 
      case BluetoothAdapter.STATE_ON: 
       setButtonText("Bluetooth on"); 
       break; 
      case BluetoothAdapter.STATE_TURNING_ON: 
       setButtonText("Turning Bluetooth on..."); 
       break; 
      } 
     } 
    } 
}; 

참고. 당신의 Activity에서

그리고, 등록하고 다음과 같이 BroadcastReceiver 등록을 취소,

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    /* ... */ 

    // Register for broadcasts on BluetoothAdapter state change 
    IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); 
    registerReceiver(mReceiver, filter); 
} 

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

    /* ... */ 

    // Unregister broadcast listeners 
    unregisterReceiver(mReceiver); 
} 
+0

Alex, 거의 작동합니다. 몇 가지. 첫째, BluetoothDevice는 기기를 페어링 할 때 사용하고 BluetoothDevice.ACTION_BOND_STATE_CHANGED 만 있으므로 여기서는 적용되지 않으므로 BluetoothAdapter를 등록해야한다고 생각합니다. 나는 또한 a를 추가했다; broadcastReceiver 코드 (난 단지 같은 문제가있을 것이고, 이것을 읽을 미래의 사람들을 위해서 쓰고있다.) 그러나, 나는 "Java.lang.RuntimeException : ActivityInfo를 인스턴스화할 수 없다."라는 것을 의미한다. 내 활동이 매니페스트에 등록되어 있지 않습니다. 수신기를 등록해야합니까? – raingod

+0

하! 나는 바보 야 :) 내 문제를 알아 냈어. 그래서, 내가 말한 코드 것들은 따로하고, 이것은 아름답게 작동합니다 :) Alex에게 감사드립니다! – raingod

+0

아무 문제 없어! 오타에 대해 유감스럽게 생각한다. 내가 타이프하고 있었던 것에 따라 나는 한 조가 있어야한다라는 것을 알고 있었다 :). –