2

我第一次使用 coreBluetooth。这是我的实现是否有任何问题。

@synthesize CM,activePeripheral;

- (id)init
{
    if ((self = [super init]))
    {
        CM = [[CBCentralManager alloc]initWithDelegate:self queue:dispatch_get_main_queue()];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (int) scanForPeripherals
{

    if (self.CM.state != CBCentralManagerStatePoweredOn)
    {
        NSLog(@"CoreBluetooth is %s",[self centralManagerStateToString:self.CM.state]);
        return -1;
    }

    [self.CM scanForPeripheralsWithServices:[NSArray arrayWithObject:[CBUUID UUIDWithString:@"180D"]] options:@{CBCentralManagerScanOptionAllowDuplicatesKey: @YES}];
    return 0;
}

- (const char *) centralManagerStateToString: (int)state
{
    switch(state)
    {
        case CBCentralManagerStateUnknown:
            return "State unknown (CBCentralManagerStateUnknown)";
        case CBCentralManagerStateResetting:
            return "State resetting (CBCentralManagerStateUnknown)";
        case CBCentralManagerStateUnsupported:
            return "State BLE unsupported (CBCentralManagerStateResetting)";
        case CBCentralManagerStateUnauthorized:
            return "State unauthorized (CBCentralManagerStateUnauthorized)";
        case CBCentralManagerStatePoweredOff:
            return "State BLE powered off (CBCentralManagerStatePoweredOff)";
        case CBCentralManagerStatePoweredOn:
            return "State powered up and ready (CBCentralManagerStatePoweredOn)";
        default:
            return "State unknown";
    }

    return "Unknown state";
}


- (void) connectPeripheral:(CBPeripheral *)peripheral {
    printf("Connecting to peripheral with UUID : %s\r\n",[self UUIDToString:peripheral.UUID]);

    self.activePeripheral = peripheral;
    self.activePeripheral.delegate = self;
    [self.CM connectPeripheral:self.activePeripheral options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey]];
}


-(const char *) UUIDToString:(CFUUIDRef)UUID
{
    if (!UUID)
        return "NULL";

    CFStringRef s = CFUUIDCreateString(NULL, UUID);
    return CFStringGetCStringPtr(s, 0);
}


#pragma mark -Central manager delegate method

- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{

    NSLog(@"hits it");
    if (central.state != CBCentralManagerStatePoweredOn) {
        return;
    }

    isOn=YES;
}

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
    NSLog(@"Received periferal :%@",peripheral);
    NSLog(@"Ad data :%@",advertisementData);

    [MBProgressHUD hideHUDForView:self.view animated:YES];
}


- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
    NSLog(@"Connected peripheral %@",peripheral);
}


- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
    NSLog(@"Error occured :%@",[error localizedDescription]);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)scanDevices:(id)sender {
     if (isOn) {
       [MBProgressHUD showHUDAddedTo:self.view animated:YES];
       [self scanForPeripherals];
    }
}

每次它给出日志

 CoreBluetooth is State unknown (CBCentralManagerStateUnknown)

更新问题

在@Michael Dautermann 提出建议后,我知道我的 centralManagerDidUpdateState 委托方法没有命中。我不知道为什么会这样。如果有人找到原因,请通知我。

谢谢。

4

3 回答 3

3

CoreBluetooth 只是在启动过程中。

您需要先使用此委托方法:

- (void) centralManagerDidUpdateState: (CBCentralManager *) central;

一旦完成,您可以开始扫描外围设备。

于 2013-06-22T09:20:07.947 回答
1

几天后,我检查了 CentralManager 的初始化,我知道 firstViewController 上的 init 方法从未调用过。我通过打印登录 init 方法检查。

- (id)init
{
    NSLog(@"hello init");
    if ((self = [super init]))
    {
        CM = [[CBCentralManager alloc]initWithDelegate:self queue:dispatch_get_main_queue()];
    }
    return self;
}

- (void)viewDidLoad
{   CM = [[CBCentralManager alloc]initWithDelegate:self queue:dispatch_get_main_queue()];
    isOn=NO;
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

因此,我在 viewDidLoad 中初始化了中央管理器,现在它已初始化,并且最终调用了 centralManagerDidUpdateState。现在打印CBCentralManagerStateUnsupported,这不是问题,因为 iPhone 4 不支持它。感谢 michael 帮助我很多。

于 2013-06-26T04:12:13.713 回答
0

你在哪里打电话scanDevices?我觉得你说得太早了。你应该在里面调用它centralManagerDidUpdateState

- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{

    NSLog(@"hits it");
    if (central.state != CBCentralManagerStatePoweredOn) {
        [self scanDevices]:
    }

    isOn=YES;
}
于 2013-06-23T09:45:18.997 回答