我是iOS新手任何人请给我一些建议。我的过程是获取有关蓝牙环扫描仪的状态。蓝牙扫描仪工作正常,我的应用程序从扫描仪获取数据。但现在我的任务是,每当我打开我的应用程序环扫描仪应该自动连接。而且我想在我的应用程序中显示蓝牙状态图标。
1)我的应用程序启动时已连接或已连接的环形扫描仪 - 蓝牙图标(蓝色) 2)未连接环形扫描仪 - 蓝牙图标(红色) 3)由于缺乏活动,环形扫描仪失去连接或睡眠 - 蓝牙图标(灰色)。
我已经开始研究这个概念但是没有调用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];
}
答案 0 :(得分:0)
您只需将外围设备的UUID
保存在内部数据库中,并尝试在使用相同UUID
启动应用程序后重新连接它。
将其保存在
中- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{
NSLog(@"peripheral.identifier=%@",peripheral.identifier.UUIDString);
}
连接外围设备后,您可以读取扫描仪提供的任何值。