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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
//
// TuyaSmartBLEManager.h
// TuyaSmartBLEKit
//
// Copyright (c) 2014-2021 Tuya Inc. (https://developer.tuya.com)
 
#import <Foundation/Foundation.h>
#import <TuyaSmartDeviceCoreKit/TuyaSmartDeviceCoreKit.h>
#import "TYBLEAdvModel.h"
 
/// @brief TuyaSmartBLEManager provides methods for developers to activator, control or OTA BLE device.
///
/// The two types of OTA. Provides firmware and MCU types.
typedef enum : NSUInteger {
    /// The firmware OTA type.
    TuyaSmartBLEOTATypeFirmware = 0,
    /// The MCU OTA type.
    TuyaSmartBLEOTATypeMCU,
} TuyaSmartBLEOTAType;
 
NS_ASSUME_NONNULL_BEGIN
 
@class TuyaSmartBLEManager;
 
@protocol TuyaSmartBLEManagerDelegate <NSObject>
 
@optional
 
/// The notification for bluetooth status changing.
///
/// @param isPoweredOn      Bluetooth status, on or off
- (void)bluetoothDidUpdateState:(BOOL)isPoweredOn;
 
/// Results of the scanning inactive devices.
///
/// @param uuid         The UUID for the device.
/// @param productKey   The product Id for the device.
/// @deprecated This method is deprecated, Use TuyaSmartBLEManager::didDiscoveryDeviceWithDeviceInfo: instead.
- (void)didDiscoveryDeviceWithUUID:(NSString *)uuid productKey:(NSString *)productKey __deprecated_msg("This method is deprecated, Use TuyaSmartBLEManager::didDiscoveryDeviceWithDeviceInfo: instead");
 
/// Results of the scanning inactive devices.
///
/// @param deviceInfo   The advertisingData model for the inactive device.
- (void)didDiscoveryDeviceWithDeviceInfo:(TYBLEAdvModel *)deviceInfo;
 
/// The result of the activator BLE devices.
///
/// @param manager          The class itself.
/// @param deviceModel      When activator successfully, this param will be called with DeviceModel.
/// @param error            This error will be called if some error occurred.
- (void)bleManager:(TuyaSmartBLEManager *)manager didFinishActivateDevice:(TuyaSmartDeviceModel *)deviceModel error:(NSError *)error;
 
/// Receive the transmission data from the device.
///
/// @param data     Transmission data receiving from the device.
/// @param devId    The device Id for the device.
- (void)bleReceiveTransparentData:(NSData *)data devId:(NSString *)devId;
 
@end
 
@interface TuyaSmartBLEManager : NSObject
 
/// Single instance.
+ (instancetype)sharedInstance;
 
/// A boolean value indicates whether the mobile phone's Bluetooth is on or off.
@property (nonatomic, assign, readonly) BOOL isPoweredOn;
 
/// Delegate for scanning and Bluetooth status change notification.
@property (nonatomic, weak) id<TuyaSmartBLEManagerDelegate> delegate;
 
/// Start listening broadcast package for BLE devices.
///
/// If an inactive device is scanned, the device info will be callback by `TuyaSmartBLEManagerDelegate::didDiscoveryDeviceWithDeviceInfo:`
///
/// If an active device is scanned, it will be automatically connected.
///
/// @param clearCache Whether to clean up the broadcast packets of scanned devices.
- (void)startListening:(BOOL)clearCache;
 
/// Stop listening broadcast package for BLE devices.
///
/// @param clearCache Whether to clean up the broadcast packets of scanned devices.
- (void)stopListening:(BOOL)clearCache;
 
/// Connect device
///
/// @param uuid         The UUID for the device.
/// @param productKey   The product Id for the device.
/// @param success      When connect successfully, this block will be called success.
/// @param failure      This block will be called if some error occurred.
- (void)connectBLEWithUUID:(NSString *)uuid
                productKey:(NSString *)productKey
                   success:(TYSuccessHandler)success
                   failure:(TYFailureHandler)failure;
 
/// Disconnect device
///
/// @param uuid         The UUID for the device.
/// @param success      When disconnect successfully, this block will be called success.
/// @param failure      This block will be called if some error occurred.
- (void)disconnectBLEWithUUID:(NSString *)uuid
                      success:(TYSuccessHandler)success
                      failure:(TYFailureError)failure;
 
 
/// Query device information before activator.
///
/// @param uuid         The UUID for the device.
/// @param productKey   The product Id for the device.
/// @param success      When query successfully, this block will be called with a string for device name.
/// @param failure      This block will be called if some error occurred.
///
/// @deprecated This method is deprecated, Use TuyaSmartBLEManager::queryDeviceInfoWithUUID:productKey:success:failure: instead.
- (void)queryNameWithUUID:(NSString *)uuid
               productKey:(NSString *)productKey
                  success:(void(^)(NSString *name))success
                  failure:(TYFailureError)failure __deprecated_msg("This method is deprecated, Use TuyaSmartBLEManager::queryDeviceInfoWithUUID:productKey:success:failure instead");
 
