Я рву на себе волосы из-за этих проблем. Я пытаюсь подключиться к устройствам BLE, но не вижу, что я сделал не так в своем коде ниже.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_cm = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
+ (NSString*)UUIDString:(CFUUIDRef)uuid {
CFStringRef string = CFUUIDCreateString(NULL, uuid);
return (__bridge_transfer NSString*)string;
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
if (central.state == CBCentralManagerStatePoweredOn) {
[self scanForPeripherals];
}
}
- (void)centralManager:(CBCentralManager *)central
didDiscoverPeripheral:(CBPeripheral *)peripheral
advertisementData:(NSDictionary *)advertisementData
RSSI:(NSNumber *)RSSI {
// NSLog(@"Received peripheral : \n%@", peripheral);
// NSLog(@"Adv data : %@", advertisementData);
[peripheral setDelegate:self];
[central connectPeripheral:peripheral options:nil];
[peripheral readRSSI];
}
- (int)scanForPeripherals {
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:NO], CBCentralManagerScanOptionAllowDuplicatesKey,
nil];
[_cm scanForPeripheralsWithServices:nil options:options];
return 0;
}
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
NSLog(@"didConnectPeripheral");
}
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
NSLog(@"didDisconnectPeripheral");
}
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
NSLog(@"failed to connect");
}
- (void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(NSError *)error {
NSLog(@"didReadRSSI");
}
Эти устройства не мои. Я не знаю его UUID близости, но, насколько я знаю, он не понадобится для подключения через CoreBluetooth, верно?
Все устройства обнаружены в didDiscoverPeripheral:
, в селекторе пытался их подключить. Но после этого ничего не происходит.
Должен ли я ожидать диалога с запросом пароля для сопряжения, когда я звоню didDiscoverPeripheral:
? Если да, то я не вижу никакого диалога, почему?
В документах Apple четко указано, что после попытки подключения к устройству вы должны получить вызов либо didConnectPeripheral
, либо didFailToConnectPeripher
, но я не получил ни одного.
есть идеи? Уже почти неделю пытаюсь. Ценю каждую помощь, спасибо.
didDiscoverPeripheral
? Пробовали ли вы удалить вызов[peripheral readRSSI]
, который следует сразу за вашим запросом на подключение? Вы не должны выдавать этот запрос, пока не подключитесь. Я всегда предлагаю людям попробовать бесплатное приложение LightBlue из магазина приложений в качестве теста, чтобы убедиться, что их устройство рекламирует и можно ли подключить. - person Paulw11   schedule 15.10.2014connectPeripheral
ничего не происходило. - person Mysteltainn   schedule 15.10.2014