80,349
社区成员
发帖
与我相关
我的任务
分享
ba.getProfileProxy(context, bs, BluetoothProfile.A2DP);
ba.getProfileProxy(context, bs, BluetoothProfile.HEADSET);
BluetoothProfile.ServiceListener bs = new BluetoothProfile.ServiceListener() {
@Override
public void onServiceConnected(int profile, BluetoothProfile proxy) {
Log.i("log", "onServiceConnected");
try {
if (profile == BluetoothProfile.HEADSET) {
bh = (BluetoothHeadset) proxy;
if (bh.getConnectionState(device) != BluetoothProfile.STATE_CONNECTED){
bh.getClass()
.getMethod("connect", BluetoothDevice.class)
.invoke(bh, device);
}
} else if (profile == BluetoothProfile.A2DP) {
a2dp = (BluetoothA2dp) proxy;
if (a2dp.getConnectionState(device) != BluetoothProfile.STATE_CONNECTED){
a2dp.getClass()
.getMethod("connect", BluetoothDevice.class)
.invoke(a2dp, device);
}
}
if (bh != null&&a2dp != null) {
A2dpConnectionThread.stop = false;
new A2dpConnectionThread(context, device, a2dp, bh).start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(int profile) {
}
};
就可以搞定了,a2dp是用于播放音乐,headset是打电话