蓝牙特性设置为零

时间:2015-09-30 00:27:27

标签: ios objective-c iphone macos bluetooth

我正在运行一个应用程序,通过蓝牙从我的Mac与我的iPhone进行通信,反之亦然。它完全正常工作(有两个生成的特性,一个从Mac到iPhone,一个从iPhone到Mac)除了,偶尔我的特性从mac到iPhone没有更新,我认为这是由于mac上的charachteristic被设置没有明显原因,没有。

所以要在我的Mac上更新我的字符:

- (void)updateValueForCharacteristic:(int)sendID {
    NSLog(@"sent %d", sendID);

    if (self.characteristic != nil) {
        dispatch_block_t block = ^(void) {
            NSData *value = [NSData dataWithBytes:&sendID length:sizeof(sendID)];
            [_managerout updateValue:value forCharacteristic:self.characteristic onSubscribedCentrals:nil];
        };

        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_async(queue, block);
    }else{
        //why is this sometimes being called?!
    }
}

并在iPhone上接收我:

- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
    if (error != nil) {
        NSLog(@"Error updating value: %@", error.localizedDescription);

        return;
    }

    CBUUID *characteristicUUID = [CBUUID UUIDWithString:_kCharacteristicUUIDin];
    NSInteger rec = 0;
    [characteristic.value getBytes:&rec length:sizeof(rec)];
    NSLog(@"recieved %ld", (long)rec);

    if ([characteristic.UUID isEqual:characteristicUUID]) {
        NSLog(@"officially recieved %ld", (long)rec);
        //handle char (sometimes not noticing)
    }
}

无论如何,我可以验证我的iPhone已经收到了char。我也应该废弃这种技术,尝试编写(而不是更新)字符,或尝试使用多个字符而不仅仅是一个字符?

我真的需要确保iPhone得到它,否则程序就没用了。

更新

我已将else块添加到if (self.characteristic != nil) {,当我的字符未被更新时,正在调用else块!但我无法找到为什么自我的特征被设定为零?!而且我应该如何重新创建特征呢?

project

0 个答案:

没有答案
相关问题