android 手机怎么实现和蓝牙耳机建立连接

移动开发 码拜 9年前 (2015-07-11) 2460次浏览 0个评论

如题,手机和蓝牙耳机配对之后,怎么建立连接

 
听音乐的话,需要建立A2DP的连接,使用BluetoothA2dpService类

Class BluetoothA2dpService

int connectSink(String address)
int disconnectSink(String address)

 
引用 1 楼 leonchen1982 的回复:

听音乐的话,需要建立A2DP的连接,使用BluetoothA2dpService类

Class BluetoothA2dpService

int connectSink(String address)
int disconnectSink(String address)

怎么搞,android api里面也没有这个类啊

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协议连接外设蓝牙设备

 
引用 3 楼 leonchen1982 的回复:

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协议连接外设蓝牙设备

但是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是打电话


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明android 手机怎么实现和蓝牙耳机建立连接
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!