我正在尝试使用CoreBluetooth框架从设备中读取所有可用服务及其特征值。
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral
advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
NSLog(@"Received peripheral : \n%@", peripheral);
NSLog(@"Adv data : %@", advertisementData);
self.activeperipheral=peripheral;
[self.myCentralManager connectPeripheral:peripheral options:nil];
if(peripheral.state==CBPeripheralStateConnected){
peripheral.delegate=self;
NSLog(@"Connected");
}
else
NSLog(@"Not Connected");
NSArray *serviceUUIDs=[advertisementData objectForKey:CBAdvertisementDataServiceUUIDsKey];
for(CBUUID *foundserviceUUIDs in serviceUUIDs){
if([serviceUUIDs containsObject:foundserviceUUIDs]){
NSLog(@"%@",serviceUUIDs);
}
}
}
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{
for(CBService *service in peripheral.services){
[peripheral discoverCharacteristics:nil forService:service];
NSLog(@"Discover Service:%@",service);
}
}
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{
for(CBCharacteristic *characteristic in service.characteristics){
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
self.myCharacteristic=characteristic;
NSLog(@"NotifyValue set on %@",characteristic);
//[peripheral readValueForCharacteristic:myCharacteristic];
}
}
-(void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
if(characteristic.isNotifying)
NSLog(@"Notification began on %@",characteristic);
}
-(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
NSLog(@"%@",characteristic);
// NSLog(@"Characteristic Value Updated");
}
- (int)scanForPeripherals {
NSLog(@"Scanning");
if(self.myCentralManager.state!=CBCentralManagerStatePoweredOn)
NSLog(@"Turn on Bluetooth");
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:NO], CBCentralManagerScanOptionAllowDuplicatesKey,
nil];
NSLog(@"Scanning");
[myCentralManager scanForPeripheralsWithServices:nil options:options];
return 0;
}
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
NSLog(@"Peripheral Connected");
peripheral.delegate=self;
//CBUUID *serviceUUID=[CBUUID UUIDWithString:@"1804"];
[peripheral discoverServices:nil];
//[myCentralManager stopScan];
}
- (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");
}
@end
问题是我不能为除了BatteryLevel特征以外的其他特征设置setNotifyValue: Yes,并且我只收到相同特征的通知。
这是我的输出结果。 你有什么想法,我在这里做错了什么吗? 谢谢
CBCharacteristicPropertyNotify
查看其属性。否则,您必须读取该值。换句话说:if (characteristic & CBCharacteristicPropertyNotify){//setNotifyValue}else {[peripheral readValueForCharacteristic:characteristic]}
- Larme