不久前我有一个类似的问题,试图连接蓝牙设备到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版手机中测试过这个解决方案,它工作的很好。
对不起任何英文错误。我希望这可以帮助你。
这是什么电话,设备是目前在nexus线上的设置? – nandeesh
我使用的手机是Galaxy S3 w/AT&T。我无法确定它是否会出现在那里,但我想它会。 – JamesB41