Skip to content

Instantly share code, notes, and snippets.

@madsunrise
Created July 13, 2017 11:42
Show Gist options
  • Save madsunrise/37288cb84cb470700abedae44a03a151 to your computer and use it in GitHub Desktop.
Save madsunrise/37288cb84cb470700abedae44a03a151 to your computer and use it in GitHub Desktop.
Java flashing
private void flashFromJava(final IHasKKM kkm, final String fileName) {
new Thread(new Runnable() {
@Override
public void run() {
try {
if (runCommand("EC 01") != 0) {
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Error running EC 01", Toast.LENGTH_SHORT).show();
}
});
return;
}
if (runCommand("EC 00") != 0) {
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "EC 00 вернула не ноль", Toast.LENGTH_SHORT).show();
}
});
return;
}
if (kkm.getKKM().CheckDownloadMode() != 0) {
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Error checking download mode" +
"", Toast.LENGTH_SHORT).show();
}
});
return;
}
byte[] buffer = new byte[BUFFER_SIZE];
File file = new File(Environment.getExternalStorageDirectory(), fileName);
InputStream stream = new BufferedInputStream(new FileInputStream(file));
int bytesRead = stream.read(buffer);
final int totalBlocks = (int) file.length() / BUFFER_SIZE + 1;
kkm.getKKM().ExitDownloadMode();
if (kkm.getKKM().StartFlashing(totalBlocks) != 0) {//runCommand("EC 03 41 1e") != 0) {
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Error running EC 03 41 1e", Toast.LENGTH_SHORT).show();
}
});
return;
}
int i = 0;
while (bytesRead != -1) {
String str = "EC 04 " + getHexCounter(i) + ' ' + toHexString(buffer, bytesRead);
final int response = kkm.getKKM().WriteBlock(buffer, bytesRead, i, str);
i++;
if (response != 0) {
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "ERROR WRITING (" + response + ")", Toast.LENGTH_LONG).show();
}
});
break;
}
bytesRead = stream.read(buffer);
}
if (kkm.getKKM().CheckFlashingSuccess() != 0) {
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Error checking flash status!", Toast.LENGTH_SHORT).show();
}
});
return;
}
kkm.getKKM().FinishFlashing();
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Done!", Toast.LENGTH_SHORT).show();
}
});
} catch (final Exception ex) {
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Exception! " + ex.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
}).start();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment