Esempio n. 1
0
 public byte[] getRecentCalRecordsTest() {
   Log.d(TAG, "Reading Cal Records page range...");
   int recordType = Constants.RECORD_TYPES.CAL_SET.ordinal();
   int endPage = readDataBasePageRange(recordType);
   Log.d(TAG, "Reading Cal Records page...");
   return readDataBasePageTest(recordType, endPage);
 }
Esempio n. 2
0
 private int readDataBasePageRange(int recordType) {
   ArrayList<Byte> payload = new ArrayList<Byte>();
   Log.d(TAG, "adding Payload");
   payload.add((byte) recordType);
   Log.d(TAG, "Sending write command");
   writeCommand(Constants.READ_DATABASE_PAGE_RANGE, payload);
   Log.d(TAG, "About to call getdata");
   byte[] readData = read(MIN_LEN).getData();
   Log.d(TAG, "Going to return");
   return ByteBuffer.wrap(readData).order(ByteOrder.LITTLE_ENDIAN).getInt(4);
 }
Esempio n. 3
0
  private ReadPacket read(int numOfBytes) {
    byte[] readData = new byte[numOfBytes];
    int len = 0;
    try {
      //            UsbInterface mDataInterface = mDevice.getInterface(1);
      //            UsbEndpoint mReadEndpoint = mDataInterface.getEndpoint(1);
      //            byte[] mReadBuffer;
      //            mReadBuffer = new byte[16 * 1024];
      //
      //            int readAmt = Math.min(readData.length, mReadBuffer.length);
      //            synchronized (mReadBufferLock) {
      //
      //
      //                Log.d(TAG, "Read about to call bulk transfer.");
      //                if (len < 0) {
      //                    // This sucks: we get -1 on timeout, not 0 as preferred.
      //                    // We *should* use UsbRequest, except it has a bug/api oversight
      //                    // where there is no way to determine the number of bytes read
      //                    // in response :\ -- http://b.android.com/28023
      //                    if (IO_TIMEOUT == Integer.MAX_VALUE) {
      //                        // Hack: Special case "~infinite timeout" as an error.
      //                        len = -1;
      //                    }
      //                    len = 0;
      //                }
      //
      ////              System.arraycopy(mReadBuffer, 0, readData, 0, readAmt);
      //            }
      //            len = mConnection.bulkTransfer(mReadEndpoint, readData, readAmt, IO_TIMEOUT);

      len = mSerialDevice.getPorts().get(0).read(readData, IO_TIMEOUT);

      Log.d(TAG, "Read " + len + " byte(s) complete.");

      // Add a 100ms delay for when multiple write/reads are occurring in series
      Thread.sleep(100);

      // TODO: this debug code to print data of the read, should be removed after
      // finding the source of the reading issue
      String bytes = "";
      int readAmount = len;
      for (int i = 0; i < readAmount; i++) bytes += String.format("%02x", readData[i]) + " ";
      Log.d(TAG, "Read data: " + bytes);
      ////////////////////////////////////////////////////////////////////////////////////////

    } catch (Exception e) {
      Log.e(TAG, "Unable to read from serial device.", e);
    }
    byte[] data = Arrays.copyOfRange(readData, 0, len);
    return new ReadPacket(data);
  }
Esempio n. 4
0
  @TargetApi(19)
  private void scanLeDevice(final boolean enable) {
    if (enable) {
      // Stops scanning after a pre-defined scan period.
      Log.d(TAG, "Start scan 19");
      mHandler.postDelayed(
          new Runnable() {
            @Override
            public void run() {
              is_scanning = false;
              bluetooth_adapter.stopLeScan(mLeScanCallback);
              invalidateOptionsMenu();
            }
          },
          SCAN_PERIOD);

      is_scanning = true;
      bluetooth_adapter.startLeScan(mLeScanCallback);
    } else {
      is_scanning = false;
      if (bluetooth_adapter != null && bluetooth_adapter.isEnabled()) {
        bluetooth_adapter.stopLeScan(mLeScanCallback);
      }
    }
    invalidateOptionsMenu();
  }
Esempio n. 5
0
 public ReadData(UsbSerialDriver device, UsbDeviceConnection connection, UsbDevice usbDevice) {
   mSerialDevice = device;
   mConnection = connection;
   mDevice = usbDevice;
   try {
     mSerialDevice.getPorts().get(0).open(connection);
   } catch (IOException e) {
     Log.d("FAILED WHILE", "trying to open");
   }
   //        }
 }
