1

使用:HTC Legend 和 HTC Salsa

我正在使用以下方法计算速度:

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) {
            }
        }

并使用编写一些测试数据

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

写入也在另一个线程中,while(true) 也是。

我得到以下结果。

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

这让我很困惑,因为手机规格说明他们使用蓝牙® 2.1 和 EDR

能够达到 260KB/S,但我什至没有达到旧标准的 90KB/s

我不确定这是我的流和读/写调用(我正在使用缓冲的数据输入流)还是我计算错误或信息错误?

4

2 回答 2

3

我认为速度取决于您对发送和接收线程的实现,因为您将 2 个 Android 设备与您自己的应用程序连接起来。你能发布你的实现吗?

我也遇到了同样的问题。
我正在使用 ACER TAB A500 与连接到 PC 的蓝牙棒进行通信,但仅发送数据时我得到的结果甚至更慢 12.3KB/s。

这就是为什么我做了一些实验。我发送了 10000 次消息,我发现数据速率取决于消息的长度。

对于 1KB 消息,数据速率为 232KB/s。
对于 40Byte 消息,数据速率为 18KB/s。
对于 1Byte 消息,数据速率为 0.48KB/s。

这是我的代码:

// 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;
}
于 2012-02-13T15:28:11.403 回答
0

不确定这是否有助于解决您的速度问题,我可能在您的代码片段中忽略了这个细节,但是您是否在同一个线程上阅读和写作?文档建议您不要,

首先,您应该使用专用线程进行所有流读取和写入。这很重要,因为 read(byte[]) 和 write(byte[]) 方法都是阻塞调用。

蓝牙安卓开发者

于 2015-06-21T09:07:12.537 回答