【问题标题】:Modifying Apple's EADemo project to display bytes修改 Apple 的 EADemo 项目以显示字节
【发布时间】:2012-05-09 18:32:17
【问题描述】:

我正在尝试了解 EADemo 的工作原理以及外部附件框架的工作原理。 EADemo 可在此处获得:

http://developer.apple.com/library/ios/#samplecode/EADemo/Introduction/Intro.html

我要做的就是修改 Apple 的 EADemo 项目以显示它接收到的字节(假设它们是 ASCII 类型的字符),而不是仅仅计算它接收到的字节数。所以我将 EASessionTransferViewController.m...从:

- (void)_sessionDataReceived:(NSNotification *)notification
{
    EADSessionController *sessionController = (EADSessionController *)[notification object];
    uint32_t bytesAvailable = 0;

    while ((bytesAvailable = [sessionController readBytesAvailable]) > 0) {
        NSData *data = [sessionController readData:bytesAvailable];
        if (data) {
            _totalBytesRead += bytesAvailable;
        }
    }

    [_receivedBytesLabel setText:[NSString stringWithFormat:@"Bytes Received from Session: %d", _totalBytesRead]];
}

@end

到...

- (void)_sessionDataReceived:(NSNotification *)notification
{
    EADSessionController *sessionController = (EADSessionController *)[notification object];
    uint32_t bytesAvailable = 0;

    while ((bytesAvailable = [sessionController readBytesAvailable]) > 0) {
        NSData *data = [sessionController readData:bytesAvailable];
        if (data) {
            _totalBytesRead += bytesAvailable;
            NSString *asciiStringFromData = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
        }
    }
    [_receivedBytesLabel setText:[NSString stringWithFormat:@"ASCII bytes read: %@", asciiStringFromData]];
}

@end

但这根本不起作用。上次我试了,什么都没有。它连接到一个蓝牙板,它只是回显它收到的 ASCII 字符或字符串。

有人可以帮忙吗?

【问题讨论】:

  • 您确认收到回复了吗?当你说“它什么都不显示”时,你的意思是它显示了“ASCII bytes read:”而没有其他任何东西,还是你的意思是它甚至没有显示?
  • 它显示“ASCII bytes read:”,但它什么也没显示。 Board 肯定可以正常工作,因为它与显示回声的 android 蓝牙程序一起使用。
  • 它适用于 Android 的事实毫无意义。董事会需要做几件事来与 iPhone 通信,而它不需要与 Android 做。原始程序在修改之前是否正确报告正在读取字节?
  • 布拉德,是的,它符合 MFi 标准。这是 Roving Networks RN-41-APL。
  • 也许我遗漏了一些东西,但是你的asciiStringFromData 没有在你的while 循环之外声明是有原因的吗?因为您是在 while 循环中声明它,但访问它以写入循环外的显示。我认为您的应用程序甚至不会像这样编译。

标签: ios bluetooth external-accessory mfi


【解决方案1】:

您应该尝试使用NSUTF8StringEncoding 编码而不是NSASCIIStringEncoding

这是一个例子:

NSString* asciiStringFromData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多