Esempio n. 6
0
 public SensorRecord[] getRecentSensorRecords(int numOfRecentPages) {
   if (numOfRecentPages < 1) {
     throw new IllegalArgumentException("Number of pages must be greater than 1.");
   }
   Log.d(TAG, "Reading Sensor page range...");
   int recordType = Constants.RECORD_TYPES.SENSOR_DATA.ordinal();
   int endPage = readDataBasePageRange(recordType);
   Log.d(TAG, "Reading " + numOfRecentPages + " Sensor page(s)...");
   numOfRecentPages = numOfRecentPages - 1;
   SensorRecord[] allPages = new SensorRecord[0];
   for (int i = Math.min(numOfRecentPages, endPage); i >= 0; i--) {
     int nextPage = endPage - i;
     Log.d(TAG, "Reading #" + i + " Sensor pages (page number " + nextPage + ")");
     SensorRecord[] ithSensorRecordPage = readDataBasePage(recordType, nextPage);
     SensorRecord[] result = Arrays.copyOf(allPages, allPages.length + ithSensorRecordPage.length);
     System.arraycopy(ithSensorRecordPage, 0, result, allPages.length, ithSensorRecordPage.length);
     allPages = result;
   }
   Log.d(TAG, "Read complete of Sensor pages.");
   return allPages;
 }
Esempio n. 7
0
 @TargetApi(21)
 private void scanLeDeviceLollipop(final boolean enable) {
   if (enable) {
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
       lollipopScanner = bluetooth_adapter.getBluetoothLeScanner();
     }
     if (lollipopScanner != null) {
       Log.d(TAG, "Starting scanner 21");
       // Stops scanning after a pre-defined scan period.
       mHandler.postDelayed(
           new Runnable() {
             @Override
             public void run() {
               is_scanning = false;
               if (bluetooth_adapter != null && bluetooth_adapter.isEnabled()) {
                 lollipopScanner.stopScan(mScanCallback);
               }
               invalidateOptionsMenu();
             }
           },
           SCAN_PERIOD);
       ScanSettings settings =
           new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
       is_scanning = true;
       lollipopScanner.startScan(null, settings, mScanCallback);
     } else {
       try {
         scanLeDevice(true);
       } catch (Exception e) {
         Log.e(TAG, "Failed to scan for ble device", e);
       }
     }
   } else {
     is_scanning = false;
     if (bluetooth_adapter != null && bluetooth_adapter.isEnabled()) {
       lollipopScanner.stopScan(mScanCallback);
     }
   }
   invalidateOptionsMenu();
 }
Esempio n. 8
0
  public CalRecord(byte[] packet) {
    super(packet);
    slope = ByteBuffer.wrap(packet).order(ByteOrder.LITTLE_ENDIAN).getDouble(8);
    intercept = ByteBuffer.wrap(packet).order(ByteOrder.LITTLE_ENDIAN).getDouble(16);
    scale = ByteBuffer.wrap(packet).order(ByteOrder.LITTLE_ENDIAN).getDouble(24);
    unk[0] = packet[32];
    unk[1] = packet[33];
    unk[2] = packet[34];
    decay = ByteBuffer.wrap(packet).order(ByteOrder.LITTLE_ENDIAN).getDouble(35);
    numRecords = packet[43];
    long displayTimeOffset = (getDisplayTime().getTime() - getSystemTime().getTime()) / (1000);
    int start = 44;
    for (int i = 0; i < numRecords; i++) {
      Log.d("CalDebug", "Loop #" + i);
      byte[] temp = new byte[SUB_LEN];
      System.arraycopy(packet, start, temp, 0, temp.length);
      calSubrecords[i] = new CalSubrecord(temp, displayTimeOffset);
      start += SUB_LEN;
    }

    Log.d("ShareTest", "slope: " + slope + " intercept: " + intercept);
  }
Esempio n. 9
0
 @Override
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
   IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
   SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
   if (scanResult == null || scanResult.getContents() == null) {
     return;
   }
   if (scanResult.getFormatName().equals("CODE_128")) {
     Log.d(TAG, "Setting serial number to: " + scanResult.getContents());
     prefs.edit().putString("share_key", scanResult.getContents()).apply();
     returnToHome();
   }
 }
