如题,手机和蓝牙耳机配对之后,怎么建立连接
解决方案
20
BluetoothA2dpService是底层的Service类,你可以通过BluetoothA2dp类来使用它
android.bluetooth.BluetoothA2dp
首先需要绑定BluetoothA2dpService,以下是部分代码:
private BluetoothAdapter mBTAdapter;
private BluetoothA2dp mBTA2DP;
mBTAdapter = BluetoothAdapter.getDefaultAdapter();
mBTAdapter.getProfileProxy(mContext, mProfileServiceListener, BluetoothProfile.A2DP);
private BluetoothProfile.ServiceListener mProfileServiceListener = new BluetoothProfile.ServiceListener() {
@Override
public void onServiceConnected(int profile, BluetoothProfile proxy) {
mBTA2DP = (BluetoothA2dp)proxy;
break;
}
}
取得mBTA2DP实例后,mBTA2DP.connect(BluetoothDevice device); 即可通过A2DP协议连接外设蓝牙设备
android.bluetooth.BluetoothA2dp
首先需要绑定BluetoothA2dpService,以下是部分代码:
private BluetoothAdapter mBTAdapter;
private BluetoothA2dp mBTA2DP;
mBTAdapter = BluetoothAdapter.getDefaultAdapter();
mBTAdapter.getProfileProxy(mContext, mProfileServiceListener, BluetoothProfile.A2DP);
private BluetoothProfile.ServiceListener mProfileServiceListener = new BluetoothProfile.ServiceListener() {
@Override
public void onServiceConnected(int profile, BluetoothProfile proxy) {
mBTA2DP = (BluetoothA2dp)proxy;
break;
}
}
取得mBTA2DP实例后,mBTA2DP.connect(BluetoothDevice device); 即可通过A2DP协议连接外设蓝牙设备