package com.tydic.idreader;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.provider.Settings;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 作者：like on 2019-04-29 16:16
 * <p>
 * 邮箱：like@tydic.com
 * <p>
 * 描述：蓝牙设备获取
 */
public class BluetoothHelper {

    /**
     * 蓝牙适配器
     */
    public BluetoothAdapter mBluetoothAdapter;

    private OnBluetoothScanListener listener;

    private Context mContext;

    public BluetoothHelper(Context context) {
        this.mContext = context;
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        context.registerReceiver(mReceiver, filter);
        filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        context.registerReceiver(mReceiver, filter);
    }

    /**
     * 判断是否含有蓝牙模块
     *
     * @return
     */
    public Boolean isSupportBluetooth() {
        return mBluetoothAdapter != null;
    }

    /**
     * 蓝牙开关是否打开
     *
     * @return
     */
    public Boolean isEnabled() {
        if (isSupportBluetooth()) {
            return mBluetoothAdapter.isEnabled();
        }
        return false;
    }

    /**
     * 获取已经配对的设备
     *
     * @return
     */
    public List<BluetoothDeviceBean> getBondedDevices() {
        Set<BluetoothDevice> devices = mBluetoothAdapter.getBondedDevices();
        List<BluetoothDeviceBean> list = new ArrayList<>();
        for (BluetoothDevice device : devices) {
            BluetoothDeviceBean bean = new BluetoothDeviceBean();
            bean.setName(device.getName());
            bean.setAddress(device.getAddress());
            list.add(bean);
        }
        return list;
    }


    /**
     * 扫描附近蓝牙设备
     */
    public void scanBluetoothDevices(OnBluetoothScanListener listener) {
        this.listener = listener;
        if (mBluetoothAdapter.isDiscovering()) {
            mBluetoothAdapter.cancelDiscovery();
        }
        mBluetoothAdapter.startDiscovery();
    }

    /**
     * 取消扫描
     */
    public void cancelScan(){
        if (mBluetoothAdapter.isDiscovering()) {
            mBluetoothAdapter.cancelDiscovery();
        }
    }

    /**
     * 销毁事件
     */
    public void onDestory() {
        if (mBluetoothAdapter != null) {
            mBluetoothAdapter.cancelDiscovery();
        }
        mContext.unregisterReceiver(mReceiver);
    }

    /**
     * 设置蓝牙
     */
    public void settingBluetooth(Activity activity) {
        Intent intent = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS);
        activity.startActivity(intent);
    }

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
                    if (device != null && listener != null) {
                        if ((device.getName() != null) && (!device.getName().equals(""))) {
                            BluetoothDeviceBean bean = new BluetoothDeviceBean();
                            bean.setName(device.getName());
                            bean.setAddress(device.getAddress());
                            listener.onResult(bean);
                        }
                    }
                }
            } else {
                //扫描完成
                if (listener != null) {
                    listener.onFinish();
                }
            }
        }
    };

}

