如何在iPhone App中通过蓝牙以二进制形式传输数据 [英] How to transfer a data in Binary form via Bluetooth in iPhone App

查看:371
本文介绍了如何在iPhone App中通过蓝牙以二进制形式传输数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为iOS和Mac开发设备驱动程序软件。在这里,我想通过blutoogh将二进制形式的数字数据从我的iPhone应用程序传输到设备。假设我想转移数据,如2013年1月20日上午10:30,我需要以二进制顺序转移200120131030。意味着第一个二进制的20然后是二进制的01然后二进制的2013等等...
其他设备没有在iOS上运行。
我将此数字转换为NSData但无法理解NSData是否为abinary数据。有没有办法让它二进制并转移它。 (我可以通过蓝牙传输数据)

I am developing a device driver software for iOS and mac. In this I want to transfer a Number data in Binary form from my iPhone App to a device via blutoogh. Suppose I want to transfer a Data like 20 Jan 2013 10:30 am the i need to transfer 200120131030 in a binary sequence. means first binary of 20 then binary of 01 then binary of 2013 and so on... other device is not running on iOS. I convert this number to NSData but can't understand that NSData is abinary data or not. Is there a way to make it binary and transfer it. (I can transferred data via bluetooth)

推荐答案

假设您决定将日期表示为12位数的字符串,或12字节。您可以这样获得NSData:

Let's say you decide to represent the date as a string of 12 digits, or 12 bytes. You can get NSData this way:

NSDate *date = // the date you start with

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"ddMMYYYYHHmm"];
NSString *dateString = [formatter stringFromDate:date];

// dateString can be any string you wish to send.  In this example, it represents a date
NSData *data = [dateString dataUsingEncoding:NSUTF8StringEncoding];

您可以使用NSData上的字节选择器获取指向字节数据的指针。假设您要复制数据:

You can get a pointer to the byte data with the bytes selector on NSData. Say you want to copy out the data:

NSUInteger length = [data length];
char *buffer = (char *)malloc(length);
memcpy(buffer, [data bytes], length);

正如我在评论中提到的,更紧凑的序列化是一个长整数。你可以得到这样的小数据:

As I mentioned in my comment, a more compact serialization is a long integer. You can get smaller data like this:

unsigned long dateInt = [dateString intValue];
NSData *data = [NSData dataWithBytes:&dateInt length:sizeof(dateInt)];

...然后以相同的方式获取字节数。要记住的重要一点是这些字节的发送者和接收者必须就如何解释它们达成一致。

... then get the bytes out the same way. The important thing to remember is that the sender and receiver of these bytes must agree on how to interpret them.

这篇关于如何在iPhone App中通过蓝牙以二进制形式传输数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