public void JavaScriptArgs (string args) { List<string> services = new List<string>(); List<string> characteristics = new List<string>(); List<string> CCCD = new List<string>(); string charavalue = null; bool servicflag = false; bool charcterflag = false; bool CCCDflag= false; bool valueflag = false; char[] trimarray = { '\\', '[', ']', '"', ':' }; Regex r = new Regex(@"""([0-9a-zA-Z_\-\s]+)"""); Match m = r.Match(args); bool scanning = true; string value = m.Value.Trim(trimarray); while (scanning) { switch (value) { case "serviceUuids": { servicflag = true; charcterflag = false; CCCDflag = false; valueflag = false; m = m.NextMatch(); value = m.Value.Trim(trimarray); break; } case "serviceUuid": { servicflag = true; charcterflag = false; CCCDflag = false; valueflag = false; m = m.NextMatch(); value = m.Value.Trim(trimarray); break; } case "characteristicUuids": { servicflag = false; charcterflag = true; CCCDflag = false; valueflag = false; m = m.NextMatch(); value = m.Value.Trim(trimarray); break; } case "characteristicUuid": { servicflag = false; charcterflag = true; CCCDflag = false; valueflag = false; m = m.NextMatch(); value = m.Value.Trim(trimarray); break; } case "descriptorUuid": { servicflag = false; charcterflag = false; CCCDflag = true; valueflag = false; m = m.NextMatch(); value = m.Value.Trim(trimarray); break; } case "value": { servicflag = false; charcterflag = false; CCCDflag = false; valueflag = true; m = m.NextMatch(); value = m.Value.Trim(trimarray); break; } default: { if (value == "") { scanning = false; break; } else { if (servicflag) services.Add(value); else if (charcterflag) characteristics.Add(value); else if (CCCDflag) CCCD.Add(value); else if (valueflag) charavalue = value; m = m.NextMatch(); value = m.Value.Trim(trimarray); break; } } } } CurrentJSGattProfileArgs = new JavaScriptServiceArgs[services.Count]; for (int i = 0; i < services.Count; i++) { if (CCCD.Count == 0 && charavalue==null) CurrentJSGattProfileArgs[i] = new JavaScriptServiceArgs(services[i],characteristics.Count); else if(CCCD.Count!=0) CurrentJSGattProfileArgs[i] = new JavaScriptServiceArgs(services[i],characteristics.Count,CCCD[0]); else if (charavalue!=null) CurrentJSGattProfileArgs[i] = new JavaScriptServiceArgs(services[i], characteristics.Count, System.Text.Encoding.UTF8.GetBytes(charavalue)); for (int j = 0; j < characteristics.Count; j++) { CurrentJSGattProfileArgs[i].characteristics[j] = characteristics[j]; } } }
public void JavaScriptArgs(string args) { List <string> services = new List <string>(); List <string> characteristics = new List <string>(); List <string> CCCD = new List <string>(); string charavalue = null; bool servicflag = false; bool charcterflag = false; bool CCCDflag = false; bool valueflag = false; char[] trimarray = { '\\', '[', ']', '"', ':' }; Regex r = new Regex(@"""([0-9a-zA-Z_\-\s]+)"""); Match m = r.Match(args); bool scanning = true; string value = m.Value.Trim(trimarray); while (scanning) { switch (value) { case "serviceUuids": { servicflag = true; charcterflag = false; CCCDflag = false; valueflag = false; m = m.NextMatch(); value = m.Value.Trim(trimarray); break; } case "serviceUuid": { servicflag = true; charcterflag = false; CCCDflag = false; valueflag = false; m = m.NextMatch(); value = m.Value.Trim(trimarray); break; } case "characteristicUuids": { servicflag = false; charcterflag = true; CCCDflag = false; valueflag = false; m = m.NextMatch(); value = m.Value.Trim(trimarray); break; } case "characteristicUuid": { servicflag = false; charcterflag = true; CCCDflag = false; valueflag = false; m = m.NextMatch(); value = m.Value.Trim(trimarray); break; } case "descriptorUuid": { servicflag = false; charcterflag = false; CCCDflag = true; valueflag = false; m = m.NextMatch(); value = m.Value.Trim(trimarray); break; } case "value": { servicflag = false; charcterflag = false; CCCDflag = false; valueflag = true; m = m.NextMatch(); value = m.Value.Trim(trimarray); break; } default: { if (value == "") { scanning = false; break; } else { if (servicflag) { services.Add(value); } else if (charcterflag) { characteristics.Add(value); } else if (CCCDflag) { CCCD.Add(value); } else if (valueflag) { charavalue = value; } m = m.NextMatch(); value = m.Value.Trim(trimarray); break; } } } } CurrentJSGattProfileArgs = new JavaScriptServiceArgs[services.Count]; for (int i = 0; i < services.Count; i++) { if (CCCD.Count == 0 && charavalue == null) { CurrentJSGattProfileArgs[i] = new JavaScriptServiceArgs(services[i], characteristics.Count); } else if (CCCD.Count != 0) { CurrentJSGattProfileArgs[i] = new JavaScriptServiceArgs(services[i], characteristics.Count, CCCD[0]); } else if (charavalue != null) { CurrentJSGattProfileArgs[i] = new JavaScriptServiceArgs(services[i], characteristics.Count, System.Text.Encoding.UTF8.GetBytes(charavalue)); } for (int j = 0; j < characteristics.Count; j++) { CurrentJSGattProfileArgs[i].characteristics[j] = characteristics[j]; } } }