Search code examples
androidnetwork-programmingandroid-networking

Android WifiInfo.getMacAddress on Toshiba Folio


I've been using WifiInfo.getMacAddress string as seen on my nexus one or any other mobile device I've been able to use, i.e. in the format of 01:02:03:04:05:06

But one of the users of my app just reported a crash on a Toshiba Folio100, my MAC address parsing failed. Since I've not any log files and the report is anonymous, I cannot contact that man, so I ask here if you know how it's encoded in such kind of device.

I suppose it could be like 010203040506 but I'm not so confident.


Solution

  • Although I can't comment on The Toshiba Folio specifically, I would urge you to reconsider how you parse the MAC address string.

    I wrote code many years ago to get the MAC addresses from NICs in PCs - as it used the cards' PC drivers, the format could vary depending on manufacturer. The docs for WifiInfo.getMACAddress() don't actually give a guarantee of how this string is returned so consider preparing for all eventualities.

    I saw the following formats...

    01:02:03:04:05:06 // Delimited with ':' and padded to 2 chars
    1:2:3:4:5:6 // Delimited but if < 16, there was no leading '0'
    010203040506 // No delimiters but octets ALWAYS pre-padded with '0' when < 16
    

    NOTE: Also the alphabetic characters may have been upper or lower case (although consistent in each case, i.e., all upper or all lower case). Example...

    0A0B0C0D0E0F
    0a0b0c0d0e0f
    A:B:C:D:E:F
    a:b:c:d:e:f
    

    To cope with the above, I first forced the string toLower().

    I then checked for presence of ':'. If it existed I'd split the string into an array and I'd then check the length of each 'octet string' in the array and if length = 1 then padding wasn't used so I'd prefix '0'. Finally I'd reassemble the string by concatenating each of the array elements using ':' as a delimiter.

    If the string didn't contain ':' then I'd confirm that its length = 12, at this point I'd rebuild the string using each pair of characters and delimit with ':'.

    If the string wasn't delimited and its length wan't 12 then it would be impossible to parse correctly and I had to assume that requesting the MAC address string had failed and I reported a bad MAC address.