无法将数据写入硬件外设

时间:2013-10-25 18:30:37

标签: ios objective-c cocoa-touch core-bluetooth cbperipheral

我正在使用CoreBlueTooth API将数据写入我们从某个硬件制造公司收到的外设。根据规范,他们为我们提供了一系列特征UUID来写入数据。一旦我们想要完成,我们需要在其中一个特征中写入0。现在,问题是,当我尝试发送String / Integer并将它们转换为NSData时,它无法正常工作。我想我需要在那些可写特性中发送字节流。有人可以帮助我,我怎么能转换我的NSString& NSNumber在发送之前将数据编入字节流。以下是我尝试过的转换代码:

- (void)writeCharactersticData:(NSDictionary *)iData toPeripheral:(CBPeripheral *)iPeripheral {
    NSArray *charactersticsIDs = [NSArray arrayWithArray:iData.allKeys];
    self.writeCharactersticsCount  = charactersticsIDs.count;

    for (CBUUID *uuid in charactersticsIDs) {
        if (self.peripheralCharacterstics[uuid]) {



            NSData *payload;

            if ([iData[uuid] isKindOfClass:[NSNumber class]]) {
                NSInteger data = ((NSNumber *)iData[uuid]).integerValue;
//                int integerSize = sizeof(data);
//                
//                uint8_t bytes[integerSize];
//                
//                
//                NSLog(@"Integer data = %d", data);
//                
//                int8_t tx = (int8_t)data;
//                bytes[0] = tx;
//                payload = [NSData dataWithBytes:bytes length:sizeof(data)];

                payload = [NSData dataWithBytes:&data length:sizeof(data)];
            } else if ([iData[uuid] isKindOfClass:[NSString class]]) {

                int stringSize = sizeof(iData[uuid]);
                uint8_t bytes[stringSize];
                NSScanner *scanner = [NSScanner scannerWithString:iData[uuid]];

                for (int i=0; i<stringSize; i++) {
                    unsigned int value;
                    [scanner scanHexInt:&value];
                    bytes[i] = (uint8_t)value;
                }

                payload = [NSData dataWithBytes:bytes length:stringSize];
//                payload = [iData[uuid] dataUsingEncoding:NSUTF8StringEncoding];
            }

            [self.discoveredPeripheral writeValue:payload forCharacteristic:self.peripheralCharacterstics[uuid] type:CBCharacteristicWriteWithResponse];
        }
    }
}

3 个答案:

答案 0 :(得分:2)

这是修复,它适用于我

//Integer to NSData
+(NSData *) IntToNSData:(NSInteger)data
{
    Byte *byteData = (Byte*)malloc(4);
    byteData[3] = data & 0xff;
    byteData[2] = (data & 0xff00) >> 8;
    byteData[1] = (data & 0xff0000) >> 16;
    byteData[0] = (data & 0xff000000) >> 24;
    NSData * result = [NSData dataWithBytes:byteData length:4];
    NSLog(@"result=%@",result);
    return (NSData*)result;
}

//NSData to Integer
+(NSInteger) NSDataToInt:(NSData *)data
{
    unsigned char bytes[4];
    [data getBytes:bytes length:4];
    NSInteger n = (int)bytes[0] << 24;
    n |= (int)bytes[1] << 16;
    n |= (int)bytes[2] << 8;
    n |= (int)bytes[3];
    return n;
}

感谢http://coding-snippet.blogspot.in/2013/05/blog-post.html

答案 1 :(得分:1)

这不是您在此处遇到的基于核心蓝牙的问题。

对于调试,您可以使用

NSLog(@"%@", payload);

对于你的字符串到NSData的转换,你的方法看起来很复杂。我建议像

那样简单
NSData* payload = [iData[uuid] dataUsingEncoding:NSUTF8StringEncoding];
if (payload.length > 20)
{
    // handle error. most LE peripherals don't support longer values.
}

另一个错误可能是您在写入值时将ASCII 0 '0'与二进制零'\0'混合。

答案 2 :(得分:0)

只需使用 https://github.com/LGBluetooth/LGBluetooth这将使您的工作变得更容易