@Override public void onPeersAvailable(WifiP2pDeviceList wifiP2pDeviceList) { if (!connectionAttemptInProgress.compareAndSet(false, true)) { return; } if (connected.get()) { return; } for (WifiP2pDevice device : wifiP2pDeviceList.getDeviceList()) { if (device.deviceAddress.equals(target)) { WifiP2pConfig config = new WifiP2pConfig(); config.deviceAddress = device.deviceAddress; config.wps.setup = WpsInfo.PBC; config.groupOwnerIntent = 0; // I want the other device to be the group owner (or 'server') Log.d(TAG, "Trying to connect to "+device.deviceAddress+" "+device.deviceName+" Owner: "+device.isGroupOwner()); connect(config); break; } } }
private static String getP2pDeviceStatus(int deviceStatus) { Log.d(TAG, "Peer status :" + deviceStatus); switch (deviceStatus) { case WifiP2pDevice.AVAILABLE: return "Available"; case WifiP2pDevice.INVITED: return "Invited"; case WifiP2pDevice.CONNECTED: return "Connected"; case WifiP2pDevice.FAILED: return "Failed"; case WifiP2pDevice.UNAVAILABLE: return "Unavailable"; default: return "Unknown"; } }
static String getDeviceStatus(int deviceStatus) { Log.d(FileTransferService.TAG, "Peer status :" + deviceStatus); switch (deviceStatus) { case WifiP2pDevice.AVAILABLE: return "Available"; case WifiP2pDevice.INVITED: return "Invited"; case WifiP2pDevice.CONNECTED: return "Connected"; case WifiP2pDevice.FAILED: return "Failed"; case WifiP2pDevice.UNAVAILABLE: return "Unavailable"; default: return "Unknown"; } }
private void connectToPeer (WifiP2pDevice device) { WifiP2pConfig config = new WifiP2pConfig(); config.deviceAddress = device.deviceAddress; config.wps.setup = WpsInfo.PBC; mManager.connect(mChannel, config, new WifiP2pManager.ActionListener() { @Override public void onSuccess() { // WiFiDirectBroadcastReceiver will notify us. Ignore for now. } @Override public void onFailure(int reason) { } }); }
private static String getDeviceStatus(int deviceStatus) { Log.d(WiFiDirectActivity.TAG, "Peer status :" + deviceStatus); switch (deviceStatus) { case WifiP2pDevice.AVAILABLE: return "Available"; case WifiP2pDevice.INVITED: return "Invited"; case WifiP2pDevice.CONNECTED: return "Connected"; case WifiP2pDevice.FAILED: return "Failed"; case WifiP2pDevice.UNAVAILABLE: return "Unavailable"; default: return "Unknown"; } }
@Override public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; if (v == null) { LayoutInflater vi = (LayoutInflater) getActivity().getSystemService( Context.LAYOUT_INFLATER_SERVICE); v = vi.inflate(R.layout.row_devices, null); } WifiP2pDevice device = items.get(position); if (device != null) { TextView top = (TextView) v.findViewById(R.id.device_name); TextView bottom = (TextView) v.findViewById(R.id.device_details); if (top != null) { top.setText(device.deviceName); } if (bottom != null) { bottom.setText(getDeviceStatus(device.status)); } } return v; }
public static String getDeviceStatus(int statusCode) { switch (statusCode) { case WifiP2pDevice.CONNECTED: return "Connected"; case WifiP2pDevice.INVITED: return "Invited"; case WifiP2pDevice.FAILED: return "Failed"; case WifiP2pDevice.AVAILABLE: return "Available"; case WifiP2pDevice.UNAVAILABLE: return "Unavailable"; default: return "Unknown"; } }
/** * 切断する */ protected void internalDisconnect(final WifiP2pManager.ActionListener listener) { if (DEBUG) Log.v(TAG, "internalDisconnect:"); if (mWifiP2pManager != null) { if ((mWifiP2pDevice == null) || (mWifiP2pDevice.status == WifiP2pDevice.CONNECTED)) { // 接続されていないか、既に接続済みの時 if (mChannel != null) { mWifiP2pManager.removeGroup(mChannel, listener); } } else if (mWifiP2pDevice.status == WifiP2pDevice.AVAILABLE || mWifiP2pDevice.status == WifiP2pDevice.INVITED) { // ネゴシエーション中の時 mWifiP2pManager.cancelConnect(mChannel, listener); } } }
private static String getDeviceStatus(int deviceStatus) { Log.d(WiFiDirectActivity.TAG, "Peer status :" + deviceStatus); switch (deviceStatus) { case WifiP2pDevice.AVAILABLE: return "Available"; //可以连接 case WifiP2pDevice.INVITED: return "Invited"; //邀请连接 case WifiP2pDevice.CONNECTED: return "Connected"; //已连接 case WifiP2pDevice.FAILED: return "Failed"; //失败 case WifiP2pDevice.UNAVAILABLE: return "Unavailable"; //不可以连接 default: return "Unknown"; } }
@Override public void cancelDisconnect() { if(mManager != null){ final DeviceListFragment fragment = (DeviceListFragment)getFragmentManager().findFragmentById(R.id.frag_list); if(fragment.getDevice() == null || fragment.getDevice().status == WifiP2pDevice.CONNECTED){ disconnect(); }else if(fragment.getDevice().status == WifiP2pDevice.AVAILABLE || fragment.getDevice().status == WifiP2pDevice.INVITED){ mManager.cancelConnect(mChannel, new WifiP2pManager.ActionListener() { @Override public void onSuccess() { } @Override public void onFailure(int reason) { } }); } } }
private static String getDeviceStatus(int deviceStatus){ Log.e(WiFiDirectActivity.TAG, "Peer status: "+deviceStatus); switch(deviceStatus){ case WifiP2pDevice.AVAILABLE: return "Avaiable"; case WifiP2pDevice.INVITED: return "Invited"; case WifiP2pDevice.CONNECTED: return "Conntend"; case WifiP2pDevice.FAILED: return "Failed"; case WifiP2pDevice.UNAVAILABLE: return "Unavailable"; default: return "Unkonw"; } }
@Override public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; if(v == null){ LayoutInflater vi = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = vi.inflate(R.layout.row_devices, null); } WifiP2pDevice device = items.get(position); if(device != null){ TextView top = (TextView)v.findViewById(R.id.device_name); TextView bottom = (TextView)v.findViewById(R.id.device_details); if(null != top){ top.setText(device.deviceName); } if(null != bottom){ bottom.setText(device.deviceAddress); } } return v; }
/** * Takes a WifiP2pDevice and returns a String of readable device information * @param wifiP2pDevice * @return */ public String p2pDeviceToString(WifiP2pDevice wifiP2pDevice) { if (wifiP2pDevice != null) { String strDevice = "Device name: " + wifiP2pDevice.deviceName; strDevice += "\nDevice address: " + wifiP2pDevice.deviceAddress; if (wifiP2pDevice.equals(thisDevice)) { strDevice += "\nIs group owner: " + isGroupOwner(); } else { strDevice += "\nIs group owner: false"; } strDevice += "\nStatus: " + deviceStatusToString(wifiP2pDevice.status) + "\n"; return strDevice; } else { Log.e(TAG, "WifiP2pDevice is null"); return ""; } }
/** * Translates a device status code to a readable String status * @param status * @return A readable String device status */ public String deviceStatusToString(int status) { if (status == WifiP2pDevice.AVAILABLE) { return "Available"; } else if (status == WifiP2pDevice.INVITED) { return "Invited"; } else if (status == WifiP2pDevice.CONNECTED) { return "Connected"; } else if (status == WifiP2pDevice.FAILED) { return "Failed"; } else if (status == WifiP2pDevice.UNAVAILABLE) { return "Unavailable"; } else { return "Unknown"; } }
@Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) { // UI update to indicate wifi p2p status. int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1); if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) { // Wifi Direct mode is enabled //activity.setIsWifiP2pEnabled(true); } else { //activity.setIsWifiP2pEnabled(false); } Log.d(TAG, "P2P state changed"); } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) { Log.d(TAG, "P2P peers changed"); } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) { Log.d(getClass().getName(), "P2P connection changed"); NetworkInfo networkInfo = intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO); if (networkInfo.isConnected()) { // we are connected with the other device, request connection // info to find group owner IP manager.requestConnectionInfo(channel, connectionInfoListener); } } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) { // NOTE(JS): Getting the hardware id of this device and passing it to the activity so it can be shown WifiP2pDevice thisDevice = intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE); transfer.setConnectionSpecificInfo(thisDevice.deviceAddress); Log.d(TAG, "This device changed"); } }
@Override public void onPeersAvailable(WifiP2pDeviceList peerList) { peers.clear(); peers.addAll(peerList.getDeviceList()); //Log.d(TAG,peers.toString()); // if Device on Inviting or Connected, terminate. if (onConnecting || wfd.status.p2p_status.equals("Connected") || wfd.status.p2p_status.equals("Invited")){ return; } // Search Opponent Device in Peer List for(int i=0; i<peers.size(); ++i){ if (peers.get(i).deviceName.equals(wfd.getOpponentID())){ onConnecting = true; WifiP2pDevice device = peers.get(i); WifiP2pConfig config = new WifiP2pConfig(); config.deviceAddress = device.deviceAddress; config.wps.setup = WpsInfo.PBC; Log.d(TAG,"connect challenge"); wfd.connect(config); return; } } // Can't Found Opponent Device Log.d(TAG,"can't found device"); }
protected void updateThisDevice(WifiP2pDevice device) { // status String s = getP2pDeviceStatus(device.status); status.p2p_status = s; // notify if(s.equals("Connected")){ toast("P2P接続完了…"); }else{ // Turn off the light controlWfdButton(ButtonCmd.OFF); } }
@Override public void updateThisDevice(WifiP2pDevice device) { // stopServer(); Device dev = new Device(device.deviceName, device.deviceAddress, device.status); final int N = mCallbacks.beginBroadcast(); for (int i=0; i<N; i++) { try { mCallbacks.getBroadcastItem(i).deviceUpdate(dev); } catch (RemoteException e) {} } mCallbacks.finishBroadcast(); if (readyToStop) stopSelf(); }
@Override public void onPeersAvailable(WifiP2pDeviceList peers) { List<Device> peerList = new ArrayList<Device>(); for (WifiP2pDevice peer : peers.getDeviceList()){ peerList.add(new Device(peer.deviceName, peer.deviceAddress, peer.status)); } // Broadcast to all clients the new value. final int N = mCallbacks.beginBroadcast(); for (int i=0; i<N; i++) { try { mCallbacks.getBroadcastItem(i).updatePeersList(peerList); } catch (RemoteException e) {} } mCallbacks.finishBroadcast(); }
@Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) { // UI update to indicate wifi p2p status. int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1); if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) { // Wifi Direct mode is enabled listener.setIsWifiP2pEnabled(true); } else { listener.setIsWifiP2pEnabled(false); } } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) { // request available peers from the wifi p2p manager. This is an // asynchronous call and the calling activity is notified with a // callback on PeerListListener.onPeersAvailable() if (manager != null) manager.requestPeers(channel, (PeerListListener) listener); } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) { if (manager == null) return; NetworkInfo networkInfo = (NetworkInfo) intent .getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO); if (networkInfo.isConnected()) { // we are connected with the other device, request connection // info to find group owner IP manager.requestConnectionInfo(channel, (ConnectionInfoListener) listener); } } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) { listener.updateThisDevice((WifiP2pDevice) intent.getParcelableExtra( WifiP2pManager.EXTRA_WIFI_P2P_DEVICE)); } }
@Override public void showDetails(WifiP2pDevice device) { DeviceDetailFragment fragment = (DeviceDetailFragment) getFragmentManager() .findFragmentById(R.id.frag_detail); fragment.showDetails(device); }
@Override public void cancelDisconnect() { /* * A cancel abort request by user. Disconnect i.e. removeGroup if * already connected. Else, request WifiP2pManager to abort the ongoing * request */ if (manager != null) { final DeviceListFragment fragment = (DeviceListFragment) getFragmentManager() .findFragmentById(R.id.frag_list); if (fragment.getDevice() == null || fragment.getDevice().status == WifiP2pDevice.CONNECTED) { disconnect(); } else if (fragment.getDevice().status == WifiP2pDevice.AVAILABLE || fragment.getDevice().status == WifiP2pDevice.INVITED) { manager.cancelConnect(channel, new ActionListener() { @Override public void onSuccess() { Toast.makeText(WiFiDirectActivity.this, "Aborting connection", Toast.LENGTH_SHORT).show(); } @Override public void onFailure(int reasonCode) { Toast.makeText(WiFiDirectActivity.this, "Connect abort request failed. Reason Code: " + reasonCode, Toast.LENGTH_SHORT).show(); } }); } } }
/** * @param context * @param textViewResourceId * @param objects */ public WiFiPeerListAdapter(Context context, int textViewResourceId, List<WifiP2pDevice> objects) { super(context, textViewResourceId, objects); items = objects; }
@Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); Log.d(WiFiServiceDiscoveryActivity.TAG, action); if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) { if (manager == null) { return; } NetworkInfo networkInfo = (NetworkInfo) intent .getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO); if (networkInfo.isConnected()) { // we are connected with the other device, request connection // info to find group owner IP Log.d(WiFiServiceDiscoveryActivity.TAG, "Connected to p2p network. Requesting network details"); manager.requestConnectionInfo(channel, (ConnectionInfoListener) activity); } else { // It's a disconnect } } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION .equals(action)) { WifiP2pDevice device = (WifiP2pDevice) intent .getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE); Log.d(WiFiServiceDiscoveryActivity.TAG, "Device status -" + device.status); } }
private void connect(WifiP2pDevice device) { duration = System.currentTimeMillis() - startTime; Log.d(TAG, "connect WifiP2pDevice: starting connection (duration: "+duration+" ms)"); WifiP2pConfig config = new WifiP2pConfig(); config.deviceAddress = device.deviceAddress; config.wps.setup = WpsInfo.PBC; manager.connect(channel, config, new LogActionListener("WiFiClient.connect")); }
/** * 指定した機器へ接続を試みる * @param device */ public void connect(@NonNull final WifiP2pDevice device) { if (DEBUG) Log.v(TAG, "connect:device=" + device); final WifiP2pConfig config = new WifiP2pConfig(); config.deviceAddress = device.deviceAddress; config.wps.setup = WpsInfo.PBC; connect(config); }
/** * 周辺のWiFi Direct機器の状態が変化した時のコールバックリスナーを呼び出す * @param devices */ protected void callOnUpdateDevices(@NonNull final List<WifiP2pDevice> devices) { if (DEBUG) Log.v(TAG, "callOnUpdateDevices:"); for (final WiFiP2pListener listener: mListeners) { try { listener.onUpdateDevices(devices); } catch (final Exception e1) { Log.w(TAG, e1); mListeners.remove(listener); } } }
@Override public void onPeersAvailable(final WifiP2pDeviceList peers) { if (DEBUG) Log.v(TAG, "onPeersAvailable:peers=" + peers); final Collection<WifiP2pDevice> devices = peers.getDeviceList(); synchronized (mAvailableDevices) { mAvailableDevices.clear(); mAvailableDevices.addAll(devices); } callOnUpdateDevices(mAvailableDevices); }
/** * Update UI for this device. * * @param device WifiP2pDevice object */ public void updateThisDevice(WifiP2pDevice device) { this.device = device; TextView view = (TextView) mContentView.findViewById(R.id.my_name); view.setText(device.deviceName); view = (TextView) mContentView.findViewById(R.id.my_status); view.setText(getDeviceStatus(device.status)); }
/** * Updates the UI with device data * * @param device the device to be displayed */ public void showDetails(WifiP2pDevice device) { this.device = device; this.getView().setVisibility(View.VISIBLE); TextView view = (TextView) mContentView.findViewById(R.id.device_address); view.setText(device.deviceAddress); view = (TextView) mContentView.findViewById(R.id.device_info); view.setText(device.toString()); }