Hi All,
As a new android developer, I'm working on an app that connect to Bluetooth device (BLE).
I'm using Java Bluetooth classes including
BlutoothGatt to connect/disconnect to that device.
Everything works fine.
The problem:
I have a binary file which I'm trying to send to the Bluetooth device as
firmware update. so I switch from device image/file to this file. before switching I do some preparation including BLE disconnect and connect again which takes a while then another method reads the file and send it to the Bluetooth device. The problem is the second method runs before BLE is connecting again which cause an error.
Connecting and disconnecting happens as a result of the first method. I need the app to wait till the first method is fully executed and all background functions including connection is back.
I tried using semaphore but it doesn't seem to work. What is the right way to do it?
Any help will be really appreciated?
What I have tried:
My first method that checks software before sending file:
private void checkSoftware(){
bleLoader.switchImg();
}
Method used to prepare Bluetooth device before update/upload .bin file:
public void switchImg(){
byte[] data = new byte[4];
byte[] value = new byte[] {(byte)0x04,(byte)0x00,(byte)0x10,(byte)0x10};
System.arraycopy(value,0,mFileImgHdr.uid,0,value.length);
Log.d(LOG_TAG,"value1: " + value[0] + " value2: " + value[1] +" value3: "+ value[2] + " value4: " + value[3]);
System.arraycopy(value,0,data,0,value.length);
mOadService = gatt.getService(UUID.fromString(SERVICE_OAD_UUID));
mCharListOad = mOadService.getCharacteristics();
for (BluetoothGattCharacteristic characteristic : mCharListOad){
if(characteristic !=null){
if(characteristic.getUuid().toString().equals(IMG_IDENTIFY_UUID)){
Log.d(LOG_TAG,"switch image - write characteristic");
characteristic.setValue(data);
mCharIdentify = characteristic;
characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
boolean sent = gatt.writeCharacteristic(characteristic);
if(sent){
Log.d(LOG_TAG, "switch image - characteristic written");
}else{
Log.d(LOG_TAG, "switch image - failed to write characteristic");
}
}
}
}
}
I used semaphore but seems not to work:
Semaphore sem ;
sem = new Semaphore(1,true);
try {
sem.acquire();
checkSoftware();
}catch (final InterruptedException ex){
ex.printStackTrace();
Log.d(LOG_TAG,ex.toString());
}