注意:我可以访问BLE设备和软件,因此解决方案可能涉及更改。
外围设备只能在大约25-30%的尝试期间连接和传输所需的数据。
有没有人知道为什么我们会遇到这种行为,以及我们如何修复BLE外设和/或中心以最大化发现和连接的可能性?
更新:
以下是我的代码的所有与BLE相关的部分,通过外围设备发现:
在我的AppDelegate.h
:
@property (nonatomic, strong) NSArray *services;
@property (nonatomic, strong) NSArray *characteristics;
@property (nonatomic, strong) CBCentralManager *centralManager;
@property (nonatomic, strong) CBPeripheral *peripheral;
在我的AppDelegate.m
:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.services = @[[CBUUID UUIDWithString: @"A1B0"]];
self.characteristics = @[[CBUUID UUIDWithString: @"0321"]];
self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
switch ([central state]) {
case CBCentralManagerStateUnauthorized:
NSLog(@"*** This app is not authorized to use Bluetooth Low Energy ***");
break;
case CBCentralManagerStatePoweredOff:
NSLog(@"*** Bluetooth on this device is currently powered off ***");
break;
case CBCentralManagerStatePoweredOn:
[self.centralManager scanForPeripheralsWithServices:self.services options:nil];
break;
default:
NSLog(@"*** The state of the BLE Manager is unknown ***");
}
}
- (void) centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
self.peripheral = peripheral;
[self.centralManager connectPeripheral:peripheral options:nil];
}
而且要清楚,这在前景中完美地工作(每次尝试)。它的仅背景扫描表现得很奇怪。