我是iOS的新手,请给我一些建议。我的过程了解蓝牙环形扫描仪的状态。蓝牙扫描仪工作正常,我的应用从扫描仪获取数据。但是现在我的任务是,每当我打开我的应用程序时,环形扫描仪应自动连接。我也想在我的应用程序中显示蓝牙状态图标。
1)环形扫描仪已连接或在我的应用启动时已连接-蓝牙图标(蓝色)
2)未连接环形扫描仪-蓝牙图标(红色)
3)Ring Scanner由于缺乏 Activity 而失去连接或进入睡眠状态-蓝牙图标(灰色)。
我已经开始研究这个概念,但是未调用centralManager方法。这是我的代码。任何人请帮助我
h.file
#import <CoreBluetooth/CoreBluetooth.h>
@property (nonatomic, strong) NSString *connected;
@property (nonatomic) CBCentralManager *bluetoothManager;
m.file
- (void)viewDidLoad
{
[super viewDidLoad];
_bluetoothManager = [[CBCentralManager alloc] initWithDelegate:self
queue:nil
options:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0]
forKey:CBCentralManagerOptionShowPowerAlertKey]];
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
NSString *stateString = nil;
switch(_bluetoothManager.state)
{
case CBCentralManagerStateResetting: stateString = @"The connection with the system service was momentarily lost, update imminent."; break;
case CBCentralManagerStateUnsupported: stateString = @"The platform doesn't support Bluetooth Low Energy."; break;
case CBCentralManagerStateUnauthorized: stateString = @"The app is not authorized to use Bluetooth Low Energy."; break;
case CBCentralManagerStatePoweredOff: stateString = @"Bluetooth is currently powered off.";
{
UIAlertView *BluetoothOff = [[UIAlertView alloc] initWithTitle:@"Warning!!" message:@"Turn ON Bluetooth in setting!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[BluetoothOff show];
}
break;
case CBCentralManagerStatePoweredOn: stateString = @"Bluetooth is currently powered on and available to use.";
{
// [self.bluetoothManager scanForPeripheralsWithServices:nil options:nil];
[self startScan];
}
break;
default: stateString = @"State unknown, update imminent.";
break;
}
NSLog(@"Bluetooth State: %@",stateString);
}
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral*)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
NSLog(@"DiscoverPeripheral@");
}
-(void)centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *)peripherals
{
NSLog(@"RetrievePeripherals@");
}
-(void)centralManager:(CBCentralManager *)central didRetrieveConnectedPeripherals:(NSArray *)peripherals
{
NSLog(@"RetrieveConnectedPeripherals@");
}
-(void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
NSLog(@"Connection Failed@");
}
-(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
NSLog(@"Disconnected@");
}
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
[peripheral setDelegate:self];
[peripheral discoverServices:nil];
self.connected = [NSString stringWithFormat:@"Connected: %@", peripheral.state == CBPeripheralStateConnected ? @"YES" : @"NO"];
}
- (void) startScan
{
NSLog(@"Start scanning");
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], CBCentralManagerScanOptionAllowDuplicatesKey, nil];
[self.bluetoothManager scanForPeripheralsWithServices:nil options:options];
}
最佳答案
您只需将外围设备的UUID
保存在内部数据库中,并在应用程序启动后使用相同的UUID
尝试重新连接它。
保存在
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{
NSLog(@"peripheral.identifier=%@",peripheral.identifier.UUIDString);
}
连接外围设备后,您可以读取扫描仪提供的任何值。
关于ios - 未调用中央管理器方法。,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37208048/