Android Bluetooth: Slow data rates calculated from

2020-07-30 02:23发布

问题:

Using: HTC Legend and HTC Salsa

I'm calculating the speed using:

while(true)
        {
            try 
            {
                int num = in.read(buffer);
                if(reading == false)
                {
                    prevTime = SystemClock.uptimeMillis();
                    reading = true;
                }
                else
                {
                    //Calculate KB/s
                    count += num;
                    Long deltaTime = SystemClock.uptimeMillis()- prevTime;
                    if(deltaTime >= 1000)
                    {
                        Float speed =  (float)count/deltaTime;
                        Log.d(TAG,"Data: " + speed + "KB/s");
                        count = 0;
                        prevTime = SystemClock.uptimeMillis();
                    }
                }

            } catch (IOException e) {
            }
        }

And writing some test data using

out.writeUTF("ababababababababababababababababbabababaababababababababababababababababbabababaababababababababababababababababbabababa" +
                        "ababababababababababababababababbabababaababababababababababababababababbabababaababababababababababababababababbabababa" +
                        "ababababababababababababababababbabababaababababababababababababababababbabababaababababababababababababababababbabababa" +
                        "ababababababababababababababababbabababaababababababababababababababababbabababaababababababababababababababababbabababa");
out.flush();

The writes are within another threads while(true) also.

I'm getting the following results.

02-13 18:17:16.897: D/krazyTag(3432): Data: 31.554672KB/s
02-13 18:17:17.927: D/krazyTag(3432): Data: 29.854227KB/s
02-13 18:17:18.977: D/krazyTag(3432): Data: 29.285034KB/s 
02-13 18:17:20.067: D/krazyTag(3432): Data: 38.446888KB/s 
02-13 18:17:21.097: D/krazyTag(3432): Data: 35.89484KB/s 
02-13 18:17:22.127: D/krazyTag(3432): Data: 33.67118KB/s 
02-13 18:17:23.227: D/krazyTag(3432): Data: 33.512726KB/s
02-13 18:17:24.307: D/krazyTag(3432): Data: 33.277622KB/s

Which is confusing me since the phones specs state they use Bluetooth® 2.1 with EDR

Which is capable of 260KB/S but I'm not even getting the old standard 90KB/s

I'm not sure if it's my stream and read/write calls (I'm using a buffered datainputstream) Or if I'm calculating things wrong or have the wrong information?

回答1:

I think the speed depends on your implementation of the Send and Receive threads, since you connects 2 Android devices with your own applications. Could you post your implementation?

I got the same problem also.
I am using ACER TAB A500 to communicate with a Bluetooth stick connected to PC and I got even slower result 12,3KB/s for sending data only.

That's why I did some experiments. I sent a message for 10000times and I got that the data rate depends on the length of the message.

For 1KB message, the data rate is 232KB/s.
For 40Byte message, the data rate is 18KB/s.
For 1Byte message, the data rate is 0.48KB/s.

Here is my code:

// Get the BluetoothDevice object.
while(true){
    driverBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    driverBluetoothDevice = driverBluetoothAdapter.getRemoteDevice("XX:XX:XX:XX:XX:XX");
    if (driverBluetoothDevice == null){
    break;
    }

    Method insecureMethod = driverBluetoothDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[] { int.class });
    byte portNumber = 5; // The SPP in port 5.
    driverBluetoothSocket = (BluetoothSocket) insecureMethod.invoke(driverBluetoothDevice, portNumber);

// Try to connect to the Bluetooth device.
try {
    driverBluetoothSocket.connect();
} catch (IOException e1) {
    // Failed to connect to the device
        break;
}

    // Open input and output stream.
try {
    driverInputStream = driverBluetoothSocket.getInputStream();
} catch (IOException e) {
    break;
}
try {
    driverOutputStream = driverBluetoothSocket.getOutputStream();
} catch (IOException e) {
    break;
}

byte[] message = new byte[3000];
Random randomGenerator = new Random();
for (int i = 0; i < message.length; i++){
    message[i] = (byte) randomGenerator.nextInt(100); 
}

Date TimeValue = new Date();
long TimeStamp1 = TimeValue.getTime();
for (int i = 0; i < 10000; i++){
    try {
        driverOutputStream.write(message, 0, message.length);
    } catch (IOException e) {
        break;
    }
    }

TimeValue = new Date();
long TimeStamp2 = TimeValue.getTime();
long TimeDifference = TimeStamp2 - TimeStamp1;
TimeDifference = 0;
    break;
}


回答2:

Not sure if this helps with your speed problem and I could have overlooked this detail in your code snippet but are you reading and writing on the same thread? The documentation suggests you don't,

First and foremost, you should use a dedicated thread for all stream reading and writing. This is important because both read(byte[]) and write(byte[]) methods are blocking calls.

Bluetooth Android Developer