iOS蓝牙低能量无法检测外围设备

iOS蓝牙低能量无法检测外围设备,ios,bluetooth-lowenergy,core-bluetooth,Ios,Bluetooth Lowenergy,Core Bluetooth,我的应用程序无法检测外围设备。我用浅蓝色来模拟蓝牙低能量外设,我的应用程序根本感觉不到。我甚至在两台设备上安装了浅蓝色,以确保它能够正确地生成外围信号,并且是正确的。有什么建议吗?我的标签正在更新,NSLog显示扫描正在开始 提前谢谢 #import <UIKit/UIKit.h> #import <CoreBluetooth/CoreBluetooth.h> @interface ViewController : UIVi

我的应用程序无法检测外围设备。我用浅蓝色来模拟蓝牙低能量外设,我的应用程序根本感觉不到。我甚至在两台设备上安装了浅蓝色,以确保它能够正确地生成外围信号,并且是正确的。有什么建议吗?我的标签正在更新,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
#导入
#进口
@界面ViewController:UIViewController
@属性(弱、非原子)IBOutlet UITextField*navDestination;
@结束
#导入“ViewController.h”
@实现视图控制器
-(iAction)连接:(id)发送方{
}
-(iAction)导航目的地:(id)发送方{
NSString*destinationText=self.navDestination.text;
}
-(无效)viewDidLoad{
}
-(无效)视图将消失:(BOOL)已设置动画{
[超级视图将消失:动画];
}
-(无效)未收到记忆警告{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
@结束
#进口
#导入“ViewController.h”
@接口BlueToothViewController:UIViewController
@属性(强,非原子)CBCentralManager*centralManager;
@性质(强的,非原子的)是在外周发现的;
@属性(强,非原子)NSMutableData*数据;
@属性(强,非原子)IBOutlet UITextView*textview;
@性质(弱,非原子)IBUILabel*charLabel;
@属性(弱,非原子)IBUILabel*断开连接;
@属性(弱、非原子)IBUILabel*myPeripherals;
@性质(弱、非原子)IBUILabel*aLabel;
-(无效)中央管理者数据状态:(CBCentralManager*)中央;
-(无效)centralManger:(CBCentralManager*)central DidDiscoveryPeripheral:(CBPeripheral*)peripheral广告数据:(NSDictionary*)广告数据RSSI:(NSNumber*)RSSI;
-(无效)中央管理器:(CBCentralManager*)中央DID无法连接外围设备:(CBPeripheral*)外围设备错误:(N错误*)错误;
-(b)清理;
-(无效)中央管理器:(CBCentralManager*)中央外设:(CBPeripheral*)外设;
-(无效)外围设备:(CBPeripheral*)外围设备DID发现服务:(N错误*)错误;
-(void)外围设备:(cbperipal*)外围设备DID发现特性服务:(CBService*)服务错误:(NSError*)错误;
-(无效)中央管理器:(CBCentralManager*)中央数据断开连接外围设备:(CBPeripheral*)外围设备错误:(NSError*)错误;
-(void)外围设备:(CBPeripheral*)外围设备didUpdateValueForCharacteristic:(CBCharacteristic*)特征错误:(NSError*)错误;
-(void)外围设备:(CBPeripheral*)外围设备didUpdateNotificationStateForCharacteristic:(CBCharacteristic*)特征错误:(NSError*)错误;
@结束
@接口BlueToothViewController()
@结束
@BlueToothViewController的实现
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
如果(自我){
//自定义初始化
}
回归自我;
}
-(无效)viewDidLoad{
_centralManager=[[CBCentralManager alloc]initWithDelegate:自队列:nil选项:nil];
_数据=[[NSMutableData alloc]init];
}
-(无效)视图将消失:(BOOL)已设置动画{
[超级视图将消失:动画];
[_CentralManagerStopScan];
}
-(无效)未收到记忆警告{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
-(void)CentralManagerDipDateState:(CBCentralManager*)central{
//您应该测试所有场景
if(central.state==CBCentralManagerStateUnknown){
self.aLabel.text=@“我不做任何事情,因为我的状态未知。”;
返回;
}
if(central.state==CBCentralManagerStatePoweredOn){
//扫描设备

[_CentralManagerScanFor PeripheranswithServices:nil选项:@{CBCentralManager可选Low DuplicateSkey:@YES}]; NSLog(@“扫描已开始”); } if(central.state==CBCentralManagerStateResetting){ self.aLabel.text=@“我不做任何事情,因为我的状态正在重置。”; 返回; } if(central.state==cbcentralmanagerstate不受支持){ self.aLabel.text=@“我不做任何事情,因为我的状态不受支持。”; 返回; } if(central.state==CBCentralManagerStateUnauthorized){ self.aLabel.text=@“我不做任何事情,因为我的状态未经授权。”; 返回; } if(central.state==CBCentralManagerStatePoweredOff){ self.aLabel.text=@“我什么都不做,因为我的状态已关闭。”; 返回; } } -(无效)centralManger:(CBCentralManager*)central DidDiscoveryPeripheral:(CBPeripheral*)peripheral广告数据:(NSDictionary*)广告数据RSSI:(NSNumber*)RSSI{ NSLog(@“发现%@在%@”,peripheral.name,RSSI); self.myPeripherals.text=[NSString stringWithFormat:@“%@%@”,peripheral.name,RSSI]; 如果(_discoveredPerepheral!=外围设备){ //保存外围设备的副本 _发现的外围=外围; //连接 NSLog(@“连接到外围设备%@”,外围设备); [_CentralManagerConnectPeripheral:外围设备选项:无]; self.aLabel.text=[NSString stringWithFormat:@“%@”,外围设备]; } } -(无效)中央管理器:(CBCentralManager*)中央DID无法连接外围设备:(CBPeripheral*)外围设备错误:(N错误*)错误{ NSLog(@“连接失败”); [自我清理]; } -(无效)清理{ //看看我们是否订阅了外设上的特征 如果(_discoveredPerepheral.services!=nil){ 用于(CBService*服务在_discoveredPerepheral.services中){ if(service.characteristics!=nil){ 例如
@interface BlueToothViewController () <CBCentralManagerDelegate>