0

i get the following Exception

I/System.out: java.net.SocketException: socket failed: EACCES (Permission denied)

I have a closed network without internet access, only router, android device and raspberry pi.

My AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>

<uses-sdk android:minSdkVersion="16" />

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

i have only one Activity MainActivity:

public class MainActivity extends Activity {

private static final int PORT= 8888;
private static final int TIMEOUT_MS = 500;



@Override
protected void onCreate(Bundle savedInstanceState) {
    final TextView tView;
    Button buttonRed, buttonGreen, buttonBlue, buttonWhite;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tView = (TextView) findViewById(R.id.showroom_welcome);

    buttonRed = (Button) findViewById(R.id.buttonred);
    buttonRed.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            /*send message xyz*/
            try {
                String message = "\"xyz\"";
                DatagramSocket socket = new DatagramSocket(PORT);
                socket.setBroadcast(true);
                socket.setSoTimeout(TIMEOUT_MS);
                DatagramPacket packet = new DatagramPacket(message.getBytes(), message.length(), getBroadcastAddress(), PORT); //InetAddress.getByName("192.168.100.255"), PORT );
                socket.send(packet);
            }catch(UnsupportedEncodingException uee){
                tView.setText(uee.getMessage());
                System.out.println(uee.getMessage());

            }catch (SocketException se){
                tView.setText(se.getMessage() );
                System.out.println(se+"\n");

            }catch (IOException ioe){
                tView.setText(ioe.getMessage());
                System.out.println(ioe + "\n");
            }
        }
    });
}

It is a UDP client that sends only xyz to raspi. Do you have an idea what is the problem?

Julia M.
  • 21
  • 1
  • 2

3 Answers3

2

You're only setting up the permissions for the State of your network. You only need the state if you want an information in which network you are or if you want to change the network programmatically.

<uses-permission android:name="android.permission.CHANGE_WIFI_STATET" />

Is wrong change it to:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />

You also need the the Internet Permission:

<uses-permission android:name="android.permission.INTERNET"/>
Erythrozyt
  • 802
  • 6
  • 21
1

Add the INTERNET permission:

<uses-permission android:name="android.permission.INTERNET" />
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

This is an issue with your permissions. Your missing the Internet-permission:

<uses-permission android:name="android.permission.INTERNET"/>

Adding this to your code should fix your problem.

Hope that helps!

Community
  • 1
  • 1
Felix Gerber
  • 1,615
  • 3
  • 30
  • 40