kakopappa / arduino-esp8266-alexa-wemo-switch

Amazon Alexa + WeMos switch made with Arduino D1 Mini
https://sinric.pro
MIT License
281 stars 249 forks source link

Wrong use of "String:indexOf" #21

Open UlliBien opened 7 years ago

UlliBien commented 7 years ago

I tested your program. Works very fine! But there is a little bug in the code.

This is the code used for detecting a search request: if (request.indexOf('M-SEARCH') > 0) { if (request.indexOf("urn:Belkin:device:**") > 0) { ...

The first indexOf gives a warning "To many chars in char constant". It is compiled to "indexOf('M')". Replacing it with indexOf("M-SEARCH") does not work, because "M-SEARCH" is at position 0. indexOf() returns -1 if a string is not found!

Replace the code by if (request.indexOf('M-SEARCH') > -1) { if (request.indexOf("urn:Belkin:device:**") > -1) { ...

or

if (request.indexOf('M-SEARCH') == 0) { if (request.indexOf("urn:Belkin:device:**") > -1) { ...

szakharchenko commented 7 years ago

You just want to use double quotes instead of single quotes, and startsWith would better describe the intent.