gpt4 book ai didi

iphone - iOS 核心蓝牙/iBeacon : Advertise an iBeacon and a peripheral service concurrently

转载 作者:行者123 更新时间:2023-12-03 18:22:50 30 4
gpt4 key购买 nike

我正在为 iOS 编写一个应用程序,该应用程序要求该应用程序同时通告 iOS iBeacon 以及通告外围服务。有必要宣传该服务,而不是简单地在外设上发现该服务,因为用例需要中央(用 BLE 术语)在由于靠近 iBeacon 而被 iOS 唤醒(但仍在后台)后连接到外设。在中心后台运行的应用程序只能通过可用服务发现外围设备,而不是发现所有外围设备 [] ;我的代码可以宣传服务或 iBeacon,但我还没有弄清楚如何同时宣传这两者。 iBeacon 有可能使用 38 字节可用空间中的 21 字节,而根本没有足够的空间来宣传信标和服务?

这有效(信标):

self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid 
major:1
minor:1
identifier:@"bentboolean"];
NSMutableDictionary *dict = [[self.beaconRegion peripheralDataWithMeasuredPower:nil] mutableCopy];
[self.peripheralManager startAdvertising:dict ];

这有效(服务):

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setValue:@[serviceUUID] forKey:CBAdvertisementDataServiceUUIDsKey];
[self.peripheralManager startAdvertising:dict ];

将两者加在一起,尝试同时宣传这两种服务是行不通的。它只宣传 Beacon,而不宣传服务:

self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid 
major:1
minor:1
identifier:@"bentboolean"];
NSMutableDictionary *dict = [[self.beaconRegion peripheralDataWithMeasuredPower:nil] mutableCopy];
[dict setValue:@[serviceUUID] forKey:CBAdvertisementDataServiceUUIDsKey];
[self.peripheralManager startAdvertising:dict ];

感谢您的浏览!

最佳答案

我能够分别为接收器和信标使用单独的 CLLocationManager 和 CLBeaconRegion 来实现此目的:

#import "GRBothViewController.h"

@interface GRBothViewController ()

@end

@implementation GRBothViewController

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

- (void)viewDidLoad
{
[super viewDidLoad];


self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;

self.locationScanner = [[CLLocationManager alloc] init];
self.locationScanner.delegate = self;

[self initBeacon];
[self initRegion];
[self locationManager:self.locationManager didStartMonitoringForRegion:self.scannerRegion];
}

- (void)initBeacon {
NSLog(@"Starting beacon");

NSUUID *uuid = [[NSUUID alloc] initWithUUIDString: @"23542266-18D1-4FE4-B4A1-23F8195B9D39"];
self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
major:1
minor:1
identifier:@"com.thisisgrow.Grow"];
[self transmitBeacon:self];
}

- (IBAction)transmitBeacon:(id)sender {
self.beaconPeripheralData = [self.beaconRegion peripheralDataWithMeasuredPower:nil];
self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self
queue:nil
options:nil];
}

-(void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral {
if (peripheral.state == CBPeripheralManagerStatePoweredOn) {
NSLog(@"Powered On");
[self.peripheralManager startAdvertising:self.beaconPeripheralData];
} else if (peripheral.state == CBPeripheralManagerStatePoweredOff) {
NSLog(@"Powered Off");
[self.peripheralManager stopAdvertising];
}
}

- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region {
[self.locationManager startRangingBeaconsInRegion:self.beaconRegion];
}

- (void)initRegion {
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"23542266-18D1-4FE4-B4A1-23F8195B9D39"];
_scannerRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:@"com.thisisgrow.Grow"];
_scannerRegion.notifyEntryStateOnDisplay = YES;
[_locationScanner startMonitoringForRegion:self.scannerRegion];
}

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
//SCANNER
[self.locationScanner startRangingBeaconsInRegion:self.scannerRegion];
}

-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
//SCANNER HAS LEFT THE AREA
[self.locationScanner stopRangingBeaconsInRegion:self.scannerRegion];
}

-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region {
CLBeacon *beacon = [[CLBeacon alloc] init];
NSLog(@"Beacons: %d", [beacons count]);
if(beacons && [beacons count]>0){
beacon = [beacons firstObject];
}
else{

}
}

这里唯一的限制是设备无法检测到自身。

关于iphone - iOS 核心蓝牙/iBeacon : Advertise an iBeacon and a peripheral service concurrently,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19351856/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com