IOS Core Bluetooth : Writing NSData for Characteristic

﹥>﹥吖頭↗ 提交于 2019-12-21 02:54:26

问题


I am using the following code to write the 0xDE value for a Bluetooth Caracteristic (Reset Device) using the IOS Core Bluetooth :

...
NSData *bytes = [@"0xDE" dataUsingEncoding:NSUTF8StringEncoding];
[peripheral writeValue:bytes
            forCharacteristic:characteristic
            type:CBCharacteristicWriteWithResponse];
...

is there any mistake in my code because the value is not written properly?


回答1:


Try creating your data with an array of single byte values.

const uint8_t bytes[] = {0xDE};
NSData *data = [NSData dataWithBytes:bytes length:sizeof(bytes)];

This is a useful approach for creating arbitrary constant data. For more bytes,

const uint8_t bytes[] = {0x01,0x02,0x03,0x04,0x05};
NSData *data = [NSData dataWithBytes:bytes length:sizeof(bytes)];

If you want to create data to send using variables, I would recommend using NSMutableData and appending the bytes that you need. It isn't very pretty, but it is easy to read / understand, especially when you are matching a packed struct on the embedded side. Example below is from a BLE project where we were making a simple communication protocol.

NSMutableData *data = [[NSMutableData alloc] init];

//pull out each of the fields in order to correctly
//serialize into a correctly ordered byte stream
const uint8_t start     = PKT_START_BYTE;
const uint8_t bitfield  = (uint8_t)self.bitfield;
const uint8_t frame     = (uint8_t)self.frameNumber;
const uint8_t size      = (uint8_t)self.size;

//append the individual bytes to the data chunk
[data appendBytes:&start    length:1];
[data appendBytes:&bitfield length:1];
[data appendBytes:&frame    length:1];
[data appendBytes:&size     length:1];



回答2:


Swift 3.0: In case anyone is wondering the format for Swift is slightly different as writeValue can get the count from the array.

let value: UInt8 = 0xDE
let data = Data(bytes: [value])
peripheral.writeValue(data, for: characteristic, type: .withResponse)



回答3:


This code will fix the problem :

NSData * data = [self  dataWithHexString: @"DE"];
[peripheral writeValue:data forCharacteristic:characteristic                                       
                            type:CBCharacteristicWriteWithResponse];

dataWithHexString implementation :

- (NSData *)dataWithHexString:(NSString *)hexstring
{
    NSMutableData* data = [NSMutableData data];
    int idx;
    for (idx = 0; idx+2 <= hexstring.length; idx+=2) {
        NSRange range = NSMakeRange(idx, 2);
        NSString* hexStr = [hexstring substringWithRange:range];
        NSScanner* scanner = [NSScanner scannerWithString:hexStr];
        unsigned int intValue;
        [scanner scanHexInt:&intValue];
        [data appendBytes:&intValue length:1];
    }
    return data;
}



回答4:


What you are, in fact, doing here is writing the string "0xDE" to the characteristic. If you want to use binary/octal notation, you need to stay away from strings.

int integer = 0xDE;
NSData *data = [[NSData alloc] initWithBytes:&integer length:sizeof(integer)];
[peripheral writeValue:data
     forCharacteristic:characteristic
                  type:CBCharacteristicWriteWithResponse];



回答5:


The answer by bensarz is almost correct. Except one thing: you shouldn't use sizeof(int) as the length for NSData. The size of int is 4 or 8 bytes (depending on the architecture). As you want to send 1 byte, use uint8_t or Byte instead:

uint8_t byteToWrite = 0xDE;
NSData *data = [[NSData alloc] initWithBytes:&byteToWrite length:sizeof(&byteToWrite)];
[peripheral writeValue:data
     forCharacteristic:characteristic
                  type:CBCharacteristicWriteWithResponse];

Of courser you could also use int as the variable's type, but you have to initialize NSData with the length of 1.



来源:https://stackoverflow.com/questions/32906324/ios-core-bluetooth-writing-nsdata-for-characteristic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!