1
wei
2021-01-21 62d098cb78296feaa6f786a20748921338db838c
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//
// TuyaSmartBLEDeviceProtocol.h
// TuyaSmartBLEKit
//
// Copyright (c) 2014-2021 Tuya Inc. (https://developer.tuya.com)
 
#import <TYBluetooth/TYBluetooth.h>
#import "TuyaSmartBLEDeviceProtocol.h"
 
@class TYBLEScan;
@class TYBLEAgent;
@protocol TuyaSmartBLEDeviceProtocol;
 
/// @brief TuyaSmartBLEDeviceProtocol provides protocol methons for developers to implement activator, control or OTA and others by itself protocol.
 
NS_ASSUME_NONNULL_BEGIN
 
#pragma mark - Scan Protocol
 
@protocol TuyaSmartBLEScanProtocol <NSObject>
 
typedef void(^TYBLEScanResultHandher)(id<TuyaSmartBLEDeviceProtocol> deviceInfo);
 
/// Results of the scanning inactive devices.
///
/// @param agent        Scan agent.
/// @param scan         Scan class itself.
/// @param peripheral   Scanned Bluetooth peripheral.
/// @param error        Scan Error.
/// @param handler      Scan callback.
- (void)bleAgent:(TYBLEAgent *)agent bleScan:(TYBLEScan *)scan didScanBLEDevicePeripheral:(TYBLEPeripheral *)peripheral error:(NSError *)error scanResultHandher:(TYBLEScanResultHandher)handler;
 
@end
 
#pragma mark -------------------------------------------------------------
 
 
#pragma mark - Active Protocol
 
@protocol TuyaSmartBLEActiveProtocol <NSObject>
 
/// Activator BLE device.
///
/// @param deviceInfo       The advertisingData model for the BLE device.
/// @param homeId           The Id for the current home.
/// @param success          When activator successfully, this block will be called with DeviceModel.
/// @param failure          This block will be called if some error occurred.
- (void)startActiveWithDevice:(id<TuyaSmartBLEDeviceProtocol>)deviceInfo
                       homeId:(long long)homeId
                      success:(void(^)(TuyaSmartDeviceModel *deviceModel))success
                      failure:(TYFailureError)failure;
 
@end
 
#pragma mark -------------------------------------------------------------
 
 
#pragma mark - Config Protocol
 
@protocol TuyaSmartBLEConfigProtocol <NSObject>
 
/// Dp command publish.
///
/// @param deviceInfo   Tuya BEL protocol-based device object.
/// @param dps          The dp dictionary.
/// @param success      When query successfully, this block will be called.
/// @param failure      This block will be called if some error occurred.
- (void)publishDpsWithDeviceInfo:(id<TuyaSmartBLEDeviceProtocol>)deviceInfo
                             dps:(NSDictionary *)dps
                             success:(TYSuccessHandler)success
                             failure:(TYFailureError)failure;
 
/// Query device dp data by Bluetooth channel.
///
/// @param deviceInfo   Tuya BEL protocol-based device object.
/// @param dpIds        Array of dpId's to be queried.
/// @param success      When query successfully, this block will be called.
/// @param failure      This block will be called if some error occurred.
- (void)publishQueryDpCommand:(id<TuyaSmartBLEDeviceProtocol>)deviceInfo
                        dpIds:(NSArray *)dpIds
                      success:(TYSuccessHandler)success
                      failure:(TYFailureError)failure;
 
/// Connect device
///
/// @param deviceInfo   Tuya BEL protocol-based device object.
/// @param success      When connect successfully, this block will be called success.
/// @param failure      This block will be called if some error occurred.
- (void)connectWithDeviceInfo:(id<TuyaSmartBLEDeviceProtocol>)deviceInfo
                      success:(TYSuccessHandler)success
                      failure:(TYFailureError)failure;
 
