0

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

有人可以帮忙吗?

4

1 回答 1

0

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

这是一个例子:

NSString* asciiStringFromData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
于 2015-06-21T21:16:56.210 回答