蓝牙和WIFI

蓝牙笔记 | A2DP连接 a2dpConnect

2019-12-31  本文已影响0人  力卉编程

A2DP(高级音频传送规格)– 允许传输立体声音频信号。 (相比用于 HSP 和 HFP 的单声道加密,质量要好得多)。

mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothAdapter.getProfileProxy(XApplication.getInstance(), new   profileListener(), BluetoothProfile.A2DP);
//a2dpConnect(device)
//获取A2DP的代理:
   public class profileListener implements BluetoothProfile.ServiceListener {
    @Override
    public void onServiceConnected(int profile, BluetoothProfile proxy) {
        Log.i(TAG, "connect to a2dp server");
        a2dpProfile = proxy;
    }
    @Override
    public void onServiceDisconnected(int profile) {
        Log.i(TAG, "disconnect to a2dp server");
        a2dpProfile = null;
    }
}
//判断A2DP的连接状态:
  public boolean getA2dpState(BluetoothDevice device){
    if(a2dpProfile != null && a2dpProfile.getConnectionState(device) == BluetoothProfile.STATE_CONNECTED){
        return true;
    }
    return false;
}
public void a2dpConnect(BluetoothDevice device){
    if(a2dpProfile != null){
        BluetoothA2dp a2dp = (BluetoothA2dp) a2dpProfile;
        Class clazz = a2dp.getClass();
        Method m2;
        try {
            Log.i(TAG,"use reflect to connect a2dp");
            m2 = clazz.getMethod("connect",BluetoothDevice.class);
            m2.invoke(a2dp, device);
        } catch (Exception e) {
            Log.e(TAG,"error:" + e.toString());
        }
    }
}

完~~
文 | 力卉编程

上一篇下一篇

猜你喜欢

热点阅读