如何在Oreo +中获得ble的断开连接事件?

时间:2019-02-07 10:01:08

标签: android bluetooth-lowenergy android-bluetooth

我尝试使用广播接收器进行连接并使用Bluetooth适配器ack连接事件,但无法获取断开连接事件。我可以建立连接,断开连接的事件,但是无法获得断开连接的事件。

This is my code please help me with that.

@Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();
        if (action != null) {

            if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
                final int bluetoothState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                        BluetoothAdapter.ERROR);
                switch (bluetoothState) {
                    case BluetoothAdapter.STATE_ON:
                        //Bluethooth is on, now you can perform your tasks
                        Log.e("STAGE ", "State On");
                        break;
                    case BluetoothAdapter.STATE_OFF:
                        Log.e("STAGE ", "State Off");
                        break;
                    case BluetoothAdapter.STATE_CONNECTED:
                        Log.e("STAGE ", "STATE_CONNECTED");
                        break;
                    case BluetoothAdapter.STATE_CONNECTING:
                        Log.e("STAGE ", "STATE_CONNECTING");
                        break;
                    case BluetoothAdapter.STATE_DISCONNECTED:
                        Log.e("STAGE ", "SSTATE_DISCONNECTED");
                        break;
                    case BluetoothAdapter.STATE_DISCONNECTING:
                        Log.e("STAGE ", "STATE_DISCONNECTING");
                        break;
                }
            }
        }
    }

在我的代码中

case BluetoothAdapter.STATE_CONNECTING:
Log.e("STAGE ", "STATE_CONNECTING");
break;

case BluetoothAdapter.STATE_DISCONNECTING:
Log.e("STAGE ", "STATE_DISCONNECTING");
break;

不起作用,没有任何日志。

我使用此代码注册接收者

BluetoothReciever bluetoothReciever = new BluetoothReciever();
        IntentFilter intentFilter = new IntentFilter("android.bluetooth.adapter.action.STATE_CHANGED");
intentFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        registerReceiver(bluetoothReciever, intentFilter);

0 个答案:

没有答案