// Path: blelibrary/src/main/java/com/github/captain_miao/android/ble/constant/BleScanState.java
// public enum BleScanState {
// 
//     SCAN_TIMEOUT(-2, "SCAN_SUCCESS_TIME_OUT"),
//     BLUETOOTH_OFF(-1, "BLUETOOTH_OFF"),
//     SCAN_SUCCESS(0, "SCAN_SUCCESS"),
//     SCAN_FAILED_ALREADY_STARTED(1, "SCAN_FAILED_ALREADY_STARTED"),
//     SCAN_FAILED_APPLICATION_REGISTRATION_FAILED(2, "SCAN_FAILED_APPLICATION_REGISTRATION_FAILED"),
//     SCAN_FAILED_INTERNAL_ERROR(3, "SCAN_FAILED_INTERNAL_ERROR"),
//     SCAN_FAILED_FEATURE_UNSUPPORTED(4, "SCAN_FAILED_FEATURE_UNSUPPORTED"),
//     SCAN_FAILED_OUT_OF_HARDWARE_RESOURCES(5, "SCAN_FAILED_OUT_OF_HARDWARE_RESOURCES");
// 
// 
//     BleScanState(int code, String message) {
//         this.code = code;
//         this.message = message;
//     }
// 
//     private int code;
//     private String message;
// 
//     public int getCode() {
//         return code;
//     }
// 
// 
//     public String getMessage() {
//         return message;
//     }
// 
//     public static BleScanState newInstance(int code) {
//         switch (code) {
//             case -2:
//                 return BleScanState.SCAN_TIMEOUT;
//             case -1:
//                 return BleScanState.BLUETOOTH_OFF;
// 
//             case 1:
//                 return BleScanState.SCAN_FAILED_ALREADY_STARTED;
//             case 2:
//                 return BleScanState.SCAN_FAILED_APPLICATION_REGISTRATION_FAILED;
//             case 3:
//                 return BleScanState.SCAN_FAILED_INTERNAL_ERROR;
//             case 4:
//                 return BleScanState.SCAN_FAILED_FEATURE_UNSUPPORTED;
//             case 5:
//                 return BleScanState.SCAN_FAILED_OUT_OF_HARDWARE_RESOURCES;
//             default:
//                 return BleScanState.SCAN_SUCCESS;
//         }
//     }
// }
// 
// Path: blelibrary/src/main/java/com/github/captain_miao/android/ble/utils/BleLog.java
// public class BleLog {
// 	private static boolean isPrintLog = true;
// 
// 	private BleLog() {}
// 
// 	public static boolean isPrintLog() {
// 		return isPrintLog;
// 	}
// 
// 	public static void setPrintLog(boolean isPrintLog) {
// 		BleLog.isPrintLog = isPrintLog;
// 	}
// 
// 	public static void v(String tag, String msg) {
// 		if (isPrintLog) {
// 			Log.v(tag, msg);
// 		}
// 	}
// 
// 	public static void d(String tag, String msg) {
// 		if (isPrintLog) {
// 			Log.d(tag, msg);
// 		}
// 	}
// 
// 	public static void i(String tag, String msg) {
// 		if (isPrintLog) {
// 			Log.i(tag, msg);
// 		}
// 	}
// 
// 	public static void w(String tag, String msg) {
// 		if (isPrintLog) {
// 			Log.w(tag, msg);
// 		}
// 	}
// 
// 	public static void e(String tag, String msg) {
// 		if (isPrintLog) {
// 			Log.e(tag, msg);
// 		}
// 	}
// }


import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import com.github.captain_miao.android.ble.constant.BleScanState;
import com.github.captain_miao.android.ble.utils.BleLog;
package com.github.captain_miao.android.ble;



/**
 * @author YanLu
 * @since 15/9/14
 *
 * Android4.3 and 4.4 scan bluetooth
 */

public class JellyBeanBleScanner extends BaseBleScanner {
    private final static String TAG = JellyBeanBleScanner.class.getName();

    public BluetoothAdapter mBluetooth = null;
    private SimpleScanCallback mScanCallback = null;
    public JellyBeanBleScanner(Context context,SimpleScanCallback callback) {
        mScanCallback = callback;
        BluetoothManager bluetoothMgr = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
        mBluetooth = bluetoothMgr.getAdapter();
    }

