扫描蓝牙设备时未使用NSTimer更新iOS背景视图

Raju yourPepe

我正在尝试获取蓝牙扫描页面以使用蓝牙配对BLE设备。我发现NSUInteger[ _ble.scannedPeripheral count ]在扫描时确实发生了变化。但是,在执行时,背景视图图像和页面甚至无法更改。如果显示可用BLE设备的变量从0更改为1,2或3,您能否告诉我其他方法来更改页面?

以下是我的代码:(仅相关)

- (void)viewDidAppear:(BOOL)animated
{
    if (_ble)
    {
        _ble.delegate = (id) self;
        _ble.btStatus = BT_IDLE;
        [_ble startScanning];
    }

    [NSTimer scheduledTimerWithTimeInterval:0.2f  target:self selector:@selector(reloadData) userInfo:nil repeats:YES];

}


-(void) reloadData {


    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // time consuming workout
            dispatch_async(dispatch_get_main_queue(), ^{
            // UI update workout for bluetooth scanning

            if( [ _ble.scannedPeripheral count ] > 0 ){
                [self stopAnimatingImages];
                [self setTapDemo :  [UIImage imageNamed:@"pairing_d.png"]  : @"Pairing"  : @"#C4CCCF"] ;
            }else{
                [self setTapDemo : [self loadingImage] : @"Pairing"  : @"#C4CCCF"] ;
                [self animateImages];
            }
        });
    });
}




- (void) setTapDemo: (UIImage *) cover  : (NSString *) title : (NSString *) colorHex{

    image = [UIImage imageNamed:@"shaded_cal.png"];
    imageA = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    _container = [[UIView alloc] initWithFrame:[self.view bounds]];
    [imageA setImage:cover];
    imageA.userInteractionEnabled = YES;
    UITapGestureRecognizer *myGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(touchesBegan:)];
    myGesture.numberOfTapsRequired = 1;
    myGesture.delegate=self;
    [imageA addGestureRecognizer:myGesture];
    [imageA setContentMode:UIViewContentModeScaleAspectFill];

    myLabelQ = [self constructLabelT:title:0.27:0.08: colorHex:25];
    myLabelBack =[self constructLabelT:@"BACK":0.04:0.01:@"#C4CCCF":18] ;
    if( bleCount > 0){

         for(NSUInteger  i = 0 ; i < [ _ble.scannedPeripheral count ] ; i ++){
            DevicePeriperal *device;
            NSString *uuid = [_ble.scannedPeripheralKey objectAtIndex:i];
             NSLog (@"device uuid = %@",  uuid);
             if (uuid)
             {
                 device = [_ble.scannedPeripheral objectForKey:uuid];
                 NSData * ssx = device.advertdata ;
                 device.rowIndex = i;
                 NSLog (@"device advert = %@",  ssx);
                 if([ssx length] > 0){
                     NSData *macD = [ssx subdataWithRange:NSMakeRange(0, 6)];
                     NSData *pairD = [ssx subdataWithRange:NSMakeRange(6, 1)];
                     NSString* newStr =  [self hexRepresentationWithSpaces:pairD : NO];
                     NSString* newMAC =  [self hexRepresentationWithSpaces:macD : YES];
                     NSLog (@"newStr = %@", newStr );
                     NSLog (@"newMAC = %@", newMAC );
                     _checkSumByte = [self calculateChecksum:newMAC];
                 }

                 NSLog (@"device = %@", device.uuid);
                 if (device )
                 {
                     UIImage *dImage = [UIImage imageNamed:@"device_u.png"];
                     float change =  0.15*i;
                     float yPosition = 0.25 + change ;
                     [imageA addSubview:[self deviceGet:dImage:device.deviceName: 0.40 : yPosition : @"#C4CCCF"]];
                 }
             }

         }
         //UIImage *dImage = [UIImage imageNamed:@"device_u.png"];
         //[imageA addSubview:[self deviceGet:dImage:@"x": 0.40 : 0.25 : @"#C4CCCF"]];
         //[imageA addSubview:[self deviceGet:dImage:@"x": 0.40 : 0.40 : @"#C4CCCF"]];
         //[imageA addSubview:[self deviceGet:dImage:@"x": 0.40 : 0.55 : @"#C4CCCF"]];
         //[imageA addSubview:myLabelS3];

         myLabelS1 = [self constructLabelT:@"SPOTTED":0.27:0.723: colorHex:25];
         myLabelS2 =[self constructLabelT:@"(choose the one you want to connect)":0.55:0.76:@"#C4CCCF":10] ;
         myLabelS3 = [self constructLabelT:@"devices":0.30:0.76: colorHex:25];

     }else{

         myLabelS1 = [self constructLabelT:@"SCANNING":0.27:0.723: colorHex:25];
         myLabelS2 =[self constructLabelT:@"devices":0.51:0.76:@"#C4CCCF":25] ;
         myLabelS3 = [self constructLabelT:@"for":0.30:0.76: colorHex:25];
     }

    [imageA addSubview:myLabelQ];
    [imageA addSubview:myLabelBack];

    [imageA addSubview:myLabelS1];
    [imageA addSubview:myLabelS2];

    [imageA addSubview:myLabelS3];
    [_container addSubview:imageA];

    [self.view addSubview:_container];
    [self.view sendSubviewToBack:_container];

}
射手

