Android蓝牙:从BluetoothSocket计算的慢数据速率

时间:2012-02-13 08:08:17

标签: android sockets bluetooth rate

使用: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();

写入在另一个线程内,而(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

由于手机规格声明他们使用Bluetooth® 2.1 with EDR

,这让我感到困惑

哪个是capable of 260KB/S,但我甚至没有得到旧的标准90KB / s

我不确定这是我的流和读/写调用(我正在使用缓冲的datainputstream) 或者,如果我计算错误或有错误的信息?

2 个答案:

答案 0 :(得分:3)

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

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

这就是我做一些实验的原因。我发了一条消息10000次,我得知数据速率取决于消息的长度。

  

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

这是我的代码:

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

答案 1 :(得分:0)

不确定这是否会对您的速度问题有所帮助,我可能忽略了您的代码段中的这些细节,但是您是否在同一个线程上进行读写?文档建议你不要,

  

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

Bluetooth Android Developer