2012-10-22 85 views
11

我有两个配对的蓝牙设备(我的汽车的头部单元的手机音频和一个单独的A2DP蓝牙接收器)。在我的手机上有一个“用于媒体音频”的复选框,我必须手动切换到我的A2DP输出才能连接到我的车的扬声器。我的目标是以编程方式切换。切换A2DP设备(安卓)

我尝试使用AudioManager类与过时的setBluetoothA2dpOn和setBluetoothScoOn,但都没有任何效果。我能够获得蓝牙配对设备的列表,并获得我想要切换的连接的句柄,但我似乎无法完全理解。我也尝试获取默认的蓝牙适配器,然后使用getProfileProxy,但我觉得我在那里吠叫错误的树。

任何人都可以指向正确的方向吗?基本上我想要做的就是检查“用于媒体音频”框。

+1

这是什么电话,设备是目前在nexus线上的设置? – nandeesh

+0

我使用的手机是Galaxy S3 w/AT&T。我无法确定它是否会出现在那里,但我想它会。 – JamesB41

回答

0

首先,你需要设置程序来激活蓝牙在手机上,并选择它应该通过

bluetoothAdapter.disable()/enable() 

(林不知道关于配对配对的设备,但是这必须通过一些配置来完成活动)

那么你应该设置A2DP连接到汽车的音响

请点击此链接,试图找到它的代码,如果我有时间我会尽量帮您找到它,但它的一个开始= ]

hidden & internal api's

3

不久前我有一个类似的问题,试图连接蓝牙设备到android手机。尽管您的设备配置文件有所不同,但我认为解决方案是相同的。

首先,你需要在项目中创建一个名为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; 
} 

}

最后,连接您的A2DP装置,挑一个蓝光从配对设备列表中选择etoothDevice并将其作为参数connect方法发送。一定要选择具有正确配置文件的设备,否则您将会遇到异常情况。

我已经在Android 2.3版手机中测试过这个解决方案,它工作的很好。

对不起任何英文错误。我希望这可以帮助你。

+0

我没有看到在类型IBluetoothA2dp中实现了“存根”的位置,请澄清一下? – digiphd