简体   繁体   English

蓝牙插座图片传输距离的Android到Windows

[英]Picture transfer on Bluetooth sockets from Android to Windows

I can't find a solution for this problem: I made a program on android to shot a picture (jpg format) and send it over the Bluetooth channel to an Embedded Windows XP pc. 我找不到解决此问题的方法:我在android上制作了一个程序来拍摄图片(jpg格式),然后通过蓝牙通道将其发送到Embedded Windows XP pc。 I also made the C++ application for Windows to receive the picture. 我还制作了Windows的C ++应用程序来接收图片。
The communication is implemented using Bluetooth sockets. 使用蓝牙插座实现通信。 In Android the app reads the file in chunks of 1024 bytes and write every chunk on the socket until the picture is finished. 在Android中,应用程序以1024字节的块读取文件,并将每个块写入套接字,直到图片完成。 On Windows side I read the bytes in chunck of 1024 bytes until the recv(...) function returns a value > 0. 在Windows端,我读取1024字节的块中的字节,直到recv(...)函数返回值> 0。

The result is that on Android side it seems all the bytes are correctly read and sent, but on the Windows side I (almost) always receive only 70-80% of the bytes. 结果是,在Android端,似乎所有字节均已正确读取和发送,但在Windows端,我(几乎)始终只接收70-80%的字节。 I can even see the correct bytes received, as it is possible to visualize the jpg (missing bytes on the bottom part of the image are shown in grey). 我什至可以看到收到的正确字节,因为可以可视化jpg(图像底部缺少的字节以灰色显示)。 Only rarely I can receive the entire picture. 我很少能收到整个图片。 Here I post the relevant part of code for the Android side: 在这里,我发布了Android方面的代码的相关部分:

String picturePath = "/sdcard/VERDE/PTR-0_15-31-24.jpg";
File picture = new File(picturePath);
if (picture.exists())
{
    Log.d(TAG, "File " + picture.getAbsolutePath() + " exists!");
    try
    {
        FileInputStream fis = new FileInputStream(picture);
        ostream = socket.getOutputStream();
        byte[] buf = new byte[1024];
        int read = 0;
        int totBytes = 0;
        while ((read = fis.read(buf)) != -1)
        {
            totBytes = totBytes + read;
            ostream.write(buf, 0, read);
            Log.d(TAG, "Image - Read: " + read + " - Total "
                                + totBytes + " bytes!");
        }
        ostream.flush();
        ostream.close();
        fis.close();
    } catch (UnknownHostException ex)
    {
        System.out.println(ex.getMessage());
    } catch (IOException ex)
    {
        System.out.println(ex.getMessage());
    }
} else
{
    Log.d(TAG, "File " + picture.getAbsolutePath()
            + " does not exist!");
}

This is the relevant part of code of the C++ application: 这是C ++应用程序代码的相关部分:

if (_outFile != NULL) 
{
    _outFile.open(result.c_str(), ofstream::binary); //"pic.jpg"
    cout << "File opened!" << endl;
} else 
{
    cout << "Can't open file!" << endl;
}
while ((received = recv(client, buf, sizeof(buf), 0)) > 0) 
{
    cout << "R:" << received << " ";
    if (received > 0) 
    {
        totalBytes += received;
        if (_outFile.is_open()) 
        {
            _outFile.write(buf, received); 
            cout << " (Total: " << totalBytes << " B)" << endl;
        } else
        cout << "Error in recv() function, received bytes = "
                        << received << endl;

    } else 
        cout << "R:" << received << " ";
}

I've seen tens of blogs and posts on this topic but noone seems to have a similar problem! 我已经看过数十篇有关此主题的博客和帖子,但似乎没有人遇到类似的问题! Please, help me if you have any idea! 请帮忙,如果您有任何想法!

Thanks! 谢谢!

I have exactly the same problem (but instead of bloutoth with normal sockets). 我有完全一样的问题(但不是普通套接字造成的破坏)。 Did you already find anything useful how to fix this? 您是否已经找到有用的解决方法? I tried everything and still get the same error. 我尝试了所有操作,但仍然收到相同的错误。

Else I think I found out what the problem is, but I don't know to to solve this. 否则,我想我找出了问题所在,但我不知道要解决这个问题。 In the last step, when there are <1024 Bytes transferred, I assigned buffer to a string, and then cout the length of the string, which was smaller than the bytes received in the last step. 在最后一步中,当传输的字节数小于1024时,我将缓冲区分配给一个字符串,然后确定该字符串的长度,该长度小于在最后一步中接收到的字节。 I think somehow he is not receiving everything in the last step, when the data <1024 Bytes 我认为当数据<1024字节时,他不知何故没有在最后一步收到所有信息

I found the problem in my code! 我在代码中发现了问题!
I thought it was on the receiveing side and indeed it was on the transmitting Android code. 我认为它是在接收方,的确是在发送Android代码。
The problem was that the socket.close() function was called before all the bytes were actually transferred! 问题是在实际传输所有字节之前调用了socket.close()函数!
I realized that trying first with a Thread.CurrentThread().sleep(2000) before closing the socket, but this is not optimized. 我意识到在关闭套接字之前先尝试使用Thread.CurrentThread()。sleep(2000),但这并没有进行优化。
The best way, imho, is to implement a small protocol of control data exchange between client and server in order to ensure the picture is completely transferred (here a pseudocode example): 最好的方法,恕我直言,是在客户端和服务器之间实现控制数据交换的小型协议,以确保图片完全传输(此处为伪代码示例):

  1. client authenticate with "clientName:pictureName:picLengthInBytes" 客户端使用“ clientName:pictureName:picLengthInBytes”进行身份验证
  2. server response: "AuthenticationOK" 服务器响应:“ AuthenticationOK”
  3. client send picture bytes 客户端发送图片字节
  4. server check: 服务器检查:

    if (received bytes == picLengthInBytes) if(接收的字节== picLengthInBytes)

    reply("pictureOK") 回复(“ pictureOK”)

  5. perform other client operations if needed (in my case -> delete picture) 根据需要执行其他客户端操作(在我的情况下->删除图片)
and then close the socket only when closing the application or stopping the thread. 然后仅在关闭应用程序或停止线程时关闭套接字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM