我正在尝试通过 Core Bluetooth 发送一个 typedef 对象,另一个设备将可以访问并能够使用该对象。@implementation NeeAppDelegate typedef struct { float a, b, c,
char Name[20];
} MyObject;
所以在这里我声明了我想通过核心蓝牙发送的 typedef 类,我认为这是可以的。然后在后面的方法中:
-(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)];
}
现在我将类切换到我处理数据发送的外围 ViewController。
- (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];
}
这是我用来将数据发送到另一台设备的 dataSend 方法
- (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;
}
}
}
}
现在在我的 CentralManager 类中,我将如何设法访问 byteArray 中的数据?我知道从您需要使用的字节数组中获取值的格式
*(float*)&[NeeAppDelegate sharedInstance].byteArray[0];