从BLE设备读取数据 [英] Read data from BLE device

查看:126
本文介绍了从BLE设备读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从蓝牙设备(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 。以下是我的代码。

I am trying to read data from a bluetooth device (BR-LE4.0-S2). I was able to connect BLE device, but not able to read data from it.I don't have any specification about BLE services and it's characteristics. Here what my issue is - (void)peripheral:didUpdateValueForCharacteristic:error: not getting called. I followed tutorial "https://developer.apple.com/library/ios/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/PerformingCommonCentralRoleTasks/PerformingCommonCentralRoleTasks.html#//apple_ref/doc/uid/TP40013257-CH3-SW2" .Following is my code.

我的要求是读取数据从BLE设备不断。非常感谢任何帮助。

What my requirement is to read data continuously from BLE device. Any help is greatly appreciated.

- (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];
}


推荐答案

从a读取值BLE外围设备,请按照以下步骤操作

To read a value from a BLE peripheral device, follow these steps


  1. 扫描可用设备

  1. Scan for avilable devices

NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBCentralManagerScanOptionAllowDuplicatesKey];
[self.myCentralManager scanForPeripheralsWithServices:nil options:options];`


  • On检测到一个设备,将回到didDiscoverPeripheral委托方法。然后与检测到的BLE设备建立连接

  • On detecting a device, will get a call back to "didDiscoverPeripheral" delegate method. Then establish a connection with detected BLE device

    -(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];
    
        }
    }
    


  • 成功连接后,请求BLE设备中提供的所有服务

  • On successful connection, request for all the services available in the BLE device

    - (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];
    }
    


  • 请求每项服务中可用的所有特征

  • Request all the characteristics available in each services

    - (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];
        }
    }
    


  • 获得所需的特征细节后,我们需要订阅它,这让外围设备知道我们想要它包含的数据

  • Once we get the required characteristics detail, we need to subscribe to it, which lets the peripheral know we want the data it contains

    - (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];
            }
        }
    }
    


  • 完成所有这些步骤,BLE设备将通过委托方法通知您通知状态更改

  • Completing all these steps, BLE device will let you know the notification status change through delegate method

    - (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设备的任何通知

    You will recieve any notification from BLE device in the following method

    - (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.        
        }
    }
    

    这篇关于从BLE设备读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