Click here to Skip to main content
15,880,543 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
Java
private void checkSoftware(){
        bleLoader.switchImg();

    }


Method used to prepare Bluetooth device before update/upload .bin file:
Java
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");
                    }
                }
            }
        }
    }

Java
//As a result of the previous method BLE device takes a while then disconnect and make a connection again.
//I can't check if the BLE/device is connected right after as it's still connected for a while as i said

I used semaphore but seems not to work:
Java
Semaphore sem ;
        sem = new Semaphore(1,true); //using 0 stops execution
        try {
            sem.acquire();
            checkSoftware();

//            try{
//                //update GUI here
//                //lbl_progressDescription.setText("working on it...");
//            } finally {
//                sem.release();
//            }
        }catch (final InterruptedException ex){
            ex.printStackTrace();
            Log.d(LOG_TAG,ex.toString());
        }
Posted
Comments
David Crow 3-Jun-18 18:45pm    
When your Bluetooth device is fully connected, you should receive a BluetoothDevice.ACTION_ACL_CONNECTED notification in your broadcast receiver's onReceive() method. At that point, you can start the file transfer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900