【发布时间】:2014-12-10 05:31:45
【问题描述】:
我正在摆脱这个问题。我正在尝试连接到 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: 时,我会期待与配对密码请求对话吗?
如果是这样,我没有看到任何对话框,这是为什么呢?
从苹果文档中,它清楚地指出,在尝试连接到设备后,您应该得到didConnectPeripheral 或didFailToConnectPeripher 的调用,但我没有。
有什么想法吗?我已经尝试了将近一个星期。 感谢每一个帮助,谢谢。
【问题讨论】:
-
你接到
didDiscoverPeripheral的电话了吗?您是否尝试过在您的连接请求之后立即删除对[peripheral readRSSI]的呼叫?在您连接之前,您不应该发出该请求。我总是建议人们尝试从应用商店下载免费的 LightBlue 应用来测试他们的设备是否有广告并且是否可以连接 -
是的,我试过了,这个代码的第一个版本,它什么也不做,只是发现并尝试连接,但在
connectPeripheral执行之后什么也没发生。 -
试试 LightBlue - 这样您至少可以确认硬件是可连接的。还可以尝试将要连接的外围设备存储在一个属性中,这样它就不会被释放
-
试过了,所有这些都显示在 LightBlue 中,没有为任何设备显示任何服务。有 2 个 Estimotes BLE 和另外 2 个我的 国家定制 BLE*。询问 estimotes BLE 导致连接成功,但几秒钟后,LightBlue 说“断开连接警报”,并且有一个红色文本说“数据已过时”,另外 2 个设备询问显示另一个页面,如 estimotes,仅此而已跨度>
标签: ios objective-c delegates bluetooth-lowenergy core-bluetooth