2016-08-22 3 views
0

현재 연결된 블루투스 기기에 따라 음량을 조정하는 앱을 만들고 있습니다. 나는 음악의 볼륨을 변경하고자 할 때Android 블루투스가 기기 연결시 감지합니다.

나는 문제를 데

블루투스 장치가 연결되어있을 때 내 애플 감지과 볼륨을 변경할 수 있지만, 너무 빨리 여기

간다처럼입니다 코드의 일부이다 : 나는 볼륨을 변경할 때 주위의 3000ms의 지연을 추가하는 경우

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

     if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) { 
      deviceConnected(d); 
     } 
     else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) { 
      deviceDisconnected(d); 
     } 
    } 
}; 

private void deviceConnected(BluetoothDevice bluetoothDevice) { 
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
    boolean showToasts = prefs.getBoolean("show_toasts", false); 
    if (showToasts) { Toast.makeText(this, getResources().getString(R.string.connected_to) + " " + bluetoothDevice.getName(), Toast.LENGTH_SHORT).show(); } 

    mDeviceDAO = new DeviceDAO(this); 
    mDeviceDAO.open(); 

    DeviceOptions device = mDeviceDAO.select(bluetoothDevice.getAddress()); 
    if (device == null) { return; } 

    if(device.getActivated() == 0) { 
     return; 
    } 

    int v = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
    saveLastVolume(v); 

    int volume = device.getVolume(); 

    int flag = 0; 
    if(prefs.getBoolean("show_am_ui", false)) { 
     flag = AudioManager.FLAG_SHOW_UI; 
    } 
    mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 
      volume, 
      flag); 
} 

, 제대로 작동하지만 그것은 깨끗한 해결책이 아니다. 실제로 블루투스 장치가 "완전히"연결되기 전에 연결을위한 축배가 표시됩니다.

같은 일을하는 앱도 있지만 동일한 일을하는 것으로 보입니다. 당신의 축배가 연결된 블루투스 프로파일을 (당신이 "완전히"연결 말했다 정확히) 의미하지 않는다 연결되는 ACL, 즉 나타나면

Here is its githublink

답변

0

그래 자네 말이 맞아, 캡처가 ACL connected 또는 ACL disconnected 무엇, 그것은 단지 두 장치간에 설정된 물리적 연결을 의미하므로 다른 이벤트를 수신 할 수도 있습니다. HFP/A2dp가 연결되어 있으면 연결되어있는 실제 프로필입니다.

그러나 ACL disconnected은 블루투스 연결이 실제로 끊어지는 이정표입니다.

+0

awnser을 이용해 주셔서 감사합니다. 몇 가지 조사를 해봤는데 클래스 "BluetoothA2dp"를 발견했습니다. 저에게 도움이 될 수있는 getConnectionState가 있습니다. 그러나 변화를 감지하기 위해서는 청취자가 필요하지만 주목할만한 것은 없습니다. –

+0

당신은 또한'BluetoothHeadset'의 ACTION_CONNECTION_STATE_CHANGED를 참조 할 수 있습니다. 이것은 또한 Android에서 가장 많이 사용되는 프로필 연결에 대한 참조입니다. –

+0

Awnsers을 주셔서 감사합니다, 나는 결국 BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED (연결) 및 BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED (연결이 끊어지면)를 들음으로써 그것을 할 수있었습니다. –

관련 문제