Android Bluetooth: Slow data rates calculated from BluetoothSocket

送分小仙女□ 提交于 2019-12-04 20:59:48

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;
}

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!