didUpdateValueForCharacteristic(setNotifyValue:YES)不起作用

use*_*170 1 ios core-bluetooth bluetooth-lowenergy

我希望在值发生变化时收到通知.我正在学习本教程 - > 核心蓝牙简介:构建心率监测器

我用这个蓝牙设备 - > IC卡读卡器(索尼产品)

- (void)viewDidLoad {
    [super viewDidLoad];
    _myCentralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
    [_myCentralManager scanForPeripheralsWithServices:nil options:nil];
    self.myCentralManager = _myCentralManager;
}

#pragma mark - CBCentralManagerDelegate

// method called whenever you have successfully connected to the BLE peripheral
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
    [peripheral setDelegate:self];
    [peripheral discoverServices:nil];
    NSString *connected = [NSString stringWithFormat:@"Connected: %@", peripheral.state == CBPeripheralStateConnected ? @"YES" : @"NO"];
    NSLog(@"%@", connected);
}

// CBCentralManagerDelegate - This is called with the CBPeripheral class as its main input parameter. This contains most of the information there is to know about a BLE peripheral.
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{
    NSLog(@"Discovered %@", _peripheral.name);
    NSString *localName = [advertisementData objectForKey:CBAdvertisementDataLocalNameKey];

    if ([localName length] > 0) {
        NSLog(@"Found the : %@", localName);
      //  [self.myCentralManager stopScan];
        self.peripheral = peripheral;
        peripheral.delegate = self;
        [self.myCentralManager connectPeripheral:peripheral options:nil];
    }
}

// method called whenever the device state changes.
- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
    // Determine the state of the peripheral
    if ([central state] == CBCentralManagerStatePoweredOff) {
        NSLog(@"CoreBluetooth BLE hardware is powered off");
    }
    else if ([central state] == CBCentralManagerStatePoweredOn) {
        NSLog(@"CoreBluetooth BLE hardware is powered on and ready");
    }
    else if ([central state] == CBCentralManagerStateUnauthorized) {
        NSLog(@"CoreBluetooth BLE state is unauthorized");
    }
    else if ([central state] == CBCentralManagerStateUnknown) {
        NSLog(@"CoreBluetooth BLE state is unknown");
    }
    else if ([central state] == CBCentralManagerStateUnsupported) {
        NSLog(@"CoreBluetooth BLE hardware is unsupported on this platform");
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

#pragma mark - CBPeripheralDelegate

// CBPeripheralDelegate - Invoked when you discover the peripheral's available services.
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{
    for (CBService *service in peripheral.services) {
        NSLog(@"Discovered service: %@", service.UUID);
        [peripheral discoverCharacteristics:nil forService:service];
    }
}

// Invoked when you discover the characteristics of a specified service.
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{
    // Deal with errors (if any)
    if (error) {
        NSLog(@"Error discovering characteristics: %@", [error localizedDescription]);
        return;
    }

    // Again, we loop through the array, just in case.
    for (CBCharacteristic *characteristic in service.characteristics) {

        [peripheral setNotifyValue:YES forCharacteristic:characteristic];
    }

    for (CBCharacteristic *aChar in service.characteristics)
    {
        [_peripheral setNotifyValue:YES forCharacteristic:aChar];
        NSLog(@"Found characteristic : %@ UUID : %@",aChar.value,aChar.UUID);
        NSString *value = [[NSString alloc] initWithData:aChar.value encoding:NSUTF8StringEncoding];
        NSLog(@"Value %@",value);
    }
}

// Invoked when you retrieve a specified characteristic's value, or when the peripheral device notifies your app that the characteristic's value has changed.
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
    if (error) {
        NSLog(@"Error reading characteristics: %@", [error localizedDescription]);
        return;
    }

    if (characteristic.value != nil) {
        //value here.
    }

    NSLog(@"Characteristic value : %@ with ID %@", characteristic.value, characteristic.UUID);
    //[delegate characteristicValueRead:characteristic.value];
    NSLog(@"Caaled characteristic: %@",characteristic.value);
     [self getHeartBPMData:characteristic error:error];
    // Add your constructed device information to your UITextView
}
Run Code Online (Sandbox Code Playgroud)

控制台日志是:

