iOS CoreBluetooth 更新值 : and didUpdateValueForCharacteristic:

标签 ios objective-c iphone bluetooth

我正在开发一个 iOS 应用程序,它通过蓝牙 4.0 与 bluegiga 的 WT11i 通信
iOS 作为中心运行,WT11i 作为外设。

WT11i广播服务的特点是Write/Notify

当我的应用程序连接到外围设备(WT11i)后,外围设备会不断地向我的应用程序发送一个计数器的值。

NS日志:

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

代码:

- (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);
}

我可以使用以下方法从我的应用向我的外围设备发送一条短消息

-(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");
}

writeValue: 到 Peripheral 的结果会反射(reflect)在这里

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

当我的外围设备收到消息时,它会通过向我的应用程序发回确认“消息已收到”来确认。

大部分时间沟通都很好。然而,有时我的应用程序会在我向外围设备发送消息后立即错过更新。

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

注意我没有从外围设备收到数字“6”? 我检查了外围设备上的日志,它说它已经向我的应用程序发送了“6”。我最大的怀疑在于 iOS corebluetooth,其中 writeValue: 与 didUpdateValueForCharacteristic: 冲突,因此导致我的“6”丢失。

有人遇到过这个问题吗?有什么建议吗?

最佳答案

可能是连接间隔问题。您可以在单个连接间隔内发送一些数据。一旦收到来自外围设备的成功确认,接下来发送一些数据。这样做可以减少丢失数据包的可能性。

请引用: Connection Interval Core Bluetooth

关于iOS CoreBluetooth 更新值 : and didUpdateValueForCharacteristic:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18866082/

相关文章:

ios - 使用长按来切换

ios - 使用 iOS SDK 将照片发布到 Facebook 粉丝专页

ios - 主线程在 viewDidLoad 中的并发队列上执行 dispatch_async,或者在方法内执行事务

ios - 你如何从 UIViewController 访问 scrollViewDidScroll

ios - 从 iPhone 相机 (AVCaptureSession) 捕获 24 bpp 位图中的图像

iphone - 自定义 NSOperation 中的随机崩溃

iphone - 是否可以更改 PKAddPassesViewController 的标题?

ios - 从过滤数组和主数组中删除相同的对象

iphone - 长时间运行的 REST API

objective-c - iPad - 同一屏幕中有多个 UITableView