package com.oo.seex.sys_state.state;

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

import com.oo.seex.sys_state.Config;
import com.oo.seex.sys_state.Register;
import com.oo.seex.sys_state.state.bean.BluetoothState;

/**
 * Created by gyp on 17/7/4.
 */

public class BluetoothStateReceiver extends BaseReceiver {
    public BluetoothStateReceiver(Context context, Register register) {
        super(context, register);
        addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
            int blueState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
            BluetoothState state = new BluetoothState();
            state.setState(blueState);
            register.callback(Config.FLAG_BLUETOOTH_STATE, state);
            // TODO: 17/7/4
        }
    }

    @Override
    public void regist() {
        super.regist();
        //初始化时拿一下蓝牙开关状态
        BluetoothAdapter blueadapter = BluetoothAdapter.getDefaultAdapter();
        BluetoothState state = new BluetoothState();
        if (blueadapter != null) {
            state.setState(blueadapter.isEnabled() ? BluetoothAdapter.STATE_ON : BluetoothAdapter.STATE_OFF);
            register.callback(Config.FLAG_BLUETOOTH_STATE, state);
        }
    }
}
