1

我正在使用 CoreBlueTooth API 将数据写入我们从某个硬件制造公司收到的外围设备中。根据规范,他们为我们提供了一系列特征 UUID 以将数据写入其中。一旦我们想要完成,我们需要在其中一个特征中写入 0。现在,问题是当我尝试发送字符串/整数并将它们转换为 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];
        }
    }
}
4

3 回答 3

2

Here is the fix, It works for me

//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;
}

Thanks to http://coding-snippet.blogspot.in/2013/05/blog-post.html

于 2013-12-10T14:48:20.833 回答
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'

于 2013-10-26T11:14:39.990 回答
0

只需使用 https://github.com/LGBluetooth/LGBluetooth它会让您的工作轻松 100 倍

于 2014-02-15T10:15:57.763 回答