From c84ced3eff3fadb3a3192a0e5a0011856c029a96 Mon Sep 17 00:00:00 2001 From: "matthias.ringwald" Date: Fri, 28 May 2010 21:55:29 +0000 Subject: [PATCH] use fallback to latin1 for invalid UTF8 names --- CocoaTouch/src/BTstackManager.m | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/CocoaTouch/src/BTstackManager.m b/CocoaTouch/src/BTstackManager.m index 6422a2a8c..64eb66175 100644 --- a/CocoaTouch/src/BTstackManager.m +++ b/CocoaTouch/src/BTstackManager.m @@ -449,18 +449,26 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe // get lenght: first null byte or max 248 chars int nameLen = 0; while (nameLen < 248 && packet[9+nameLen]) nameLen++; + // Bluetooth specification mandates UTF-8 encoding... NSString *name = [[NSString alloc] initWithBytes:&packet[9] length:nameLen encoding:NSUTF8StringEncoding]; - device.name = name; - // set in device info - NSString *addrString = [[device addressString] retain]; - NSMutableDictionary * deviceDict = [deviceInfo objectForKey:addrString]; - if (!deviceDict){ - deviceDict = [NSMutableDictionary dictionaryWithCapacity:3]; - [deviceInfo setObject:deviceDict forKey:addrString]; + // but fallback to latin-1 for non-standard products like old Microsoft Wireless Presenter + if (!name){ + name = [[NSString alloc] initWithBytes:&packet[9] length:nameLen encoding:NSISOLatin1StringEncoding]; + } + // check again + if (name){ + device.name = name; + // set in device info + NSString *addrString = [[device addressString] retain]; + NSMutableDictionary * deviceDict = [deviceInfo objectForKey:addrString]; + if (!deviceDict){ + deviceDict = [NSMutableDictionary dictionaryWithCapacity:3]; + [deviceInfo setObject:deviceDict forKey:addrString]; + } + [deviceDict setObject:name forKey:PREFS_REMOTE_NAME]; + [addrString release]; + [self sendDeviceInfo:device]; } - [deviceDict setObject:name forKey:PREFS_REMOTE_NAME]; - [addrString release]; - [self sendDeviceInfo:device]; } discoveryDeviceIndex++; [self discoveryRemoteName];