//
//  KLTBluetoothManager.h
//  KaiLiTe
//
//  Created by boosal on 2016/9/8.
//  Copyright © 2016年 boosal. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>
#import "GlucoseData+CoreDataClass.h"
#import "ReceiveData+CoreDataClass.h"

typedef NS_ENUM (NSInteger, BluetoothManagerStatus) {
    BluetoothManagerStatusScanStart,//0
    BluetoothManagerStatusScanStop,//1
    BluetoothManagerStatusTimeOut,//2
    BluetoothManagerStatusDisconnected,//3
    BluetoothManagerStatusConnected,//4
    BluetoothManagerStatusHandshakeFail,//5
    BluetoothManagerStatusSelfCheckFail,//6
    BluetoothManagerStatusInitialReady,//7
    BluetoothManagerStatusInitialStart,//8
    BluetoothManagerStatusInitialEnd,//9
    BluetoothManagerStatusGetGLU,//10
    BluetoothManagerStatusResendStart,//11
    BluetoothManagerStatusResendEnd,//12
    BluetoothManagerStatusClosed,//13
    BluetoothManagerStatusInitial,//14
    BluetoothManagerStatusTryToConnect,//15
    BluetoothManagerStatusModifyVoltageFailed,//16
    BluetoothManagerStatusSensorLostPower,//17 发射器未绑定进行解绑提醒
    BluetoothManagerStatusUpdateBindWatchSuccess,//18 手机绑定自研手表成功或更新绑定周期成功
    BluetoothManagerStatusBindWatchTimeOut,//19 手机绑定自研手表超时
};

typedef NS_ENUM(NSInteger, RceiveDataType) {
    ReceiveDataBeforeSetUserReference, //初始化期间收到的数据，和初始化完成但是没有输入userBG的数据，直接存起来
    ReceiveDataAfterSetUserReference,  //初始化结束并且设置过userBG之后，收到的数据（计算出对应的血糖数据展示出来）
};

typedef NS_ENUM(NSInteger, ResendDataType) {
    ResendDataBeforeSetUserBG,  // 重发数据，未设置参比
    ResendDataAfterSetUserBG // 重发数据，设置参比之后
};

@protocol KLTBluetoothDelegate <NSObject>

@optional

- (void)onFindDevices:(NSMutableArray *)array connectedDevice:(CBPeripheral *)peripheral;

@end


@interface KLTBluetoothManager : NSObject <CBCentralManagerDelegate, CBPeripheralDelegate> {
    NSTimer *_connectTimer; // 定时器，记录扫描的时间
    NSTimer *_rescanTimer; // 定时器，每隔3分钟扫描一次
    NSTimer *_bgConnectTimer; // 用于后台强连接的定时器
}

@property (nonatomic, strong) CBCentralManager *centralManager;
@property (nonatomic, strong) CBCharacteristic *writeReadCharacteristic; //读写的characteristic
@property (nonatomic, strong) CBCharacteristic *notifyCharacteristic; //通知的characteristic
@property (nonatomic, strong) NSMutableArray *peripherals;
@property (nonatomic, assign) BluetoothManagerStatus status;
@property (nonatomic, strong) CBPeripheral *connectedPeripheral;
@property (nonatomic, strong) CBPeripheral *readyToConnectedPeripheral;

@property (nonatomic, weak) id <KLTBluetoothDelegate> bluetoothDelegate;
@property (nonatomic, assign) RceiveDataType dataType;
@property (nonatomic, assign) ResendDataType resendType;
@property (nonatomic, strong) Device *currentDevice;
@property (nonatomic, assign, readonly) CBManagerState centralManagerState;

+ (KLTBluetoothManager *)sharedManager;
- (void)startScan;
- (void)stopScan;
- (void)closeBleSensor;
- (void)forceEndBleSensor;
- (void)connectPeripheral:(CBPeripheral *)peripheral;

@end
