iOS client Bluetooth library based on CoreBlueTooth package

    Recently, there have been fewer projects in the company, so I have time to share the Bluetooth library packaged in the previous project.

    The native CoreBluetooth library implements data feedback or status update by proxy. This has the disadvantage that the code is very discrete, and it will lead to a high degree of coupling with the business layer, which is inconvenient for porting. Another thing is that when the business layer reads and writes data, it needs to care about too many low-level things such as characteristic values ​​and response modes. as follows:

- (void)writeValue:(NSData *)data forCharacteristic:(CBCharacteristic *)characteristic type:(CBCharacteristicWriteType)type;

    For this reason (slots), CoreBlueTooth is packaged again. The overall module design diagram is as follows:


illustrate:

The wrapper library contains two modules. BLEKit is a client management module that provides Bluetooth control API and Bluetooth data read and write protocol API

The BLEService module is the encapsulation of the Bluetooth application layer protocol (business layer protocol), which is provided to the business layer read and write and data notification API

Instructions:

1. The CentralManager expresses the current state in the form of a state machine

/**
 * @enum BLECentralState
 *
 * @discussion BLE Center Device Manager state machine
 * @constant BLECentralStateInvalid: Invalid state, bluetooth is not turned on or bluetooth is not available
 * @constant BLECentralStateIdle: idle state, no work started or work interrupted
 * @constant BLECentralStateScaning: Scanning peripheral device status
 * @constant BLECentralStateUnDiscovered: no peripheral state found, timeout or other reasons
 * @constant BLECentralStateDiscovered: The surrounding state has been discovered
 * @constant BLECentralStateConnecting: Connecting peripheral state
 * @constant BLECentralStateUnConnected: not connected successfully, connection failed state
 * @constant BLECentralStateConnected: The connection is successful state, note: This state indicates that the peripheral is connected and there are available services
 * @constant BLECentralStateRuning: The normal working state of the BLE central device, indicating that Bluetooth data communication is possible
 */
typedef NS_ENUM(NSInteger, BLECentralState)
{
    BLECentralStateInvalid,
    BLECentralStateIdle,
    BLECentralStateScaning,
    BLECenteralStateUnDiscovered,
    BLECentralStateDiscovered,
    BLECentralStateConnecting,
    BLECentralStateUnConnected,
    BLECentralStateConnected,
    BLECentralStateRuning,
};

The control class API is as follows:

/**
 * Start scanning peripherals until stopScan is called
 */
- (void)startScan;

/**
 * Start scanning for peripherals
 *
 * @param timeout scan timeout
 */
- (void)startScanWithTimeout:(NSInteger)timeout;

/**
 * stop scanning
 */
- (void)stopScan;

/**
 * Start connecting peripherals
 *
 * @param bracelet peripheral properties
 */
- (void)connect:(BLEPeripheral *)bracelet;

/**
 * Disconnect bluetooth
 */
- (void)disConnect;

Status changes are reported to the business layer by way of notification, considering that there are multiple business modules monitoring the BLE status

/**
 * BLE Centeral status changes are reported to the business layer by way of notification. Considering that there are multiple business modules monitoring the BLE status,
 * So use notification as interactive mode.
 */
extern NSString *const  BLECentralStateDidChangedNotification;

2. BLE Serivice implements the business layer protocol stackProtocol based on the protocol in the CentralManager module. The CentralManager module only provides protocol channels, and the specific protocol format needs to be defined by the business layer.

3. For the specific API call method, please refer to the source code:

https://github.com/tomtcl/BluetoothKit

Guess you like

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