Android 8.0实现蓝牙遥控器自动配对

作者:lojotee 时间:2021-08-05 08:24:16 

本文要实现的是在 android 8.0 的平台上,蓝牙遥控器与TV自动配对,具体就是在TV端打开配对界面,TV端开始搜索远程蓝牙设备,按下遥控器按键让蓝牙遥控器进入对码模式,此时蓝牙遥控器就能作为一个远程蓝牙设备被发现,TV端扫描到这个远程蓝牙设备(蓝牙遥控器),就会自动进行配对连接。

话不多说,直接上代码分析。

public class RcConnectActivity extends Activity {

        private static final String TAG = "RcConnectActivity";
        private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        private BluetoothDevice mBluetoothDevice;
        private BluetoothReceiver mBluetoothReceiver = null;
        private boolean isConnected = false;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            Log.d(TAG, "onCreate");
            super.onCreate(savedInstanceState);
            setContentView(R.layout.rc_connect);
            registerReceiver();
            if (mBluetoothAdapter != null) {
                if (mBluetoothAdapter.isEnabled()) {
                    mBluetoothAdapter.startDiscovery();
                    Log.d(TAG, "mBluetoothAdapter.startDiscovery");
                } else {
                    mBluetoothAdapter.enable();
                    Log.d(TAG, "mBluetoothAdapter.enable");
                }
            } else {
                Toast.makeText(this, R.string.bluetooth_tip, Toast.LENGTH_SHORT).show();
            }
        }

首先我们要注册一个广播 * ,用来接收蓝牙扫描搜索配对过程中一些蓝牙相关的广播,以便进行相对应的操作。

public void registerReceiver() {
        Log.d(TAG, "registerReceiver");
        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothDevice.ACTION_FOUND);
        filter.addAction(BluetoothInputDevice.ACTION_CONNECTION_STATE_CHANGED);
        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        mBluetoothReceiver = new BluetoothReceiver();
        registerReceiver(mBluetoothReceiver, filter);
    }

BluetoothDevice.ACTION_FOUND 也就是 android.bluetooth.device.action.FOUND,当发现远程蓝牙设备的时候,系统就会发出这条广播。接收这条广播需要以下权限。

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

BluetoothInputDevice.ACTION_CONNECTION_STATE_CHANGED:当蓝牙连接状态改变时,就会发送此广播。

BluetoothAdapter.ACTION_STATE_CHANGED:也就是 android.bluetooth.adapter.action.STATE_CHANGED 当本地蓝牙适配器的状态改变时,比如打开蓝牙或者关闭蓝牙的时候,就会发送此广播。

BluetoothAdapter.ACTION_DISCOVERY_FINISHED:当本地蓝牙适配器完成设备扫描搜索过程的时候,就会发送此广播。

注册完广播接着就是通过 BluetoothAdapter.getDefaultAdapter() 来获取本地蓝牙适配器,如果硬件不支持蓝牙的话,那么返回值为null。如果能获取到,证明TV端是有可用的蓝牙模块,接着通过 isEnabled() 这个方法来判断TV端的蓝牙模块是否已经打开并且可以使用,相当于 getBluetoothState() == STATE_ON 。如果已经打开蓝牙,那么就可以通过 startDiscovery() 进行扫描蓝牙设备,否则就通过 enable() 来打开蓝牙。
startDiscovery() 需要<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />这个权限。

private class BluetoothReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            
            String action = intent.getAction(); 
            Log.d(TAG, "action = " + action);

            if(BluetoothDevice.ACTION_FOUND.equals(action)){ 
                mBluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                Log.d(TAG, "find device : " + "[ "+ mBluetoothDevice.getName() +" ]" + ":" + mBluetoothDevice.getAddress());
                
                if (mBluetoothDevice.getName() == null || !mBluetoothDevice.getName().equals("RCSP")) {
                    return;
                } else 
                {
                    if (mBluetoothDevice.getBondState() == BluetoothDevice.BOND_NONE) {
                        Log.d(TAG, "attemp to bond: " + "[ " + mBluetoothDevice.getName() + " ]");
                        try {
                            mBluetoothDevice.createBond();
                            isConnected = true;
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } 
                        
                }
            } else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
                int status = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
                if (BluetoothAdapter.STATE_ON == status) {
                    mBluetoothAdapter.startDiscovery();
                    Log.d(TAG, "mBluetoothAdapter.startDiscovery---STATE_ON");
                }
            } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                if (!isConnected) {
                    mBluetoothAdapter.startDiscovery();
                    Log.d(TAG, "mBluetoothAdapter.startDiscovery---ACTION_DISCOVERY_FINISHED");
                }
            } else if (BluetoothInputDevice.ACTION_CONNECTION_STATE_CHANGED.equals(action)) {
                int newState = intent.getExtras().getInt(BluetoothProfile.EXTRA_STATE);
                switch (newState) {
                    case BluetoothProfile.STATE_CONNECTING:
                        Log.d(TAG, "CONNECTING");
                        Toast.makeText(context, R.string.bluetooth_connecting, Toast.LENGTH_SHORT).show();
                        break;
                    case BluetoothProfile.STATE_CONNECTED:
                        Log.d(TAG, "CONNECTED");
                        Toast.makeText(context, R.string.bluetooth_connected, Toast.LENGTH_SHORT).show();
                        RcConnectActivity.this.finish();
                        break;
                }
            }    
        }
    }

