iOS BLE SDK Document

Demo

https://github.com/iKangtai/BLE_SDK_Demo_iOS

Bluetooth Low Energy SDK

Define

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/// YCBLEState
typedef NS_ENUM(NSInteger, YCBLEState) {
/// Valid
YCBLEStateValid = 0,
/// Unknown
YCBLEStateUnknown,
/// Unsupported
YCBLEStateUnsupported,
/// Unauthorized
YCBLEStateUnauthorized,
/// PoweredOff
YCBLEStatePoweredOff,
/// Resetting
YCBLEStateResetting
};

/// Define BLE notify type
typedef NS_ENUM(NSInteger, YCNotifyType) {
/// Set the thermometer to centigrade
YCNotifyTypeSetTemperatureUnitC,
/// Set the thermometer to fahrenheit
YCNotifyTypeSetTemperatureUnitF,
/// Set time
YCNotifyTypeSetTime,
/// Get the thermometer's power
YCNotifyTypeGetPower,
/// Get Firmware Version
YCNotifyTypeGetFirmwareVersion,
/// Return temperatures' number to thermometer
YCNotifyTypeTemperatureACK,
/// Transmit temperature
YCNotifyTypeTransmitTemperature,
};

/// Temperature flag
typedef NS_ENUM(NSInteger, YCTemperatureFlag) {
/// Online
YCTemperatureFlagOnline,
/// Offline
YCTemperatureFlagOffline,
};

/// Thermometer type
typedef NS_ENUM(NSInteger, YCThermometerType) {
/// Thermometer
YCThermometerTypeThree,
/// Forehead temperature gun
YCThermometerTypeFour,
};

YCPeripheralDelegate

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@protocol YCPeripheralManagerDelegate <NSObject>

@required

-(void)peripheralManager:(YCPeripheralManager *)manager
didUpdateState:(YCBLEState)state;

-(void)peripheralManager:(YCPeripheralManager *)manager
didFindPeripherals:(NSArray <YCPeripheral *>*)peripherals;

-(void)peripheralManager:(YCPeripheralManager *)manager
didConnectPeripheral:(YCPeripheral *)peripheral;

-(void)peripheralManager:(YCPeripheralManager *)manager
didUploadWithPeripheral:(YCPeripheral *)peripheral
temperatures:(NSArray <YCTemperature *>*)temperatures;

-(void)peripheralManager:(YCPeripheralManager *)manager
didSetNotifyToPeripheral:(YCPeripheral *)peripheral
type:(YCNotifyType)type
result:(NSString *)result
error:(NSError * _Nullable)error;

@optional

-(void)peripheralManagerDidStartScan:(YCPeripheralManager *)manager;

-(void)peripheralManagerDidStopScan:(YCPeripheralManager *)manager;

-(void)peripheralManager:(YCPeripheralManager *)manager
didFailToConnectPeripheral:(YCPeripheral *)peripheral
error:(NSError *)error;

-(void)peripheralManager:(YCPeripheralManager *)manager
didDisConnectPeripheral:(YCPeripheral *)peripheral
error:(NSError * _Nullable)error;

@end

Class

YCPeripheral

1
2
3
4
5
6
7
8
9
10
11
12
13

/// The current connected peripheral/divice
@property (nonatomic, strong, nullable) CBPeripheral *peripheral;
/// Firmware version
@property (nonatomic, copy) NSString *firmwareVersion;
/// The MAC address of current connected device
@property (nonatomic, copy) NSString *macAddress;
/// Peripheral Name
@property (nonatomic, copy) NSString *name;
/// Type
@property (nonatomic, assign) YCThermometerType type;
/// Connect state
@property (nonatomic, assign) BOOL connected;

YCTemperature

1
2
3
4
5
6
7

/// Temperature
@property (nonatomic, assign) double temperature;
/// The measure time
@property (nonatomic, copy) NSString *time;
/// Flag
@property (nonatomic, assign) YCTemperatureFlag flag;

YCPeripheralManager

1
2
3
4
5
6
7
/// Delegate
@property (nonatomic, weak) id <YCPeripheralManagerDelegate> delegate;
/// Maximum number of connections allowed, max is 5, min is 1
@property (nonatomic, assign) NSInteger maxPeripherals;

/// The shared Thermometer object for the process.
+(instancetype)shared;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
Start scan.
- parameters:
- macList: The thermometer's mac address will be connected. If this is nil, the SDK will connect the first thermometer be found.
*/
-(void)startScan;


/**
Connect the currently connected device.
*/
-(void)connectPeripheral:(YCPeripheral *)peripheral;

/**
Disconnect the currently connected device.
*/
-(void)disconnectPeripheral:(YCPeripheral *)peripheral;

/**
Stop scan.
*/
-(void)stopScan;

/**
Send specific commands to the device.
- parameters:
- type: BLENotifyType
*/
-(void)setNotifyToPeripheral:(YCPeripheral *)peripheral type:(YCNotifyType)type value:(NSInteger)value;

Access Guide

Add SDK reference

Add YCPeripheral.framework to your project.

Init before using SDK

Set [YCPeripheralManager shared]’s delegate and implementation YCPeripheralManagerDelegate protocol.

Scan nearby Bluetooth devices

[[YCPeripheralManager shared] startScan];

Connect the Bluetooth device

[[YCPeripheralManager shared] connectPeripheral:peripheral];

Manually release resources when the call is completed

[[YCPeripheralManager shared] stopScan];

[[YCPeripheralManager shared] disconnectPeripheral:peripheral];