-2

我正在尝试通过 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];
4

1 回答 1

0

The simplest way to checkout LGBluetooth -> https://github.com/SocialObjects-Software/LGBluetooth

After that, use this code (All stuff will be handled by LGBluetooth, even connection)

LGUtils writeData:self.transactData
                    charactUUID:FCBBLEPaymentTransactionCharUUID
                    serviceUUID:FCBBLEPaymentServiceUUID
                     peripheral:aPeripheral
                     completion:^(NSError *error)
              {
              }];

Here is a sample code, that will convert you NSString to equivalent NSData

         NSString *someString = @"123456789";
         const char *transactStr = [someString UTF8String];
         int8_t *transactBytes = malloc(strlen(transactStr) * sizeof(int8_t));

         size_t transactDataSize = (strlen(transactStr) * sizeof(int8_t)) / 2;

         int curssorOffset = 0;
         for (int i = 0; i < transactDataSize; i++) {
             transactBytes[i] = ((transactStr[curssorOffset] - '0') * 16) + (transactStr[curssorOffset + 1] - '0');
             curssorOffset += 2;
         }
         NSData *transactData = [NSData dataWithBytes:transactBytes
                                               length:transactDataSize];
         free(transactBytes);
         [LGUtils writeData:transactData
                charactUUID:@"5ec0"
                serviceUUID:@"4ca1"
                 peripheral:aPeripheral
                 completion:^(NSError *error)
          {
              aCallback(error);
              [aPeripheral disconnectWithCompletion:nil];
          }];
于 2014-07-31T15:04:06.527 回答