Click here to Skip to main content
15,879,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
{
....
//Finding the write characteristic
//uint8_t val = 1;
//NSData* valData = [NSData dataWithBytes:(void*)&val length:sizeof(val)];
NSData* valData = @"1301040238faba000bca3c";
[peripheral writeValue:valData forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
.....
}

- (void) peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
    NSLog(@"Did write characteristic value : %@ with ID %@", characteristic.value, characteristic.UUID);
    NSLog(@"With error: %@", [error localizedDescription]);
    
}


My problem is i have value in hexadecimal like "130104 0238faba 000bca3c"
i need to send this value to valData
Posted

You have created the data as a string, not a Hex value, so it is not valid. You should create it as a byte array, something like:
C++
// I am not familiar with Objective-C but it should be
// similar to the following:

uint8_t valData[] = { 0x13, 0x01, 0x04, 0x02, 0x38, 0xfa, 0xba, 0x00, 0x0b, 0xca, 0x3c;
 
Share this answer
 
I have solved my problem. This is the sample solution.

NSMutableData *concatenate = [NSMutableData data];
uint16_t first=0x1cbd;
uint8_t second = 0x01;
uint8_t third = 0xfa;
uint8_t fourth=0xea
                
NSData* one = [NSData dataWithBytes:(void*)&first length:sizeof(first)];
NSData* two = [NSData dataWithBytes:(void*)&second length:sizeof(second)];
NSData* three = [NSData dataWithBytes:(void*)&third length:sizeof(third)];
NSData* four = [NSData dataWithBytes:(void*)&fourth length:sizeof(fourth)];
                
[concatenate appendData:one];
[concatenate appendData:two];
[concatenate appendData:three];
[concatenate appendData:four];
              
[peripheral writeValue:concatenate forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900