1

我们正在开发蓝牙 iOS 应用程序。首先我们需要最近的蓝牙设备names,放入uitableView。所以我们使用corebluetooth.framework。像这样尝试

- (void)viewDidLoad {
    [super viewDidLoad];
    self.peripherals=[NSMutableArray new];
    NSDictionary *scanOptions = @{CBCentralManagerScanOptionAllowDuplicatesKey:@(YES)};
    NSArray *services = @[[CBUUID UUIDWithString:@"F3501201-3F04-4744-BE91-154BB7D51BDC"]];

    centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];

}
-(void) centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
    NSLog(@"Discovered peripheral %@",peripheral.identifier.UUIDString);
     NSLog(@"Discovered peripheral %@",peripheral.name);
    [self.peripherals addObject:peripheral];
    [central connectPeripheral:peripheral options:nil];

}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
    switch (central.state)
    {
        case CBCentralManagerStateUnsupported:
        {
            NSLog(@"State: Unsupported");
        } break;

        case CBCentralManagerStateUnauthorized:
        {
            NSLog(@"State: Unauthorized");
        } break;

        case CBCentralManagerStatePoweredOff:
        {
            NSLog(@"State: Powered Off");
        } break;

        case CBCentralManagerStatePoweredOn:
        {
            NSLog(@"State: Powered On");
            [centralManager scanForPeripheralsWithServices:services options:scanOptions];
        } break;

        case CBCentralManagerStateUnknown:
        {
            NSLog(@"State: Unknown");
        } break;

        default:
        {
        }

    }
}

我们得到了State: Powered On。但是 didDiscoverPeripheral 方法从未被调用过。我们是新手。我们需要当我的应用程序打开时,所有蓝牙设备都放在 UItableView 中,然后我们点击特定的设备。我的代码中有什么错误请指导我们。

4

1 回答 1

2

尝试在扫描所有设备时提供此信息。

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], CBCentralManagerScanOptionAllowDuplicatesKey, nil];

 [self.centralManager scanForPeripheralsWithServices:nil options:options];

在方法中获取设备列表后didDiscoverPeripheral delegate,通过调用discoverServices:nil外围设备找到正确的服务 UUID。因此您可以扫描具有正确服务 UUID 的外围设备。

(或)您可以使用https://github.com/l0gg3r/LGBluetooth

你需要做的就是,

[[LGCentralManager sharedInstance] scanForPeripheralsByInterval:5
                                                         completion:^(NSArray *peripherals) {
     }];
于 2015-08-27T13:39:02.143 回答