2013-09-04 3 views
0

작업을 시작할 때 블루투스 페어링 된 장치가 있는지 확인한 다음 결과에 따라 작업을 수행해야합니다. 이를 위해, 나는 다음을 수행하십시오 onResume에서동작이 블루투스 페어링 된 장치가 있는지 알아야합니다.

:

<activity 
     android:name=".Configuration" 
     android:label="@string/config_title" > 
     <intent-filter> 
      <action android:name="android.bluetooth.device.action.ACL_CONNECTED"/> 
      <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED"/> 
     </intent-filter> 
    </activity> 

하지만 여전히 : 매니페스트에

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

     /**Do something if connected*/ 
     if(action.equals("android.bluetooth.device.action.ACL_CONNECTED")) { 
      //Action 
     } 
     /**Do something if disconnected*/ 
     else if (action.equals("android.bluetooth.device.action.ACL_DISCONNECTED")) {    
      Toast.makeText(getApplicationContext(), "No device paired", Toast.LENGTH_SHORT).show(); 
     } 
    } 
}; 

: 다음

protected void onResume() { 
    super.onResume(); 

    /**Filters para comprobar el BroadcastReceiver*/ 
    IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED); 
    IntentFilter filter2 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED); 
    this.registerReceiver(mReceiver, filter1); 
    this.registerReceiver(mReceiver, filter2); 

, 나는 브로드 캐스트 리시버를 사용 뭔가해야하는 일을 잊어 버렸거나 무언가 틀렸어 야합니다.

답변

1

누군가가 관심이 있다면, 나는이 문제를 해결받을 방법이 있습니다 :

이 작업을 수행 conected입니다 모든 장치는, 나는 BluetoothChat 예제 응용 프로그램에 따라 방법을 사용했는지 확인합니다. 이 클래스에서

public class Transmission { 
    .... 

    /** 
    * Return the current connection state. 
    */ 
    public synchronized int getState() { 
     return GlobalVar.mState; 
    } 

, 나는 일부 변수 선언 :

public class GlobalVar { 
    .... 

    public static int mState; 

    // Constants that indicate the current connection state 
    public static final int STATE_NONE = 0;  // we're doing nothing 
    public static final int STATE_LISTEN = 1;  // now listening for incoming connections 
    public static final int STATE_CONNECTING = 2; // now initiating an outgoing connection 
    public static final int STATE_CONNECTED = 3; // now connected to a remote device 

마지막을, 이것은 내가 작업을 수행하고 내가 그것을 초기화의 체크인 할 경우 어떤 conected입니다있을 경우 클래스입니다 기기 :

/**Check that we're actually connected before trying anything*/ 
    if (GlobalVar.mTransmission.getState() == GlobalVar.STATE_CONNECTED) { 
     //If there is a conection, do an action 
    } 
    /**If there is no connection, then shows toast*/ 
    else { 
     Toast.makeText(this, R.string.not_connected, Toast.LENGTH_SHORT).show(); 
관련 문제