[英]iOS Bluetooth: central- peripheral got connected before accepting pairing request
藍牙連接時出現問題,因為我正在按照CBCentral和CBPeripherel概念與一台設備進行通訊。 但是,Peripherel設備在接受配對請求之前就已連接到Central,基本上Central在接受配對請求之前將一些數據傳遞給外圍設備。
-- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
for (CBCharacteristic *characteristic in service.characteristics)
{
NSLog(@"for loop for charactersitc %@",characteristic.UUID);
// And check if it's the right one
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:TRANSFER_SLIDEINDEX_CHARACTERISTIC_UUID]])
{
// If it is, subscribe to it
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
}
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:TRANSFER_SCROLL_CHARACTERISTIC_UUID]])
{
// If it is, subscribe to it
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
}
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:TRANSFER_FULLSCREEN_CHARACTERISTIC_UUID]])
{
// If it is, subscribe to it
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
}
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:SLIDELENGTH_CHARACTERISTIC_UUID]])
{
// WRITE VALUES TO REMOTE
NSInteger length = self.presentationDataSourceArray.count;
NSData *chunk = [NSData dataWithBytes: &length length: sizeof(length)];
[self.discoveredPeripheral writeValue:chunk forCharacteristic:characteristic
type:CBCharacteristicWriteWithResponse];
[self.discoveredPeripheral setNotifyValue:YES forCharacteristic:characteristic];
}
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:DEVICENAME_CHARACTERISTIC_UUID]])
{
NSString *deviceName = [[UIDevice currentDevice]name];
NSData *chunk = [deviceName dataUsingEncoding:NSUTF8StringEncoding];
[self.discoveredPeripheral writeValue:chunk forCharacteristic:characteristic
type:CBCharacteristicWriteWithResponse];
[self.discoveredPeripheral setNotifyValue:YES forCharacteristic:characteristic];
NSLog(@"Sending Device Name");
}
if ([characteristic.UUID isEqual: [CBUUID UUIDWithString:CURRENTSLIDE_CHARACTERISTIC_UUID]])
{
self.currentSlideIndexCharacterestics = (CBMutableCharacteristic*) characteristic;
NSInteger count = self.currentPage;
NSData *chunk = [NSData dataWithBytes: &count length: sizeof(count)];
[self.discoveredPeripheral writeValue:chunk forCharacteristic:characteristic
type:CBCharacteristicWriteWithResponse];
[self.discoveredPeripheral setNotifyValue:YES forCharacteristic:characteristic];
}
_isRemoteConnected = YES;
}
問題不在您的APP中,而是在BLE設備中。
BLE設備的數據可以不成對寫入和讀取,完全可以。
僅當您嘗試訪問加密字符的數據時才啟動配對請求,這是在外圍設備側配置的,如下所示:
emailCharacteristic = [[CBMutableCharacteristic alloc]
initWithType:emailCharacteristicUUID
properties:CBCharacteristicPropertyRead
| CBCharacteristicPropertyNotifyEncryptionRequired
value:nil permissions:CBAttributePermissionsReadEncryptionRequired];
因此,在您的情況下,外圍設備的某些字符需要配對,而其他字符則不需要。
您可以在CorebluetoothGuide中看到更多
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.