    private BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() {
        @Override
        public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
            mScanCallback.onBleScan(device, rssi, scanRecord);
        }
    };



    @SuppressWarnings(value={"deprecation"})
    @Override
    public void onStartBleScan(long timeoutMillis) {
        long delay = timeoutMillis == 0 ? defaultTimeout : timeoutMillis;
        if(mBluetooth != null) {
            isScanning = mBluetooth.startLeScan(leScanCallback);
            timeoutHandler.postDelayed(timeoutRunnable, delay);
            BleLog.i(TAG, "mBluetooth.startLeScan() " + isScanning);
        } else {
// Path: blelibrary/src/main/java/com/github/captain_miao/android/ble/constant/BleScanState.java
// public enum BleScanState {
// 
//     SCAN_TIMEOUT(-2, "SCAN_SUCCESS_TIME_OUT"),
//     BLUETOOTH_OFF(-1, "BLUETOOTH_OFF"),
//     SCAN_SUCCESS(0, "SCAN_SUCCESS"),
//     SCAN_FAILED_ALREADY_STARTED(1, "SCAN_FAILED_ALREADY_STARTED"),
//     SCAN_FAILED_APPLICATION_REGISTRATION_FAILED(2, "SCAN_FAILED_APPLICATION_REGISTRATION_FAILED"),
//     SCAN_FAILED_INTERNAL_ERROR(3, "SCAN_FAILED_INTERNAL_ERROR"),
//     SCAN_FAILED_FEATURE_UNSUPPORTED(4, "SCAN_FAILED_FEATURE_UNSUPPORTED"),
//     SCAN_FAILED_OUT_OF_HARDWARE_RESOURCES(5, "SCAN_FAILED_OUT_OF_HARDWARE_RESOURCES");
// 
// 
//     BleScanState(int code, String message) {
//         this.code = code;
//         this.message = message;
//     }
// 
//     private int code;
//     private String message;
// 
//     public int getCode() {
//         return code;
//     }
// 
// 
//     public String getMessage() {
//         return message;
//     }
// 
//     public static BleScanState newInstance(int code) {
//         switch (code) {
//             case -2:
//                 return BleScanState.SCAN_TIMEOUT;
//             case -1:
//                 return BleScanState.BLUETOOTH_OFF;
// 
//             case 1:
//                 return BleScanState.SCAN_FAILED_ALREADY_STARTED;
//             case 2:
//                 return BleScanState.SCAN_FAILED_APPLICATION_REGISTRATION_FAILED;
//             case 3:
//                 return BleScanState.SCAN_FAILED_INTERNAL_ERROR;
//             case 4:
//                 return BleScanState.SCAN_FAILED_FEATURE_UNSUPPORTED;
//             case 5:
//                 return BleScanState.SCAN_FAILED_OUT_OF_HARDWARE_RESOURCES;
//             default:
//                 return BleScanState.SCAN_SUCCESS;
//         }
//     }
// }
// 
// Path: blelibrary/src/main/java/com/github/captain_miao/android/ble/utils/BleLog.java
// public class BleLog {
// 	private static boolean isPrintLog = true;
// 
// 	private BleLog() {}
// 
// 	public static boolean isPrintLog() {
// 		return isPrintLog;
// 	}
// 
// 	public static void setPrintLog(boolean isPrintLog) {
// 		BleLog.isPrintLog = isPrintLog;
// 	}
// 
// 	public static void v(String tag, String msg) {
// 		if (isPrintLog) {
// 			Log.v(tag, msg);
// 		}
// 	}
// 
// 	public static void d(String tag, String msg) {
// 		if (isPrintLog) {
// 			Log.d(tag, msg);
// 		}
// 	}
// 
// 	public static void i(String tag, String msg) {
// 		if (isPrintLog) {
// 			Log.i(tag, msg);
// 		}
// 	}
// 
// 	public static void w(String tag, String msg) {
// 		if (isPrintLog) {
// 			Log.w(tag, msg);
// 		}
// 	}
// 
// 	public static void e(String tag, String msg) {
// 		if (isPrintLog) {
// 			Log.e(tag, msg);
// 		}
// 	}
// }


// Path: blelibrary/src/main/java/com/github/captain_miao/android/ble/JellyBeanBleScanner.java
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import com.github.captain_miao.android.ble.constant.BleScanState;
import com.github.captain_miao.android.ble.utils.BleLog;

package com.github.captain_miao.android.ble;



/**
 * @author YanLu
 * @since 15/9/14
 *
 * Android4.3 and 4.4 scan bluetooth
 */

public class JellyBeanBleScanner extends BaseBleScanner {
    private final static String TAG = JellyBeanBleScanner.class.getName();

    public BluetoothAdapter mBluetooth = null;
    private SimpleScanCallback mScanCallback = null;
    public JellyBeanBleScanner(Context context,SimpleScanCallback callback) {
        mScanCallback = callback;
        BluetoothManager bluetoothMgr = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
        mBluetooth = bluetoothMgr.getAdapter();
    }

    private BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() {
        @Override
        public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
            mScanCallback.onBleScan(device, rssi, scanRecord);
        }
    };



    @SuppressWarnings(value={"deprecation"})
    @Override
    public void onStartBleScan(long timeoutMillis) {
        long delay = timeoutMillis == 0 ? defaultTimeout : timeoutMillis;
        if(mBluetooth != null) {
            isScanning = mBluetooth.startLeScan(leScanCallback);
            timeoutHandler.postDelayed(timeoutRunnable, delay);
            BleLog.i(TAG, "mBluetooth.startLeScan() " + isScanning);
        } else {
            mScanCallback.onBleScanFailed(BleScanState.BLUETOOTH_OFF);//bluetooth is off