Home Articles

无法将数据写入硬件外设

Asked
Viewed 785 times
1

我正在使用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 Answers

  • 2

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

  • 1

    这不是您在此处遇到的基于Core Bluetooth的问题 .

    对于调试,您可以使用

    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' 混合 .

  • 0

    这是修复,它适用于我

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

Related