samedi 21 avril 2018

Calculate Upload Speed in Android

I am trying to write a Bluetooth bandwidth checker app.

According to Android Documentation, dOut.size(): Returns the current value of the counter written, the number of bytes written to this data output stream so far.

I want to check how much data has been written to the outputstream every 200ms.

I have tried using dOut.size() to get the current number of bytes being written to the OutputStream but it gives constant values.

Is it possible to check the current number of bytes written to the target every 200ms or in any interval?

public void write(byte[] bytes) {
        previousSize = 0;
        try {
            dOut = new DataOutputStream(bandwidthOutStream);
            if (isFirstTime) {
                isFirstTime = false;
                // Share the sent message with the UI activity.
                Message writtenBWStatus = bandwidthHandler.obtainMessage(
                        Constants.MessageConstants.BW_START_WRITE, counter, -1, bytes);
                writtenBWStatus.sendToTarget();

                // Share the sent message with the UI activity.
                Thread sendMessage = new Thread() {
                    @Override
                    public void run() {
                        t.schedule(new TimerTask() {
                            @Override
                            public void run() {
                                if ((sizeOfDataBeingSent != 0) & (sizeOfDataBeingSent <= Constants.Packet.BW_FILE_SIZE)) {
                                    Log.i(Constants.TAG, "I am repeating BW_WRITE");
                                    amountOfDataSent = sizeOfDataBeingSent - previousSize;
                                    previousSize = sizeOfDataBeingSent;
                                    Log.i(Constants.TAG, "Data now sent:" + amountOfDataSent);
                                    Message writtenMsg = bandwidthHandler.obtainMessage(
                                            Constants.MessageConstants.BW_WRITE, counter, -1, null);
                                    writtenMsg.sendToTarget();
                                } else {
                                    t.cancel();
                                    return;
                                }
                            }
                        }, 0, Constants.Miscellaneous.BW_TIME_INTERVAL);
                    }
                };

                if (!(sendMessage.isAlive())) {
                    sendMessage.start();
                }

            }


                dOut.writeInt(bytes.length); // write length of the message
                dOut.write(bytes);// write the message
                sizeOfDataBeingSent = dOut.size();
                flushOutStream();
            }




        } catch (IOException e) {
            Log.e(Constants.TAG, "Error occurred when sending BW", e);

            // Send a failure message back to the activity.
            Message writeErrorMsg =
                    bandwidthHandler.obtainMessage(Constants.MessageConstants.BW_FAIL_TO_SEND);
            Bundle bundle = new Bundle();
            bundle.putString("status",
                    "Couldn't send data to the other device");
            writeErrorMsg.setData(bundle);
            bandwidthHandler.sendMessage(writeErrorMsg);
        }
    }

Aucun commentaire:

Enregistrer un commentaire