package com.common.bluetooth.service;

import android.bluetooth.BluetoothAdapter;
import android.content.Context;

import androidx.annotation.Nullable;

import com.common.bluetooth.callback.BtScanCallBack;
import com.common.bluetooth.interfaces.IBluetoothSearch;

/**
 * 经典蓝牙搜索
 *
 * @author wangym
 * @since 2021-10-19
 */
public class BluetoothClassicSearcher implements IBluetoothSearch {
    private static final String TAG = "BluetoothClassicSearcher";
    private final Context mContext;
    private final BluetoothAdapter mBluetoothAdapter;

    public BluetoothClassicSearcher(Context mContext, BluetoothAdapter mBluetoothAdapter) {
        this.mContext = mContext;
        this.mBluetoothAdapter = mBluetoothAdapter;
    }

    @Override
    public void stopScan() {
        if (mBluetoothAdapter.isDiscovering()) {
            mBluetoothAdapter.cancelDiscovery();
        }
    }

    @Override
    public boolean isScanning() {
        return mBluetoothAdapter.isDiscovering();
    }

    @Override
    public void startScan(long delayTime, @Nullable BtScanCallBack callBack) {
        // 每次启动前，检查一下蓝牙是否已经在扫描之中
        stopScan();
        mBluetoothAdapter.startDiscovery();
    }
}
