Wp8 build issues: How to detect network, and convert byte[] to string

Hello,

I’m trying to finish my first Unity game and I’d like to release it on Windows Phone first.

But I’m currently facing 2 major issues and I can’t find a way to solve them.

  • How to detect if an internet connection is available?

I tried 2 methods so far, one with WebClient.OpenRead, one with Ping, but both will fail the build.

  • How to convert byte[ ] to string?

Same here I tried multiple methods mainly with System.Text.Encoding but none will allow me to build targetting WP8 platform

Thank you in advance for any tips you could provide regarding those issues

Hi,

you can use this to check for internet connection:

As for converting byte[ ] to string: WP8 doesn’t allow all the encodings that are available on desktop, but it supports 3 most commonly used ones: UTF8, UTF16 and UTF16-big endian.

So, you can do this:

byte[] myBytes = ProduceBytes();
Encoding.UTF8.GetString(myBytes);

Thank you for your reply,

As far as I know Application.internetReachability is not a reliable way to check if internet is available, as stated in the linked doc:
Note: Do not use this property to determine the actual connectivity

Regarding Encoding I tried multiple times to use it but it always endup this way:
Error building Player: Exception: Error: method System.String System.Text.Encoding::GetString(System.Byte[ ]) doesn’t exist in target framework.

My bad about GetString - only an overload with 3 parameters is available on WP8:

http://msdn.microsoft.com/en-us/library/windows/apps/05cts4c3(v=vs.105).aspx

byte[] myBytes = ProduceBytes();
Encoding.UTF8.GetString(myBytes, 0, myBytes.Length);

As for internet connection - how about you try to connect to the service you are using? I mean, you could try opening a TCP socket to Google.com on port 80 for all it’s worth, but in the end I think it’s only important that you’re able to use it for what you need.

Thank you again, the 3 parameters GetString did the trick!

As for network detection I made a call to google using WWW and checked for errors, so far it seems to work, I’ll just replace google with my service and I should be good to go.