繁体   English   中英

iOS蓝牙低功耗无法检测外围设备

[英]iOS bluetooth low energy not detecting peripherals

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

提前致谢。

#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

似乎您没有在声明您实现了CBCentralManagerDelegate协议。 更改您的BlueToothViewController接口以添加CBCentralManagerDelegate。

@interface BlueToothViewController () <CBCentralManagerDelegate>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM