开发者

Bluetooth txt file byte increases?

开发者 https://www.devze.com 2023-01-01 12:23 出处:网络
i am able to xfer files from 1 mobile device to anoth开发者_Python百科er. When the sender sends this text file of 8 bytes, the receiver end will become a 256bytes txt file and when i open the contents

i am able to xfer files from 1 mobile device to anoth开发者_Python百科er. When the sender sends this text file of 8 bytes, the receiver end will become a 256bytes txt file and when i open the contents of the txt file, there are my infos plus alot of square boxes. Here is my code from the sender:

            string fileName = @"SendTest.txt";
            System.Uri uri = new Uri("obex://" + selectedAddr + "/" + System.IO.Path.GetFileName(fileName)); 
            ObexWebRequest request = new ObexWebRequest(uri);

            Stream requestStream = request.GetRequestStream(); 
            FileStream fs = File.OpenRead(fileName);

            byte[] buffer = new byte[1024]; 
            int readBytes = 1;

            while (readBytes != 0) 
            {
            readBytes = fs.Read(buffer,0, buffer.Length);
            requestStream.Write(buffer,0, readBytes);
            }

            requestStream.Close();
            ObexWebResponse response = (ObexWebResponse)request.GetResponse();
            MessageBox.Show(response.StatusCode.ToString());
            response.Close();

Any1 knws how do i solve it?


It seems 256 bytes is the minimum packet size in bluetooth session. Since your file size is smaller than 256 bytes the payload is filled in with some special character. Try to read the 256 byte array until EOF (^z) character and take bytes till EOF only and save to disk. You need to discard the payload.


So when you add some debugging to your code sample, adding a dump of readBytes after the Read call what do you see? 8 then 0 alone?

What language if your file in? A western language or something eastern for instance?

What are the contents of the file before and after? What bytes are being added and where?


Anyways, i solved error using:

string fileName = @"SendTest.txt";

String adr = "0025677FB346";

Uri uri = new Uri("obex://" + adr + "/" + System.IO.Path.GetFileName(fileName));

ObexWebRequest request = new ObexWebRequest(uri);

request.ReadFile(fileName);

ObexWebResponse response = (ObexWebResponse)request.GetResponse();

MessageBox.Show(response.StatusCode.ToString());

response.Close();

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号