I'm trying to send a typedef object through Core Bluetooth that another device will have access to and be able to use. @implementation NeeAppDelegate typedef struct { float a, b, c,
char Name[20];
} MyObject;
So here I declared my typedef class that I want to send through Core Bluetooth which I think is okay. Then in a later method:
-(void) method
}
//declare the typedef object
MyObject myObject;
//cast it into a Byte Array
_byteArray = (Byte *) &object;
//Set all bins in the array to 0
memset(&object, 0, sizeof(MyObject));
//Assign the values to myObject
myObject.a = 1;
myObject.b = 2 ;
myObject.c = 3;
//This value could be changed that's why I have the array set to 20
NSString * name = @"Name";
strcpy(myObject.Name, [name UTF8String]);
_byteData = [NSData alloc] initWithBytes:_byteArray length: sizeof(MyObject)];
}
Now I switch classes to a peripheral ViewController where I handle the data sending.
- (void) peripheralManager : (CBPeripheralManager *) peripheral
central : (CBCentral *) central
didSubscribeToCharacteristic : (CBCharacteristic *) characteristic
{
if (m_dataToSend != nil)
{
[m_dataToSend release];
}
m_dataToSend = [NeeAppDelegate sharedInstance].byteData;
m_sendDataIndex = 0;
[self sendData];
}
Here's the dataSend method I used in order to send the data to another device
- (void) sendData
{
if (m_sendDataIndex >= m_dataToSend.length) // Is there any left to send?
{
// No data left. send EOD
[m_peripheralManager updateValue:[NSData data]
forCharacteristic:m_transferCharacteristic
onSubscribedCentrals:nil];
[m_peripheralManager stopAdvertising];
//Resume the beacon advertising
[_viewControllerNeedle.needlePeripheralManager startAdvertising:_viewControllerNeedle.beaconSpecificPeripheralData];
}
else
{
BOOL bDidSend = YES;
NSInteger amountToSend = 0;
// There's data left, so send until the callback fails, or we're done.
while (bDidSend)
{
amountToSend = m_dataToSend.length - m_sendDataIndex;
if (amountToSend > NOTIFY_MTU)
{
amountToSend = NOTIFY_MTU;
}
// Copy out the data we want
bDidSend = [m_peripheralManager updateValue:[NSData dataWithBytes:m_dataToSend.bytes + m_sendDataIndex
length:amountToSend]
forCharacteristic:m_transferCharacteristic
onSubscribedCentrals:nil];
if (bDidSend)
{
m_sendDataIndex += amountToSend;
}
}
}
}
Now in my CentralManager class How would I manage to get access to the data within the byteArray? I know the format to get a value from a byte array you need to use
*(float*)&[NeeAppDelegate sharedInstance].byteArray[0];