流程分析:

1、如果TV端的蓝牙模块已经打开,那么就执行 startDiscovery(),否则通过 enable() 打开蓝牙,此时会接收到 BluetoothAdapter.ACTION_STATE_CHANGED 这条广播。蓝牙有四种状态,分别是STATE_OFF、STATE_TURNING_ON、STATE_ON、STATE_TURNING_OFF。当蓝牙状态为STATE_ON,表示蓝牙已经打开并且已经准备就绪,此时才可以进行startDiscovery(),否则 startDiscovery() 会返回false,无法扫描搜索远程蓝牙设备。

2、扫描搜索到远程设备之后,判断是不是目标设备,目标设备蓝牙遥控器的名字为 RCSP 。如果 getName() 获取到的名字为null,或者不是 RCSP,直接 return,不进行任何操作。android 8.0 要对 getName() 为 null 进行处理,不然程序会运行出错。如果搜索到目标设备,通过 createBond() 方法,实现自动配对。

3、startDiscovery() 会进行大约12秒的扫描搜索,有可能此时我们的目标设备还没有进入对码模式,还不能被TV端发现,从而也无法自动配对。当扫描搜索完成之后,会发送 BluetoothAdapter.ACTION_DISCOVERY_FINISHED 这条广播,此时我们在判断目标设备是否已经配对连接,如果没有,再次调用 startDiscovery() 进行扫描搜索。

4、当目标设备在进行自动配对的时候,我们通过接收BluetoothInputDevice.ACTION_CONNECTION_STATE_CHANGED 这条广播,来判断目标设备的状态,并用 Toast 提示配对是否成功。

来源:https://blog.csdn.net/lojotee/article/details/103693201

标签:Android,蓝牙,遥控器
0
投稿

猜你喜欢

  • 浅谈用java实现事件驱动机制

    2022-07-12 18:06:03
  • 关于idea中ssm框架的编码问题分析

    2023-06-03 04:40:21
  • java中将一个List等分成n个list的工具方法(推荐)

    2022-04-12 09:25:09
  • springboot自定义Starter的具体流程

    2022-01-26 05:08:06
  • 深入解析Java的设计模式编程中的模板方法模式

    2023-11-01 13:31:14
  • Spring Data JPA带条件分页查询实现原理

    2023-07-23 21:32:54
  • View触发机制API实现GestureDetector OverScroller详解

    2023-01-24 00:57:08
  • 微信js-sdk+JAVA实现“分享到朋友圈”和“发送给朋友”功能详解

    2021-10-20 23:13:51
  • JavaSE的类和对象你真的了解吗

    2021-09-17 21:14:59
  • JVM致命错误日志详解(最新推荐)

    2023-08-22 07:42:30
  • Android Studio ADB网络调试汇总

    2023-07-13 22:10:07
  • Java方法重载Overload原理及使用解析

    2021-11-21 14:23:14
  • Java语言实现快速幂取模算法详解

    2022-06-08 13:18:51
  • spring boot使用拦截器修改请求URL域名 换 IP 访问的方法

    2022-08-21 20:15:53
  • Java虚拟机GC日志分析

    2023-02-21 15:04:40
  • ExpandableListView实现二级列表购物车

    2022-07-27 05:54:13
  • C#在Excel表格中插入、编辑和删除批注

    2023-01-12 13:52:20
  • Java反射(Class类,Class对象获取)

    2021-06-27 05:55:24
  • C#6.0新语法示例详解

    2023-11-16 03:43:42
  • 使用java实现http多线程断点下载文件(一)

    2023-11-23 15:45:51
  • asp之家 软件编程 m.aspxhome.com