package com.xhly.manageapp.broadcastreceiver;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.widget.Toast;

public class BLEStateReceiver extends BroadcastReceiver {
 
    private static final String BLE_STATE_OFF = "android.bluetooth.BluetoothAdapter.STATE_OFF";
    private static final String BLE_STATE_ON = "android.bluetooth.BluetoothAdapter.STATE_ON";
    private static BLEStateReceiver receiver = new BLEStateReceiver();
 
    /**
     * 注册
     *
     * @param context
     */

    public static void register(Context context) {
      /*  IntentFilter filter = new IntentFilter();
        filter.setPriority(Integer.MAX_VALUE);
        // 监视蓝牙关闭和打开的状态
        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        filter.addAction(BLE_STATE_OFF);
        filter.addAction(BLE_STATE_ON);
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        context.registerReceiver(receiver, filter);*/
    }
 
    /**
     * 注销
     *
     * @param context
     */
    public static void unregister(Context context) {
       // context.unregisterReceiver(receiver);
    }
 
    @Override
    public void onReceive(final Context context, Intent intent) {
        // 检测蓝牙状态的逻辑
        checkBLEState(context, intent);
    }

    /**
     * 检测蓝牙状态
     *
     * @param context
     * @param intent
     */
    private synchronized void checkBLEState(Context context, Intent intent) {
        int BLEState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
        Log.i("执行监听", "--------蓝牙监听" +BLEState);
        switch (BLEState) {
            case BluetoothAdapter.STATE_ON:
                // 蓝牙已经打开
                Toast.makeText(context, "蓝牙打开", Toast.LENGTH_LONG).show();
                break;
            case BluetoothAdapter.STATE_TURNING_OFF:
                // 蓝牙正在关闭
                Toast.makeText(context, "蓝牙关闭" , Toast.LENGTH_LONG).show();
                break;
        }
    }
}