在发现特征后,我设置了对特定特征的通知,如下所示:
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
在此之后调用didUpdateNotificationStateForCharacteristic,通知属性为true但未调用didUpdateValueForCharacteristic.Android人员从设备获取值,因此硬件设备没有问题。
以下是ref:
的完整代码- (void)viewDidLoad {
[super viewDidLoad];
_centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
// You should test all scenarios
if (central.state != CBCentralManagerStatePoweredOn) {
return;
}
if (central.state == CBCentralManagerStatePoweredOn) {
// Scan for devices
[_centralManager scanForPeripheralsWithServices:nil options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];
NSLog(@"Scanning started");
}
}
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
NSLog(@"Discovered %@ at %@", peripheral.name, RSSI);
if (_discoveredPeripheral != peripheral) {
// Save a local copy of the peripheral, so CoreBluetooth doesn't get rid of it
_discoveredPeripheral = peripheral;
// And connect
NSLog(@"Connecting to peripheral %@", peripheral);
[_centralManager connectPeripheral:peripheral options:nil];
}
}
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
NSLog(@"Failed to connect");
[self cleanup];
}
- (void)cleanup {
// See if we are subscribed to a characteristic on the peripheral
if (_discoveredPeripheral.services != nil) {
for (CBService *service in _discoveredPeripheral.services) {
if (service.characteristics != nil) {
for (CBCharacteristic *characteristic in service.characteristics) {
}
}
}
}
[_centralManager cancelPeripheralConnection:_discoveredPeripheral];
}
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
NSLog(@"Connected");
[_centralManager stopScan];
NSLog(@"Scanning stopped");
self.discoveredPeripheral = peripheral;
peripheral.delegate = self;
[peripheral discoverServices:@[[CBUUID UUIDWithString:SERVICE2_UUID]]];
}
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
if (error) {
NSLog(@"Error discovering services: %@", [error localizedDescription]);
return;
}
NSLog(@"services:%@",peripheral.services);
for (CBService *service in peripheral.services) {
if([service.UUID isEqual:[CBUUID UUIDWithString:SERVICE2_UUID] ])
{
[_discoveredPeripheral discoverCharacteristics:nil forService:service];
}
}
// Discover other characteristics
}
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
if (error) {
NSLog(@"Error discovering characteristics: %@", [error localizedDescription]); return;
}
NSLog(@"charac:%@",service.characteristics);
if([service.UUID isEqual:[CBUUID UUIDWithString:SERVICE2_UUID] ]) {
for (CBCharacteristic *characteristic in service.characteristics)
{
NSLog(@"loop if enetrred");
NSLog(@"bool:%i",characteristic.isNotifying);
if(characteristic.properties== CBCharacteristicPropertyIndicate) {
[_discoveredPeripheral setNotifyValue:YES forCharacteristic:characteristic];
}
}
}
}
- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
if (error) {
NSLog(@"Error changing notification state: %@ with charac:%@", error.localizedDescription,characteristic);
}
// Notification has started
if (characteristic.isNotifying) {
NSLog(@"Notification began on %@", characteristic);
}
}
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
NSLog(@"Characteristic value : %@ with ID %@", characteristic.value, characteristic.UUID);
NSLog(@"bool:%i",characteristic.isNotifying);
NSLog(@"char:%@",characteristic);
if (error) {
NSLog(@"Error:%@",error.localizedDescription);
return;
}
}
请帮我解决这个问题..