Implementation of ios CoreBluetooth central device

The implementation of the central device is roughly divided into the following steps:

1. Create a central management class, CentralManager

2. Scan and discover peripherals.

3. Connect peripherals.

4. Scan all services connected to peripherals.

5. Scan the characteristics of all searched services.

6. Read or write or subscribe features.

 

 

Implementation:

 

1. The same as the peripherals, CoreBluetooth is introduced first. After that, two protocols are implemented, namely CBCentralManagerDelegate and CBPeripheralDelegate. Declare a CBCentralManager in .h named centralManager. Declare a mutable array to access all connected peripherals.

#import <CoreBluetooth/CoreBluetooth.h>
@property (strong, nonatomic) CBCentralManager *centralManager;
@property (strong, nonatomic) NSMutableArray *peripherals;

 

2. Initialize the centralManager declared in .h in .m.

centralManager = [[CBCentralManager alloc] initWithDelegate:self queue: nil options:nil];

 

3. Implement the proxy method centralManagerDidUpdateState, like the implementation of peripheral devices, to detect whether the device supports Bluetooth enabled.

- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
    switch (central.state) {
    case CBPeripheralManagerStatePoweredOn:
        [central scanForPeripheralsWithServices:nil options:nil];
        break;
    default:
        NSLog(@"Bluetooth function is not supported or not turned on.");
        break;
    }
}

    The above scanForPeripheralsWithServices:nil is the method used to perform the scan for peripherals.

 

4. Because scanForPeripheralsWithServices:nil is executed above, the method after scanning to peripherals should be implemented next.

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

    if ([peripheral.name isEqualToString:kPeripheralName]) {

        if(![self.peripherals containsObject:peripheral]){
            [self.peripherals addObject:peripheral];
        }        
        [self.centralManager connectPeripheral:peripheral options:nil];
    }
}

     If the searched device already exists in the array, connect directly. If not, add the peripheral to the array, and then connect. The connectPeripheral method is used to connect the searched peripherals.

 

5. Connect to external devices

- (void)centralManager:(CBCentralManager *)central
        didConnectPeripheral:(CBPeripheral *)peripheral
        {
    [self.centralManager stopScan];
    peripheral.delegate = self;
    [peripheral discoverServices:@[[CBUUID UUIDWithString:kServiceUUID]]];
}

    Because the peripheral is connected, use the stopScan method to stop scanning. Then execute discoverService to scan for services.

 

6. After scanning the peripheral services, didDiscoverServices will be called. So implement the didDiscoverServices method.

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

    CBUUID *serviceUUID = [CBUUID UUIDWithString:kServiceUUID];
    CBUUID *characteristicUUID = [CBUUID UUIDWithString:kCharacteristicUUID];
    for (CBService *service in peripheral.services) {
        if([service.UUID isEqual:serviceUUID]){
            [peripheral discoverCharacteristics:nil forService:service];
        }
    }
}

    After searching for the service, execute the discoverCharacteristics method to search for all the characteristics.

 

7. After scanning the characteristics of the service, read or write or subscribe for the characteristics.

- (void)peripheral:(CBPeripheral *)peripheral
        didDiscoverCharacteristicsForService:(CBService *)service
             error:(NSError *)error
{
    CBUUID *serviceUUID = [CBUUID UUIDWithString:kServiceUUID];
    CBUUID *characteristicUUID = [CBUUID UUIDWithString:kCharacteristicUUID];
    if ([service.UUID isEqual:serviceUUID]) {
        for (CBCharacteristic *characteristic in service.characteristics) {
            if ([characteristic.UUID isEqual:characteristicUUID]) {

                [peripheral setNotifyValue:YES forCharacteristic:characteristic];

            }
        }
    }
}

      Above are all the features in the search service, if a feature matches what I'm looking for, do something with that feature.

      The above code uses subscription, and can also read and write, depending on the type defined when the feature is defined in the peripheral.

readValueForCharacteristic //read
setNotifyValue //Subscribe
writeValue   //写

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326527198&siteId=291194637