在iPhone设备之间通过蓝牙发送和接收文本

时间:2018-11-30 11:27:09

标签: ios objective-c core-bluetooth

我正在开发一个应用程序,希望在其中两个iPhone设备之间共享数据。我能够从设备A发送数据并在设备B中检索到数据。现在,我想从设备B发送数据,并将其检索到设备A。我已使用以下代码传输数据。

*警报中收到的显示数据

- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
    if (error) {
        NSLog(@"Error discovering characteristics: %@", [error localizedDescription]);
        return;
    }

    NSString *stringFromData = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];

    // Have we got everything we need?
    if ([stringFromData isEqualToString:@"EOM"]) {

        // We have, so show the data,
        [self.textview setText:[[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding]];

        // Cancel our subscription to the characteristic
        [peripheral setNotifyValue:NO forCharacteristic:characteristic];

        // and disconnect from the peripehral
        [self.centralManager cancelPeripheralConnection:peripheral];
    }
    else
    {
        // Otherwise, just add the data on to what we already have
        [[[UIAlertView alloc]initWithTitle:stringFromData message:@"" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
    }

    [self.data appendData:characteristic.value];

    // Log it
    NSLog(@"Received: %@", stringFromData);
}

*发送数据的方法

- (void)sendData
{
    // First up, check if we're meant to be sending an EOM
    static BOOL sendingEOM = NO;

    if (sendingEOM) {

        // send it
        BOOL didSend = [self.peripheralManager updateValue:[@"EOM" dataUsingEncoding:NSUTF8StringEncoding] forCharacteristic:self.transferCharacteristic onSubscribedCentrals:nil];

        // Did it send?
        if (didSend) {

            // It did, so mark it as sent
            sendingEOM = NO;

            NSLog(@"Sent: EOM");
        }

        // It didn't send, so we'll exit and wait for peripheralManagerIsReadyToUpdateSubscribers to call sendData again
        return;
    }

    // We're not sending an EOM, so we're sending data

    // Is there any left to send?

    if (self.sendDataIndex >= self.dataToSend.length) {

        // No data left.  Do nothing
        return;
    }

    // There's data left, so send until the callback fails, or we're done.

    BOOL didSend = YES;

    while (didSend) {

        // Make the next chunk

        // Work out how big it should be
        NSInteger amountToSend = self.dataToSend.length - self.sendDataIndex;

        // Can't be longer than 20 bytes
        if (amountToSend > NOTIFY_MTU) amountToSend = NOTIFY_MTU;

        // Copy out the data we want
        NSData *chunk = [NSData dataWithBytes:self.dataToSend.bytes+self.sendDataIndex length:amountToSend];

        // Send it
        didSend = [self.peripheralManager updateValue:chunk forCharacteristic:self.transferCharacteristic onSubscribedCentrals:nil];

        // If it didn't work, drop out and wait for the callback
        if (!didSend) {
            return;
        }

        NSString *stringFromData = [[NSString alloc] initWithData:chunk encoding:NSUTF8StringEncoding];
        NSLog(@"Sent: %@", stringFromData);

        // It did send, so update our index
        self.sendDataIndex += amountToSend;

        // Was it the last one?
        if (self.sendDataIndex >= self.dataToSend.length) {

            // It was - send an EOM

            // Set this so if the send fails, we'll send it next time
            sendingEOM = YES;

            // Send it
            BOOL eomSent = [self.peripheralManager updateValue:[@"EOM" dataUsingEncoding:NSUTF8StringEncoding] forCharacteristic:self.transferCharacteristic onSubscribedCentrals:nil];

            if (eomSent) {
                // It sent, we're all done
                sendingEOM = NO;

                NSLog(@"Sent: EOM");
            }

            return;
        }
    }
}

谢谢

0 个答案:

没有答案