> 2014-03-23 21:37:37.215 CBTutorial[2736:60b] CoreBluetooth[WARNING] <CBCentralManager: 0x1455dec0> is not powered on
2014-03-23 21:37:37.253 CBTutorial[2736:60b] CoreBluetooth BLE hardware is powered on and ready
2014-03-23 21:37:37.257 CBTutorial[2736:60b] Discovered (null)
2014-03-23 21:37:37.261 CBTutorial[2736:60b] Discovered (null)
2014-03-23 21:37:37.263 CBTutorial[2736:60b] Found the : PaSoRi
2014-03-23 21:37:37.493 CBTutorial[2736:60b] Connected: YES
2014-03-23 21:37:37.726 CBTutorial[2736:60b] Discovered service: Unknown (<233e8100 3a1b1c59 9bee1803 73dd03a1>)
2014-03-23 21:37:37.728 CBTutorial[2736:60b] Discovered service: Device Information
2014-03-23 21:37:37.732 CBTutorial[2736:60b] Found characteristic : <0000ffff ff0200fe d7131600> UUID : Unknown (<233e8101 3a1b1c59 9bee1803 73dd03a1>)
2014-03-23 21:37:37.733 CBTutorial[2736:60b] Value (null)
2014-03-23 21:37:37.735 CBTutorial[2736:60b] Found characteristic : <000000> UUID : Unknown (<233e8102 3a1b1c59 9bee1803 73dd03a1>)
2014-03-23 21:37:37.736 CBTutorial[2736:60b] Value 
2014-03-23 21:37:37.738 CBTutorial[2736:60b] Found characteristic : <0000ff00 ff00> UUID : Unknown (<233e8103 3a1b1c59 9bee1803 73dd03a1>)
2014-03-23 21:37:37.739 CBTutorial[2736:60b] Value (null)
2014-03-23 21:37:37.742 CBTutorial[2736:60b] Found characteristic : <> UUID : Unknown (<233e8104 3a1b1c59 9bee1803 73dd03a1>)
2014-03-23 21:37:37.744 CBTutorial[2736:60b] Value 
2014-03-23 21:37:37.746 CBTutorial[2736:60b] Found characteristic : <> UUID : Unknown (<233e8105 3a1b1c59 9bee1803 73dd03a1>)
2014-03-23 21:37:37.747 CBTutorial[2736:60b] Value 
2014-03-23 21:37:37.749 CBTutorial[2736:60b] Found characteristic : <> UUID : Unknown (<233e8106 3a1b1c59 9bee1803 73dd03a1>)
2014-03-23 21:37:37.750 CBTutorial[2736:60b] Value 
2014-03-23 21:37:37.752 CBTutorial[2736:60b] Found characteristic : <> UUID : Unknown (<233e8107 3a1b1c59 9bee1803 73dd03a1>)
2014-03-23 21:37:37.753 CBTutorial[2736:60b] Value 
2014-03-23 21:37:37.756 CBTutorial[2736:60b] Found characteristic : <41697250 61536f52 69> UUID : Manufacturer Name String
2014-03-23 21:37:37.758 CBTutorial[2736:60b] Value AirPaSoRi
2014-03-23 21:37:37.760 CBTutorial[2736:60b] Found characteristic : <4d6f6465 6c4e756d 62657230 31> UUID : Model Number String
2014-03-23 21:37:37.762 CBTutorial[2736:60b] Value ModelNumber01
2014-03-23 21:37:37.764 CBTutorial[2736:60b] Found characteristic : <4669726d 77617265 3031> UUID : Firmware Revision String
2014-03-23 21:37:37.765 CBTutorial[2736:60b] Value Firmware01
2014-03-23 21:37:37.767 CBTutorial[2736:60b] Found characteristic : <536f6674 77617265 3031> UUID : Software Revision String
2014-03-23 21:37:37.768 CBTutorial[2736:60b] Value Software01
Run Code Online (Sandbox Code Playgroud)

为什么我无法接收didUpdateValueForCharacteristic(甚至setNotifyValue:YES)回调?(我已经尝试触摸IC卡)请帮帮我.

Gab*_*ana 5

首先.

在您的代码中,您正在调用:

_myCentralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
[_myCentralManager scanForPeripheralsWithServices:nil options:nil];
Run Code Online (Sandbox Code Playgroud)

在viewDidLoad中.这是个错误.你应该叫scanForPeripheralsWithServices:options:centralManagerDidUpdateState:只有在场景state == CBCentralManagerStatePoweredOn

第二:

这是您发现的外设分配给一个保留一个很好的做法CBPeripheralcentralManager: didConnectPeripheral:,如:

self.myPeripheral = peripheral;
self.myPeripheral.delegate = self;
Run Code Online (Sandbox Code Playgroud)

第三

peripheral: didUpdateValueForCharacteristic: error: 被调用:

  • readValueForCharacteristic:
  • setNotifyValue: forCharacteristic

为什么readValueForCharacteristic:不调用真的很奇怪peripheral: didUpdateValueForCharacteristic: error:,也许还有一些额外的问题.

你能试着打电话setNotifyValue: forCharacteristic看看会发生什么吗?

顺便说一句,最好的CoreBluetooth教程是Apple Core蓝牙编程指南