iOS BLE SDK Written by oneself

Keywords: SDK iOS github less

iOS-SimpleBLEKit

Simple rough tool classes for BLE on iOS. The process is simple and intuitive. Suitable for beginners.
github address: https://github.com/billzbh/iOS-SimpleBLEKit

I. demo effect

iPad demo:

iphone demo:

Second, the background of writing this very SimpleBLE

At work, there are new development tasks from time to time, which require access to new Bluetooth devices, and the message communication protocols of Bluetooth devices may also be different. So the protocol in SDK written before is not universal. But for the Bluetooth device connection, it's almost the same, the process is the same.

Moreover, the beginner began to program Bluetooth, a pile of delegate s is still a bit embarrassed. I've thought about using the framework of Baby Buletooth before, but once I get started, I find that the cost of learning to get started is still relatively high. For beginners, I think SDK should be as simple as possible. Even if the function is incomplete. The first urgent task for beginners is to get the communication through first. Slowly follow-up with more understanding, can be more complex business to modify SDK source code.

Three. Advantages

  1. Simple, involving only two objects.
  2. It can connect multiple devices at the same time without affecting each other's communication.
  3. All connected devices can be managed, and only disconnection of all devices is currently supported.
  4. Provide some static methods to process NSData for beginners to use

IV. Call process description

(1) Brief introduction of BLEManager object in SDK

  1. Only responsible for the search device and the management of all searched (including connected) devices, SDK will transfer the central device object to the peripheral object SimplePeripheral.
  2. Other ways of handling NSData
  3. Specific usage, you can see SDK. h at a glance.

(2) Simple Peripheral in SDK

  1. Objects that hold CBPeripheral
  2. After setting some UUID parameters before the connection, go straight to work
  3. block can define the complete rules of receiving packets by itself, then SDK can realize sending and receiving data of different communication protocols according to your receiving business logic.

(3) Examples of the simplest process:

  • Import where you need to use BLEManager
#import <SimpleBLEKit/BLEManager.h>
  • Call once in AppDelegate.m
[BLEManager getInstance];//Initialization
  • SimplePeripheral imports are needed
#import <SimpleBLEKit/SimplePeripheral.h>
  • Execute search function, report peripheral object to upper app
[[BLEManager getInstance] stopScan];

[[BLEManager getInstance] startScan:^(SimplePeripheral *peripheral) {
    //Display the name of the search peripheral object
    [peripheral getPeripheralName];           
} timeout:-1];//- 1 means searching all the time, if set to 10, it means stop searching after 10 seconds
  • After you get SimplePeripheral, set up the service UUID, read and write UUID
[_selectedPeripheral setServiceUUID:serviceuuid Notify:notifyuuid Write:writeuuid];
  • Start connecting
[_selectedPeripheral connectDevice:^(BOOL isPrepareToCommunicate) {
    NSLog(@"equipment%@",isPrepareToCommunicate?@"Connected":@"Disconnected");
    //Notify UI Layer Connection Results    
}];
  • Setting Packet Receiving Rules

For example, in the communication protocol you debug, if the number of bytes reaches 30, the data will be collected, then you can do this:

[_selectedPeripheral setResponseEvaluator:^BOOL(NSData * inputData) {
    if(inputData.length>=30)
        return YES;//Report Pack Complete
    return NO;    
}];

For example, your agreement may be more complex. Provides that the first byte must be 02, the second byte must be the length of the subsequent valid data, and the last byte must be 03.

Start DataLen Data End
0x02 0x?? N bytes = DataLen 0x03

Then you can do this:

[_selectedPeripheral setResponseEvaluator:^BOOL(NSData * inputData) {
    Byte *packBytes = (Byte*)[inputData bytes];
    if (packBytes[0]!=0x02) {
        return NO;
    }
    int dataLen = packBytes[1];
    int inputDataLen = (int)inputData.length;
    //Packet complete data should be first 1 byte + length 1 byte + end 1 byte + intermediate data N byte
    if(inputDataLen < dataLen + 1 + 1 + 1)
        return NO;

    if(packBytes[1+dataLen]!=0x03)
        return NO;

    return YES;//Report Pack Complete
}];
  • Send and receive data

Classical usage:

[_selectedPeripheral sendData:data receiveData:^(NSData *outData, NSString *error) 
{   
    if(error){
        //A timeout error occurred
    }else{
        //Receive a complete package data according to the rules you set up before. Analyse the meaning of data by oneself
    }
} Timeout:-1];

Other uses:
1. Setting data only, while block is nil and timeout is - 1 means sending only and not caring whether data is received or not.
2. Setting data, block only, but timeout of - 1 means that data needs to be received, but never timed out.
3. Setting block only, but data is nil and timeout is - 1 means waiting for Notify's data

  • Disconnect
[_selectedPeripheral disconnect];

This is the complete communication process.

V. Other interfaces

//Bluetooth name
-(NSString* _Nonnull)getPeripheralName;
//Query whether the connection has been made
-(BOOL)isConnected;
//Whether to open log printing, default is NO
-(void)setIsLog:(BOOL)isLog;
//Whether to automatically reconnect after disconnection, default is NO
-(void)setIsAutoReconnect:(BOOL)isAutoReconnect;
//Set the notification type for writing data, default is CBCharacteristicWriteWithoutResponse
-(void)setResponseType:(CBCharacteristicWriteType)ResponseType;
//Set whether to send subcontracted. If it is greater than 0, it will be subcontracted according to the numerical value. If it is less than 0, it will not be subcontracted. The default is no subcontracting
-(void)setMTU:(int)MTU;
//Sets whether the data is received and returned to the Bluetooth device to respond to the data. Default should not answer
-(void)setAck:(BOOL)ack withData:(NSData* _Nullable)data withACKEvaluator:(PacketEvaluator _Nullable)ackEvaluator;

//Not tested yet, but actually using setResponseEvaluator
-(void)setResponseMatch:(NSString* _Nonnull)prefixString sufferString:(NSString* _Nonnull)sufferString HighByteIndex:(int)highIndex LowByteIndex:(int)lowIndex;
-(void)setResponseMatch:(NSString* _Nonnull)prefixString sufferString:(NSString* _Nonnull)sufferString NSDataExpectLength:(int)expectLen;

Notes

  • Initialize the BLEManager object in AppDelegate.
  • I heard that BLE can connect up to 7 peripherals without testing.
  • Import the project directly to copy the generated framework. If you need to merge the simulator version with the live version. See CreaeFrameWork.txt in the framework project
  • Backstage mode is not considered for the time being, in fact, I do not have a good solution, to be simple and rude.
  • I don't need to read broadcast data or RSS. I haven't used it so far. I need to use myself to implement it in peripherals.
  • Thank you for CCTV, for your daughter, for the rubbish Apple BLE interface. Oh, fuck me!

Posted by healthnut on Sat, 20 Apr 2019 12:30:33 -0700