#I am developing an Android app to update

1 messages ยท Page 1 of 1 (latest)

meager cove
#

ahh..... i hated working on Android BLE stuff, lol, is the device able to accept that transfer? what happens if you try a smaller file?

slate hollow
#

nearly 70kb transffered and gatt suddenly disconnected from ble connection

#

can i share my code here?

meager cove
#

you could, but a gist or something would be better

#

is it consistently at 70kb every time?

#

also, for hardware and BLE in particular, it's highly possible the firmware has issues or the Android BLE stack has issues, so your code might be perfect and it still fails

slate hollow
#

i shared code on above link but it doesnt look good. ๐Ÿ˜€

#
private BluetoothGattCharacteristic otaCharacteristic;
private int mtuSize = 20;
private static final UUID OTA_SERVICE_UUID = UUID.fromString("0000ff00-0000-1000-8000-00805f9b34fb");
private static final UUID OTA_CHARACTERISTIC_UUID = UUID.fromString("0000ff01-0000-1000-8000-00805f9b34fb");

private void transferFirmwareData(byte[] firmwareData) {
  int offset = 0;
  while (offset < firmwareData.length) {
      int packetSize = Math.min(mtuSize, firmwareData.length - offset);  // Get the maximum chunk size based on MTU
      byte[] chunk = Arrays.copyOfRange(firmwareData, offset, offset + packetSize);
      writeCharacteristic(otaCharacteristic, chunk);
      offset += packetSize;

      // Add a small delay between packets to prevent overload (if needed)
      try {
          Thread.sleep(500);  
          Log.d(TAG, "Time sleep");
      } catch (InterruptedException e) {
          e.printStackTrace();
      }
  }

  Log.d(TAG, "Firmware data transfer complete");
}

// Helper function to write data to the OTA characteristic
private boolean writeCharacteristic(BluetoothGattCharacteristic characteristic, byte[] data) {
    if (mBluetoothLeService == null || characteristic == null) {
        Log.e(TAG, "BluetoothGatt not initialized or characteristic is null");
        return false;
    }

    
    // Set the chunk data to the characteristic and write it
    characteristic.setValue(data);
    boolean result = mBluetoothLeService.writeCharacteristic(characteristic);


    Log.d(TAG, "writeCharacteristic result: " + result);
    return result;

}
meager cove
#

yeah, to me, there's no obvious issue with that code, so try doing some more tests with the hardware or try a different device

slate hollow
#

discord provide better format than code share ๐Ÿ˜„

#

i didnt check transffered file size every time but last time was 70kb
i will check more but before test i have some questions.

meager cove
#

sure, go ahead

slate hollow
#

i am not sure current uuid "0000ff00" is correct ota service uuid

meager cove
#

that iirc depends on your BLE device

slate hollow
#

right
but i dont have any doc for my ble gatt.
thats problem here

meager cove
#

ah yeah that's unfortunate, not much you can do without that documentation

slate hollow
#

i checked all supported services from my gatt.

#

this is screenshot

#

and i have 2nd question,
i am trying to implement reconnect logic on my code.
but during transfer firmare, gatt connection suddely closed and gatt reboots automatically,
on this case, how can i implement reconnect logic?

#

i am just web dev and its my very first time on android. ๐Ÿ™‚

meager cove
#

yeah, for retry logic, that also depends on how much the device supports it, so i think there's probably some ways to look at the connection state and just try again

#

when i did firmware updates, i sent a command to enter firmware update mode, then sent the bytes over, and if there was an error or connection drop, i would do the reconnect logic, then query to see if it's still in firmware update mode, then resumed sending from the previous bytes, repeat that until it finished, then send the finalize command

#

but again, that was maybe just that device i was working with

slate hollow
#

thanks for your advise.

#

btw do you think is there any problem on my code?

#

and it will be great if u can share your previous code

meager cove
#

no, I don't think your code necessarily has a problem

#

unfortunately, my code was in kotlin and under NDA so i can't share it, but i can review it and give you some suggestions. also, the technique I used was probably not so great compared to the stuff available now, anyway

slate hollow
#

no problem,
what i am not sure here is how to enter OTA mode and how to trigger install command once firmware file transffered successfully.

meager cove
#

right, that depends on the device

#

without real documentation, you're stuck because the firmware decides how to do that stuff

#

the general strategy is to wait for the device to tell you the command was successful instead of the arbitrary 500ms sleep you have

