package com.device.comm.mylibrary.NativeDevice.cgmadi.impl;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.os.Handler;
import android.telephony.TelephonyManager;

import com.device.comm.mylibrary.NativeDevice.cgmadi.Bt4ThCallback;
import com.device.comm.mylibrary.NativeDevice.cgmadi.IBtConnection;
import com.device.comm.mylibrary.NativeDevice.cgmadi.bean.RequestBean;
import com.device.comm.mylibrary.NativeDevice.cgmadi.bean.ResponseBean;
import com.device.comm.mylibrary.NativeDevice.cgmadi.bean.THStatus;
import com.device.comm.mylibrary.NativeDevice.cgmadi.enums.OBJCode;
import com.device.comm.mylibrary.NativeDevice.cgmadi.enums.OPCode;
import com.device.comm.mylibrary.NativeDevice.cgmadi.util.DataFormatUtil;
import com.device.comm.mylibrary.NativeDevice.cgmadi.util.ResponseAnalysis;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import org.json.JSONArray;

public class IBtConnectionImpl<T extends Context>
  implements IBtConnection
{
  TelephonyManager mTelephonyManager;
  private String MEI = "";
  private final String sdk_version = "3.2.2";
  private String time = "2015-12-02 17:28:00";
  private String emitterCode = "00000";
  private String mdata;
  private JSONArray data = new JSONArray();
  public static final String THStringUUID = "0000ffb2-0000-1000-8000-00805f9b34fb";
  Map<String, UUID> servicesUUIDMap = new HashMap();
  private BluetoothManager mBluetoothManager;
  private BluetoothAdapter mBluetoothAdapter;
  public String mBluetoothDeviceAddress;
  private BluetoothDevice blueToothDevice;
  private BluetoothGatt mBluetoothGatt;
  private T context = null;
  private static int requestInt = 0;
  private static int requestCode = 0;
  private BluetoothGattCharacteristic mSCharacteristic;
  private boolean isRequest = false;
  
  public boolean isRequest()
  {
    return this.isRequest;
  }
  
  public void setRequest(boolean isRequest)
  {
    this.isRequest = isRequest;
  }
  
  private IBtConnectionImpl() {
    super();
  }
  
  public static enum connectionStateEnum
  {
    isNull,  isConnecting,  isConnected,  isDisconnecting;
  }
  
  public connectionStateEnum mConnectionState = connectionStateEnum.isNull;
  
  static class SingletonHolder
  {
    static IBtConnectionImpl instance = new IBtConnectionImpl();
    
    SingletonHolder() {}
  }
  
  public static IBtConnectionImpl getInstance()
  {
    return SingletonHolder.instance;
  }
  
  public BluetoothDevice getBlueToothDevice()
  {
    return this.blueToothDevice;
  }
  
  public void setBlueToothDevice(BluetoothDevice blueToothDevice)
  {
    this.blueToothDevice = blueToothDevice;
  }
  
  private void setContext(T context)
  {
    this.context = context;
  }
  
  Handler mHandler = new Handler();
  
  public boolean createConnect(String address)
  {
    try
    {
      close();
      
      Thread.sleep(500L);
    }
    catch (Exception localException1) {}
    try
    {
      this.mBluetoothDeviceAddress = address;
      
      System.out.println("IBtConnectionImpl connect" + address + 
        this.mBluetoothGatt);
      if ((this.mBluetoothAdapter == null) || (address == null))
      {
        System.out.println("IBtConnectionImpl connect BluetoothAdapter not initialized or unspecified address.");
        return false;
      }
      if (!this.mBluetoothAdapter.isEnabled())
      {
        this.mBluetoothAdapter.enable();
        try
        {
          Thread.sleep(5000L);
        }
        catch (Exception localException2) {}
      }
      this.blueToothDevice = this.mBluetoothAdapter.getRemoteDevice(address);
      if (this.blueToothDevice == null)
      {
        System.out.println("IBtConnectionImpl Device not found.  Unable to connect.");
        return false;
      }
      synchronized (this)
      {
        System.out.println("准备连接");
        this.mBluetoothGatt = this.blueToothDevice.connectGatt(this.context, false, 
          this.mGattCallback);
      }
      return false;
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    return false;
  }
  
  public boolean initialize(T context)
  {
    System.out.println("IBtConnectionImpl initialize" + this.mBluetoothManager);
    if (context == null)
    {
      System.out.println("IBtConnectionImpl initialize context is null");
      return false;
    }
    setContext(context);
    if (this.mBluetoothManager == null)
    {
//      this.mBluetoothManager =
//        ((BluetoothManager)context.getSystemService("bluetooth"));
      if (this.mBluetoothManager == null)
      {
        System.out.println("IBtConnectionImpl initialize context is null");
        return false;
      }
    }
    this.mBluetoothAdapter = this.mBluetoothManager.getAdapter();
    if (this.mBluetoothAdapter == null)
    {
      System.out.println("IBtConnectionImpl Unable to obtain a BluetoothAdapter");
      return false;
    }
    return true;
  }
  
  public boolean sendRequest(RequestBean request)
  {
//    if (!isRequest())
//    {
//      ((Bt4ThCallback)this.context).bt4ThFailure(requestInt);
//      return Boolean.FALSE.booleanValue();
//    }

    System.out.println("IBtConnectionImpl 已经连接上蓝牙，环境就绪，发送命令开始........ ");
    requestCode = request.getRequestCode();
    requestInt = request.getRequest();
    byte[] sendData = request.toTrancation();
    System.out.println(DataFormatUtil.bytes2HexString(sendData));
    try
    {
      if (sendTHData(sendData))
      {
        System.out.println("IBtConnectionImpl 命令数据发送成功");
        return Boolean.TRUE.booleanValue();
      }
      ((Bt4ThCallback)this.context).bt4ThFailure(requestInt);
      System.out.println("IBtConnectionImpl 发送数据失败");
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    return Boolean.FALSE.booleanValue();
  }
  
  public void close()
  {
    System.out.println("IBtConnectionImpl close");
    if (this.mBluetoothGatt == null) {
      return;
    }
    this.mBluetoothGatt.close();
    this.mBluetoothGatt = null;
  }
  
  public void disconnect()
  {
    System.out.println("IBtConnectionImpl disconnect");
    if ((this.mBluetoothAdapter == null) || (this.mBluetoothGatt == null))
    {
      System.out.println("IBtConnectionImpl BluetoothAdapter not initialized");
      return;
    }
    this.mBluetoothGatt.disconnect();
  }
  
  protected final BluetoothGattCallback mGattCallback = new BluetoothGattCallback()
  {
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState)
    {
      System.out.println("BluetoothGattCallback----onConnectionStateChange" + 
        newState);
      try
      {
        Thread.sleep(500L);
      }
      catch (InterruptedException e)
      {
        e.printStackTrace();
      }
      if (newState == 2)
      {
        if (IBtConnectionImpl.this.mBluetoothGatt.discoverServices()) {
          System.out.println(" BluetoothGattCallback Attempting to start service discovery:");
        } else {
          System.out.println("BluetoothGattCallback Attempting to start service discovery:not success");
        }
      }
      else if (newState == 0)
      {
        IBtConnectionImpl.this.setRequest(false);
        IBtConnectionImpl.this.mConnectionState = IBtConnectionImpl.connectionStateEnum.isNull;
        System.out
          .println("BluetoothGattCallback Disconnected from GATT server.");
        List<UUID> list = new ArrayList();
        for (Entry<String, UUID> entry : IBtConnectionImpl.this.servicesUUIDMap.entrySet()) {
          list.add((UUID)entry.getValue());
        }
        IBtConnectionImpl.this.close();
        
        ((Bt4ThCallback)IBtConnectionImpl.this.context).disConnectTh((UUID[])list.toArray(new UUID[list.size()]));
      }
    }
    
    public void onServicesDiscovered(BluetoothGatt gatt, int status)
    {
      System.out.println("onServicesDiscovered " + status);
      if (status == 0) {
        IBtConnectionImpl.this.initGattServices(IBtConnectionImpl.this.mBluetoothGatt.getServices());
      } else {
        System.out.println("BluetoothGattCallback onServicesDiscovered received:" + 
          status);
      }
    }
    
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {}
    
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)
    {
      if (status == 0) {
        System.out.println("onCharacteristicRead" + 
          characteristic.getUuid().toString());
      }
    }
    
    public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor characteristic, int status)
    {
      System.out.println("onDescriptorWrite  " + 
        characteristic.getUuid().toString() + " " + status);
    }
    
    public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)
    {
      if (!IBtConnectionImpl.this.checkReturn(characteristic)) {
        return;
      }
      ResponseBean response = IBtConnectionImpl.this.getReturnData(characteristic);
      if (response == null)
      {
        ((Bt4ThCallback)IBtConnectionImpl.this.context).bt4ThFailure(IBtConnectionImpl.requestCode);
      }
      else
      {
        Object rtn = null;
        switch (IBtConnectionImpl.requestCode)
        {
        case 1: 
          System.out.println("Bt4Th-startMonitor:" + response);
          rtn = Boolean.valueOf(ResponseAnalysis.booleanAnalysis(response,
            OPCode.SET, OBJCode.MONITOR));
          System.out.println("Bt4Th-startMonitor:" + rtn);
          ((Bt4ThCallback)IBtConnectionImpl.this.context).bt4ThSuccess(IBtConnectionImpl.requestInt, rtn);
          break;
        case 2: 
          System.out.println("Bt4Th-stopMonitor:" + response);
          rtn = Boolean.valueOf(ResponseAnalysis.booleanAnalysis(response, 
            OPCode.SET, OBJCode.MONITOR));
          System.out.println("Bt4Th-stopMonitor:" + rtn);
          ((Bt4ThCallback)IBtConnectionImpl.this.context).bt4ThSuccess(IBtConnectionImpl.requestInt, rtn);
          break;
        case 12: 
          System.out.println("Bt4Th-getNumberDataFromIndex:" + 
            response);
          rtn = ResponseAnalysis.electricAnalysis(response, 
            OPCode.GET, OBJCode.NDFI);
          System.out.println("Bt4Th-getNumberDataFromIndex:" + 
            rtn.toString());
          break;
        case 13: 
          System.out.println("Bt4Th-getAllDataFromIndex:" + response);
          rtn = ResponseAnalysis.electricAnalysis(response, 
            OPCode.GET, OBJCode.ADFI);
          System.out.println("Bt4Th-getAllDataFromIndex:" + 
            rtn.toString());
          break;
        case 3: 
          System.out.println("Bt4Th-getAllDataCount:" + response);
          Integer[] values = ResponseAnalysis.intAnalysis(response, 
            OPCode.GET, OBJCode.AC);
          if (values != null) {
            rtn = values[0];
          }
          System.out.println("Bt4Th-getAllDataCount:" + rtn);
          ((Bt4ThCallback)IBtConnectionImpl.this.context).bt4ThSuccess(IBtConnectionImpl.requestInt, rtn);
          break;
        case 4: 
          System.out.println("Bt4Th-getAllData:" + response);
          rtn = Boolean.valueOf(ResponseAnalysis.booleanAnalysis(response, 
            OPCode.GET, OBJCode.AD));
          System.out.println("Bt4Th-getAllData:" + rtn.toString());
          break;
        case 5: 
          System.out.println("Bt4Th-getName:" + response);
          rtn = ResponseAnalysis.stringASCIIAnalysis(response, 
            OPCode.GET, OBJCode.NAME);
          System.out.println("Bt4Th-getName:" + rtn);
          ((Bt4ThCallback)IBtConnectionImpl.this.context).bt4ThSuccess(IBtConnectionImpl.requestInt, rtn);
          break;
        case 6: 
          System.out.println("Bt4Th-getMaxNumber:" + response);
          Integer[] v = ResponseAnalysis.intAnalysis(response, 
            OPCode.GET, OBJCode.MAXNUM);
          if (v != null) {
            rtn = v[0];
          }
          System.out.println("Bt4Th-getMaxNumber:" + rtn);
          ((Bt4ThCallback)IBtConnectionImpl.this.context).bt4ThSuccess(IBtConnectionImpl.requestInt, rtn);
          break;
        case 7: 
          System.out.println("Bt4Th-disConnection:" + response);
          rtn = Boolean.valueOf(ResponseAnalysis.booleanAnalysis(response, 
            OPCode.SET, OBJCode.CON_DIS));
          System.out.println("Bt4Th-disConnection:" + rtn);
          ((Bt4ThCallback)IBtConnectionImpl.this.context).bt4ThSuccess(IBtConnectionImpl.requestInt, rtn);
          
          IBtConnectionImpl.this.disconnect();
          break;
        case 8: 
          System.out.println("Bt4Th-getSn:" + response);
          rtn = ResponseAnalysis.stringASCIIAnalysis(response, 
            OPCode.GET, OBJCode.SN);
          System.out.println("Bt4Th-getSn:" + rtn);
          ((Bt4ThCallback)IBtConnectionImpl.this.context).bt4ThSuccess(IBtConnectionImpl.requestInt, rtn);
          break;
        case 9: 
          System.out.println("Bt4Th-getVersion:" + response);
          rtn = ResponseAnalysis.stringASCIIAnalysis(response, 
            OPCode.GET, OBJCode.VERSION);
          System.out.println("Bt4Th-getVersion:" + rtn);
          break;
        case 10: 
          System.out.println("Bt4Th-doAuthorization:" + response);
          rtn = Boolean.valueOf(ResponseAnalysis.booleanAnalysis(response, 
            OPCode.GET, OBJCode.AUTHO));
          System.out.println("Bt4Th-doAuthorization:" + rtn);
          ((Bt4ThCallback)IBtConnectionImpl.this.context).bt4ThSuccess(IBtConnectionImpl.requestInt, rtn);
          break;
        case 11: 
          System.out.println("Bt4Th-getRecord:" + response);
          rtn = ResponseAnalysis.statusAnalysis(response, OPCode.GET, 
            OBJCode.RECORD);
          System.out.println("Bt4Th-getRecord:" + rtn);
          break;
        case 14: 
          System.out.println("Bt4Th-getMonitorStatus:" + response);
          rtn = ResponseAnalysis.intToBooleanAnalysis(response, 
            OPCode.GET, OBJCode.MONITOR);
          ((Bt4ThCallback)IBtConnectionImpl.this.context).bt4ThSuccess(IBtConnectionImpl.requestInt, rtn);
          System.out.println("Bt4Th-getMonitorStatus:" + rtn);
          break;
        }
      }
    }
  };
  
  private void initGattServices(List<BluetoothGattService> gattServices)
  {
//    System.out.println("initGattServices init");
//    if (gattServices == null) {
//      return;
//    }
//    List<BluetoothGattCharacteristic> gattCharacteristics = null;
//    BluetoothGattDescriptor descriptor = null;
//    Iterator localIterator2;
//    for (Iterator localIterator1 = gattServices.iterator(); localIterator1.hasNext(); localIterator2.hasNext())
//    {
//      BluetoothGattService gattService = (BluetoothGattService)localIterator1.next();
//      String tempUuid = gattService.getUuid().toString();
//      UUID servicesUUID = gattService.getUuid();
//      gattCharacteristics = gattService.getCharacteristics();
//      localIterator2 = gattCharacteristics.iterator();
////      continue;
//      BluetoothGattCharacteristic gattCharacteristic;
//              = (BluetoothGattCharacteristic)localIterator2.next();
//      tempUuid = gattCharacteristic.getUuid().toString();
//      if (tempUuid.equals("0000ffb2-0000-1000-8000-00805f9b34fb"))
//      {
//        this.servicesUUIDMap.put(tempUuid, servicesUUID);
//
//        this.mSCharacteristic = gattCharacteristic;
//        this.mBluetoothGatt.setCharacteristicNotification(
//          this.mSCharacteristic, true);
//        descriptor = (BluetoothGattDescriptor)this.mSCharacteristic.getDescriptors().get(0);
//        descriptor
//          .setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
//        this.mBluetoothGatt.writeDescriptor(descriptor);
//        setRequest(true);
//        System.out.println("已经找到，连接成功！");
//        this.mConnectionState = connectionStateEnum.isConnected;
//
//        ((Bt4ThCallback)this.context).bt4ThConnectSucess();
//        return;
//      }
//    }
  }
  
  private boolean sendTHData(byte[] sendData)
  {
    if (this.mConnectionState == connectionStateEnum.isConnected)
    {
      if (this.mSCharacteristic == null)
      {
        System.out.println("sendTHData 11111");
        initGattServices(this.mBluetoothGatt.getServices());
      }
      this.mSCharacteristic.setValue(sendData);
      if (this.mBluetoothGatt.writeCharacteristic(this.mSCharacteristic)) {
        return true;
      }
    }
    return true;
  }
  
  protected static LinkedList<Byte> elList = new LinkedList();
  
  protected boolean checkReturn(BluetoothGattCharacteristic characteristic)
  {
    System.out.println("$$$$$$$$$$$$校验返回结果");
    byte[] temp = characteristic.getValue();
    if (temp != null)
    {
      String str = DataFormatUtil.bytes2HexString(temp);
      Integer[] eleByteNum;
      Double[] elc;
      Double[] ecc;
      if (temp.length == 9)
      {
        System.out.println("*返回值*：" + 
          DataFormatUtil.bytes2HexString(temp));
        
        byte[] eleByteNumber = { temp[6], temp[5] };
        byte[] eleByte = { temp[8], temp[7] };
        byte[] eccByte = { temp[2], temp[1] };
        eleByteNum = 
          DataFormatUtil.byteArray2Int(eleByteNumber);
        elc = DataFormatUtil.byteArray2Elec(eleByte);
        ecc = DataFormatUtil.byteArray2Vcc(eccByte);
        if ((elc != null) && (elc.length > 0) && (eleByteNum != null) && 
          (eleByteNum.length > 0))
        {
          System.out.println("checkReturn： 电流序号" + eleByteNum[0] + 
            " 电流值" + elc[0]);
          ((Bt4ThCallback)this.context).bt4ThElec(eleByteNum[0], elc[0], ecc[0]);
          
          JSONArray datas = new JSONArray();
          JSONArray data = new JSONArray();
          datas.put(data.put(eleByteNum[0]).put(elc[0]));
        }
        return false;
      }
      System.out.println("checkReturn：返回命为：" + str);
      if (isCommandWord(temp))
      {
        System.out.println("command true");
        return Boolean.TRUE.booleanValue();
      }
      if (requestCode == 9)
      {
        System.out.println("Bt4Th-getVersion:" + str);
        String rtn = ResponseAnalysis.stringASCIIAnalysis(temp);
        System.out.println("Bt4Th-getVersion rtn:" + rtn);
        ((Bt4ThCallback)this.context).bt4ThSuccess(requestInt, rtn);
        return Boolean.FALSE.booleanValue();
      }
      if (requestCode == 11)
      {
        System.out.println("Bt4Th-getRecord:" + str);
        THStatus[] rtn = ResponseAnalysis.statusAnalysis(temp);
        System.out.println("Bt4Th-getRecord rtn:" + 
          Arrays.toString(rtn));
        ((Bt4ThCallback)this.context).bt4ThSuccess(requestInt, rtn);
        return Boolean.FALSE.booleanValue();
      }
      if (str.startsWith("FF113355EE"))
      {
        if (requestCode == 10)
        {
          ((Bt4ThCallback)this.context).bt4ThSuccess(requestInt, Boolean.valueOf(true));
          return Boolean.FALSE.booleanValue();
        }
        return Boolean.TRUE.booleanValue();
      }
      Double[] elValues = null;
      byte[] elArray = null;
      if (((requestCode == 4) || 
        (requestCode == 13) || 
        (requestCode == 12)) && 
        (temp != null))
      {
//        elc = (ecc = temp).length;
//        for (eleByteNum = 0; eleByteNum < elc; eleByteNum++)
//        {
//          byte b = ecc[eleByteNum];
//          elList.add(Byte.valueOf(b));
//        }

        int myElc = temp.length;
        for (int m = 0; m < myElc; m++)
        {
          byte b = temp[m];
          elList.add(Byte.valueOf(b));
        }
        if (elList.size() >= 2)
        {
          int length = elList.size() % 2 == 0 ? elList.size() : 
            elList.size() - 1;
          elArray = new byte[length];
          for (int i = 0; i < length; i++) {
            elArray[i] = ((Byte)elList.removeFirst()).byteValue();
          }
        }
      }
      if (elArray != null)
      {
        System.out.println("getData rtn:" + 
          DataFormatUtil.bytes2HexString(elArray));
        switch (requestCode)
        {
        case 4: 
          elValues = DataFormatUtil.byteArray2Elec(
            DataFormatUtil.changeHighAndLow(temp));
          System.out.println("getAllData rtn:" + 
            Arrays.toString(elValues));
          ((Bt4ThCallback)this.context).bt4ThSuccess(requestInt, elValues);
          return Boolean.FALSE.booleanValue();
        case 13: 
          elValues = DataFormatUtil.byteArray2Elec(
            DataFormatUtil.changeHighAndLow(temp));
          System.out.println("getAllDataFromIndex rtn:" + 
            Arrays.toString(elValues));
          ((Bt4ThCallback)this.context).bt4ThSuccess(requestInt, elValues);
          return Boolean.FALSE.booleanValue();
        case 12: 
          elValues = DataFormatUtil.byteArray2Elec(
            DataFormatUtil.changeHighAndLow(temp));
          System.out.println("getNumberDataFromIndex rtn:" + 
            Arrays.toString(elValues));
          ((Bt4ThCallback)this.context).bt4ThSuccess(requestInt, elValues);
          return Boolean.FALSE.booleanValue();
        }
      }
      return false;
    }
    return Boolean.FALSE.booleanValue();
  }
  
  private boolean isCommandWord(byte[] commandByte)
  {
    if ((commandByte == null) || (commandByte.length < 5)) {
      return false;
    }
    if ((isGetOrSet(commandByte[3])) && (isCommand(commandByte[4]))) {
      return true;
    }
    return false;
  }
  
  private boolean isGetOrSet(byte command)
  {
    if (command == OPCode.GET.getValue().byteValue()) {
      return true;
    }
    if (command == OPCode.SET.getValue().byteValue()) {
      return true;
    }
    return false;
  }
  
  private boolean isCommand(byte command)
  {
    if (command == OBJCode.VOLTAGE.getValue().byteValue()) {
      return true;
    }
    if (command == OBJCode.RECORD.getValue().byteValue()) {
      return true;
    }
    if (command == OBJCode.NDFI.getValue().byteValue()) {
      return true;
    }
    if (command == OBJCode.ADFI.getValue().byteValue()) {
      return true;
    }
    if (command == OBJCode.AC.getValue().byteValue()) {
      return true;
    }
    if (command == OBJCode.AD.getValue().byteValue()) {
      return true;
    }
    if (command == OBJCode.MONITOR.getValue().byteValue()) {
      return true;
    }
    if (command == OBJCode.ADVINTV.getValue().byteValue()) {
      return true;
    }
    if (command == OBJCode.AUTHO.getValue().byteValue()) {
      return true;
    }
    if (command == OBJCode.PW.getValue().byteValue()) {
      return true;
    }
    if (command == OBJCode.CLEINTV.getValue().byteValue()) {
      return true;
    }
    if (command == OBJCode.CLENUM.getValue().byteValue()) {
      return true;
    }
    if (command == OBJCode.NAME.getValue().byteValue()) {
      return true;
    }
    if (command == OBJCode.MAXNUM.getValue().byteValue()) {
      return true;
    }
    if (command == OBJCode.CON_DIS.getValue().byteValue()) {
      return true;
    }
    if (command == OBJCode.SN.getValue().byteValue()) {
      return true;
    }
    if (command == OBJCode.VERSION.getValue().byteValue()) {
      return true;
    }
    if (command == OBJCode.CLET_ID.getValue().byteValue()) {
      return true;
    }
    return false;
  }
  
  private ResponseBean getReturnData(BluetoothGattCharacteristic characteristic)
  {
    List<Byte> responseData = new ArrayList();
    byte[] temp = characteristic.getValue();
    if (temp != null)
    {
      String str = DataFormatUtil.bytes2HexString(temp);
      if ((requestCode != 7) && 
        (str.startsWith("FF113355EE")))
      {
        ((Bt4ThCallback)this.context).bt4ThFailure(requestInt);
        System.out.println("requestInt命令被舍弃了：断开了连接" + requestInt);
      }
      switch (requestCode)
      {
      case 1: 
        System.out.println("Bt4Th-startMonitor:" + str);
        break;
      case 2: 
        System.out.println("Bt4Th-stopMonitor:" + str);
        break;
      case 12: 
        System.out.println("Bt4Th-getNumberDataFromIndex:" + str);
        break;
      case 13: 
        System.out.println("Bt4Th-getAllDataFromIndex:" + str);
      case 3: 
        System.out.println("Bt4Th-getAllDataCount:" + str);
        break;
      case 4: 
        System.out.println("Bt4Th-getAllData:" + str);
        break;
      case 5: 
        System.out.println("Bt4Th-getName:" + str);
        this.emitterCode = str;
        break;
      case 6: 
        System.out.println("Bt4Th-getMaxNumber:" + str);
        break;
      case 7: 
        System.out.println("Bt4Th-disConnection:" + str);
        break;
      case 8: 
        System.out.println("Bt4Th-getSn:" + str);
        break;
      case 9: 
        System.out.println("Bt4Th-getVersion:" + str);
        break;
      case 10: 
        System.out.println("Bt4Th-doAuthorization:" + str);
        break;
      case 11: 
        System.out.println("Bt4Th-getRecord:" + str);
        break;
      }
      for (int i = 0; i < temp.length; i++) {
        responseData.add(Byte.valueOf(temp[i]));
      }
    }
    if (responseData.size() >= 5)
    {
      ResponseBean response = new ResponseBean();
      byte[] event = new byte[3];
      for (int i = 0; i < event.length; i++) {
        event[i] = ((Byte)responseData.remove(0)).byteValue();
      }
      response.setEvent(event);
      
      response.setOpCode(OPCode.valueOf((Byte)responseData.remove(0)));
      
      response.setObjCode(OBJCode.valueOf((Byte)responseData.remove(0)));
      
      Byte[] array = (Byte[])responseData.toArray(new Byte[0]);
      byte[] datas = new byte[array.length];
      for (int i = 0; i < array.length; i++) {
        datas[i] = array[i].byteValue();
      }
      response.setData(datas);
      
      return response;
    }
    return null;
  }
}


/* Location:              /Users/lvwang2002/Desktop/CodeSource/work/DY_GLUCOSE/thapi_test/libs/cgmthapi.jar!/com/sanme/cgmadi/bluetooth4/impl/IBtConnectionImpl.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       0.7.1
 */