Esempio n. 10
0
  @Override
  protected void onListItemClick(ListView l, View v, int position, long id) {
    Log.d(TAG, "Item Clicked");
    final BluetoothDevice device = mLeDeviceListAdapter.getDevice(position);
    if (device == null || device.getName() == null) return;
    Toast.makeText(this, R.string.connecting_to_device, Toast.LENGTH_LONG).show();

    ActiveBluetoothDevice btDevice =
        new Select().from(ActiveBluetoothDevice.class).orderBy("_ID desc").executeSingle();

    final SharedPreferences prefs =
        PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    prefs.edit().putString("last_connected_device_address", device.getAddress()).apply();
    if (btDevice == null) {
      ActiveBluetoothDevice newBtDevice = new ActiveBluetoothDevice();
      newBtDevice.name = device.getName();
      newBtDevice.address = device.getAddress();
      newBtDevice.save();
    } else {
      btDevice.name = device.getName();
      btDevice.address = device.getAddress();
      btDevice.save();
    }
    if (device.getName().toLowerCase().contains("dexcom")) {
      if (!CollectionServiceStarter.isBTShare(getApplicationContext())) {
        prefs.edit().putString("dex_collection_method", "DexcomShare").apply();
        prefs.edit().putBoolean("calibration_notifications", false).apply();
      }
      if (prefs.getString("share_key", "SM00000000").compareTo("SM00000000") == 0
          || prefs.getString("share_key", "SM00000000").length() < 10) {
        requestSerialNumber(prefs);
      } else returnToHome();

    } else if (device.getName().toLowerCase().contains("bridge")) {
      if (!CollectionServiceStarter.isDexbridgeWixel(getApplicationContext()))
        prefs.edit().putString("dex_collection_method", "DexbridgeWixel").apply();
      if (prefs.getString("dex_txid", "00000").compareTo("00000") == 0
          || prefs.getString("dex_txid", "00000").length() < 5) {
        requestTransmitterId(prefs);
      } else returnToHome();

    } else if (device.getName().toLowerCase().contains("drip")) {
      if (!(CollectionServiceStarter.isBTWixel(getApplicationContext())
          || CollectionServiceStarter.isWifiandBTWixel(getApplicationContext()))) {
        prefs.edit().putString("dex_collection_method", "BluetoothWixel").apply();
      }
      returnToHome();
    } else {
      returnToHome();
    }
  }
Esempio n. 11
0
 private void writeCommand(int command) {
   byte[] packet = new PacketBuilder(command).compose();
   if (mSerialDevice != null) {
     try {
       //                UsbInterface mDataInterface = mDevice.getInterface(1);
       //                UsbEndpoint mWriteEndpoint = mDataInterface.getEndpoint(0);
       //                mConnection.bulkTransfer(mWriteEndpoint, packet, packet.length,
       // IO_TIMEOUT);
       mSerialDevice.getPorts().get(0).write(packet, IO_TIMEOUT);
     } catch (Exception e) {
       Log.e(TAG, "Unable to write to serial device.", e);
     }
   }
 }
Esempio n. 12
0
  @TargetApi(21)
  private void initializeScannerCallback() {
    Log.d(TAG, "initializeScannerCallback");
    mScanCallback =
        new ScanCallback() {
          @Override
          public void onBatchScanResults(final List<ScanResult> results) {
            runOnUiThread(
                new Runnable() {
                  @Override
                  public void run() {
                    for (ScanResult result : results) {
                      BluetoothDevice device = result.getDevice();
                      if (device.getName() != null && device.getName().length() > 0) {
                        mLeDeviceListAdapter.addDevice(device);
                      }
                    }
                    mLeDeviceListAdapter.notifyDataSetChanged();
                  }
                });
          }

          @Override
          public void onScanResult(int callbackType, ScanResult result) {
            final BluetoothDevice device = result.getDevice();
            runOnUiThread(
                new Runnable() {
                  @Override
                  public void run() {
                    if (device.getName() != null && device.getName().length() > 0) {
                      mLeDeviceListAdapter.addDevice(device);
                      mLeDeviceListAdapter.notifyDataSetChanged();
                    }
                  }
                });
          }
        };
  }
Esempio n. 13
0
 public MeterRecord[] getRecentMeterRecords() {
   Log.d(TAG, "Reading Meter page...");
   int recordType = Constants.RECORD_TYPES.METER_DATA.ordinal();
   int endPage = readDataBasePageRange(recordType);
   return readDataBasePage(recordType, endPage);
 }
Esempio n. 14
0
 public int readDisplayTimeOffset() {
   Log.d(TAG, "Reading display time offset...");
   writeCommand(Constants.READ_DISPLAY_TIME_OFFSET);
   byte[] readData = read(MIN_LEN).getData();
   return ByteBuffer.wrap(readData).order(ByteOrder.LITTLE_ENDIAN).getInt() & 0xffffffff;
 }
Esempio n. 15
0
 public long readSystemTime() {
   Log.d(TAG, "Reading system time...");
   writeCommand(Constants.READ_SYSTEM_TIME);
   byte[] readData = read(MIN_LEN).getData();
   return ByteBuffer.wrap(readData).order(ByteOrder.LITTLE_ENDIAN).getInt() & 0xffffffff;
 }
Esempio n. 16
0
 public int readBatteryLevel() {
   Log.d(TAG, "Reading battery level...");
   writeCommand(Constants.READ_BATTERY_LEVEL);
   byte[] readData = read(MIN_LEN).getData();
   return ByteBuffer.wrap(readData).order(ByteOrder.LITTLE_ENDIAN).getInt();
 }