package com.abbidot.tracker.receiver

import android.bluetooth.BluetoothAdapter
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.abbidot.baselibrary.constant.ConState
import com.abbidot.baselibrary.constant.EventName
import com.abbidot.baselibrary.eventbus.XEventBus
import com.abbidot.baselibrary.util.LogUtil
import com.abbidot.tracker.util.bluetooth.SRBleUtil
import com.clj.fastble.BleManager

/**
 *Created  by .yzq on 2022/5/23/023.
 * @link
 * @description:监听蓝牙开关操作广播
 */
class BluetoothMonitorReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context?, intent: Intent?) {

        intent?.apply {
            when (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0)) {
                BluetoothAdapter.STATE_TURNING_ON -> {
                    LogUtil.e("蓝牙正在打开")
                }

                BluetoothAdapter.STATE_ON -> {
                    LogUtil.e("蓝牙已经打开")
                    XEventBus.post(EventName.BluetoothSwitch, ConState.BLUETOOTH_ON)
                }

                BluetoothAdapter.STATE_TURNING_OFF -> {
                    LogUtil.e("蓝牙正在关闭")
                    SRBleUtil.instance.cancelBleScan("关闭蓝牙,取消扫描")
                    BleManager.getInstance().disconnectAllDevice()
                }

                BluetoothAdapter.STATE_OFF -> {
                    LogUtil.e("蓝牙已经关闭")
                    XEventBus.post(EventName.BluetoothSwitch, ConState.BLUETOOTH_OFF)
                }
            }
        }
    }


}