每次调用时setTapDemo :::,您都会创建一个新_container视图并将其添加到中self.view因为您永远不会在重新初始化之前从超级视图中删除旧视图,所以self.view通过计时器重复包含越来越多的子视图,这些子视图最终将消耗所有内存,然后您的应用程序将崩溃。

此外,[self.view sendSubviewToBack:_container]每次在计时器触发时都会被调用,并且您永远不会删除旧的_container,因此任何新的内容_container都会被隐藏在后面。

总之,我想您确实在更改时创建了update _container[ _ble.scannedPeripheral count ]但它仍落后于其他子视图。因此,您可以尝试修改如下代码:

- (void) setTapDemo: (UIImage *) cover  : (NSString *) title : (NSString *) colorHex{

    // remove any old _container view
    if (_container) [_container removeFromSuperView];    

    image = [UIImage imageNamed:@"shaded_cal.png"];
    imageA = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    _container = [[UIView alloc] initWithFrame:[self.view bounds]];
    [imageA setImage:cover];
    imageA.userInteractionEnabled = YES;
    UITapGestureRecognizer *myGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(touchesBegan:)];
    myGesture.numberOfTapsRequired = 1;
    myGesture.delegate=self;
    [imageA addGestureRecognizer:myGesture];
    [imageA setContentMode:UIViewContentModeScaleAspectFill];

    myLabelQ = [self constructLabelT:title:0.27:0.08: colorHex:25];
    myLabelBack =[self constructLabelT:@"BACK":0.04:0.01:@"#C4CCCF":18] ;
    if( bleCount > 0){

         for(NSUInteger  i = 0 ; i < [ _ble.scannedPeripheral count ] ; i ++){
            DevicePeriperal *device;
            NSString *uuid = [_ble.scannedPeripheralKey objectAtIndex:i];
             NSLog (@"device uuid = %@",  uuid);
             if (uuid)
             {
                 device = [_ble.scannedPeripheral objectForKey:uuid];
                 NSData * ssx = device.advertdata ;
                 device.rowIndex = i;
                 NSLog (@"device advert = %@",  ssx);
                 if([ssx length] > 0){
                     NSData *macD = [ssx subdataWithRange:NSMakeRange(0, 6)];
                     NSData *pairD = [ssx subdataWithRange:NSMakeRange(6, 1)];
                     NSString* newStr =  [self hexRepresentationWithSpaces:pairD : NO];
                     NSString* newMAC =  [self hexRepresentationWithSpaces:macD : YES];
                     NSLog (@"newStr = %@", newStr );
                     NSLog (@"newMAC = %@", newMAC );
                     _checkSumByte = [self calculateChecksum:newMAC];
                 }

                 NSLog (@"device = %@", device.uuid);
                 if (device )
                 {
                     UIImage *dImage = [UIImage imageNamed:@"device_u.png"];
                     float change =  0.15*i;
                     float yPosition = 0.25 + change ;
                     [imageA addSubview:[self deviceGet:dImage:device.deviceName: 0.40 : yPosition : @"#C4CCCF"]];
                 }
             }

         }
         //UIImage *dImage = [UIImage imageNamed:@"device_u.png"];
         //[imageA addSubview:[self deviceGet:dImage:@"x": 0.40 : 0.25 : @"#C4CCCF"]];
         //[imageA addSubview:[self deviceGet:dImage:@"x": 0.40 : 0.40 : @"#C4CCCF"]];
         //[imageA addSubview:[self deviceGet:dImage:@"x": 0.40 : 0.55 : @"#C4CCCF"]];
         //[imageA addSubview:myLabelS3];

         myLabelS1 = [self constructLabelT:@"SPOTTED":0.27:0.723: colorHex:25];
         myLabelS2 =[self constructLabelT:@"(choose the one you want to connect)":0.55:0.76:@"#C4CCCF":10] ;
         myLabelS3 = [self constructLabelT:@"devices":0.30:0.76: colorHex:25];

     }else{

         myLabelS1 = [self constructLabelT:@"SCANNING":0.27:0.723: colorHex:25];
         myLabelS2 =[self constructLabelT:@"devices":0.51:0.76:@"#C4CCCF":25] ;
         myLabelS3 = [self constructLabelT:@"for":0.30:0.76: colorHex:25];
     }

    [imageA addSubview:myLabelQ];
    [imageA addSubview:myLabelBack];

    [imageA addSubview:myLabelS1];
    [imageA addSubview:myLabelS2];

    [imageA addSubview:myLabelS3];
    [_container addSubview:imageA];

    //[self.view addSubview:_container];
    //[self.view sendSubviewToBack:_container];
    // One line of code can do this trick

    [self.view insertSubview:_container atIndex:0];
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

