Пример #1
0
        @Override
        public void onReceive(Context context, Intent intent) {

          final String action = intent.getAction();
          Log.d(TAG, "action: " + action);

          if (BluetoothLeService.ACTION_DATA_NOTIFY.equals(action)) {
            byte[] value = intent.getByteArrayExtra(BluetoothLeService.EXTRA_DATA);
            String uuidStr = intent.getStringExtra(BluetoothLeService.EXTRA_UUID);
            if (uuidStr.equals(mCharIdentify.getUuid().toString())) {
              // Image info notification
              mTargImgHdr.ver = Conversion.buildUint16(value[1], value[0]);
              mTargImgHdr.imgType = ((mTargImgHdr.ver & 1) == 1) ? 'B' : 'A';
              mTargImgHdr.len = Conversion.buildUint16(value[3], value[2]);
              displayImageInfo(mTargImage, mTargImgHdr);
            }
          } else if (BluetoothLeService.ACTION_DATA_WRITE.equals(action)) {
            int status =
                intent.getIntExtra(BluetoothLeService.EXTRA_STATUS, BluetoothGatt.GATT_SUCCESS);
            if (status != BluetoothGatt.GATT_SUCCESS) {
              Log.e(TAG, "Write failed: " + status);
              Toast.makeText(context, "GATT error: status=" + status, Toast.LENGTH_SHORT).show();
            }
          }
        }
Пример #2
0
  private boolean loadFile(String filepath, boolean isAsset) {
    boolean fSuccess = false;

    // Load binary file
    try {
      // Read the file raw into a buffer
      InputStream stream;
      if (isAsset) {
        stream = getAssets().open(filepath);
      } else {
        File f = new File(filepath);
        stream = new FileInputStream(f);
      }
      stream.read(mFileBuffer, 0, mFileBuffer.length);
      stream.close();
    } catch (IOException e) {
      // Handle exceptions here
      mLog.setText("File open failed: " + filepath + "\n");
      return false;
    }

    // Show image info
    mFileImgHdr.ver = Conversion.buildUint16(mFileBuffer[5], mFileBuffer[4]);
    mFileImgHdr.len = Conversion.buildUint16(mFileBuffer[7], mFileBuffer[6]);
    mFileImgHdr.imgType = ((mFileImgHdr.ver & 1) == 1) ? 'B' : 'A';
    System.arraycopy(mFileBuffer, 8, mFileImgHdr.uid, 0, 4);
    displayImageInfo(mFileImage, mFileImgHdr);

    // Verify image types
    boolean ready = mFileImgHdr.imgType != mTargImgHdr.imgType;
    int resid = ready ? R.style.dataStyle1 : R.style.dataStyle2;
    mFileImage.setTextAppearance(this, resid);

    // Enable programming button only if image types differ
    mBtnStart.setEnabled(ready);

    // Expected duration
    mEstDuration = ((PKT_INTERVAL * mFileImgHdr.len * 4) / OAD_BLOCK_SIZE) / 1000;
    displayStats();

    // Log
    mLog.setText("Image " + mFileImgHdr.imgType + " selected.\n");
    mLog.append(ready ? "Ready to program device!\n" : "Incompatible image, select alternative!\n");

    updateGui();

    return fSuccess;
  }