Skip to content

Commit d0f5062

Browse files
committed
delete bond. rebond.
1 parent e61145a commit d0f5062

File tree

4 files changed

+76
-7
lines changed

4 files changed

+76
-7
lines changed

app/src/main/java/com/samsung/microbit/core/bluetooth/BLEManager.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,6 @@ public void onServicesDiscovered(BluetoothGatt gatt, int status) {
779779
} else {
780780
Log.v(TAG, "Hardware Type: V2");
781781
cD.mhardwareVersion = 1;
782-
783782
}
784783
BluetoothUtils.setPairedMicroBit(MBApp.getApp(), cD);
785784
bleState |= state;

app/src/main/java/com/samsung/microbit/ui/activity/PairingActivity.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1414,6 +1414,21 @@ private void startPairingSecureBle(BluetoothDevice device) {
14141414
if(device.getBondState() == BluetoothDevice.BOND_BONDED) {
14151415
logi("Device is already bonded.");
14161416
stopScanning();
1417+
1418+
// Rebond
1419+
try {
1420+
Method m = device.getClass().getMethod("removeBond", (Class[]) null);
1421+
m.invoke(device, (Object[]) null);
1422+
} catch (Exception e) { Log.e(TAG, e.getMessage()); }
1423+
1424+
// Sleep for 200ms
1425+
try {
1426+
Thread.sleep(200);
1427+
} catch (InterruptedException e) {
1428+
e.printStackTrace();
1429+
}
1430+
device.createBond();
1431+
14171432
//Get device name from the System settings if present and add to our list
14181433
ConnectedDevice newDev = new ConnectedDevice(newDeviceCode.toUpperCase(),
14191434
newDeviceCode.toUpperCase(), false, newDeviceAddress, 0, null,
@@ -1422,7 +1437,6 @@ private void startPairingSecureBle(BluetoothDevice device) {
14221437
} else {
14231438
logi("device.createBond returns " + device.createBond());
14241439
}
1425-
device.createBond();
14261440
}
14271441

14281442
@Override

app/src/main/java/com/samsung/microbit/ui/activity/ProjectActivity.java

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383

8484
import static com.samsung.microbit.BuildConfig.DEBUG;
8585
import static com.samsung.microbit.ui.PopUp.TYPE_ALERT;
86+
import static com.samsung.microbit.ui.PopUp.TYPE_CHOICE;
8687
import static com.samsung.microbit.ui.PopUp.TYPE_PROGRESS_NOT_CANCELABLE;
8788
import static com.samsung.microbit.ui.PopUp.TYPE_SPINNER_NOT_CANCELABLE;
8889
import static com.samsung.microbit.ui.activity.PopUpActivity.INTENT_ACTION_UPDATE_LAYOUT;
@@ -1123,6 +1124,18 @@ protected void startFlashing(int flashingType) {
11231124

11241125
MBApp application = MBApp.getApp();
11251126
int hardwareType = currentMicrobit.mhardwareVersion;
1127+
if(hardwareType != MICROBIT_V1 && hardwareType != MICROBIT_V2) {
1128+
// Ask the user what type of hardware they have
1129+
PopUp.show(getString(R.string.dfu_what_hardware),
1130+
getString(R.string.dfu_what_hardware_title),
1131+
R.drawable.error_face,
1132+
R.drawable.red_btn,
1133+
PopUp.GIFF_ANIMATION_ERROR,
1134+
TYPE_ALERT,
1135+
null,
1136+
null
1137+
);
1138+
}
11261139

11271140

11281141
// Create tmp hex for V1 or V2
@@ -1284,9 +1297,12 @@ private String[] universalHexToDFU(String inputPath, int hardwareType) {
12841297

12851298
int application_size = 0;
12861299
int next = 0;
1287-
boolean records_wanted = false;
1300+
boolean records_wanted = true;
12881301
boolean is_fat = false;
1302+
boolean is_v2 = false;
1303+
boolean uses_ESA = false;
12891304
ByteArrayOutputStream lastELA = new ByteArrayOutputStream();
1305+
ByteArrayOutputStream lastESA = new ByteArrayOutputStream();
12901306

12911307
try {
12921308
fis = new FileInputStream(inputPath);
@@ -1308,6 +1324,7 @@ private String[] universalHexToDFU(String inputPath, int hardwareType) {
13081324
switch (b_type) {
13091325
case 'A': // Block start
13101326
is_fat = true;
1327+
records_wanted = false;
13111328

13121329
// Check data for id
13131330
if (bs[b_x + 9] == '9' && bs[b_x + 10] == '9' && bs[b_x + 11] == '0' && bs[b_x + 12] == '1') {
@@ -1329,6 +1346,19 @@ private String[] universalHexToDFU(String inputPath, int hardwareType) {
13291346
outputHex.write(bs, b_x, next);
13301347
}
13311348
break;
1349+
case '2':
1350+
uses_ESA = true;
1351+
1352+
ByteArrayOutputStream currentESA = new ByteArrayOutputStream();
1353+
currentESA.write(bs, b_x, next);
1354+
1355+
// If ESA has changed write
1356+
if (!Arrays.equals(currentESA.toByteArray(), lastESA.toByteArray())) {
1357+
lastESA.reset();
1358+
lastESA.write(bs, b_x, next);
1359+
outputHex.write(bs, b_x, next);
1360+
}
1361+
break;
13321362
case '1':
13331363
// EOF
13341364
// Ensure KV storage is erased
@@ -1363,13 +1393,31 @@ private String[] universalHexToDFU(String inputPath, int hardwareType) {
13631393
// Copy record to hex
13641394
// Record starts at b_x, next long
13651395
// Calculate address of record
1366-
int b_ela = (charToInt((char) lastELA.toByteArray()[9]) << 12) | (charToInt((char) lastELA.toByteArray()[10]) << 8) | (charToInt((char) lastELA.toByteArray()[11]) << 4) | (charToInt((char) lastELA.toByteArray()[12]));
1396+
int b_a = 0;
1397+
if(lastELA.size() > 0 && !uses_ESA) {
1398+
b_a = 0;
1399+
b_a = (charToInt((char) lastELA.toByteArray()[9]) << 12) | (charToInt((char) lastELA.toByteArray()[10]) << 8) | (charToInt((char) lastELA.toByteArray()[11]) << 4) | (charToInt((char) lastELA.toByteArray()[12]));
1400+
b_a = b_a << 16;
1401+
}
1402+
if(lastESA.size() > 0 && uses_ESA) {
1403+
b_a = 0;
1404+
b_a = (charToInt((char) lastESA.toByteArray()[9]) << 12) | (charToInt((char) lastESA.toByteArray()[10]) << 8) | (charToInt((char) lastESA.toByteArray()[11]) << 4) | (charToInt((char) lastESA.toByteArray()[12]));
1405+
b_a = b_a * 16;
1406+
}
1407+
13671408
int b_raddr = (charToInt((char) bs[b_x + 3]) << 12) | (charToInt((char) bs[b_x + 4]) << 8) | (charToInt((char) bs[b_x + 5]) << 4) | (charToInt((char) bs[b_x + 6]));
1368-
int b_addr = b_ela << 16 | b_raddr;
1409+
int b_addr = b_a | b_raddr;
1410+
13691411

13701412
int lower_bound = 0; int upper_bound = 0;
13711413
if(hardwareType == MICROBIT_V1) { lower_bound = 0x18000; upper_bound = 0x38000; }
1372-
if(hardwareType == MICROBIT_V2) { lower_bound = 0x27000; upper_bound = 0x71FFF; }
1414+
if(hardwareType == MICROBIT_V2 && is_fat) { lower_bound = 0x27000; upper_bound = 0x71FFF; } // Current Universal Hex files are s140
1415+
if(hardwareType == MICROBIT_V2 && !is_fat) { lower_bound = 0x1C000; upper_bound = 0x77000; } // Current C++ programs are S113
1416+
1417+
// Check for Cortex-M4 Vector Table
1418+
if(b_addr == 0x10 && bs[b_x + 41] != 'E' && bs[b_x + 42] != '0') { // Vectors exist
1419+
is_v2 = true;
1420+
}
13731421

13741422
if ((records_wanted || !is_fat) && b_addr >= lower_bound && b_addr < upper_bound) {
13751423
outputHex.write(bs, b_x, next);
@@ -1414,18 +1462,22 @@ private String[] universalHexToDFU(String inputPath, int hardwareType) {
14141462
ret[0] = hexToFlash.getAbsolutePath();
14151463
ret[1] = Integer.toString(application_size);
14161464

1417-
if(hardwareType == MICROBIT_V2 && is_fat == false) {
1465+
/*
1466+
if(hardwareType == MICROBIT_V2 && (!is_v2 && !is_fat)) {
14181467
ret[1] = Integer.toString(-1); // Invalidate hex file
14191468
}
1469+
*/
14201470

14211471
return ret;
14221472
} catch (IOException e) {
14231473
e.printStackTrace();
14241474
}
14251475

14261476
} catch (FileNotFoundException e) {
1477+
Log.v(TAG, "File not found.");
14271478
e.printStackTrace();
14281479
} catch (IOException e) {
1480+
Log.v(TAG, "IO Exception.");
14291481
e.printStackTrace();
14301482
}
14311483

app/src/main/res/values/strings.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,5 +375,9 @@
375375
<string name="reconnect_text">You\'ve been disconnected from the micro:bit board. Do you want to reconnect?</string>
376376
<string name="reconnect_ok_button">Reconnect</string>
377377
<string name="reset_microbit_because_of_hex_file_wrong">. Please press RESET button!</string>
378+
379+
<string name="dfu_what_hardware_title">Unable to determine your micro:bit type</string>
380+
<string name="dfu_what_hardware">Try again, if the problem persists please attempt a USB flash</string>
381+
378382
</resources>
379383

0 commit comments

Comments
 (0)