蓝牙关闭时,iBeacon可以扫描iOS设备吗?

来自分类Dev

蓝牙LE扫描未检测到设备

来自分类Dev

扫描蓝牙设备时出现错误

来自分类Dev

蓝牙扫描设备

来自分类Dev

CoreBLuetooth限制ios无法扫描所有蓝牙设备?

来自分类Dev

iOS Core蓝牙扫描问题,找不到目标BT设备

来自分类Dev

使用NSTimer的iOS位置更新

来自分类Dev

在iOS设备和蓝牙设备之间发送数据时,是否始终需要使用蓝牙配置文件?

来自分类Dev

iOS:使用NSTimer写入Bluetooth LE设备

来自分类Dev

使用MIT App Inventor 2扫描蓝牙设备

来自分类Dev

在 Qt 中扫描蓝牙设备

来自分类Dev

在 iOS 中使用 Autolayout 更改设备旋转时的视图方向

来自分类Dev

iOS设备与蓝牙设备的通信

来自分类Dev

在服务中使用$ http时,视图未更新

来自分类Dev

在附近有蓝牙设备时唤醒ios应用

来自分类Dev

BlueZ / Pybluez-连接到外围设备时进行蓝牙LE扫描

来自分类Dev

在配对或连接蓝牙扫描仪(Inateck)和移动设备时,活动被破坏

来自分类Dev

蓝牙设备名称未显示

来自分类Dev

我的容器视图下的视图未显示在 iOS 设备上

来自分类Dev

iOS-蓝牙到串行设备-可以使用吗?

来自分类Dev

视图加载时启动NSTimer

来自分类Dev

iOS设备与启用了蓝牙的设备的通信

来自分类Dev

角度,更改模型时视图未更新

来自分类Dev

骨干视图在辅助提取时未更新

来自分类Dev

SwiftUI视图在更改@ObservedObject时未更新

来自分类Dev

角度,更改模型时视图未更新

来自分类Dev

iOS Google Maps sdk-使用子视图而不是主视图时未指向标记

来自分类Dev

iOS扫描经典蓝牙-获取地址-IBeacons

来自分类Dev

iOS扫描经典蓝牙-获取地址-IBeacons

Related 相关文章

热门标签

归档