CoreBluetooth将值写入缓存外设

时间:2014-07-14 19:36:58

标签: ios objective-c bluetooth core-bluetooth

所以我正在使用两个应用。一个充当中心,另一个是外围设备。可能有多个设备充当外围设备。当Central启动时,它会开始扫描运行我称之为UUIDService的服务的外围设备。此服务有两个特征:vendorUUID(读取)和totalAmount(写入)。

当外围设备启动时,它会开始向UUIDService广告-[UIDevice identifierForVendor]字符串作为vedorUUID的值和nil的{​​{1}} < / p>

在Central上,我有一个名为totalAmount.的单例,它包含一个名为BluetoothManager的可变数组,用于跟踪连接到中心的外围设备。这是一个connectedUsers个对象的数组,它有两个属性:uuid(NSString)和periph(CBPeripheral)。

当Central发现外围设备时,它会创建用户对象并填写对BTUser的引用:

periph

这段代码有效。 -(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI { NSLog(@"Discovered periph advertising UUID Service"); BTUser* user = [[BTUser alloc] init]; user.periph = peripheral; [_centralManager connectPeripheral:user.periph options:nil]; [_connectedUsers addObject:user]; NSLog(@"User periph: %@", user.periph.identifier); } 现在包含一个_connectedUsers BTUser。然后连接:

periph

我已经确认这项工作,并且外围设备继续发现服务和相关特征。我遇到的问题是当我使用外设引用写入-(void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral { NSLog(@"Connected periph: %@", peripheral.name); BTUser* thisUser; for (BTUser* user in _connectedUsers) { if ([user.periph.identifier isEqual: peripheral.identifier]) { thisUser = user; break; } } thisUser.periph.delegate = self; [thisUser.periph discoverServices:@[_uuidServiceUUID]]; } 时。

totalAmount

它使用于写入值的块,-(void)initializePaymentWithUser:(BTUser*)user forAmount:(CGFloat)amount { _isListeningForUsers = NO; [self stopScanningForUsers]; NSLog(@"disconnecting from all other users"); for (BTUser* u in _connectedUsers) { if (![u isEqual:user]) { [u disconnectFromCentral:_centralManager]; } } CBService* uuidService = user.periph.services[0]; for (CBCharacteristic* cha in uuidService.characteristics) { if ([cha.UUID isEqual:_uuidServiceTotalAmountUUID]) { NSLog(@"Writing total amount %.2f", amount); NSData* data = [[NSString stringWithFormat:@"%.2f", amount] dataUsingEncoding:NSUTF8StringEncoding]; [user.periph writeValue:data forCharacteristic:cha type:CBCharacteristicWriteWithResponse]; break; } } } 仍然具有与最初连接时相同的user.periph。但是我收到了错误:

-[CBPeripheral identifier]

外围设备从不调用CoreBluetooth[WARNING] <CBPeripheral: 0x14ed28c0 identifier = B4CB35CC-2C78-7543-B083-E0B373EF66D0, Name = "iPhone", state = connected> is not a valid peripheral

为了完成这些问题,这是外围设备的代码块,负责宣传-peripheral:didWriteValueForCharacteristic:error

UUIDService

0 个答案:

没有答案