|
听音乐的话,需要建立A2DP的连接,使用BluetoothA2dpService类
Class BluetoothA2dpService int connectSink(String address) |
|
|
怎么搞,android api里面也没有这个类啊 |
|
|
20分 |
BluetoothA2dpService是底层的Service类,你可以通过BluetoothA2dp类来使用它
android.bluetooth.BluetoothA2dp 首先需要绑定BluetoothA2dpService,以下是部分代码: private BluetoothAdapter mBTAdapter; mBTAdapter = BluetoothAdapter.getDefaultAdapter(); private BluetoothProfile.ServiceListener mProfileServiceListener = new BluetoothProfile.ServiceListener() { 取得mBTA2DP实例后,mBTA2DP.connect(BluetoothDevice device); 即可通过A2DP协议连接外设蓝牙设备 |
|
但是BluetoothA2dp没有connect()这个方法,编译不能通过啊 |
|
|
已经解决
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是打电话 |
|