Android实现蓝牙耳机连接

前言

最近看了下蓝牙耳机连接的问题,查阅了相关资料,再此做一个总结。
本文参考以下链接:
Android实现主动连接蓝牙耳机
再次对作者表示感谢。

今天涉及的内容有:

  1. 流程讲解
  2. 新建广播BluetoothReceiver,用于监听处理蓝牙连接过程中各状态
  3. 在MainActivity中调用
  4. 注意的点
  5. 效果图

下面做以讲解

一. 流程讲解

在实现蓝牙耳机链接的时候,需要做一些前期工作,第一步,判断设备是否支持蓝牙。

1.1 设备是否支持蓝牙
   /**设备是否支持蓝牙**/
    public boolean isSupportBluetooth() {
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBluetoothAdapter != null) {
            return true;
        }
        return false;
    }

若支持蓝牙,则需要判断设备蓝牙是否打开

1.2 设备蓝牙是否开启
    /**蓝牙是否已经启动**/
    public boolean isBluetoothOpen() {
        if (mBluetoothAdapter != null) {
            return mBluetoothAdapter.isEnabled();
        }
        return false;
    }

如果蓝牙没有开启,则需要请求开启蓝牙

1.3 请求开启蓝牙
   /**请求启动蓝牙**/
    public void requestStartBluetooth(int requestCode,Context context) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        ((MainActivity)context).startActivityForResult(enableBtIntent, requestCode);
    }

当然,蓝牙还有一个强制开启的方法:

    /**强制打开蓝牙**/
    public void openBluetooth(){
        if(isSupportBluetooth()){
            mBluetoothAdapter.enable();
        }
    }

蓝牙开启后,接下来是查询已配对过的设备

1.3 获取配对过的设备列表
   /**查询配对设备**/
    public List<BluetoothDevice> checkDevices() {
        List<BluetoothDevice> devices=new ArrayList<>();
        if(mBluetoothAdapter!=null){
            Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
            if (pairedDevices != null && pairedDevices.size() > 0) {
                for (BluetoothDevice device : pairedDevices) {
                    devices.add(device);
                }
            }
        }
        return devices;
    }

若已配对列表中没有你的蓝牙耳机设备,则需要搜索

1.4 搜索新设备
    /**发现新设备**/
    public void findBluetoothDevice() {
        //其实是启动了一个异步线程,该方法将立即返回一个布尔值,指示发现是否已成功启动。
        // 发现过程通常涉及大约12秒的查询扫描,随后是每个找到的设备的页面扫描以检索其蓝牙名称
        if(mBluetoothAdapter!=null && mBluetoothAdapter.isEnabled() && !mBluetoothAdapter.isDiscovering()){
            if (mBluetoothAdapter.startDiscovery()) {
                LogUtil.i("=======已成功启动寻找新设备的异步线程=======");
            } else {
                LogUtil.i("=======启动寻找新设备的异步线程失败=======");
            }
        }
    }

然后便是进行蓝牙耳机的配对,连接。
以上基本的蓝牙方法,我已经封装到BluetoothManager类中。
在蓝牙耳机的搜索,配对,连接等过程中,我们需要新建一个广播,对各个过程做一个监听。

二. 新建广播BluetoothReceiver,用于监听处理蓝牙连接过程中各状态

下面给出BluetoothReceiver中主要代码:

    @Override
    public void onReceive(Context context, Intent intent){
        LogUtil.i("=========蓝牙接收处理广播========"+intent.getAction());
        BluetoothDevice device;
        switch (intent.getAction()) {
            case BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED:
                switch (intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, -1)) {
                    case BluetoothA2dp.STATE_CONNECTING:
                        device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                        LogUtil.i("device: " + device.getName() +" connecting");
                        break;
                    case BluetoothA2dp.STATE_CONNECTED:
                        device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                        LogUtil.i("device: " + device.getName() +" connected");
                        mOnBluetoothListener.deviceConnected(device);
                        break;
                    case BluetoothA2dp.STATE_DISCONNECTING:
                        device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                        LogUtil.i("device: " + device.getName() +" disconnecting");
                        break;
                    case BluetoothA2dp.STATE_DISCONNECTED:
                        device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                        LogUtil.i("device: " + device.getName() +" disconnected");
                        break;
                    default:
                        break;
                }
                break;
            case BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED:
                int state = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, -1);
                switch (state) {
                    case BluetoothA2dp.STATE_PLAYING:
                        LogUtil.i("state: playing.");
                        break;
                    case BluetoothA2dp.STATE_NOT_PLAYING:
                        LogUtil.i("state: not playing");
                        break;
                    default:
                        LogUtil.i("state: unkown");
                        break;
                }
                break;
            case BluetoothDevice.ACTION_FOUND:
                device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                int deviceClassType = device.getBluetoothClass().getDeviceClass();
                //找到指定的蓝牙设备
                if (deviceClassType == BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET
                        || deviceClassType == BluetoothClass.Device.AUDIO_VIDEO_HEADPHONES) {
                    LogUtil.i("Found device:" + device.getName()+"   address:"+device.getAddress());
                    if(StringUtil.isNotEmpty(device.getName())){
                        //添加到设备列表
                        mOnBluetoothListener.deviceFound(device);
                    }
                }else{//找到可用蓝牙
                    if(StringUtil.isNotEmpty(device.getName())){
                        LogUtil.i("=====Found device====11===" + device.getName()+"   address:"+device.getAddress());
                        //添加到设备列表
                        mOnBluetoothListener.deviceFound(device);
                    }
                }
                break;
            case BluetoothDevice.ACTION_BOND_STATE_CHANGED:
                int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.BOND_NONE);
                device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                switch (bondState){
                    case BluetoothDevice.BOND_BONDED:  //配对成功
                        LogUtil.i("Device:"+device.getName()+" bonded.");
                        //取消搜索,连接蓝牙设备
                        mOnBluetoothListener.deviceBonded(device);
                        break;
                    case BluetoothDevice.BOND_BONDING:
                        LogUtil.i("Device:"+device.getName()+" bonding.");
                        break;
                    case BluetoothDevice.BOND_NONE:
                        LogUtil.i("Device:"+device.getName()+" not bonded.");
                        //不知道是蓝牙耳机的关系还是什么原因,经常配对不成功
                        //配对不成功的话,重新尝试配对
                        mOnBluetoothListener.deviceBondNone(device);
                        break;
                    default:
                        break;

                }
                break;
            case BluetoothAdapter.ACTION_STATE_CHANGED:
                state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);
                switch (state) {
                    case BluetoothAdapter.STATE_TURNING_ON:
                        LogUtil.i("BluetoothAdapter is turning on.");
                        break;
                    case BluetoothAdapter.STATE_ON:
                        LogUtil.i("BluetoothAdapter is on.");
//                        //蓝牙已打开,开始搜索并连接service
//                        findBluetoothDevice();
//                        getBluetoothA2DP();

                        mOnBluetoothListener.blootoothStateOn();
                        break;
                    case BluetoothAdapter.STATE_TURNING_OFF:
                        LogUtil.i("BluetoothAdapter is turning off.");
                        break;
                    case BluetoothAdapter.STATE_OFF:
                        LogUtil.i("BluetoothAdapter is off.");
                        break;
                }
                break;
            default:
                break;
        }
    }

