当前位置: 首页 > 知识库问答 >
问题:

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

邰勇军
2023-03-14

我的应用程序无法检测外设。我用浅蓝色来模拟蓝牙低能量外设,我的应用程序就是感觉不到。我甚至在两台设备上安装了浅蓝色,以确保它能正确地生成外围信号,并且是正确的。有什么建议吗?我的标签正在更新,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

共有1个答案

白子昂
2023-03-14

看起来您没有声明您实现了CBCentralManagerDelegate协议。更改Bluetooth ViewController界面以添加CBCentralManagerDelegate。

@interface BlueToothViewController () <CBCentralManagerDelegate>
 类似资料:
  • 我是为iOS设备开发的新手。我是苹果的长期用户,今年Spring将完成我的电气工程学士学位。我目前正在为一个设计课程做一个项目,对蓝牙低能耗以及如何在iOS(5和6)中实现它有一些疑问。 首先介绍一下这个项目的背景。我们正在为一座建筑物设计一个占用检测系统。该计划是在整个建筑中,将BLE模块连接到沃尔沃茨。这些设备将与用户的智能手机通信,并向后端系统提供位置信息。我们希望这个系统只需要很少的用户交

  • 我得到一些信息,Android设备现在可以在蓝牙低能耗(BLE)外围模式下运行。应用程序可以使用此功能从Android L中的New向附近的设备广播它们的存在。 我已经全部配置好了(即蓝牙LE广告和广告数据的设置要在广告包中进行广告),当我开始发送时,我发现我无法监听传入的连接。谁能帮我? 我是一个基于l-开发者-预览-参考的API(Added Package:android.bluetooth.

  • 有人知道如何添加蓝牙低能耗作为设备要求,只允许iOS应用程序在蓝牙LE设备上可用吗?谢啦

  • 我目前正在开发一个基于蓝牙低能耗设备的iOS应用程序。为了得到一个唯一的标识符来比较得到的外设,我必须得到外设的MAC地址。 据观察,外围设备的UUID属性在iOS设备中不同,而且外围设备要获得UUID,必须至少连接到主设备一次。因为我必须处理登机手续,所以我不想建立连接。当我通过bluetooth services portal时,我发现设备信息本身就是一项服务,除非主iOS设备和外围bluet

  • 我想知道是否有可能修改BLE信标的内容以包含额外信息。如果在末尾插入一个额外的位,则可能在一个方向上广播布尔值。从理论上讲,如果你修改了你的设备来读取额外的信息,这就行了。考虑到现有的协议,这听起来需要做很多工作。外面已经有这样的东西了吗? 对于信息,我正在mbed平台上工作,在那里你可以修改你自己的蓝牙信标有效载荷。

  • 我想通过iPhone(5s)上的应用程序,在50-100米半径范围内同时跟踪大量信标(~500)。我已经看过了规范和在线版本,我看不出使用BLE一次可以跟踪的信标数量是否有任何限制。有人知道你可以跟踪的信标数量是否有限制,或者iphone5s是否能够完成跟踪那么多信标的任务?