生活中我们经常使用蓝牙耳机来接听电话,而隨著科技的進步,現在蓝牙多了一种新的规范:A2DP(Advance Audio Distribution Profile),可以用 44.1 kHz 的 來传输声音,因此現在可以使用蓝牙耳机來享受音樂,接下來就來介紹如何在 Android 中连接 A2DP 的蓝牙设备。
使用 Bluetooth 最主要的一個类就是 BluetoothAdapter,我們会通过这個类來启动、停止蓝牙或做搜索 (Discovery)蓝牙设备的动作,首先我們先取得该类实例:
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
接著查看蓝牙是否有開啟,沒有的話我們直接將蓝牙设备打开:
public boolean enableBluetooth() {
if (!mBluetoothAdapter.isEnabled()) {
if (!mBluetoothAdapter.enable()) {
return false;
}
}
return true;
}
这里注册BluetoothDevice.ACTION_FOUND 以及 BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
intentFilter.addAction
(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
intentFilter.addAction
(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
intentFilter.addAction
(BluetoothAdapter.ACTION_STATE_CHANGED);
// 注册广播接收器,接收并处理搜索结果
registerReceiver(mReceiver, intentFilter);
接著我們就可以通过BluetoothAdapter 來搜索周围的
蓝牙设备
了:
public void startDiscovery() {
if (mBluetoothAdapter != null) {
if (mBluetoothAdapter.isDiscovering()) stopDiscovery();
mBluetoothAdapter.startDiscovery();
}
}
停止搜索周围的蓝牙设备:
public void stopDiscovery() {
if (mBluetoothAdapter != null) {
if (mBluetoothAdapter.isDiscovering())
mBluetoothAdapter.cancelDiscovery();
}
}
成功搜索到蓝牙设备之後,我們可以通过刚刚取得到的 BluetoothDevice 進行配对的动作:
public void pairDevice(BluetoothDevice device) {
stopDiscovery();
try {
Method method = device.getClass().getMethod("createBond", (Class[]) null);
method.invoke(device, (Object[]) null);
} catch (Exception e) {
e.printStackTrace();
}
}
而這個类的取得方式一樣要通过 BluetoothAdapter:
mBluetoothAdapter.getProfileProxy(mContext, new ServiceListener() {
@Override
public void onServiceConnected(int profile, BluetoothProfile proxy) {
if (BluetoothProfile.A2DP == profile) {
mBluetoothA2dp = (BluetoothA2dp) proxy;
}
}
@Override
public void onServiceDisconnected(int profile) {
if (BluetoothProfile.A2DP == profile) {
mBluetoothA2dp = null;
}
}
}, BluetoothProfile.A2DP);
成功取得 BluetoothA2dp 类後,我們就可以通过该类來连接 A2DP 的蓝牙设备了:
public void connectDevice(BluetoothDevice device) {
stopDiscovery();
try {
mBluetoothA2dp.getClass().getMethod("connect", BluetoothDevice.class).invoke(mBluetoothA2dp, device);
} catch (Exception e) {
e.printStackTrace();
}
}
最後我們可以注册BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED 這個 IntentFilter 來接收 A2DP 的连接状态:
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
if (BluetoothDevice.ACTION_FOUND.equals(intent.getAction())) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Do something...
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(intent.getAction())) {
// Do something...
} else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(intent.getAction())) {
final int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR);
final int prevState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, BluetoothDevice.ERROR);
if (state == BluetoothDevice.BOND_BONDED && prevState == BluetoothDevice.BOND_BONDING) {
// paired
} else if (state == BluetoothDevice.BOND_NONE && prevState == BluetoothDevice.BOND_BONDED) {
// unpaired
}
} else if (BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED.equals(intent.getAction())) {
final int state = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, BluetoothDevice.ERROR);
if (state == BluetoothA2dp.STATE_CONNECTED) {
// connected
} else if (state == BluetoothA2dp.STATE_DISCONNECTED) {
// disconnect
}
}
}
};
成功连接後,你就会发现蓝牙设备的声音正通过你的蓝牙耳机输出。