public ProgramInfo(byte[] mImage, SensorTagDevice device) { // this.mFileImgHdr = mFileImgHdr; mFileImgHdr = new ImageHdr(mImage); mFileBuffer = mImage; this.mBtLeService = BluetoothLeService.getInstance(); this.deviceAddress = device.getAddress(); BluetoothGattService mOadService = device.getOadService(); BluetoothGattService mConnControlService = device.getConnControlService(); // Characteristics list List<BluetoothGattCharacteristic> mCharListOad = mOadService.getCharacteristics(); List<BluetoothGattCharacteristic> mCharListCc = mConnControlService.getCharacteristics(); mServiceOk = mCharListOad.size() == 2 && mCharListCc.size() >= 3; if (mServiceOk) { mCharIdentify = mCharListOad.get(0); mCharBlock = mCharListOad.get(1); mCharBlock.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE); mCharConnReq = mCharListCc.get(1); mBtLeService.setCharacteristicNotification(deviceAddress, mCharBlock, true); if (Build.VERSION.SDK_INT >= 21) { mBtLeService.requestConnectionPriority( deviceAddress, BluetoothGatt.CONNECTION_PRIORITY_HIGH); } setConnectionParameters(); } }
public void stopProgramming() { mTimer.cancel(); mTimer.purge(); mProgramming = false; mBtLeService.setCharacteristicNotification(deviceAddress, mCharBlock, false); }
/** * Make sure connection interval is long enough for OAD (Android default connection interval is * 7.5 ms) */ private void setConnectionParameters() { byte[] value = { Conversion.loUint16(OAD_CONN_INTERVAL), Conversion.hiUint16(OAD_CONN_INTERVAL), Conversion.loUint16(OAD_CONN_INTERVAL), Conversion.hiUint16(OAD_CONN_INTERVAL), 0, 0, Conversion.loUint16(OAD_SUPERVISION_TIMEOUT), Conversion.hiUint16(OAD_SUPERVISION_TIMEOUT) }; mCharConnReq.setValue(value); mBtLeService.writeCharacteristic(deviceAddress, mCharConnReq); }
public void startProgramming() { mProgramming = true; packetsSent = 0; mCharIdentify.setValue(mFileImgHdr.getRequest()); mBtLeService.writeCharacteristic(deviceAddress, mCharIdentify); this.reset(); System.out.println("total blocks: " + nBlocks); mTimer = new Timer(); mTimer.scheduleAtFixedRate( new TimerTask() { @Override public void run() { iTimeElapsed += TIMER_INTERVAL; } }, 0, TIMER_INTERVAL); }
public void programBlock() { if (!mProgramming) return; if (iBlocks < nBlocks) { mProgramming = true; // Prepare block mOadBuffer[0] = Conversion.loUint16((short) iBlocks); mOadBuffer[1] = Conversion.hiUint16((short) iBlocks); System.arraycopy(mFileBuffer, iBytes, mOadBuffer, 2, OAD_BLOCK_SIZE); // Send block mCharBlock.setValue(mOadBuffer); boolean success = mBtLeService.writeCharacteristicNonBlock(deviceAddress, mCharBlock); Log.d(TAG, "Sent block : " + iBlocks + " to [" + deviceAddress + "]"); if (success) { // Update stats packetsSent++; iBlocks++; iBytes += OAD_BLOCK_SIZE; if (isComplete()) { Log.d(TAG, "OAD Completed on [" + deviceAddress + "]"); } } else { mProgramming = false; Log.e(TAG, "GATT writeCharacteristic failed on [" + deviceAddress + "]"); } } else { mProgramming = false; } if (!mProgramming) { stopProgramming(); } }