package com.xty.common.receiver

import android.annotation.SuppressLint
import android.bluetooth.BluetoothAdapter
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.xty.common.LogUtils
import com.xty.common.event.BlueToothConnectEvent
import org.greenrobot.eventbus.EventBus

class BluetoothMonitorReceiver : BroadcastReceiver() {

    @SuppressLint("MissingPermission")
    override fun onReceive(p0: Context?, p1: Intent?) {
        LogUtils.d("onReceive action:${p1!!.action}")
        when (p1!!.action) {
            BluetoothAdapter.ACTION_STATE_CHANGED -> {
                var state = p1.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)
                LogUtils.d("bluetooth state:$state")
                when (state) {
                    BluetoothAdapter.STATE_ON->{
                        EventBus.getDefault().post(
                            BlueToothConnectEvent()
                        )
                    }
                    BluetoothAdapter.STATE_OFF -> {
                        EventBus.getDefault().post(
                            BlueToothConnectEvent()
                        )
                    }
                }
            }
        }
    }
}