Objective-C:打包文件内容以进行蓝牙传输的最佳方式?

发布于 2024-10-21 13:07:15 字数 821 浏览 1 评论 0原文

在我的程序中,我通过 RFCOMM 连接与配对的蓝牙设备发送文件。我在接收端将接收到的字节写入文件时遇到问题。然而,接收端可能不是问题的根源。这可能是我最初在 Objective-C 中加载和发送数据的方式。因此,我的问题是,我通过 RFCOMM 连接以字节形式加载和传输文件数据的方法是否可以接受。我试图避免发布大量代码,因为它真的会得到在路上。 我只是想知道是否可以像传输一样将文件数据加载到数组中,或者是否有更好的方法来打包数据?

提前致谢。

涉及的数据类型
IOBluetoothRFCOMM通道通道
NSData myData

加载文件代码片段

// Load file data into byte array.
myData = [NSData dataWithContentsOfFile: filePath];
int bytes[(int)fileSize];
[myData getBytes:bytes length:sizeof(int) * fileSize];

传输文件数据代码片段

// Loops create an int[] chunk array that holds an amount of data from bytes[].
// These chunks (packets) are sent using...
[channel writeSync: chunk length: 1000]; // Sends the packet.

In my program I am sending a file through an RFCOMM connection with a paired bluetooth device. I am having a problem with the receiving end writing the received bytes to a file. However, the receiving end may not be the root of the problem. It may be the way I am loading and sending the data intially in Objective-C. So, my question here is whether or not my method of loading and transmitting the file's data as bytes across an RFCOMM connection is acceptable. I am trying to avoid posting a lot of code because it will truly just get in the way. I would just like to know if it is ok or not to load file data into an array as I have for transferring, or if there is a better way to package the data?

Thanks in advance.

Data Types Involved
IOBluetoothRFCOMMChannel channel
NSData myData

Load File Code Snippet

// Load file data into byte array.
myData = [NSData dataWithContentsOfFile: filePath];
int bytes[(int)fileSize];
[myData getBytes:bytes length:sizeof(int) * fileSize];

Transmit File Data Code Snippet

// Loops create an int[] chunk array that holds an amount of data from bytes[].
// These chunks (packets) are sent using...
[channel writeSync: chunk length: 1000]; // Sends the packet.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情栀口红 2024-10-28 13:07:15

您需要使用Archives或Serialization方法将字节流转换为与体系结构无关的字节流,然后通过RFCOMM通道发送,并在接收端将其转换回来
请参阅此处

You need to use the Archives or Serialization methods to convert the bytes streams into an architecture independent bytestream and then send over the RFCOMM channel, and at the receiving end convert it back
See Here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文