/// Query device information before activator
///
/// @param uuid         The UUID for the device.
/// @param productId    The product Id for the device.
/// @param success      When query successfully, this block will be called with a dictionary for device information.
/// @param failure      This block will be called if some error occurred.
- (void)queryDeviceInfoWithUUID:(NSString *)uuid
                      productId:(NSString *)productId
                        success:(TYSuccessDict)success
                        failure:(TYFailureError)failure;
 
/// Activator BLE device.
/// @param uuid The UUID for the device.
/// @param homeId The Id for the current home.
/// @param productKey The product Id for the device.
/// @param success When activator successfully, this block will be called with DeviceModel.
/// @param failure This block will be called if some error occurred.
/// @deprecated This method is deprecated, Use TuyaSmartBLEManager::activeBLE:homeId:success:failure: instead.
- (void)activeBLEWithUUID:(NSString *)uuid
                   homeId:(long long)homeId
               productKey:(NSString *)productKey
                  success:(void(^)(TuyaSmartDeviceModel *deviceModel))success
                  failure:(TYFailureHandler)failure __deprecated_msg("This method is deprecated, Use TuyaSmartBLEManager::activeBLE:homeId:success:failure instead");
 
/// 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)activeBLE:(TYBLEAdvModel *)deviceInfo
           homeId:(long long)homeId
          success:(void(^)(TuyaSmartDeviceModel *deviceModel))success
          failure:(TYFailureHandler)failure;
 
/// Publish the transmitted data.
///
/// @param devId        The device Id for the device.
/// @param data         Data to be transmitted to the device.
/// @param success      Transmission data returned by the device.
/// @param failure      This block will be called if some error occurred.
- (void)publishBleTransparentData:(NSString *)devId
                             data:(NSData *)data
                          success:(TYSuccessData)success
                          failure:(TYFailureError)failure;
 
/// Query device dp data by Bluetooth channel.
///
/// @param devId        The device Id for the device.
/// @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:(NSString *)devId
                        dpIds:(NSArray *)dpIds
                      success:(TYSuccessBOOL)success
                      failure:(TYFailureError)failure;
 
/// Send OTA package to upgrade firmware.
///
/// The `otaData` 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 uuid         The UUID for the device.
/// @param otaData      The OTA package data.
/// @param success      When ota successfully, this block will be called success.
/// @param failure      This block will be called if some error occurred.
///
/// @deprecated This method is deprecated, Use TuyaSmartBLEManager::sendOTAPack:pid:otaData:otaType:otaVersion:success:failure: instead.
- (void)sendOTAPack:(NSString *)uuid
            otaData:(NSData *)otaData
            success:(TYSuccessHandler)success
            failure:(TYFailureHandler)failure __deprecated_msg("This method is deprecated, Use TuyaSmartBLEManager::sendOTAPack:pid:otaData:otaType:success:failure instead");
 
/// Send OTA package to upgrade firmware.
///
/// The `otaData` 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 uuid         The UUID for the device.
/// @param pid          The product Id for the device.
/// @param otaData      The OTA package data.
/// @param success      When ota successfully, this block will be called success.
/// @param failure      This block will be called if some error occurred.
///
/// @deprecated This method is deprecated, Use TuyaSmartBLEManager::sendOTAPack:pid:otaData:otaType:otaVersion:success:failure: instead.
- (void)sendOTAPack:(NSString *)uuid
                pid:(NSString *)pid
            otaData:(NSData *)otaData
            success:(TYSuccessHandler)success
            failure:(TYFailureHandler)failure __deprecated_msg("This method is deprecated, Use TuyaSmartBLEManager::sendOTAPack:pid:otaData:otaType:otaVersion:success:failure instead");
 
/// 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 uuid         The UUID for the device.
/// @param pid          The product Id for the device.
/// @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)sendOTAPack:(NSString *)uuid
                pid:(NSString *)pid
            otaData:(NSData *)otaData
            otaType:(TuyaSmartBLEOTAType)otaType
         otaVersion:(NSString *)otaVersion
            success:(TYSuccessHandler)success
            failure:(TYFailureHandler)failure;
 
/// Get the RSSI of the BLE device
///
/// @param uuid        The UUID for the device.
///
/// @return The RSSI of the BLE device
- (NSInteger)getPeripheralRSSI:(NSString *)uuid;
 
/// Get the local connection status of the BLE device by device UUID
///
/// @param uuid        The UUID for the device.
///
/// @return The local connection status of the BLE device
- (BOOL)deviceStatueWithUUID:(NSString *)uuid;
 
/// Sending parameters data to the BLE device, whilc make device report data through big data channels.
/// 
/// @param devId        The device Id for the device.
/// @param paramsDict   A dictionary of parameter, defined by the protocol party.
/// @param success      When ota successfully, this block will be called success.
/// @param failure      This block will be called if some error occurred.
- (void)postBleBigDataChannel:(NSString *)devId params:(NSDictionary *)paramsDict success:(TYSuccessHandler)success failure:(TYFailureError)failure;
 
@end
 
NS_ASSUME_NONNULL_END