本文主要介紹如何使用機智云設備接入SDK來進行APP與設備之間的數(shù)據(jù)透傳。在APP和MCU之間,有兩種數(shù)據(jù)透傳方式:
利用“擴展類型”數(shù)據(jù)點進行透傳
不定義數(shù)據(jù)點直接進行數(shù)據(jù)透傳,即純透傳
先來了解什么是透傳什么是透傳?透傳即是透明傳送,即傳送網(wǎng)絡無論傳輸業(yè)務如何,只負責將需要傳送的業(yè)務傳送到目的節(jié)點,同時保證傳輸?shù)馁|量即可,而不對傳輸?shù)臉I(yè)務進行處理。
這就好比快遞郵件,郵件中間有可能通過自行車、汽車、火車、飛機的多種組合運輸方式到達您的手上,但您不用關心它們中間經(jīng)歷了哪些。
為什么要透傳呢?透傳一般都是用來讀取遠程的串口數(shù)據(jù)。例如:網(wǎng)吧內每個上網(wǎng)者都要刷身份證才能上網(wǎng),但身份證數(shù)據(jù)庫不可能放在每個網(wǎng)吧內。所以就將讀卡器的串口數(shù)據(jù)通過透傳回傳到公安局,在公安局的平臺上來比對身份證號碼。
言歸正傳,現(xiàn)在進入今天的主題,來談談,在機智云APP和MCU之間有哪兩種透傳方式。
方式一:擴展類型數(shù)據(jù)點透傳
1.定義數(shù)據(jù)點
在機智云產(chǎn)品管理中的數(shù)據(jù)點中定義一個拓展類型的數(shù)據(jù)點,如上圖所示:定義數(shù)據(jù)長度為60個byte(透傳情況下,數(shù)據(jù)長度暫時支持900字節(jié)內數(shù)據(jù)傳輸)。
2.設備上報數(shù)據(jù)協(xié)議說明
設備MCU按照數(shù)據(jù)點的定義格式上報透傳數(shù)據(jù),在完成新產(chǎn)品的定義后,每個新產(chǎn)品都有相應的串口通信協(xié)議文檔,文檔下載路徑如下圖所示:
在串口通信協(xié)議文檔文檔的4.9部分則有MCU上報數(shù)據(jù)的格式,如下圖:
圖中的dev_status(60B)即為需要透傳的數(shù)據(jù)。
3.SDK回調接口說明
APP端接收到MCU上報數(shù)據(jù)的回調接口:didReceiveData,判斷result為GizWifiErrorCode.GIZ_SDK_SUCCESS,則接收到的數(shù)據(jù)可能為普通數(shù)據(jù)點或者擴展數(shù)據(jù)點。
4.APP接收數(shù)據(jù)代碼示例
Andriod接收到MCU上報拓展數(shù)據(jù)類型代碼示例:
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray(); // 將16進制的byte轉為string, 比如byte值為{0x62,0x63}, 那么將轉為“6263”public static String bytesToHex(byte[] bytes) { char[] hexChars = new char[bytes.length * 3]; for (int j = 0; j < bytes.length; j++) { int v = bytes[j] & 0xFF; hexChars[j * 3] = hexArray[v >>> 4]; hexChars[j * 3 + 1] = hexArray[v & 0x0F]; hexChars[j * 3 + 2] = ' '; } return new String(hexChars); } @Overrideprotected void didReceiveData(GizWifiErrorCode result, GizWifiDevice device, ConcurrentHashMap dataMap, int sn) { if (result == GizWifiErrorCode.GIZ_SDK_SUCCESS) { // 普通數(shù)據(jù)點類型,有布爾型、整形和枚舉型數(shù)據(jù), 該種類型一般為可讀寫 if (dataMap.get("data") != null) { ConcurrentHashMap map = (ConcurrentHashMap) dataMap.get("data"); // 擴展數(shù)據(jù)點, key為"kuozhan" byte[] bytes = (byte[]) map.get("kuozhan"); String string = bytesToHex(bytes); Toast.makeText(this, string, Toast.LENGTH_SHORT).show(); } }} |
iOS接收到MCU上報拓展數(shù)據(jù)類型代碼示例:
- (void)device:(GizWifiDevice *)device didReceiveData:(NSError *)result data:(NSDictionary *)dataMap withSN:(NSNumber *)sn{ if (result.code == GIZ_SDK_SUCCESS) { NSLog(@"接收到數(shù)據(jù)"); NSDictionary *data = dataMap[@"data"]; NSString *kuozhan = data[@"kuozhan"]; NSLog(@"dataMap = %@", dataMap); NSLog(@"kuozhan = %@", kuozhan); }} |
5.設備上報數(shù)據(jù)案例
案例:根據(jù)數(shù)據(jù)點上報格式上報數(shù)據(jù):
FF FF 00 42 05 00 00 00 04 01 02 03 04 05 06 07 08 09 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 78
注意定義的擴展數(shù)據(jù)類型為60個字節(jié),因此上報數(shù)據(jù)的時候也一定要保證上報命令中“設備狀態(tài)”字節(jié)數(shù)為60個字節(jié),否則APP端將不會收到數(shù)據(jù)。多余的數(shù)據(jù)可以進行補零操作。
APP端接收到的dataMap字典中有一個”kuozhan”的key,對應的值即為透傳的數(shù)據(jù)。
dataMap = { alerts={ }; data ={ kuozhan = <01020304 05060708 09000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000>; }; faults={ }; } |
6.APP下發(fā)數(shù)據(jù)代碼示例
APP在GizWifiDevice類的write接口中,按照數(shù)據(jù)點定義發(fā)送擴展數(shù)據(jù)。
Andriod下發(fā)拓展數(shù)據(jù)類型命令代碼示例:
byte[] input1 = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 }; // byte[] input2 = { 11, 22, 33, 44, 55, 66, 77, 88, 99}; ConcurrentHashMap dataMap = new ConcurrentHashMap(); dataMap.put("kuozhan", input1); device.write(dataMap, 0); |
iOS下發(fā)拓展數(shù)據(jù)類型命令代碼示例:
char input1[7] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};// char input2[7] = {11, 22, 33, 44, 55, 66, 77}; NSData* data = [NSData dataWithBytes:input1 length:sizeof(input1)]; NSDictionary *request = @{@"kuozhan": data}; [self.device write:request withSN:44]; |
上述代碼中,input1與input2的透傳結果是不一樣的,input1中定義的是十六進制的數(shù)值,input2中則是十進制的數(shù)值。如果使用input1中0x11的值下發(fā)給設備,那么設備將收到0x11的十六進制值,如果使用input2中11的值下發(fā)給設備,那么設備收到將是0x0b(十進制11對應十六進制b)。使用input1下發(fā)設備,MCU收到的數(shù)據(jù)為:
FF FF 00 43 03 45 00 00 01 01 11 22 33 44 55 66 77 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 69
方式二:無數(shù)據(jù)點透傳
1.設備上報數(shù)據(jù)協(xié)議說明
MCU上報數(shù)據(jù)的格式如下表所示,包長度(len)是指從命令開始一直到校驗和的字節(jié)長度(包括命令和校驗和)。dev_status是需要透傳數(shù)據(jù),可以設置任意長度,但是暫時支持900B內傳輸。
如設備上報指令:FF FF 00 0D 05 00 00 00 06 11 22 33 44 55 66 77 F4 ;那么透傳的數(shù)據(jù)為11 22 33 44 55 66 77
2.SDK回調接口說明
APP端接收到MCU上報數(shù)據(jù)的回調接口: didReceiveData,判斷result為GizWifiErrorCode.GIZ_SDK_SUCCESS,則表示接收到數(shù)據(jù)成功,回傳參數(shù)的dataMap 中的“binary”字段的值為透傳數(shù)據(jù)。
3.APP接收數(shù)據(jù)代碼示例
Andriod接收MCU無數(shù)據(jù)點上報代碼示例:
@Overrideprotected void didReceiveData(GizWifiErrorCode result, GizWifiDevice device, ConcurrentHashMap dataMap, int sn) { if (result == GizWifiErrorCode.GIZ_SDK_SUCCESS) { // 透傳數(shù)據(jù),無數(shù)據(jù)點定義, 適合開發(fā)者自行定義協(xié)議自行解析 if (dataMap.get("binary") != null) { byte[] binary = (byte[]) dataMap.get("binary"); Log.i("info", "Binary data:" + bytesToHex(binary)); // 收到后面需要自行解析 } }} |
iOS接收MCU無數(shù)據(jù)點上報代碼示例:
- (void)device:(GizWifiDevice *)device didReceiveData:(NSError *)result data:(NSDictionary *)dataMap withSN:(NSNumber *)sn{ if (result.code == GIZ_SDK_SUCCESS) { NSLog(@"接收到數(shù)據(jù)"); NSLog(@"dataMap = %@", dataMap); NSString *binary = dataMap[@"binary"]; // 后面需要自行對binary數(shù)據(jù)進行解析 }} |
如MCU上報:FF FF 00 0D 05 00 00 00 06 11 22 33 44 55 66 77 F4則APP將打?。?Binary data:11 22 33 44 55 66 77
4.APP下發(fā)數(shù)據(jù)代碼示例
APP在GizWifiDevice類的write接口中,發(fā)送的json格式為:{“binary”: “xxxxxx”};MCU接收的數(shù)據(jù)格式如下:
Andriod下發(fā)無數(shù)據(jù)點透傳數(shù)據(jù)代碼示例:
byte[] input1 = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 };ConcurrentHashMap dataMap = new ConcurrentHashMap();dataMap.put("binary", input1);device.write(dataMap, 0); |
iOS下發(fā)無數(shù)據(jù)點透傳數(shù)據(jù)代碼示例:
char input1[7] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};// char input2[7] = {11, 22, 33, 44, 55, 66, 77}; NSData* data = [NSData dataWithBytes:input1 length:sizeof(input1)] ;NSDictionary *request = @{@"binary": data}; [self.device write:request withSN:0]; |
MCU接收到的APP下發(fā)數(shù)據(jù)格式(即以上bytes數(shù)組下發(fā)后,MCU接收到的數(shù)據(jù)):FF FF 00 0D 03 16 00 00 05 11 22 33 44 55 66 77 07
-
APP
+關注
關注
33文章
1574瀏覽量
72557 -
機智云
+關注
關注
2文章
590瀏覽量
26433
原文標題:如何使用機智云設備接入SDK,來進行APP與設備之間的數(shù)據(jù)透傳
文章出處:【微信號:IoTMaker,微信公眾號:機智云開發(fā)者】歡迎添加關注!文章轉載請注明出處。
發(fā)布評論請先 登錄
相關推薦
評論