我正在尝试从另一个Android设备将文件传输到wifi热点主机Android设备,就像SHAREIT一样。 我对此没有任何线索。虽然我试过通过wifi对等(P2P)方法
http://developer.android.com/guide/topics/connectivity/wifip2p.html
我还尝试了android SDK附带的p2p文件传输示例代码。
但主要问题是,p2p必须要有一个wifi接入点。所以我用另一个Android设备创建了一个wifi热点。我已将测试设备连接到该热点网络。但是测试设备找不到热点主机设备。因此我无法传输文件。如果我将另一个设备添加到该网络,那么测试设备可以找到新连接的设备,但不能找到主机设备,但我需要将文件传输到主机设备。
修改
我试过这段代码
final TextView deviceListText = (TextView) findViewById(R.id.list);
final WifiP2pManager wifiP2pManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
channel = wifiP2pManager.initialize(this, getMainLooper(), channelListener);
// wifiP2pManager.discoverPeers(channel, newActionListener);
// wifiP2pManager.createGroup(channel, newActionListener);
final WiFiDirectBroadcastReceiver wiFiDirectBroadcastReceiver = new WiFiDirectBroadcastReceiver(wifiP2pManager, channel, this);
mIntentFilter = new IntentFilter();
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);
final Button stop = (Button) findViewById(R.id.button);
final Button start = (Button) findViewById(R.id.button2);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
wifiP2pManager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
deviceListText.setText(" discovering peers ");
}
@Override
public void onFailure(int reason) {
deviceListText.setText("error discovering peers "+String.valueOf(reason));
}
});
}
});
myPeerListListener = new WifiP2pManager.PeerListListener()
{
@Override
public void onPeersAvailable(final WifiP2pDeviceList peers) {
Collection<WifiP2pDevice> devicesAddressObj = peers.getDeviceList();
for (final WifiP2pDevice newDevice : devicesAddressObj) {
// deviceListText.setText(newDevice.deviceName);
config.deviceAddress = newDevice.deviceAddress;
wifiP2pManager.connect(channel, config, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
deviceListText.setText(" connected ");
}
@Override
public void onFailure(int reason) {
deviceListText.setText(" not connect , reason: " + String.valueOf(reason));
}
});
}
}
};
*这是我的广播接收器*
public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {
private WifiP2pManager mManager;
private WifiManager wifiManager2;
private WifiP2pManager.Channel mChannel;
private Activity mActivity;
final TextView deviceListText = (TextView) findViewById(R.id.list);
public WiFiDirectBroadcastReceiver(WifiP2pManager manager, WifiP2pManager.Channel channel,
Activity activity) {
super();
this.mManager = manager;
this.mChannel = channel;
this.mActivity = activity;
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
StringBuilder stringBuilder = new StringBuilder();
if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
// Check to see if Wi-Fi is enabled and notify appropriate activity
} else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
// Call WifiP2pManager.requestPeers() to get a list of current peers
if (mManager != null) {
mManager.requestPeers(mChannel, myPeerListListener);
}
} else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
// Respond to new connection or disconnections
} else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
// Respond to this device's wifi state changing
runOnUiThread(new Runnable() {
@Override
public void run() {
deviceListText.setText("device's wifi state changing");
}
});
}
}
}
任何帮助将不胜感激。