Mys*_*inn 22 delegates objective-c ios core-bluetooth bluetooth-lowenergy
我正在把头发拉出来解决这个问题.我正在尝试连接到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");
}
Run Code Online (Sandbox Code Playgroud)
这些设备不是我自己的.我不知道它的接近度UUID,但据我所知,通过CoreBluetooth进行连接时不需要它吗?
didDiscoverPeripheral:
在我尝试连接它们的选择器中发现了所有设备.但那之后什么也没发生.
当我打电话给我时,我是否期望与配对密码请求对话didDiscoverPeripheral:
?如果是这样,我没有看到任何对话,为什么呢?
从苹果文件中,它清楚地表明,在尝试连接到设备之后,你应该得到一个didConnectPeripheral
或两个,didFailToConnectPeripher
但我没有.
有什么想法吗?我已经尝试了差不多一个星期了.感谢每一个帮助,谢谢.
Pau*_*w11 61
如果你没有以某种方式保留peripheral
传递给didDiscoverPeripheral
它的对象,那么一旦这个委托方法退出就会释放它,你将无法获得连接.
我建议添加一个属性来跟踪发现的外围设备
@property (strong,nonatomic) NSMutableArray *peripherals;
Run Code Online (Sandbox Code Playgroud)
在viewDidLoad
或中初始化init
self.peripherals=[NSMutableArray new];
Run Code Online (Sandbox Code Playgroud)
然后将外围设备添加到其中 didDiscoverPeripheral
-(void) centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
NSLog(@"Discovered peripheral %@",peripheral.identifier.UUIDString);
[self.peripherals addObject:peripheral];
[central connectPeripheral:peripheral options:nil];
}
Run Code Online (Sandbox Code Playgroud)