2012-10-22 34 views
11

ペアリングされたBluetoothデバイス(電話用の車のヘッドユニットとA2DP用の別のBluetooth受信機)があります。私の電話機には、「メディアオーディオに使用」というチェックボックスがあり、A2DP出力を車のスピーカーに手動で切り替える必要があります。私の目標は、プログラムでこれを切り替えることです。A2DPデバイスの切り替え(Android)

廃止予定のsetBluetoothA2dpOnとsetBluetoothScoOnの両方のAudioManagerクラスを使用しようとしましたが、いずれも効果がないようです。私は、Bluetoothペアリングされたデバイスのリストを取得し、私はトグルしたい接続へのハンドルを取得することができたが、それはかなり正しいように見えることができませんでした。私はまた、デフォルトのBluetoothアダプタを取得してgetProfileProxyを使用してみましたが、間違ったツリーを吠えているような気がします。

誰でも正しい方向に向けることができますか?基本的には、「メディアオーディオに使用」ボックスをチェックするのと同じです。

+1

これはどの電話ですか、デバイスのネクサスラインに設定されていますか? – nandeesh

+0

私が使用している電話はGalaxy S3 w/AT&Tです。私はそれがそこに存在するかどうかを確信することはできませんが、私は想像していました。 – JamesB41

答えて

0

まずあなたがペアリングがわからないイム(携帯電話のBluetoothを有効にするプログラムを設定し、それが

bluetoothAdapter.disable()/enable() 

経由でペアリングするデバイスを選択する必要がありますが、これは、いくつかの設定を介して行われなければなりませんアクティビティ)

次にあなたが

=私は時間があれば私はあなたが、そのスタートのためにそれを試してみています、そのためのコードを試してみて、見つけるために、このリンクをたどって車のステレオに接続するために、A2DPを設定する必要があります]

hidden & internal api's

3

短い時間前、私はAndroidの携帯電話にBluetoothデバイスを接続しようとして同様の問題がありました。あなたのデバイスのプロファイルは異なっていますが、私は解決策は同じだと思います。

あなたがandroid.bluetoothという名前のプロジェクトでパッケージを作成し、そこに以下のIBluetoothA2dp.aidlを置く必要がある最初:

package android.bluetooth; 

import android.bluetooth.BluetoothDevice; 

/** 
* System private API for Bluetooth A2DP service 
* 
* {@hide} 
*/ 
interface IBluetoothA2dp { 
    boolean connectSink(in BluetoothDevice device); 
    boolean disconnectSink(in BluetoothDevice device); 
    boolean suspendSink(in BluetoothDevice device); 
    boolean resumeSink(in BluetoothDevice device); 
    BluetoothDevice[] getConnectedSinks(); 
    BluetoothDevice[] getNonDisconnectedSinks(); 
    int getSinkState(in BluetoothDevice device); 
    boolean setSinkPriority(in BluetoothDevice device, int priority); 
    int getSinkPriority(in BluetoothDevice device); 

    boolean connectSinkInternal(in BluetoothDevice device); 
    boolean disconnectSinkInternal(in BluetoothDevice device); 
} 

次に、これらの機能にアクセスするには、プロジェクト内の以下のクラスを置く:

最後に
public class BluetoothA2dpConnection { 

private IBluetoothA2dp mService = null; 

public BluetoothA2dpConnection() { 

    try { 
     Class<?> classServiceManager = Class.forName("android.os.ServiceManager"); 
     Method methodGetService = classServiceManager.getMethod("getService", String.class); 
     IBinder binder = (IBinder) methodGetService.invoke(null, "bluetooth_a2dp"); 
     mService = IBluetoothA2dp.Stub.asInterface(binder); 
    } catch (ClassNotFoundException e) { 
     e.printStackTrace(); 
    } catch (SecurityException e) { 
     e.printStackTrace(); 
    } catch (NoSuchMethodException e) { 
     e.printStackTrace(); 
    } catch (IllegalArgumentException e) { 
     e.printStackTrace(); 
    } catch (IllegalAccessException e) { 
     e.printStackTrace(); 
    } catch (InvocationTargetException e) { 
     e.printStackTrace(); 
    } 
} 

public boolean connect(BluetoothDevice device) { 
    if (mService == null || device == null) { 
     return false; 
    } 
    try { 
     mService.connectSink(device); 
    } catch (RemoteException e) { 
     e.printStackTrace(); 
     return false; 
    } 
    return true; 
} 

public boolean disconnect(BluetoothDevice device) { 
    if (mService == null || device == null) { 
     return false; 
    } 
    try { 
     mService.disconnectSink(device); 
    } catch (RemoteException e) { 
     e.printStackTrace(); 
     return false; 
    } 
    return true; 
} 

}

、1つのブルーを選んで、あなたのA2DPデバイスを接続しますetoothDeviceをペアにしたデバイスのリストから取得し、connectメソッドのパラメータとして送信します。正しいプロファイルのデバイスを選択してください。そうでなければ、例外が発生します。

私はアンドロイドバージョン2.3の電話機でこの解決策をテストしましたが、うまくいきました。

ご迷惑をおかけします。私はこれがあなたを助けることを望む。

+0

「スタブ」がIBluetoothA2dpタイプで実装されている場所が表示されません。 – digiphd

関連する問題