android - socket的问题失败了

时间:2013-12-14 22:27:53

标签: java android permissions wifi

我正在尝试制作的Android应用程序的日志中出现错误。我已经发布了下面的日志和它对应的java类。

我认为这是我的清单中的权限问题,我已经使用我认为正确的权利进行了修改。

请有人可以告诉我我做错了什么。

由于

package com.example.hotornot.WifiUtils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.util.ArrayList;

import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.util.Log;

public class WifiApManager {
        private final WifiManager mWifiManager;

        public WifiApManager(Context context) {
                mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        }

        /**
     * Start AccessPoint mode with the specified
     * configuration. If the radio is already running in
     * AP mode, update the new configuration
     * Note that starting in access point mode disables station
     * mode operation
     * @param wifiConfig SSID, security and channel details as part of WifiConfiguration
     * @return {@code true} if the operation succeeds, {@code false} otherwise
     */
        public boolean setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) {
                try {
                        if (enabled) { // disable WiFi in any case
                                mWifiManager.setWifiEnabled(false);
                        }

                        Method method = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
                        return (Boolean) method.invoke(mWifiManager, wifiConfig, enabled);
                } catch (Exception e) {
                        Log.e(this.getClass().toString(), "", e);
                        return false;
                }
        }

        /**
     * Gets the Wi-Fi enabled state.
     * @return {@link WIFI_AP_STATE}
     * @see #isWifiApEnabled()
     */
        public WIFI_AP_STATE getWifiApState() {
                try {
                        Method method = mWifiManager.getClass().getMethod("getWifiApState");

                        int tmp = ((Integer)method.invoke(mWifiManager));

                        // Fix for Android 4
                        if (tmp > 10) {
                                tmp = tmp - 10;
                        }

                        return WIFI_AP_STATE.class.getEnumConstants()[tmp];
                } catch (Exception e) {
                        Log.e(this.getClass().toString(), "", e);
                        return WIFI_AP_STATE.WIFI_AP_STATE_FAILED;
                }
        }

        /**
     * Return whether Wi-Fi AP is enabled or disabled.
     * @return {@code true} if Wi-Fi AP is enabled
     * @see #getWifiApState()
     *
     * @hide Dont open yet
     */
    public boolean isWifiApEnabled() {
        return getWifiApState() == WIFI_AP_STATE.WIFI_AP_STATE_ENABLED;
    }

    /**
     * Gets the Wi-Fi AP Configuration.
     * @return AP details in {@link WifiConfiguration}
     */
    public WifiConfiguration getWifiApConfiguration() {
                try {
                        Method method = mWifiManager.getClass().getMethod("getWifiApConfiguration");
                        return (WifiConfiguration) method.invoke(mWifiManager);
                } catch (Exception e) {
                        Log.e(this.getClass().toString(), "", e);
                        return null;
                }
    }

    /**
     * Sets the Wi-Fi AP Configuration.
     * @return {@code true} if the operation succeeded, {@code false} otherwise
     */
    public boolean setWifiApConfiguration(WifiConfiguration wifiConfig) {
            try {
                        Method method = mWifiManager.getClass().getMethod("setWifiApConfiguration", WifiConfiguration.class);
                        return (Boolean) method.invoke(mWifiManager, wifiConfig);
                } catch (Exception e) {
                        Log.e(this.getClass().toString(), "", e);
                        return false;
                }
        }

        /**
     * Gets a list of the clients connected to the Hotspot, reachable timeout is 300
     * @param onlyReachables {@code false} if the list should contain unreachable (probably disconnected) clients, {@code true} otherwise
     * @return ArrayList of {@link ClientScanResult}
     */
    public ArrayList<ClientScanResult> getClientList(boolean onlyReachables) {
            return getClientList(onlyReachables, 300);
    }

        /**
     * Gets a list of the clients connected to the Hotspot 
     * @param onlyReachables {@code false} if the list should contain unreachable (probably disconnected) clients, {@code true} otherwise
     * @param reachableTimeout Reachable Timout in miliseconds
     * @return ArrayList of {@link ClientScanResult}
     */
        public ArrayList<ClientScanResult> getClientList(boolean onlyReachables, int reachableTimeout) {
                BufferedReader br = null;
                ArrayList<ClientScanResult> result = null;

                try {
                        result = new ArrayList<ClientScanResult>();
                        br = new BufferedReader(new FileReader("/proc/net/arp"));
                        String line;
                        while ((line = br.readLine()) != null) {
                                String[] splitted = line.split(" +");

                                if ((splitted != null) && (splitted.length >= 4)) {
                                        // Basic sanity check
                                        String mac = splitted[3];

                                        if (mac.matches("..:..:..:..:..:..")) {
                                                boolean isReachable = InetAddress.getByName(splitted[0]).isReachable(reachableTimeout);

                                                if (!onlyReachables || isReachable) {
                                                        result.add(new ClientScanResult(splitted[0], splitted[3], splitted[5], isReachable));
                                                }
                                        }
                                }
                        }
                } catch (Exception e) {
                        Log.e(this.getClass().toString(), e.getMessage());
                } finally {
                        try {
                                br.close();
                        } catch (IOException e) {
                                Log.e(this.getClass().toString(), e.getMessage());
                        }
                }

                return result;
        }
}

<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="18" />

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



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

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

> 12-14 22:22:38.405: E/Trace(777): error opening trace file: No such
> file or directory (2) 12-14 22:22:38.804: E/class
> com.example.hotornot.WifiUtils.WifiApManager(777): socket failed:
> EACCES (Permission denied) 12-14 22:22:38.955: D/libEGL(777): loaded
> /system/lib/egl/libEGL_emulation.so 12-14 22:22:38.985: D/(777):
> HostConnection::get() New Host Connection established 0x2a1479b0, tid
> 777 12-14 22:22:38.995: D/libEGL(777): loaded
> /system/lib/egl/libGLESv1_CM_emulation.so 12-14 22:22:38.995:
> D/libEGL(777): loaded /system/lib/egl/libGLESv2_emulation.so 12-14
> 22:22:39.065: W/EGL_emulation(777): eglSurfaceAttrib not implemented
> 12-14 22:22:39.075: D/OpenGLRenderer(777): Enabling debug mode 0

1 个答案:

答案 0 :(得分:1)

在清单中添加它可能会对其进行排序

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