java - Android:广播接收器在重新启动应用程序时未收到BluetoothDevice.ACTION_ACL_CONNECTED

标签 java android bluetooth broadcastreceiver android-bluetooth

我希望我的应用程序在重新启动应用程序时自动连接到已连接的蓝牙设备。以下是我正在执行的程序:-

  1. [最初]蓝牙设备处于“开启”状态:然后启动应用程序。
    [行为]--> 蓝牙设备配对并成功连接(接收到 Intent 'ACTION_ACL_CONNECTED')

  2. 蓝牙设备已“开启”:关闭应用程序,然后再次启动应用程序。
    [行为]--> 即使按照蓝牙设置中显示的方式进行连接,广播接收器也不会收到 Intent 'ACTION_ACL_CONNECTED'。

注意:- 关闭应用程序时,它不会断开蓝牙连接。 因此,连接成功后,应用程序会立即进入主屏幕。否则,应用程序将转到一个带有按钮的屏幕,该按钮可将其带到蓝牙设置(下面的代码中存在 onClickListener)

我是android开发新手,所以我真的不知道我哪里出错了。我查找了类似问题的解决方案并应用它们,但没有效果。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
        registerReceiver(mReceiver, filter);
        IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
        this.registerReceiver(mReceiver, filter1);
        m_app = (BtApp) getApplication();
        imagebt = (ImageView) this.findViewById(R.id.imagebt);

        imagebt.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                final Toast tag = Toast.makeText(getApplicationContext(), "Connect to device", Toast.LENGTH_LONG);
                tag.show();

                new CountDownTimer(1000, 1000)
                {

                    public void onTick(long millisUntilFinished) {tag.show();}
                    public void onFinish() {
                        //tag.show();
                    }

                }.start();

                if(mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()){
                    mBluetoothAdapter.startDiscovery();
                }

                Intent intentBluetooth = new Intent();
                intentBluetooth.setAction(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS);
                startActivity(intentBluetooth);

                }
        });

    }

private BroadcastReceiver   mReceiver   = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if ( BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
                m_app.m_main.setupCommPort();
                device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                m_app.m_device = device;
                isconnected = true;
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        if ( m_app.m_main.m_BtService != null && m_app.m_main.m_BtService.getState() != BluetoothRFCommService.STATE_CONNECTED ) {
                            m_app.m_main.m_BtService.connect(device, false);
                        }
                    }
                }, 3500);

            } else if ( BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action) ) {
                isconnected = false;
                m_app.m_main.tabHost.setCurrentTab(0);
            }

        }
    };


@Override
    protected void onStop()
    {
        unregisterReceiver(mReceiver);
        super.onStop();
    }

最佳答案

您不会收到 BluetoothDevice.ACTION_ACL_CONNECTED 事件,因为设备仍处于连接状态。仅当设备状态从断开连接更改为连接时才会触发该事件。

您有 2 个选择。

  1. 您可以将带有 BluetoothDevice.ACTION_ACL_CONNECTEDBluetoothDevice.ACTION_ACL_DISCONNECTED 过滤器的 BroadcastReceiver 放入 Service 并在后台跟踪设备连接状态。在应用程序启动时,您可以要求服务提供设备的当前状态。

  2. 您可以检查某些蓝牙配置文件是否在已连接设备列表中包含您的设备名称。

对于 API 18+,您可以使用 BluetoothManager#getConnectedDevices() 对于低于 18 的 API,您可以使用以下代码段(对于每个蓝牙配置文件)

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() {
    public void onServiceConnected(int profile, BluetoothProfile proxy) {
        for (BluetoothDevice device : proxy.getConnectedDevices()) {
            if (device.getName().contains("DEVICE_NAME")) {
                deviceConnected = true;
             }
        }

        if (!deviceConnected) {
            Toast.makeText(getActivity(), "DEVICE NOT CONNECTED", Toast.LENGTH_SHORT).show();
        }
        mBluetoothAdapter.closeProfileProxy(profile, proxy);

    }

    public void onServiceDisconnected(int profile) {
        // TODO
    }
};

mBluetoothAdapter.getProfileProxy(context, mProfileListener, BluetoothProfile.A2DP);

关于java - Android:广播接收器在重新启动应用程序时未收到BluetoothDevice.ACTION_ACL_CONNECTED,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39363681/

相关文章:

java - 从另一个应用 Intent 打开时 Gmail 应用崩溃

java - react native 错误 : 'Permission Denial: starting Intent"

java - 是否可以转换两个不同的类但具有相同的属性?

java - 如何使用查询对象比较 Spring Data MongoDB 中的 2 个字段

java - 打印所有 JVM 标志

java - 本地无状态 EJB 与远程 EJB

android - 使用 .aar NoClassDefFoundError 但类存在并且被删除

android - 使用 hcitool 获取蓝牙连接的 RSSI,崩溃

android - android 上的 BLE 外设配对引脚

c# - UWP DeviceWatcher 为关闭的设备引发添加事件