iOS developers use development of Bluetooth 4.0

Reprinted from https://www.jianshu.com/p/f0e4b0e98336

2015, when the project himself wrote a Bluetooth 4.0, when forget to write blog, write this blog recently seen quite detailed, so it is reproduced

One: Introduction

Bluetooth divided Bluetooth 2.0 and Bluetooth 4.0.
Bluetooth 2.0 is the traditional Bluetooth, Bluetooth is also known as traditional classic Bluetooth.
Because low-power Bluetooth 4.0, it is also called a low-power blue (the BLE), one set of three specifications will include a conventional Bluetooth technology, high-speed technology and low energy technology.

This article describes the use of 4.0 to BLE and solve related problems.

Two: BLE two modes

BLE is divided into two modes CBCentralMannager center model and CBPeripheralManager peripheral mode, here and share with you the main center of the development and use CBCentralMannager mode.

CBCentralMannager center model

Mobile phone (app) as the center, connecting other peripherals scene. Detailed process is as follows:

  1. The establishment of a central role
  2. Scanning peripherals
  3. Peripheral found
  4. Connecting peripherals
    4.1 connection fails
    4.2 Disconnect the
    4.3 connection is successful
  5. Scanning peripherals services
    5.1 peripherals discovery and obtain services
  6. Scanning peripherals corresponding service features
    6.1 features to discover and obtain the corresponding peripheral services
    6.2 write data to the corresponding feature
  7. Notification subscription feature
    7.1 the read data according to the characteristic

CBPeripheralManager Peripheral Mode

Use the phone as a scene peripheral devices connected to other centers operating.
PS: because of security and closeness Apple devices, Apple devices can not perform file transfer functions and links to other Bluetooth devices, so the program iOS Bluetooth development center model is CBCentralMannager programming majority.

  1. Established peripheral role
  2. Set of services and features local peripherals
  3. Posted peripherals and features
  4. Broadcast Service
  5. Response write request
  6. Send Update feature value, Subscription Center

Three: BLE development steps

Before introducing CBCentralMannager center model development steps, you first need to project as follows:

#import  " ESPFBYBLEHelper.h " 
#import <CoreBluetooth / CoreBluetooth.h> @interface ESPFBYBLEHelper () <CBCentralManagerDelegate, CBPeripheralDelegate>
 // center managers (connection management apparatus and scan) 
@Property (nonatomic, strong) CBCentralManager * centralManager;
 // storage device 
@Property (nonatomic, strong) NSMutableArray * peripherals;
 // scanning device to 
@Property (nonatomic, strong) cBPeripheral * cbPeripheral;
 // peripheral status @Property (nonatomic, ASSIGN) CBManagerState peripheralState;
 @end // Bluetooth 4.0 device name static NSString * const





= kBlePeripheralName @ " lighte290 " ;
 // Notification Service 
static NSString * const kNotifyServerUUID = @ " FF03 " ;
 // writing services 
static NSString * const kWriteServerUUID = @ " FFFF " ;
 // notice eigenvalues 
static NSString * const kNotifyCharacteristicUUID = @ " FF05 " ;
 // write the eigenvalues 
static NSString * const kWriteCharacteristicUUID = @" FF08 ";
@implementation ESPFBYBLEHelper

Which it needs to import framework CoreBluetooth

#import <CoreBluetooth/CoreBluetooth.h>

Compliance CBCentralManagerDelegate, CBPeripheralDelegate agreement

@interface ESPFBYBLEHelper ()<CBCentralManagerDelegate,CBPeripheralDelegate>

Bluetooth is then necessary to detect the state, as follows:

// called when the update 
- ( void ) centralManagerDidUpdateState: (CBCentralManager * ) Central 
{ 
    Switch (central.state) {
         Case CBManagerStateUnknown: { 
            NSLog ( @ " as known in the state " ); 
            self.peripheralState = central.state; 
        } 
            BREAK ;
         Case CBManagerStateResetting: 
        { 
            NSLog ( @ " reset state " ); 
            self.peripheralState = central.state; 
        } 
            BREAK ;
        Case CBManagerStateUnsupported: 
        { 
            NSLog ( @ " unsupported state " ); 
            self.peripheralState = central.state; 
        } 
            BREAK ;
         Case CBManagerStateUnauthorized: 
        { 
            NSLog ( @ " unauthorized state " ); 
            self.peripheralState = central.state; 
        } 
            BREAK ;
         Case CBManagerStatePoweredOff: 
        { 
            NSLog ( @ " closed state " );
            self.peripheralState = central.state;
        }
            break;
        case CBManagerStatePoweredOn:
        {
            NSLog(@"开启状态-可用状态");
            self.peripheralState = central.state;
            NSLog(@"%ld",(long)self.peripheralState);
        }
            break;
        default:
            break;
    }
}

