2014-06-08 46 views
3

我試圖通過SCO發送應用程序的所有音頻。來電後藍牙SCO失敗

我能夠成功地發送音頻,

但是,當有來電來了,我需要斷開形式SCO從而使應用程序的音頻不會與呼叫干擾,

的問題是,當我嘗試在通話後將音頻重新路由到SCO時,它不起作用。

這裏是我使用的音頻發送到SCO代碼:

public class BluetoothManager { 
// For Bluetooth connectvity 
private static String TAG = "BluetoothManager"; 
private static BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
private static AudioManager aM; 

/** 
* Set the audio manager of the device. 
* @param c: The context this method is called from 
*/ 
public static void setAudioManager(Context c) { 
    aM = (android.media.AudioManager)c.getSystemService(Context.AUDIO_SERVICE); 
} 

/** 
* Check if a Bluetooth headset is connected. If so, route audio to Bluetooth SCO. 
*/ 
private static void initializeAudioMode(Context context) { 
    BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() { 
     public void onServiceConnected(int profile, BluetoothProfile proxy) { 
      if (profile == BluetoothProfile.HEADSET) { 
       BluetoothHeadset bh = (BluetoothHeadset) proxy; 
       List<BluetoothDevice> devices = bh.getConnectedDevices(); 
       if (devices.size() > 0) { 
        enableBluetoothSCO(); 
       } 
      } 
      mBluetoothAdapter.closeProfileProxy(profile, proxy); 
     } 
     public void onServiceDisconnected(int profile) {} 
    }; 
    mBluetoothAdapter.getProfileProxy(context, mProfileListener, BluetoothProfile.HEADSET); 
} 

/** 
* Bluetooth Connectvity 
* The following methods are associated with enabling/disabling Bluetooth. 
* In the future we may want to disable other sources of audio. 
*/ 
private static void enableBluetoothSCO() { 
    aM.setMode(AudioManager.MODE_IN_CALL); 
    aM.startBluetoothSco(); 
    aM.setBluetoothScoOn(true); 
} 

/** Right now, this simply enables Bluetooth */ 
@SuppressLint("NewApi") 
public static boolean enableBluetooth(Context c) { 
    // If there is an adapter, enable it if not already enabled 
    if (mBluetoothAdapter != null) { 

     if (!mBluetoothAdapter.isEnabled()) { 
      mBluetoothAdapter.enable(); 
     } 

     setAudioManager(c); 
     initializeAudioMode(c); 
     Log.e(TAG, "SCO: " + aM.isBluetoothScoOn()); 
     Log.e(TAG, "A2DP: " + aM.isSpeakerphoneOn()); 
     return true; 
    } else { 
     Log.v(TAG, "There is no bluetooth adapter"); 
     return false; 
    } 
} 

/** Right now, this simply disables Bluetooth */ 
public static void disableBluetooth() { 
    // If there is an adapter, disabled it if not already disabled 
    if (mBluetoothAdapter != null) { 
     if (mBluetoothAdapter.isEnabled()) { 
      mBluetoothAdapter.disable(); 
     } 
    } else { 
     Log.v(TAG, "There is no bluetooth adapter"); 
    } 
} 

public static void restartBluetooth(){ 
    aM.setMode(AudioManager.MODE_IN_CALL); 

} 
public static void stopBluetooth(){ 
    aM.setMode(AudioManager.MODE_NORMAL); 

} 

} 

當我打電話stopBluetooth()正確的應用程序的聲音不會被髮送到耳機了,

但是,當我打電話restartBluetooth()音頻播放不按預期形成耳機,而是從手機揚聲器播放。

+0

我也想通過藍牙發送聲音到spcific應用程序,但無法到達那裏 這裏是我的查詢http://stackoverflow.com/questions/24134223/send-an-selected-audio-file-via-bluetooth – Namy

回答

1

SCO鏈接在通話結束後是否可能被關閉?如果是這種情況,那麼SCO鏈路也必須隨路由音頻一起提出。

你有沒有打過電話enableBluetoothSCO()內restartBluetooth()

1

你可能需要調用:

aM.startBluetoothSco(); aM.setBluetoothScoOn(true);

您所設定的模式之後。

1

在你的重啓函數裏再次初始化一切,看看它是否有效。像這樣:

public static void restartBluetooth(){ 
    enableBluetooth(getApplicationContext()); 
} 

如果這個工作,那麼這意味着當呼叫結束時,最後一次初始化由於某種原因而丟失。

0

Google Doc say's that

「手機應用程序始終具有對電話的SCO連接的使用優先級。如果這種方法被調用,而手機處於通話將被忽略。同樣,如果接收到來電或在應用程序使用SCO連接時發送,連接將會丟失,並且在通話結束時不會自動返回。「

所以當電話被切斷,你必須重新建立通過調用startBluetoothSco()連接