首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS蓝牙低功耗不检测外设

iOS蓝牙低功耗不检测外设
EN

Stack Overflow用户
提问于 2014-06-10 05:22:09
回答 1查看 488关注 0票数 0

我的应用程序无法检测外围设备。我正在使用浅蓝色来模拟蓝牙低能耗外设,而我的应用程序就是感觉不到它。我甚至在两个设备上安装了浅蓝色,以确保它能够正确地生成外围信号。有什么建议吗?我的标签正在更新,NSLog显示扫描正在开始。

提前谢谢。

代码语言:javascript
复制
#import <UIKit/UIKit.h>

        #import <CoreBluetooth/CoreBluetooth.h>







        @interface ViewController : UIViewController

        @property (weak, nonatomic) IBOutlet UITextField *navDestination;




        @end



        #import "ViewController.h"





        @implementation ViewController

        - (IBAction)connect:(id)sender {


        }

        - (IBAction)navDestination:(id)sender {

            NSString *destinationText  = self.navDestination.text;

         }


        - (void)viewDidLoad {

        }



         - (void)viewWillDisappear:(BOOL)animated {

             [super viewWillDisappear:animated];



        }



        - (void)didReceiveMemoryWarning {

            [super didReceiveMemoryWarning];

            // Dispose of any resources that can be recreated.

        }
        @end



        #import <UIKit/UIKit.h>
        #import "ViewController.h"
        @interface BlueToothViewController : UIViewController

        @property (strong, nonatomic) CBCentralManager *centralManager;

        @property (strong, nonatomic) CBPeripheral *discoveredPerepheral;

        @property (strong, nonatomic) NSMutableData *data;

        @property (strong, nonatomic) IBOutlet UITextView *textview;
        @property (weak, nonatomic) IBOutlet UILabel *charLabel;

        @property (weak, nonatomic) IBOutlet UILabel *isConnected;

        @property (weak, nonatomic) IBOutlet UILabel *myPeripherals;

        @property (weak, nonatomic) IBOutlet UILabel *aLabel;



        - (void)centralManagerDidUpdateState:(CBCentralManager *)central;

        - (void)centralManger:(CBCentralManager *)central didDiscoverPeripheral:     (CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI;

-(void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error;

-(void)cleanup;

-(void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral;



-(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error;

-(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error;

-(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error;

-(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error;

-(void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error;





@end




@interface BlueToothViewController ()

@end

@implementation BlueToothViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad {

    _centralManager = [[CBCentralManager alloc]initWithDelegate:self queue:nil options:nil];

    _data = [[NSMutableData alloc]init];



}



- (void)viewWillDisappear:(BOOL)animated {

    [super viewWillDisappear:animated];

    [_centralManager stopScan];

}



- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

- (void)centralManagerDidUpdateState:(CBCentralManager *)central {

    //you should test all scenarios

    if (central.state == CBCentralManagerStateUnknown) {

        self.aLabel.text = @"I dont do anything because my state is unknown.";

        return;

    }

    if (central.state == CBCentralManagerStatePoweredOn) {

        //scan for devices

        [_centralManager scanForPeripheralsWithServices:nil options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];

        NSLog(@"Scanning Started");

    }

    if (central.state == CBCentralManagerStateResetting) {

        self.aLabel.text = @"I dont do anything because my state is resetting.";

        return;

    }

    if (central.state == CBCentralManagerStateUnsupported) {

        self.aLabel.text = @"I dont do anything because my state is unsupported.";

        return;

    }

    if (central.state == CBCentralManagerStateUnauthorized) {

        self.aLabel.text = @"I dont do anything because my state is unauthorized.";

        return;

    }

    if (central.state == CBCentralManagerStatePoweredOff) {

        self.aLabel.text = @"I dont do anything because my state is powered off.";

        return;

    }









}

- (void)centralManger:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {



    NSLog(@"Discovered %@ at %@", peripheral.name, RSSI);

    self.myPeripherals.text = [NSString stringWithFormat:@"%@%@",peripheral.name, RSSI];

    if (_discoveredPerepheral != peripheral) {

        //save a copy of the peripheral

        _discoveredPerepheral = peripheral;

        //and connect

        NSLog(@"Connecting to peripheral %@", peripheral);

        [_centralManager connectPeripheral:peripheral options:nil];

        self.aLabel.text = [NSString stringWithFormat:@"%@", peripheral];

    }

}

-(void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {



    NSLog(@"Failed to connect");

    [self cleanup];

}

-(void)cleanup {

    //see if we are subscribed to a characteristic on the peripheral

    if (_discoveredPerepheral.services != nil) {

        for (CBService *service in _discoveredPerepheral.services) {

            if (service.characteristics != nil) {

                for (CBCharacteristic *characteristic in service.characteristics) {

                    if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"508EFF8E-F541-57EF-BD82-B0B4EC504CA9"]]) {

                        if (characteristic.isNotifying) {

                            [_discoveredPerepheral setNotifyValue:NO forCharacteristic:characteristic];

                            return;

                        }

                    }

                }

            }

        }

    }

    [_centralManager cancelPeripheralConnection:_discoveredPerepheral];

}

-(void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {

    NSLog(@"Connected");



    [_centralManager stopScan];

    NSLog(@"Scanning stopped");



    self.isConnected.text = [NSString stringWithFormat:@"Connected"];



    [_data setLength:0];



    peripheral.delegate = self;



    [peripheral discoverServices:nil];

}

-(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {

    if (error) { [self cleanup];

        return;

    }

    for (CBService *service in peripheral.services) {

        [peripheral discoverCharacteristics:nil forService:service];

    }

    //discover other characteristics

}

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

    return;

}

    for (CBCharacteristic *characteristic in service.characteristics) {



            [peripheral setNotifyValue:YES forCharacteristic:characteristic];



    }

}

-(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {



    if (error) { NSLog(@"Error");

        return;

    }

    NSString *stringFromData = [[NSString alloc]initWithData:characteristic.value encoding:NSUTF8StringEncoding];

    self.charLabel.text = [NSString stringWithFormat:@"%@", stringFromData];

    //Have we got everything we need?



    if ([stringFromData isEqualToString:@"EOM"]) {

        [_textview setText:[[NSString alloc]initWithData:self.data encoding:NSUTF8StringEncoding]];



        [peripheral setNotifyValue:NO forCharacteristic:characteristic];



        [_centralManager cancelPeripheralConnection:peripheral];



    }





}

-(void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {



    if ([characteristic.UUID isEqual:nil]) {

        return;

    }

    if (characteristic.isNotifying) {

        NSLog(@"Notification began on %@", characteristic);

    }

    else {

        [_centralManager cancelPeripheralConnection:peripheral];

    }

}

-(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {

    _discoveredPerepheral = nil;



    self.isConnected.text = [NSString stringWithFormat:@"Connecting..."];



    [_centralManager scanForPeripheralsWithServices:nil options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES}];



}



@end
EN

回答 1

Stack Overflow用户

发布于 2015-02-04 12:39:35

看起来你并没有声明你实现了CBCentralManagerDelegate协议。更改您的BlueToothViewController接口以添加CBCentralManagerDelegate。

代码语言:javascript
复制
@interface BlueToothViewController () <CBCentralManagerDelegate>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24129297

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档