三. 在MainActivity中调用

3.1 初始化时注册广播,扫描设备列表

       //注册广播
        registerReceiver();
        //初始化设备列表
        initDeviceList();
        //发现新设备
        findDevices();

其中registerReceiver方法为:

    /**注册广播**/
    private void registerReceiver(){
        mBluetoothReceiver=new BluetoothReceiver();
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        filter.addAction(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED);
        filter.addAction(BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED);
        filter.addAction(BluetoothDevice.ACTION_FOUND);
        filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        mContext.registerReceiver(mBluetoothReceiver, filter);
    }

findDevices方法为:

    /**发现新设备**/
    private void findDevices(){
        if(BluetoothManager.getInstance().isSupportBluetooth()&&BluetoothManager.getInstance().isBluetoothOpen()){
            //强制打开蓝牙
            BluetoothManager.getInstance().openBluetooth();

            List<BluetoothDevice>list=BluetoothManager.getInstance().checkDevices();
            LogUtil.i("====list=====list=="+list.size());

            Iterator<BluetoothDevice> it = list.iterator();
            while (it.hasNext()) {
                BluetoothDevice device = it.next();
                if (device != null&& StringUtil.isEmpty(device.getName())) {
                    it.remove();
                }
            }
            mDevices.addAll(list);
            myAdapter.notifyDataSetChanged();
        }
    }

3.2 点击设备列表去连接蓝牙耳机或者开启蓝牙扫描

        myAdapter.setOnRecyclerItemClickListener(new MyAdapter.OnRecyclerItemClickListener() {
            @Override
            public void onRecyclerClick(View view, int position) {
                BluetoothDevice device= mDevices.get(position);
                if(!BluetoothManager.getInstance().isSupportBluetooth()){
                    ToastUtil.showShortToast(mContext,"本设备不支持蓝牙");
                    return;
                }
                if(!BluetoothManager.getInstance().isBluetoothOpen()){
                    //去启动蓝牙
                    BluetoothManager.getInstance().requestStartBluetooth(REQUEST_ENABLE_BT,mContext);
                }else{
                    LogUtil.i("====开始配对=======");
                    //绑定BluetoothA2DP,获得service
                    BluetoothManager.getInstance().getBluetoothA2DP(mContext);
                    //开始配对
                    BluetoothManager.getInstance().createBond(device);
                }
            }
        });

3.3 关闭资源

退出app的时候需要关闭蓝牙耳机连接

       //注销蓝牙链接
        BluetoothManager.getInstance().disableAdapter();

注销广播

       //注销广播
        if(mBluetoothReceiver!=null){
            mContext.unregisterReceiver(mBluetoothReceiver);
        }

当然,你还可以考虑是否需要关闭蓝牙

//关闭蓝牙
BluetoothManager.getInstance().closeBluetooth();

四. 注意的点

蓝牙耳机的连接需要蓝牙权限,所以你得在你的mainfast.xml中以下权限设置:

    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

五. 效果图

5.gif

ok,今天的内容就讲到这里,谢谢。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 161,873评论 4 370
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,483评论 1 306
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 111,525评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,595评论 0 218
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,018评论 3 295
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,958评论 1 224
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,118评论 2 317
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,873评论 0 208
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,643评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,813评论 2 253
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,293评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,615评论 3 262
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,306评论 3 242
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,170评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,968评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,107评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,894评论 2 278

推荐阅读更多精彩内容