2016-05-13 23 views
3

我是新来的iOS任何一个请给我一些建议。我的过程获得有关蓝牙环扫描仪的状态。蓝牙扫描仪工作正常,我的应用程序从扫描仪获取数据。但现在我的任务是,每当我打开我的应用程序环扫描仪应自动连接。并且我想在我的应用程序中显示蓝牙状态图标。 (蓝色) 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(@"[email protected]"); 
    } 

    -(void)centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *)peripherals 
    { 
    NSLog(@"[email protected]"); 
    } 

    -(void)centralManager:(CBCentralManager *)central didRetrieveConnectedPeripherals:(NSArray *)peripherals 
    { 
     NSLog(@"[email protected]"); 
    } 

    -(void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error 
    { 
    NSLog(@"Connection [email protected]"); 
    } 

    -(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error 
    { 
NSLog(@"[email protected]"); 
    } 

- (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

你刚才保存的外围设备的UUID在你的内部数据库,并尝试重新连接使用相同的UUID应用程序启动后。

将它保存在

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{ 
    NSLog(@"peripheral.identifier=%@",peripheral.identifier.UUIDString); 
} 

与外围设备连接后,就可以读取扫描仪提供的任何值。

+0

嗨Atanu Mondal首先感谢你..雅是好的,现在我的问题是中央经理方法没有调用..一旦这个问题得到解决,那么我可以前进。帮我解决这个问题 – Nithya

+0

你可以试试这个代码 - (void)viewDidLoad { {super viewDidLoad]; _bluetoothManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; _bluetoothManager.delegate = self; [_bluetoothManager scanForPeripheralsWithServices:nil options:nil]; } –

+0

雅我已经尝试了你给出的代码。但仍然存在问题,方法不会被调用 – Nithya