package com.common.bluetooth.service.bt

import android.bluetooth.BluetoothAdapter
import android.content.Context
import com.common.bluetooth.callback.BtScanCallBack
import com.common.bluetooth.interfaces.IBluetoothSearch

/**
 * 经典蓝牙搜索器
 *
 * @author Alex Wang
 * @since 2022-2-17
 */
class BtClassicSearcher(
    private val mContext: Context,
    private val mBluetoothAdapter: BluetoothAdapter?
) : IBluetoothSearch {
    override fun startScan(delayTime: Long, callBack: BtScanCallBack?) {
        // 每次启动前，检查一下蓝牙是否已经在扫描之中
        stopScan()
        mBluetoothAdapter?.startDiscovery()
    }

    override fun stopScan() {
        if (mBluetoothAdapter != null && mBluetoothAdapter.isDiscovering) {
            mBluetoothAdapter.cancelDiscovery()
        }
    }

    override fun isScanning(): Boolean {
        return mBluetoothAdapter?.isDiscovering ?: false
    }
}