Implementation of ios CoreBluetooth peripheral

The use of CoreBlueTooth requires that the device must support Bluetooth 4.0, and the mobile phone model must be iPhone4 or above.

The key to CoreBlueTooth lies in two nouns, perimeter and center. Each IOS device can be a perimeter or a center, but it cannot be both a perimeter and a center.
The periphery can broadcast data, and can tell other devices around to receive the data, and the center is the device that receives the data.

If an ios mobile phone can be used as both a peripheral device and a central device, then both codes of peripheral device and central device must be implemented in the code.

The implementation of peripheral devices is roughly divided into the following steps:
   1. Create a peripheral management class, Peripheral Manager
   2. Add its own information to the Peripheral Manager, such as various services and various features included in the services.
   3. Broadcast service to inform other devices of the existence of the peripheral.
   4. After the central device is connected, interact with the central device.

Specific implementation:
    1. Introduce CoreBluetooth into a class, implement the CBPeripheralManagerDelegate protocol, declare CBPeripheralManager in the .h file, and the services and features that need to be added

#import <CoreBluetooth/CoreBluetooth.h>
@property (nonatomic, strong) CBPeripheralManager *manager;
@property (nonatomic, strong) CBMutableService*cutomService;
@property (nonatomic, strong) CBMutableCharacteristicclass*customCharacteristic;

    

     2. Initialize manager in viewDidLoad.

self.manager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil];

   

     3,实现peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral

- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral {

    switch (peripheral.state) {

        case CBPeripheralManagerStatePoweredOn:

            [self setupService];

            break;

        default:

            NSLog(@"Peripheral Manager did change state");

            break;

    }

}

   This proxy method runs when the device turns bluetooth on or off, and does some warning if the device doesn't support bluetooth. setupService is a method of adding services implemented by itself.

 

    4. Add the service and the characteristics of the service. Implement the setupService method above.

- (void)setupService {
    CBUUID *characteristicUUID = [CBUUID UUIDWithString:kCharaUUID];
    self.customCharacteristic = [[CBMutableCharacteristic alloc] initWithType: characteristicUUID properties:CBCharacteristicPropertyNotify value:nil permissions:CBAttributePermissionsReadable];
    
    CBUUID *serviceUUID = [CBUUID UUIDWithString:kServiceUUID];
    self.customService = [[CBMutableService alloc] initWithType:serviceUUID primary:YES];
    
    [self.customService setCharacteristics:@[self.customCharacteristic]];

    [self.peripheralManager addService:self.customService];
}

     CBCharacteristicPropertyNotify indicates that the characteristic can be subscribed by other devices.

 

    5. Implement peripheralManager:didAddService:error:

- (void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(NSError *)error {
    if (error == nil) {
        // Starts advertising the service
        [self.peripheralManager startAdvertising:@{ CBAdvertisementDataLocalNameKey : @"ICServer", CBAdvertisementDataServiceUUIDsKey : @[[CBUUID UUIDWithString:kServiceUUID]] }];
    }
}

   If [self.peripheralManager addService:self.customService] is executed, that is, the peripheral device adds a service, it will execute peripheralManager:didAddService:error: and then execute startAdvertising to start broadcasting.

  

    6. The method to run after receiving the message from the central device.

//subscription
-(void)peripheralManager:(CBPeripheralManager )peripheral central:(CBCentral )central didSubscribeToCharacteristic:(CBCharacteristic )characteristic;
//unsubscribe
-(void)peripheralManager:(CBPeripheralManager )peripheral central:(CBCentral )central didUnsubscribeFromCharacteristic:(CBCharacteristic )characteristic;
//read message
-(void)peripheralManager:(CBPeripheralManager )peripheral didReceiveReadRequest:(CBATTRequest )request;
//write message
-(void)peripheralManager:(CBPeripheralManager )peripheral didReceiveWriteRequests:(NSArray<CBATTRequest > *)requests;

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326565008&siteId=291194637