/// Disconnect device
///
/// @param deviceInfo   Tuya BEL protocol-based device object.
/// @param success      When disconnect successfully, this block will be called success.
/// @param failure      This block will be called if some error occurred.
- (void)disconnectWithDeviceInfo:(id<TuyaSmartBLEDeviceProtocol>)deviceInfo
                         success:(TYSuccessHandler)success
                         failure:(TYFailureError)failure;
 
/// Remove device
///
/// @param deviceInfo   Tuya BEL protocol-based device object.
/// @param success      When remove successfully, this block will be called success.
/// @param failure      This block will be called if some error occurred.
- (void)removeWithDeviceInfo:(id<TuyaSmartBLEDeviceProtocol>)deviceInfo
                     success:(TYSuccessHandler)success
                     failure:(TYFailureError)failure;
 
/// Restore factory settings
///
/// @param deviceInfo   Tuya BEL protocol-based device object.
/// @param success      When reset successfully, this block will be called success.
/// @param failure      This block will be called if some error occurred.
- (void)resetWithDeviceInfo:(id<TuyaSmartBLEDeviceProtocol>)deviceInfo
                    success:(TYSuccessHandler)success
                    failure:(TYFailureError)failure;
 
/// Send OTA package to upgrade firmware.
///
/// The `otaData`、`otaType` and `otaVersion` can be obtained from TuyaSmartFirmwareUpgradeModel. You can get TuyaSmartFirmwareUpgradeModel by TuyaSmartDevice::getFirmwareUpgradeInfo:failure: .
///
/// @note Please make sure your device is connected via Bluetooth before upgrading.
///
/// @param deviceInfo   Tuya BEL protocol-based device object.
/// @param otaData      The OTA package data.
/// @param otaType      The OTA type.
/// @param otaVersion   The OTA version.
/// @param success      When ota successfully, this block will be called success.
/// @param failure      This block will be called if some error occurred.
- (void)sendOTAPackWithDeviceInfo:(id<TuyaSmartBLEDeviceProtocol>)deviceInfo
                          otaData:(NSData *)otaData
                          otaType:(TuyaSmartBLEOTAType)otaType
                       otaVersion:(NSString *)otaVersion
                          success:(TYSuccessHandler)success
                          failure:(TYFailureError)failure;
 
/// Get the RSSI of the BLE device
///
/// @param deviceInfo       Tuya BEL protocol-based device object.
///
/// @return The RSSI of the BLE device
- (NSInteger)peripheralRSSI:(id<TuyaSmartBLEDeviceProtocol>)deviceInfo;
 
/// Get the local connection status of the BLE device by deviceInfo
///
/// @param deviceInfo       Tuya BEL protocol-based device object.
///
/// @return The local connection status of the BLE device
- (BOOL)deviceOnlineState:(id<TuyaSmartBLEDeviceProtocol>)deviceInfo;
 
@end
 
#pragma mark -------------------------------------------------------------
 
 
#pragma mark - Device Protocol
 
@protocol TuyaSmartBLEDeviceProtocol <NSObject>
 
/// Tuya BLE agent.
@property (nonatomic, strong) TYBLEAgent                        *agent;
 
/// Tuya BLE peripheral.
@property (nonatomic, strong) TYBLEPeripheral                   *peripheral;
 
/// Tuya BLE advertising model.
@property (nonatomic, strong) TYBLEAdvModel                     *advModel;
 
/// Tuya active protocol-based manager object.
@property (nonatomic, strong) id<TuyaSmartBLEActiveProtocol>    activeManager;
 
/// Tuya config protocol-based manager object.
@property (nonatomic, strong) id<TuyaSmartBLEConfigProtocol>    configManager;
 
 
@optional
/// Methods for initializing TuyaSmartBLEDeviceProtocol-based device objects.
///
/// @param activeManager      Tuya active protocol-based manager object.
/// @param configManager      Tuya config protocol-based manager object.
- (instancetype)initWithActiveManager:(id<TuyaSmartBLEActiveProtocol>)activeManager
                        configManager:(id<TuyaSmartBLEConfigProtocol>)configManager;
 
@end
 
NS_ASSUME_NONNULL_END