Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS:使用NSTimer写入蓝牙LE设备_Ios_Bluetooth Lowenergy_Nstimer - Fatal编程技术网

iOS:使用NSTimer写入蓝牙LE设备

iOS:使用NSTimer写入蓝牙LE设备,ios,bluetooth-lowenergy,nstimer,Ios,Bluetooth Lowenergy,Nstimer,我正在尝试向我的蓝牙LE设备写入一个字符,到目前为止,它已经工作了,但只有在我在DidDiscoveryCharacteristicsForService方法中这样做的时候 - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error { if (error) { [self cle

我正在尝试向我的蓝牙LE设备写入一个字符,到目前为止,它已经工作了,但只有在我在DidDiscoveryCharacteristicsForService方法中这样做的时候

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
    if (error) {
        [self cleanup];
        return;
    }

    for (CBCharacteristic *characteristic in service.characteristics) {
        if ([characteristic.UUID isEqual: CHAR_UUID]) {
            NSLog(@"Discovered characteristic: %@", characteristic);
            [peripheral setNotifyValue:YES forCharacteristic:characteristic];
            _discoveredCharacteristic = characteristic;

            char c;
            count++;
            if (count%2 == 1)
            {
                c = 0x0;
            }
            else
            {
                c = 0x7;
            }

            NSData *data = [NSData dataWithBytes: &c length: 1];
            [_discoveredPeripheral writeValue:data forCharacteristic:_discoveredCharacteristic
                                         type:CBCharacteristicWriteWithResponse];

            NSLog(@"Writing value %@ for characteristic %@", data, _discoveredCharacteristic.UUID);
        }
    }

timer = [NSTimer scheduledTimerWithTimeInterval:2
                                                 target:self selector:@selector(targetMethod:)
                                               userInfo:nil repeats:YES];
        _repeatingTimer = timer;
    }
但当我将writeValue部分移动到由计时器触发的方法中时, 它不再将值写入我的设备。为什么会这样?我做错了什么

- (void)targetMethod:(NSTimer*)theTimer{
    char c;
    count++;
    if (count%2 == 1)
    {
        c = 0x0;
    }
    else
    {
        c = 0x7;
    }

    NSData *data = [NSData dataWithBytes: &c length: 1];
    [_discoveredPeripheral writeValue:data forCharacteristic:_discoveredCharacteristic
                                 type:CBCharacteristicWriteWithResponse];

    NSLog(@"Writing value %@ for characteristic %@", data, _discoveredCharacteristic.UUID);
}

我对iOS和蓝牙都很陌生。

我当时是个哑巴,在写东西后关闭了与外设的连接。

你在哪里设置NSTimer?我在保存了对DidDiscoveryCharacteristicsForService方法中的特征的引用后立即进行了设置。计时器似乎工作了,并且运行了目标方法。您是否已通过目标方法并确认没有任何东西是
nil
?是的,并且我的外设和特征似乎都是正确的。但我找到了解决办法。我没有意识到代码在写入特征后调用了一个方法,这关闭了与我的外围设备的连接。这只是说明我对这些东西是多么的陌生,但谢谢你试着帮助我:)