我在蓝牙连接方面遇到了一些问题,因为我正在将一台设备与其他CBCentral和CBPeripherel概念进行通信。 但是,Peripherel设备在接受配对请求弹出之前连接到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;
}