代码之家  ›  专栏  ›  技术社区  ›  Shah Paneri

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

  •  1
  • Shah Paneri  · 技术社区  · 6 年前

    *警报中接收的显示数据

    - (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 回复  |  直到 6 年前