0

我正在开发 WiFi Direct 应用程序。事实证明,当我打开 WiFi Direct 并启动程序时,该应用程序崩溃了。这是我的代码

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public class MainActivity extends Activity {

//For WiFi Direct
private WifiP2pManager mManager;
private Channel mChannel;
private IntentFilter mIntentFilter= new IntentFilter();
private boolean isWifiP2pEnabled = false;
private ArrayAdapter aa;

public void setIsWifiP2pEnabled(boolean isWifiP2pEnabled) {
    isWifiP2pEnabled=isWifiP2pEnabled;

}
//End Wifi Direct
private Networking_input net;
private Networking_output net_out;
private Networking_files net_file;
private EditText inputData;
private Button sendParameters;
private Button peersSearch;
private ListView listview;
private TextView displayText;
private List<WifiP2pDevice> deviceList = new ArrayList<WifiP2pDevice>();



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //For Wifi Direct
    mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
    mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
    mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
    mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
    //End Wifi Direct

    inputData= (EditText) findViewById(R.id.editText1);
    sendParameters=(Button) findViewById(R.id.button1);
    peersSearch=(Button) findViewById(R.id.button2);
    displayText=(TextView) findViewById(R.id.textView1);
    listview=(ListView) findViewById(R.id.listView1);
    aa=new ArrayAdapter<WifiP2pDevice>(this, android.R.layout.simple_list_item_1, deviceList);

    listview.setAdapter(aa);

    //Begin for WiFi Direct
    mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
    mChannel = mManager.initialize(this, getMainLooper(), null);
    //End for WiFi Direct   


    peersSearch.setOnClickListener(new OnClickListener() {          
        @Override
        public void onClick(View v) 
        {
            discoverPeers();
        }
    });

    listview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int index,long arg3) 
        {
            connectTo(deviceList.get(index));               
        }

    });     
    sendParameters.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            net_out= new Networking_output();
            String input=inputData.getText().toString();
            net_out.execute(input);
            net_file=new Networking_files();
            net_file.execute();

        }
    });

}

BroadcastReceiver p2pStatusReceiver= new BroadcastReceiver()
{
    @Override
    public void onReceive(Context context, Intent intent) {
          int state = intent.getIntExtra(
            WifiP2pManager.EXTRA_WIFI_STATE,
            WifiP2pManager.WIFI_P2P_STATE_DISABLED);

          switch (state) {
            case (WifiP2pManager.WIFI_P2P_STATE_ENABLED): 
                peersSearch.setEnabled(true);
              break;
            default: 
                peersSearch.setEnabled(false);
          }
        }
};

private void discoverPeers()
{
    mManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {

        @Override
        public void onSuccess() {
            Toast toast=Toast.makeText(MainActivity.this, "Discovery initiated", Toast.LENGTH_SHORT);
            toast.show();

        }

        @Override
        public void onFailure(int reason) {
            Toast toast=Toast.makeText(MainActivity.this, "Discovery failed", Toast.LENGTH_SHORT);
            toast.show();

        }
    });
}

BroadcastReceiver peerDiscoveryReceiver = new BroadcastReceiver()
{
    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        mManager.requestPeers(mChannel, new PeerListListener() {

            @Override
            public void onPeersAvailable(WifiP2pDeviceList peers) {
                deviceList.clear();
                deviceList.addAll(peers.getDeviceList());
                aa.notifyDataSetChanged();

            }
        });
    }
};


@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private void connectTo(final WifiP2pDevice device)
{
    WifiP2pConfig config=new WifiP2pConfig();
    config.deviceAddress=device.deviceAddress;

    mManager.connect(mChannel, config, new ActionListener() {

        @Override
        public void onSuccess() {
            Toast toast=Toast.makeText(MainActivity.this, "Connected to " + device.deviceAddress.toString() , Toast.LENGTH_SHORT);
            toast.show();
        }

        @Override
        public void onFailure(int reason) {
            Toast toast=Toast.makeText(MainActivity.this, "Connection Failed" , Toast.LENGTH_SHORT);
            toast.show();
        }
    });
}   

BroadcastReceiver connectionChangedReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) 
        {
            String ExtraKey=WifiP2pManager.EXTRA_NETWORK_INFO;
            NetworkInfo networkinfo= (NetworkInfo)intent.getParcelableExtra(ExtraKey);
            if(networkinfo.isConnected())
            {
                mManager.requestConnectionInfo(mChannel, new ConnectionInfoListener() {                     
                    public void onConnectionInfoAvailable(WifiP2pInfo info) {
                        if(info.groupFormed)
                        {
                            if(info.isGroupOwner)
                            {
                                Toast toast=Toast.makeText(MainActivity.this, "I am Group Owner" , Toast.LENGTH_SHORT);
                                toast.show();
                            }
                            else if(info.groupFormed)
                            {
                                Toast toast=Toast.makeText(MainActivity.this, "I am Group Client" , Toast.LENGTH_SHORT);
                                toast.show();
                            }
                        }


                    }
                });
            }           

        }
 };


 @Override
 public void onResume() 
 {
        super.onResume();
        registerReceiver(peerDiscoveryReceiver, mIntentFilter);
        registerReceiver(connectionChangedReceiver, mIntentFilter);
        registerReceiver(p2pStatusReceiver, mIntentFilter);
 }



 @Override
 public void onPause() 
 {
        super.onPause();
        unregisterReceiver(peerDiscoveryReceiver);
        unregisterReceiver(connectionChangedReceiver);
        unregisterReceiver(p2pStatusReceiver);
 }

我从http://kurser.iha.dk/eit/itsmap/basic/MEIER/Professional%20Android%204%20All%20downloads/9781118102275%20PA4AD/Code%20Snippets/Chapter%2016/PA4AD_Ch16_WiFiDirect/src/com拿了一个代码/paad/wifidirect/WiFiDirectActivity.java

提前感谢您的帮助

UPD 清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidapp"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission
    android:required="true"
    android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission
    android:required="true"
    android:name="android.permission.CHANGE_WIFI_STATE"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.androidapp.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>

4

1 回答 1

0

愿这能帮助你..

我想你没有在AndroidManifest.xml文件中给予许可..

于 2013-07-05T07:38:31.340 回答