23

Bluetooth デバイス (BR-LE4.0-S2) からデータを読み込もうとしています。BLE デバイスは接続できたが、データを読み取ることができなかった。BLE サービスとその特性に関する仕様はありません。ここで私の問題が- (void)peripheral:didUpdateValueForCharacteristic:error:呼び出されていません。チュートリアル「https://developer.apple.com/library/ios/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/PerformingCommonCentralRoleTasks/PerformingCommonCentralRoleTasks.html#//apple_ref/doc/uid/TP40013257-CH3-SW2」に従いました。コード。

私の要件は、BLE デバイスから連続してデータを読み取ることです。どんな助けでも大歓迎です。

- (void)viewDidLoad
{
    self.myCentralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
    self.peripheral = [[CBPeripheral alloc] init];
    self.peripheral.delegate = self;
    [super viewDidLoad];
}

- (void) centralManagerDidUpdateState:(CBCentralManager *)central {

    switch (central.state) {
        case CBCentralManagerStatePoweredOn:
        [self.myCentralManager scanForPeripheralsWithServices:nil options:nil];
            break;
        default:
            NSLog(@"Central Manager did change state");
            break;
    }

}

- (void)centralManager:(CBCentralManager *)central
 didDiscoverPeripheral:(CBPeripheral *)peripheral
     advertisementData:(NSDictionary *)advertisementData
                  RSSI:(NSNumber *)RSSI {

    NSLog(@"Discovered %@", peripheral.name);
    [self.myCentralManager stopScan];
    NSLog(@"Scanning stopped");

    if (self.peripheral != peripheral) {
        self.peripheral = peripheral;
        NSLog(@"Connecting to peripheral %@", peripheral);
        // Connects to the discovered peripheral
    [self.myCentralManager connectPeripheral:peripheral options:nil];
    }
}

- (void)centralManager:(CBCentralManager *)central
didConnectPeripheral:(CBPeripheral *)peripheral {

    NSLog(@"Peripheral connected");

    NSLog(@"Peripheral services : %@",peripheral.services );

    [self.peripheral setDelegate:self];

    [peripheral discoverServices:nil];

}
- (void)peripheral:(CBPeripheral *)peripheral
didDiscoverServices:(NSError *)error {

    if (error) {
        NSLog(@"Error discovering service: %@", [error localizedDescription]);
        return;
    }

    for (CBService *service in peripheral.services) {
        [peripheral discoverCharacteristics:nil forService:nil];
    }
}

- (void)peripheral:(CBPeripheral *)peripheral
didDiscoverCharacteristicsForService:(CBService *)service
             error:(NSError *)error {

    int i = 0;
    for (CBCharacteristic *characteristic in service.characteristics) {

[peripheral setNotifyValue:YES forCharacteristic: characteristic];

    }
}

- (void)peripheral:(CBPeripheral *)peripheral
didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic
             error:(NSError *)error {

    NSData *data = characteristic.value;
    NSString *value = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];

    NSLog(@"Value %@",value);
    NSString *stringFromData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    NSLog(@"Data ====== %@", stringFromData);
}


- (void)peripheral:(CBPeripheral *)peripheral
didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic
             error:(NSError *)error {

    if (error) {
        NSLog(@"Error changing notification state: %@",
              [error localizedDescription]);
    }
    NSString *value = [[NSString alloc] initWithData:self.interestingCharacteristic.value encoding:NSUTF8StringEncoding];

    NSLog(@"Value %@",value);

    NSLog(@"description: %@, descriptors: %@, properties: %d, service :%@, value:%@", characteristic.description, characteristic.descriptors, characteristic.properties, characteristic.service, characteristic.value);
    NSData *data = characteristic.value;

    if (characteristic.isNotifying) {
        NSString *stringFromData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        [peripheral readValueForCharacteristic:characteristic];

        NSLog(@"Data ====== %@", @"ccdc");

    } else {
        [self.myCentralManager cancelPeripheralConnection:peripheral];

    }

}

- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
    NSLog(@"Peripheral Disconnected");
    self.peripheral = nil;

    // We're disconnected, so start scanning again
    NSDictionary *scanOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:CBCentralManagerScanOptionAllowDuplicatesKey];

    [self.myCentralManager scanForPeripheralsWithServices:nil options:scanOptions];
}
4

4 に答える 4

77

BLE ペリフェラル デバイスから値を読み取るには、次の手順に従います。

  1. 利用可能なデバイスをスキャン

    NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBCentralManagerScanOptionAllowDuplicatesKey];
    [self.myCentralManager scanForPeripheralsWithServices:nil options:options];`
    
  2. デバイスを検出すると、「didDiscoverPeripheral」デリゲート メソッドへのコールバックが返されます。次に、検出されたBLEデバイスとの接続を確立します

    -(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
    
        //Connect detected device....
        if (!peripheral.isConnected) {
            peripheral.delegate = self;
            [bluetoothManager_ connectPeripheral:peripheral options:nil];
    
        }
    }
    
  3. 接続に成功したら、BLE デバイスで利用可能なすべてのサービスを要求します

    - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{
    NSLog(@"Peripheral Connected");
    
        // Make sure we get the discovery callbacks
        peripheral.delegate = self;
    
        // Search only for services that match our UUID
        [peripheral discoverServices:nil];
    }
    
  4. 各サービスで利用可能なすべての特性をリクエストする

    - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
    
        if (error) {
            NSLog(@"Error discovering services: %@", [error localizedDescription]);
            return;
        }
        // Loop through the newly filled peripheral.services array, just in case there's more than one.
        for (CBService *service in peripheral.services) {
            [peripheral discoverCharacteristics:nil forService:service];
        }
    }
    
  5. 必要な特性の詳細を取得したら、それをサブスクライブする必要があります。これにより、含まれるデータが必要であることをペリフェラルに知らせます。

    - (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) {
            if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:REQUIRED_CHARA_ID]]) {
                // If it is, subscribe to it
                [peripheral setNotifyValue:YES forCharacteristic:characteristic];
            }
        }
    }
    
  6. これらのすべての手順を完了すると、BLE デバイスはデリゲート メソッドを介して通知ステータスの変更を通知します。

    - (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
    
        if (error) {
            NSLog(@"Error changing notification state: %@", error.localizedDescription);
        }
    
        // Notification has started
        if (characteristic.isNotifying) {
            NSLog(@"Notification began on %@", characteristic);
        }
    }
    

次の方法でBLEデバイスからの通知を受け取ります

- (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.        
    }
}
于 2013-11-12T13:23:16.073 に答える