Android蓝牙遥控器自动配对

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Android蓝牙遥控器自动配对

lojotee   2022-09-28 我要评论

本文要实现的是在 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 提示配对是否成功。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们