修改Apple的EADemo项目以显示字节

时间:2012-05-09 18:32:18

标签: ios bluetooth external-accessory mfi

我试图了解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字符或字符串。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

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

以下是一个例子:

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