【发布时间】:2017-12-25 03:45:35
【问题描述】:
在我的应用程序中,我首先使用委托方法retrieveConnectedPeripheralsWithServices 搜索以前连接的设备,如果没有找到则扫描附近的设备。这是代码
- (void)searchForPeripherals {
NSLog(@"*** scanning for Bluetooth peripherals... ***");
//Check to see if any devices were connected already
if(self.ourPeripheral != nil) {
[self connectToDevice];
} else {
//Search for previously paired devices...
bool foundPairedDevices = [self checkForAndConnectToPreviouslyPairedDevices];
//None found, scan for new devices nearby...
if(!foundPairedDevices) {
NSLog(@"No paired boxes found, checking device powered state...");
//If the app user has "bluetooth" option turned on
if(self.centralManager.state == CBCentralManagerStatePoweredOn) {
NSLog(@"--- central manager powered on.");
NSLog(@"...begin scanning...");
[self.centralManager scanForPeripheralsWithServices:nil
options:@{CBCentralManagerScanOptionAllowDuplicatesKey: @(YES)}];
//No, user has "bluetooth" turned off
} else {
NSLog(@"--- central manager is NOT powered on.");
}
}
}
}
- (bool)checkForAndConnectToPreviouslyPairedDevices {
NSLog(@"our peripheral device: %@", self.ourPeripheral);
NSArray *dcBoxesFound = [self.centralManager retrieveConnectedPeripheralsWithServices:@[[CBUUID UUIDWithString:SERVICE_UUID]]];
NSLog(@"Previously paired DC boxes?: %@", dcBoxesFound);
//Are there any previously paired devices?
if(dcBoxesFound != nil && [dcBoxesFound count] > 0) {
CBPeripheral *newestBoxFound = [dcBoxesFound firstObject];
newestBoxFound.delegate = self;
self.ourPeripheral = newestBoxFound;
self.deviceUUID = [CBUUID UUIDWithString:[newestBoxFound.identifier UUIDString]];
[self connectToDevice];
return YES;
} else {
return NO;
}
}
- (void)connectToDevice {
if(self.ourPeripheral != nil) {
[self.centralManager connectPeripheral:self.ourPeripheral options:nil];
}
}
- (void)centralManager:(CBCentralManager *)central
didDiscoverPeripheral:(CBPeripheral *)peripheral
advertisementData:(NSDictionary *)advertisementData
RSSI:(NSNumber *)RSSI {
NSLog(@"scanned and found this peripheral: %@", peripheral);
NSLog(@"advertisment data: %@", advertisementData);
if(!self.isConnectedToDevice && [[advertisementData objectForKey:@"kCBAdvDataLocalName"] localizedCaseInsensitiveContainsString:@"dc-"]) {
NSLog(@"\n");
NSLog(@"-------------------------------------------");
NSLog(@"DC peripheral found! Attempting to connect to the following...: %@", peripheral);
peripheral.delegate = self;
self.ourPeripheral = peripheral;
self.deviceUUID = [CBUUID UUIDWithString:[peripheral.identifier UUIDString]];
[self connectToDevice];
NSLog(@"-------------------------------------------");
NSLog(@"\n");
}
}
- (void)centralManager:(CBCentralManager *)central
didConnectPeripheral:(CBPeripheral *)peripheral {
NSLog(@"--- didConnectPeripheral");
peripheral.delegate = self;
[peripheral discoverServices:@[[CBUUID UUIDWithString:SERVICE_UUID]]];
}
问题
当我启动应用程序时,它通常连接得很好。一切都很好。然后在非常不可预测和奇怪的时候,BT 连接会以某种方式“卡住”。我所说的卡住的意思是它永远不会真正连接。我的盒子上的蓝灯亮着,好像系统已连接到它,但在我的应用程序中会发生这种情况。
1) retrieveConnectedPeripheralsWithServices 方法找到一个空数组,其中包含以前连接的设备,告诉调用它的方法没有找到以前连接的设备。
2) 触发scanForPeripheralsWithServices:nil 方法并使用didDiscoverPeripheral 方法开始扫描。
3) didDiscoverPeripheral 从来没有发现附近的任何带有 kCBAdvDataLocalName 前缀的“dc-”或其他的盒子
如果我要进入 iOS 设置 -> 蓝牙 -> 忘记这个设备(必须按两次,这让我觉得它以某种方式“卡住”了)然后实际上一起关闭了 BT 选项......等等2秒,然后重新打开它...然后重新启动应用程序,一切正常。
1) 应用启动
2) 查看之前没有连接的设备
3) 扫描外围设备并找到我的前缀框名称“dc-whatever”
4) 要求我与 BT 设备配对,然后我恢复完整功能
如果我随后关闭应用程序并重新启动它,retrieveConnectedPeripheralsWithServices 会毫无问题地找到我之前连接的盒子并无缝连接...
那么...这里发生了什么?为什么它有时会随机“卡住”?
编辑:
我确实意识到配对和连接不是一回事,因此某些方法的命名非常糟糕。
【问题讨论】:
标签: ios objective-c bluetooth cbperipheral cbcentralmanager