Android的蓝牙:从的BluetoothSocket计算缓慢的数据传输速率 [英] Android Bluetooth: Slow data rates calculated from BluetoothSocket
问题描述
应用:HTC联想和HTC莎莎
我使用的计算速度:
,而(真)
{
尝试
{
INT NUM = in.read(缓冲区);
如果(阅读==假)
{
prevTime = SystemClock.uptimeMillis();
读= TRUE;
}
其他
{
//计算KB /秒
数+ = NUM;
龙deltaTime = SystemClock.uptimeMillis() - prevTime;
如果(deltaTime> = 1000)
{
浮动速度=(浮点)数/ deltaTime;
Log.d(TAG,数据:+速度+KB /秒);
计数= 0;
prevTime = SystemClock.uptimeMillis();
}
}
}赶上(IOException异常E){
}
}
和使用编写一些测试数据。
<$p$p><$c$c>out.writeUTF("ababababababababababababababababbabababaababababababababababababababababbabababaababababababababababababababababbabababa" + "ababababababababababababababababbabababaababababababababababababababababbabababaababababababababababababababababbabababa" + "ababababababababababababababababbabababaababababababababababababababababbabababaababababababababababababababababbabababa" + "ababababababababababababababababbabababaababababababababababababababababbabababaababababababababababababababababbabababa"); 了out.flush();
的写操作是另外一个线程内,而(真)也。
我得到以下结果。
02-13 18:17:16.897:D / krazyTag(3432):数据:31.554672KB /秒
02-13 18:17:17.927:D / krazyTag(3432):数据:29.854227KB /秒
02-13 18:17:18.977:D / krazyTag(3432):数据:29.285034KB /秒
02-13 18:17:20.067:D / krazyTag(3432):数据:38.446888KB /秒
02-13 18:17:21.097:D / krazyTag(3432):数据:35.89484KB /秒
02-13 18:17:22.127:D / krazyTag(3432):数据:33.67118KB /秒
02-13 18:17:23.227:D / krazyTag(3432):数据:33.512726KB /秒
02-13 18:17:24.307:D / krazyTag(3432):数据:33.277622KB /秒
这是自手机功能状态混淆我,他们使用蓝牙2.1和EDR
这是能够260KB / S ,但我还没有得到旧标准90KB / S
我不知道这是否是我流和读/写调用(我使用一个缓冲的DataInputStream) 或者,如果我计算的东西错了或者有错误的信息?
我觉得速度取决于具体的实现发送和接收线程,因为你连你自己的应用程序2 Android设备。你能将你的实现?_爱
我得到了同样的问题也。
我使用的ACER TAB A500与蓝牙棒连接到PC进行沟通,我得到了更慢的结果12,3KB /秒仅用于发送数据。
这就是为什么我做了一些实验。我发了消息10000次,我得到的数据速率取决于消息的长度。
有关1KB消息时,数据速率为232KB / s的。照片对于40Byte消息,该 数据速率为18KB / s的。照片对于1字节信息时,所述数据速率是 0.48KB / s的
下面是我的code:
//获取BluetoothDevice类对象。
而(真){
driverBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
driverBluetoothDevice = driverBluetoothAdapter.getRemoteDevice(XX:XX:XX:XX:XX:XX);
如果(driverBluetoothDevice == NULL){
打破;
}
方法insecureMethod = driverBluetoothDevice.getClass()实现getMethod(createInsecureRfcommSocket,新的等级[] {} int.class)。
字节端口号= 5; //最高人民检察院在港口5。
driverBluetoothSocket =(的BluetoothSocket)insecureMethod.invoke(driverBluetoothDevice,端口号);
//尝试连接到蓝牙设备。
尝试 {
driverBluetoothSocket.connect();
}赶上(IOException异常E1){
//无法连接到该设备
打破;
}
//打开输入和输出流。
尝试 {
driverInputStream = driverBluetoothSocket.getInputStream();
}赶上(IOException异常E){
打破;
}
尝试 {
driverOutputStream = driverBluetoothSocket.getOutputStream();
}赶上(IOException异常E){
打破;
}
byte []的消息=新的字节[3000];
随机randomGenerator =新的随机();
的for(int i = 0; I&LT; message.length;我++){
消息[I] =(字节)randomGenerator.nextInt(100);
}
日期TIMEVALUE =新的日期();
长timestamp1与= TimeValue.getTime();
的for(int i = 0; I&LT; 10000;我++){
尝试 {
driverOutputStream.write(消息,0,message.length);
}赶上(IOException异常E){
打破;
}
}
TIMEVALUE =新的日期();
长期与timestamp2 = TimeValue.getTime();
长TimeDifference =与timestamp2 - timestamp1与;
TimeDifference = 0;
打破;
}
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?
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;
}
这篇关于Android的蓝牙:从的BluetoothSocket计算缓慢的数据传输速率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!