1
votes

I am developing an iOS app that communicates with a WT11i by bluegiga via Bluetooth 4.0
The iOS is running as the central while WT11i is acting as the peripheral.

The characteristics of the service broadcast by WT11i is Write/Notify

After my app is connected to the peripheral (WT11i), the peripheral will continuously send the value of a counter to my app.

NSLog:

2013-09-18 14:22:58.843 IOS_Central[412:907] Receive -> 1
2013-09-18 14:22:58.904 IOS_Central[412:907] Receive -> 2
2013-09-18 14:22:58.963 IOS_Central[412:907] Receive -> 3
2013-09-18 14:22:59.023 IOS_Central[412:907] Receive -> 4

Code:

- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
    receivingThings =true;
    if (error) {
        NSLog(@"Error discovering characteristics: %@", [error localizedDescription]);
        return;
    }
    NSString *newString = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];
    NSLog(@"Receive -> %@",newString);
}

I can send a short message from my app to my peripheral using

-(void) sendToPeripheral:(NSString*)message{
    if(self.ConnectionState == CONNECTIONSTATE_NOTCONNECTED){
        return;
    }
   NSData *mainData1= [message dataUsingEncoding:NSUTF8StringEncoding];
    [self.connectedPeripheral writeValue:mainData1 forCharacteristic:self.connectedCharacteristic type:CBCharacteristicWriteWithResponse];
    NSLog(@"Send Message");
}

Result of the writeValue: to Peripheral will be reflected here

- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
    if(error == nil){
        NSLog(@"Sent successfully");
    }
    else{
        NSLog(@"Send Fail");
    }
}

When my peripheral received the message, it will acknowledge by sending an acknowledgement "Message Received" back to my app.

Most of the time the communication is fine. However once in a while my app will miss an update right after I sent a message to the peripheral.

2013-09-18 14:22:58.843 IOS_Central[412:907] Receive -> 1
2013-09-18 14:22:58.904 IOS_Central[412:907] Receive -> 2
2013-09-18 14:22:58.963 IOS_Central[412:907] Receive -> 3
2013-09-18 14:22:59.023 IOS_Central[412:907] Receive -> 4
2013-09-18 14:22:59.050 IOS_Central[412:907] Send Message
2013-09-18 14:22:59.083 IOS_Central[412:907] Receive -> 5
2013-09-18 14:22:59.113 IOS_Central[412:907] Sent successfully
2013-09-18 14:22:59.143 IOS_Central[412:907] Receive -> 7
2013-09-18 14:22:59.203 IOS_Central[412:907] Receive -> 8
2013-09-18 14:22:59.263 IOS_Central[412:907] Receive -> Message Received
2013-09-18 14:22:59.322 IOS_Central[412:907] Receive -> 9

Notice I did not receive number "6" from my peripheral? I have checked the logs on the peripheral, it says that it has sent out "6" to my app. My biggest suspicion lies in the iOS corebluetooth, where writeValue: clashes with didUpdateValueForCharacteristic:, therefore causing my "6" to be lost.

Has anyone encountered this problem before too? Any suggestion?

1

1 Answers

1
votes

It can be the connection interval issue. You can send some amount data in single connection interval. Once received successful acknowledgment from Peripherals, send next some data. By doing this, there will be less chance of missing packets.

Please refer this: Connection Interval Core Bluetooth