- This SDK only support devices based on LW004-PB.
- We divide the communications between SDK and devices into two stages: Scanning stage, Connection stage.For ease of understanding, let’s take a look at the related classes and the relationships between them.
MKPBCentralManager
:global manager, check system’s bluetooth status, listen status changes, the most important is scan and connect to devices;
MKPBInterface
: When the device is successfully connected, the device data can be read through the interface in MKPBInterface
;
MKPBInterface+MKPBConfig
: When the device is successfully connected, you can configure the device data through the interface in MKPBInterface+MKPBConfig
;
in this stage, MKPBCentralManager
will scan and analyze the advertisement data of LW007 devices.
The device broadcast information includes whether a password is required when connecting.
1.Enter the password to connect, then call the following method to connect:
connectPeripheral:password:sucBlock:failedBlock:
2.You do not need to enter a password to connect, call the following method to connect:
connectPeripheral:sucBlock:failedBlock:
- Xcode9+, due to the DFU and Zip Framework based on Swift4.0, so please use Xcode9 or high version to develop;
- iOS12, we limit the minimum iOS system version to 12.0;
CocoaPods
SDK-PIR is available through CocoaPods.To install it, simply add the following line to your Podfile, and then import <MKLoRaWAN-PB/MKPBSDK.h>:
pod 'MKLoRaWAN-PB/SDK'
-
!!!on iOS 10 and above, Apple add authority control of bluetooth, you need add the string to “info.plist” file of your project: Privacy - Bluetooth Peripheral Usage Description - “your description”. as the screenshot below.
-
!!! In iOS13 and above, Apple added permission restrictions on Bluetooth APi. You need to add a string to the project’s info.plist file: Privacy-Bluetooth Always Usage Description-“Your usage description”.
First of all, the developer should get the sharedInstance of Manager:
MKPBCentralManager *manager = [MKPBCentralManager shared];
- 1.Set the scan delegate and complete the related delegate methods.
manager.delegate = self;
- 2.you can start the scanning task in this way:
[manager startScan];
- 3.at the sometime, you can stop the scanning task in this way:
[manager stopScan];
The MKPBCentralManager
contains the method of connecting the device.
/// Connect device function.
/// @param peripheral peripheral
/// @param password Device connection password,8 characters long ascii code
/// @param sucBlock Success callback
/// @param failedBlock Failure callback
- (void)connectPeripheral:(nonnull CBPeripheral *)peripheral
password:(nonnull NSString *)password
sucBlock:(void (^)(CBPeripheral *peripheral))sucBlock
failedBlock:(void (^)(NSError *error))failedBlock;
/// Connect device function.
/// @param peripheral peripheral
/// @param sucBlock Success callback
/// @param failedBlock Failure callback
- (void)connectPeripheral:(nonnull CBPeripheral *)peripheral
sucBlock:(void (^)(CBPeripheral *peripheral))sucBlock
failedBlock:(void (^)(NSError *error))failedBlock;
Through the manager, you can get the current Bluetooth status of the mobile phone, and the connection status of the device. If you want to monitor the changes of these two states, you can register the following notifications to achieve:
- When the Bluetooth status of the mobile phone changes,
mk_pb_centralManagerStateChangedNotification
will be posted.You can get status in this way:
[[MKPBCentralManager shared] centralStatus];
- When the device connection status changes,
mk_pb_peripheralConnectStateChangedNotification
will be posted.You can get the status in this way:
[MKPBCentralManager shared].connectState;
Register for mk_pb_deviceDisconnectTypeNotification
notifications to monitor data.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(disconnectTypeNotification:)
name:@"mk_pb_deviceDisconnectTypeNotification"
object:nil];
- (void)disconnectTypeNotification:(NSNotification *)note {
NSString *type = note.userInfo[@"type"];
/*
After connecting the device, if no password is entered within one minute, it returns 0x01. After successful password change, it returns 0x03, the device has no data communication for three consecutive minutes, it returns 0x02, and the shutdown protocol is sent to make the device shut down and return 0x04.Reset the device, return 0x05.
*/
}
#pragma mark - mk_pb_centralManagerLogDelegate
- (void)mk_pb_receiveLog:(NSString *)deviceLog {
//Log
}
- 20220520 first version;