蓝牙txt文件字节增加?
我能够将文件从一台移动设备转移到另一台移动设备。当发送者发送这个8字节的文本文件时,接收端将变成一个256字节的txt文件,当我打开txt文件的内容时,有我的信息和很多方框。这是发件人的代码:
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 知道我该如何解决它?
i am able to xfer files from 1 mobile device to another. 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看来 256 字节是蓝牙会话中的最小数据包大小。由于您的文件大小小于 256 字节,因此负载会填充一些特殊字符。尝试读取 256 字节数组直到 EOF (^z) 字符并仅获取直到 EOF 的字节并将其保存到磁盘。您需要丢弃有效负载。
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.
因此,当您向代码示例添加一些调试时,在 Read 调用后添加
readBytes
转储,您会看到什么? 8然后单独0?如果您的文件使用什么语言?例如西方语言还是东方语言?
文件前后的内容是什么?添加了哪些字节以及在哪里添加?
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?
无论如何,我使用以下方法解决了错误:
string fileName = @"SendTest.txt";
字符串地址=“0025677FB346”;
Uri uri = new Uri("obex://" + adr + "/" + System.IO.Path.GetFileName(fileName));
ObexWebRequest 请求 = new ObexWebRequest(uri);
请求.ReadFile(文件名);
ObexWebResponse 响应 = (ObexWebResponse)request.GetResponse();
MessageBox.Show(response.StatusCode.ToString());
响应.关闭();
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();