android 手机怎么实现和蓝牙耳机建立连接,连接之后可以听音乐

bdawn 2015-01-28 05:45:34
如题,手机和蓝牙耳机配对之后,怎么建立连接
...全文
1833 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
wu19910108 2017-05-19
  • 打赏
  • 举报
回复
求给份源代码啊,好人一生平安 867343376@qq.com
qq_23569249 2017-05-08
  • 打赏
  • 举报
回复
求给份源代码啊 在线等 849860977@qq.com 楼主最帅
zhanyue1124 2017-04-27
  • 打赏
  • 举报
回复
求分享一份源码,谢谢201288425@qq.com
a521314963 2017-03-30
  • 打赏
  • 举报
回复
可以分享下源码吗?526630699@qq.com
JIT_Q 2017-03-28
  • 打赏
  • 举报
回复
求一份源码,万分感谢。项目逼得很急。。。谢谢大神295495072@qq.com
筱方 2017-01-03
  • 打赏
  • 举报
回复
发份源代码给2212144704@qq.com 谢谢哦
qq_34879386 2016-12-27
  • 打赏
  • 举报
回复
发份源代码给593908871@qq.com谢谢你
jeffoverflow 2016-10-05
  • 打赏
  • 举报
回复
引用 8 楼 qq754655668 的回复:
[quote=引用 7 楼 qq754655668 的回复:] 大神跪求Demo! 754655668@qq.com
没想到真的自己做出来了..... 不过,要是有大神的Demo就更好啦[/quote]可以发我一份你的吗? 1060150073@qq.com
Lance丶紫竹 2016-09-08
  • 打赏
  • 举报
回复
引用 7 楼 qq754655668 的回复:
大神跪求Demo! 754655668@qq.com
没想到真的自己做出来了..... 不过,要是有大神的Demo就更好啦
Lance丶紫竹 2016-09-08
  • 打赏
  • 举报
回复
大神跪求Demo! 754655668@qq.com
小象一只 2015-07-13
  • 打赏
  • 举报
回复
可以分享下源码吗?
bdawn 2015-02-05
  • 打赏
  • 举报
回复
已经解决
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是打电话
bdawn 2015-01-29
  • 打赏
  • 举报
回复
引用 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()这个方法,编译不能通过啊
leonchen1982 2015-01-29
  • 打赏
  • 举报
回复
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协议连接外设蓝牙设备
bdawn 2015-01-28
  • 打赏
  • 举报
回复
引用 1 楼 leonchen1982 的回复:
听音乐的话,需要建立A2DP的连接,使用BluetoothA2dpService类 Class BluetoothA2dpService int connectSink(String address) int disconnectSink(String address)
怎么搞,android api里面也没有这个类啊
leonchen1982 2015-01-28
  • 打赏
  • 举报
回复
听音乐的话,需要建立A2DP的连接,使用BluetoothA2dpService类 Class BluetoothA2dpService int connectSink(String address) int disconnectSink(String address)

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