【问题标题】:Android Bluetooth: Slow data rates calculated from BluetoothSocketAndroid 蓝牙:从 BluetoothSocket 计算的慢速数据速率
【发布时间】:2012-02-13 08:08:17
【问题描述】:

使用: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

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

【问题讨论】:

  • 我认为它是 260Kb/s 而不是 260KB/s b--->bits B---Bytes :)

标签: android sockets bluetooth rate


【解决方案1】:

我认为速度取决于您对发送和接收线程的实现,因为您将 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;
}

【讨论】:

  • 感谢您的回复,我将尝试在一次写入调用中写入更大的数量,看看我得到了什么。顺便说一句,我最初只能获得 2KB/s 没有缓冲的 inputStream 。关于我的蓝牙实现,您特别想看到什么?
  • 好的,所以我尝试像你的那样在 for 循环中发送 3000 个字节,但速度只下降到 18 - 20KB/s。我同时在每部手机上读取和写入这么多字节。如果每个设备都只写或读而不是两者,我应该期待更好的结果吗?
  • 对我来说也是同样的情况。如果我同时写入和读取,写入速度从 12KB/s(只写)降低到 4KB/s。
【解决方案2】:

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

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

Bluetooth Android Developer

【讨论】:

    猜你喜欢
    • 2022-11-02
    • 1970-01-01
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    • 2014-01-24
    • 1970-01-01
    • 2012-02-07
    • 1970-01-01
    相关资源
    最近更新 更多