Add properties and constants, the constant need to be configured according to their own projects.
Here only one step can be implemented depending on the implementation process, the core code is as follows:

1. Establish a central role
self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
2. Scan peripherals
if (self.peripheralState ==  CBManagerStatePoweredOn){
    [self.centralManager scanForPeripheralsWithServices:nil options:nil];
}
3. Peripheral found
/ * * 
 A scanning device to 
 @param central center manager 
 @param peripheral device to scan 
 @param advertisementData advertisement information 
 @param RSSI signal strength 
 * / 
- ( void ) centralManager: (CBCentralManager *) Central didDiscoverPeripheral: (CBPeripheral *) Peripheral advertisementData : (NSDictionary <NSString *, ID > *) advertisementData the RSSI: (* the NSNumber ) the RSSI 
{ 
    NSLog ( @ " % @ " , [NSString stringWithFormat: @ " discover devices, device name:% @ " , peripheral.name]); 
}

4. Peripheral connector

[self.centralManager connectPeripheral:peripheral options:nil];
  • 4.1 connection failed didFailToConnectPeripheral
/ * * 
 The connection fails 
 @param central management center 
 apparatus @param peripheral connection failure 
 @param error error 
 * / 
- ( void ) centralManager: (CBCentralManager *) Central didFailToConnectPeripheral: (CBPeripheral *) Peripheral error: (the NSError * ) error 
{ 
    NSLog ( @ " % @ " , @ " connection failed " ); 
}
  • 4.2 Disconnect
/ * * 
 Disconnection 
 @param central center manager 
 @param peripheral device is disconnected 
 @param error error 
 * / 
- ( void ) centralManager: (CBCentralManager *) Central didDisconnectPeripheral: (CBPeripheral *) Peripheral error: (* the NSError ) error 
{ 
    NSLog ( @ " % @ " , @ " disconnected " ); 
}
  • 4.3 connection is successful
/ * * 
 Successful connection 
 
 @param central center manager 
 @param peripheral device connection success 
 * / 
- ( void ) centralManager: (CBCentralManager *) Central didConnectPeripheral: (CBPeripheral * ) Peripheral 
{ 
    NSLog ( @ " connected device:% @ Success " , peripheral.name); 
    [self.centralManager stopScan]; 
}
The scan service peripherals
// set the device agent 
. Peripheral delegate = Self;
 // Services: nil behalf scan all incoming service 
[peripheral discoverServices: nil];

5.1 peripherals to discover and obtain service

/ * * 
 Scanned service 
 corresponding to service device @param peripheral 
 @param error scan error 
 * / 
- ( void ) Peripheral: (CBPeripheral *) Peripheral didDiscoverServices: (the NSError * ) error 
{ 
    // iterate through all of the services 
    for (CBService * -service in peripheral.services) 
    { 
        NSLog ( @ " service:% @ " , service.UUID.UUIDString); 
    } 
}
6. Peripheral scanning corresponding features and services
// get the corresponding services 
        IF (! [Service.UUID.UUIDString as isEqualToString: kWriteServerUUID]) 
        { 
            return ; 
        } 
        // The service feature to scan 
        [peripheral discoverCharacteristics: nil forService: service ];

6.1 peripherals to discover and obtain the corresponding service features

/ * * 
 Scanned corresponding features 
 @param peripheral equipment 
 and services @param service feature corresponding 
 @param error error 
 * / 
- ( void ) Peripheral: (CBPeripheral *) Peripheral didDiscoverCharacteristicsForService: (CBService *) error-Service: (the NSError * ) error 
{ 
    NSLog ( @ " % @ " , Peripheral); 
}

6.2 write data to the corresponding characteristic

[peripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
7. subscription notification feature
if ([characteristic.UUID.UUIDString isEqualToString:kNotifyCharacteristicUUID]){
  [peripheral setNotifyValue:YES forCharacteristic:characteristic];
}

According to a feature data read didUpdateValueForCharacteristic 7.1

/ * * 
 According to a feature data read 
 @param peripheral device data corresponding to the read 
 @param characteristic features 
 @param error error 
 * / 
- ( void ) Peripheral: (CBPeripheral *) Peripheral didUpdateValueForCharacteristic: (give to the nonnull CBCharacteristic *) Characteristic error: (Nullable the NSError * ) error 
{ 
    IF ([characteristic.UUID.UUIDString as isEqualToString: kNotifyCharacteristicUUID]) 
    { 
        NSData * Data = characteristic.value; 
        NSLog ( @ " % @ " , Data); 
    } 
}

 

Guess you like

Origin www.cnblogs.com/hecanlin/p/11578097.html