2013-10-28 3 views
1

연결이 설정된 직후 장치에서 연결을 끊는 방법은 무엇입니까? 나는 블랙리스트 장치Android에서 Bluetooth를 통한 데이터 교환을 방지하는 방법

public class BluetoothReceiver extends BroadcastReceiver { 
    if (action.equals(BluetoothDevice.ACTION_ACL_CONNECTED)) { 
     BluetoothDevice remoteDevice = (BluetoothDevice) intent.getExtras().get(BluetoothDevice.EXTRA_DEVICE); 
     // I'd like to disconnect from remoteDevice here 
    } 
} 

당신이 mBluetoothGatt 정의 후 AndroidManifest.xml을

<receiver android:name="com.app.receivers.BluetoothReceiver" > 
    <intent-filter> 
     <action android:name="android.bluetooth.device.action.ACL_CONNECTED" /> 
    </intent-filter> 
</receiver> 

답변

1

솔루션을 따르십시오.

ACTION_UUID은 페어링 및 파일 전송 중에 전송되며 EXTRA_DEVICE으로 인해 기기를 가져올 수 있습니다. 내가 바로이 장치에서 연결을 끊을 경우에, 나는 그것은 확실히 분리하지 removeBond

private void removeBond(BluetoothDevice device) { 
    try { 
     Method m = device.getClass().getMethod("removeBond", (Class[]) null); 
     m.invoke(device, (Object[]) null); 
    } catch (Exception e) { 
     Log.e("TAG", "Failed to disconnect from the device"); 
    } 
} 

실행할 수 있습니다.

업데이트 # 1. 때로는 removeBond

호출하지만, 장치의 I/짝 후 파일이 전송 된 도착에 연결된 수신됩니다. 그래서, 유일한 방법은 내가 지금 알고있는 블루투스를 통해 데이터 교환에서 장치를 방지하는 BluetoothAdapter.getDefaultAdapter().disable()

if (action.equals(BluetoothDevice.ACTION_ACL_CONNECTED)) { 
     BluetoothDevice remoteDevice = (BluetoothDevice) intent.getExtras().get(BluetoothDevice.EXTRA_DEVICE); 
    if (isBlackListed(remoteDevice)) { 
     BluetoothAdapter.getDefaultAdapter().disable(); 
    } 
} 

를 호출하여 블루투스 모듈을 해제하는 것은

  • 신뢰성 혜택 단점들

    • 헤드셋 작동이 중지됩니다.
-1
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() { 
     @Override 
     public void onConnectionStateChange(BluetoothGatt gatt, int status, 
       int newState) { 
      // TODO Auto-generated method stub 
      String intentAction; 
      if(newState == BluetoothProfile.STATE_CONNECTED) { 
       intentAction = ACTION_GATT_CONNECTED; 
       mConnectionState = STATE_CONNECTED; 
       broadcastUpdate(intentAction); 
       Log.i(TAG, "Attempting to start service discovery:" + mBluetoothGatt.discoverServices()); 

      } else if(newState == BluetoothProfile.STATE_DISCONNECTED) { 
       intentAction = ACTION_GATT_DISCONNECTED; 
       mConnectionState = STATE_DISCONNECTED; 
       Log.i(TAG, "Disconnected from GATT server"); 
       broadcastUpdate(intentAction); 
      } 
     } 
} 

를 데이터 교환에서 내 장치를 방지해야하고, 당신이 밀어 후 다음 코드를 호출 할 수 있습니다 버튼 또는 기타 작업 :

mBluetoothGatt.disconnect(); 
+0

'BluetoothGattCallback'에는'disconnect() '라는 메소드가 없습니다. http://developer.android.com/reference/android/bluetooth/BluetoothGattCallback.html. 그리고 귀하의 코드는 [Bluetooth Low Energy] (http://developer.android.com/guide/topics/connectivity/bluetooth-le.html)에 관한 것입니다. 제 질문은 태블릿, 전화 등과 같은 일반 장치에서 연결을 끊는 것입니다. –

관련 문제