slate hollow
#

got it.
Is it possible to share me code snippet that triggering the firmware installation after firmware transfer done?

meager cove
#

sure, but it was an arbitrary write to a characteristic that only that device understood

slate hollow
#

sure i just wanna know basic workflow

#

bcz i am newbie to android

meager cove
#

yeah there's nothing specific to android for that, it'd be the same as any other code IMO

#
sendFirmwareUpdateCommand() {
  //write characteristic
  while(firmware transfer isn't complete) {
    sendFirmwareBytes();
  }
  sendFirmwareFinishedCommand();
}
slate hollow
#

thanks for your help bro.
i will test more and if i find sth, i will let u know

slate hollow
#

Hi
I tested with different firmare file and different devices.
when i tried with 39kb firmware, data transfer stopped after 5.4kb transffered (same on each attempt)

meager cove
#

yeah, that to me signifies maybe some issue with the hardware, you're using the exact same code, just different files, right?

slate hollow
#

Yes

meager cove
#

yeah, seems kind of sus, if there's nothing online, try contacting the manufacturer, that's really the only thing i can think of

slate hollow
#

i tried with other firmare file and its succeesfully transffered

#

seems there was error in previous file

#

now i am trying to trigger firmware update.

#

did you erase old firmware once u transfer new firmware?

#
private void initiateFirmwareInstallation(byte[] firmwareData) {
        // Step 1: Start the firmware write process (CMD_FW_WRITE_START)
        byte[] startWriteCommand = new byte[] {0x14};  // CMD_FW_WRITE_START
        writeCharacteristic(otaCharacteristic, startWriteCommand);

        // Step 2: Transfer the actual firmware data
        // Assuming transferFirmwareData() handles this part
        boolean isTransferSuccessful = transferFirmwareData(firmwareData);

        if (isTransferSuccessful) {
            // Step 4: Now erase existing firmware (CMD_FW_ERASE)
            byte[] eraseCommand = new byte[] {0x16};  // CMD_FW_ERASE
            writeCharacteristic(otaCharacteristic, eraseCommand);
            Log.d(TAG, "Firmware erase");

            // Step 5: Finalize the firmware upgrade (CMD_FW_UPGRADE)
            byte[] upgradeCommand = new byte[] {0x18};  // CMD_FW_UPGRADE
            writeCharacteristic(otaCharacteristic, upgradeCommand);

            Log.d(TAG, "Firmware installation initiated successfully");
        } else {
            Log.e(TAG, "Firmware transfer failed. Upgrade aborted.");
        }
    }
#

this is my workflow of firmware upgrade
step 5 is not working but can you please check if my workflow is correct?

meager cove
#

yeah, that looks pretty good. i think it could be too quick to erase and then immediately upgrade, it might take time for the firmware to catch up, that's my only thought

#

for the firmware i was working with, there was no need to erase the previous firmware during the upgrade

slate hollow
#

I got it.

#

byte[] startWriteCommand = new byte[] {0x14}; // CMD_FW_WRITE_START
writeCharacteristic(otaCharacteristic, startWriteCommand);

do you think this part is really needed?

meager cove
#

only if the firmware requires it

#

for mine, i had to send a command to enable firmware transfer mode, then another to start firmware integrity check, then another to upgrade, and another to re-enable normal mode, so it just depends on that specific firmware

slate hollow
#

Hmm, interesting. In your case, once the firmware transfer is done and the update is finished, does GATT reboot automatically?

meager cove
#

that again depends on the firmware, i don't remember if my device did or not

slate hollow
#

got it.
its a bit fun to move forward step by step.
this work experience with BLE GATT will be remembered as terrible for me. ๐Ÿ˜„

meager cove
#

hahaha yeah..... i'm glad i can at least help somewhat, because it was a really frustrating experience for me back then too lol

slate hollow
#

once i solve , i will write blog post for ble gatt. ๐Ÿ™‚

#

Oh, by the way, is there any kind of simulator for a BLE firmware .bin file? I would like to see what the firmware UI that I want to update looks like.

meager cove
#

hmm i don't think that exists unless it's available from the manufacturer

#

since the firmware is just bytes, it needs to be interpreted by the device

#

the device i was flashing didn't even have a display, so there was no UI

slate hollow
#

lol,
at least my gatt has screen, haha

#

I just followed you on X,
thanks again for your help bro.