/** * Registers a local service and then initiates a service discovery */ private void startRegistrationAndDiscovery() { Map<String, String> record = new HashMap<String, String>(); record.put(TXTRECORD_PROP_AVAILABLE, "visible"); WifiP2pDnsSdServiceInfo service = WifiP2pDnsSdServiceInfo.newInstance( SERVICE_INSTANCE, SERVICE_REG_TYPE, record); manager.addLocalService(channel, service, new ActionListener() { @Override public void onSuccess() { appendStatus("Added Local Service"); } @Override public void onFailure(int error) { appendStatus("Failed to add a service"); } }); discoverService(); }
private void addLocalService() { Map record = new HashMap<String, String>(); record.put("listenport", String.valueOf(PORT)); record.put("buddyname", "MyRevolution" + (int)(Math.random() *1000)); record.put("available", "visible"); WifiP2pDnsSdServiceInfo serviceInfo = WifiP2pDnsSdServiceInfo.newInstance("_images", "_myrevolution._tcp", record); mManager.addLocalService(mChannel, serviceInfo, new WifiP2pManager.ActionListener() { @Override public void onSuccess() { /// } @Override public void onFailure(int reason) { throw new RuntimeException("Fuck you"); } }); }
public void Start() { BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter(); if(bluetooth != null) { debug_print("My BT: " + bluetooth.getAddress() + " : " + bluetooth.getName() + " , state: " + bluetooth.getState()); String instance = bluetooth.getAddress() + " ; " + bluetooth.getName(); Map<String, String> record = new HashMap<String, String>(); record.put("available", "visible"); WifiP2pDnsSdServiceInfo service = WifiP2pDnsSdServiceInfo.newInstance(instance, WifiBase.SERVICE_TYPE, record); debug_print("Add local service :" + instance); p2p.addLocalService(channel, service, new WifiP2pManager.ActionListener() { public void onSuccess() { debug_print("Added local service"); } public void onFailure(int reason) { callback.LocalServiceStartError("Adding local service failed, error code " + reason); } }); }else{ callback.LocalServiceStartError("This device does not support Bluetooth"); } }
private void startLocalService(String instance) { Map<String, String> record = new HashMap<String, String>(); record.put("available", "visible"); WifiP2pDnsSdServiceInfo service = WifiP2pDnsSdServiceInfo.newInstance( instance, WifiBase.SERVICE_TYPE, record); debug_print("Add local service :" + instance); p2p.addLocalService(channel, service, new WifiP2pManager.ActionListener() { public void onSuccess() { debug_print("Added local service"); } public void onFailure(int reason) { debug_print("Adding local service failed, error code " + reason); } }); }
public void Start(String instance,String service_type) { Map<String, String> record = new HashMap<>(); record.put("available", "visible"); WifiP2pDnsSdServiceInfo service = WifiP2pDnsSdServiceInfo.newInstance(instance, service_type, record); debug_print("Add local service :" + instance); p2p.addLocalService(channel, service, new WifiP2pManager.ActionListener() { public void onSuccess() { lastError = -1; debug_print("Added local service"); } public void onFailure(int reason) { lastError = reason; debug_print("Adding local service failed, error code " + reason); } }); }
public void setAdvertisement() { mRecordMap.put(PORT, Integer.toString(mPort)); mRecordMap.put(IDENTIFIER, myIdentifier); mInfo = WifiP2pDnsSdServiceInfo.newInstance(instanceNamePrefix + myIdentifier, SERVICE_TYPE, mRecordMap); mManager.addLocalService(mChannel, mInfo, new ActionListener() { @Override public void onSuccess() { WfdLog.d(TAG, "addLocalService success"); } @Override public void onFailure(int reason) { Log.e(TAG, "addLocalService failure: " + reason); } }); }
@Override public void register(final int port, final boolean discover, Context context) { mP2PManager.initialize(context, context.getMainLooper(), new ChannelListener() { @Override public void onChannelDisconnected() { Log.d(TAG, "Channel disconnected..."); } }); mService.registerReceiver(mWifiReceiver, mWifiIntentFilter); final Map<String, String> record = new HashMap<String, String>(); record.put(TXTRECORD_PORT, String.valueOf(port)); mServiceInfo = WifiP2pDnsSdServiceInfo.newInstance(SERVICE_INSTANCE, SERVICE_REG_TYPE, record); mP2PManager.addLocalService(mChannel, mServiceInfo, mLocalServiceListener); if (discover) { discover(); } }
private void RegisterServerService() { //Registers a local service for clients to be able to search remote service Map<String, String> record = new HashMap<String, String>(); record.put(TXTRECORD_PROP_AVAILABLE, "visible"); record.put(TXTRECORD_SERVER_ANDROIDID, serverAndroid_id); //include server android id with the published service WifiP2pDnsSdServiceInfo service = WifiP2pDnsSdServiceInfo.newInstance(SERVER_SERVICE_INSTANCE, SERVICE_REG_TYPE, record); manager.addLocalService(channel, service, new ActionListener() { public void onSuccess() { Log.d(TAG,"Added Local Service"); } public void onFailure(int error) { Log.d(TAG,"Failed to add a service"); } }); }
private void RegisterClientService() { Log.d(TAG, "Registering ClientService"); //Registers a local service for clients to be able to search remote service Map<String, String> record = new HashMap<String, String>(); record.put(TXTRECORD_PROP_AVAILABLE, "visible"); WifiP2pDnsSdServiceInfo service = WifiP2pDnsSdServiceInfo.newInstance(CLIENT_SERVICE_INSTANCE, SERVICE_REG_TYPE, record); manager.addLocalService(channel, service, new ActionListener() { public void onSuccess() { Log.d(TAG,"Added Local Service"); } public void onFailure(int error) { Log.d(TAG,"Failed to add a service"); } }); }
@TargetApi(Build.VERSION_CODES.JELLY_BEAN) private void createService(final SalutCallback onSuccess, final SalutCallback onFailure) { manager.clearLocalServices(channel, null); Log.d(TAG, "Starting " + thisDevice.serviceName + " Transport Protocol " + TTP); //Inject the listening port along with whatever else data that is going to be sent. thisDevice.txtRecord.put("LISTEN_PORT", String.valueOf(thisDevice.servicePort)); //Create a service info object will android will actually hand out to the clients. serviceInfo = WifiP2pDnsSdServiceInfo.newInstance(thisDevice.instanceName, TTP, thisDevice.txtRecord); //Register our service. The callbacks here just let us know if the service was registered correctly, //not necessarily whether or not we connected to a device. manager.addLocalService(channel, serviceInfo, new WifiP2pManager.ActionListener() { @Override public void onSuccess() { Log.v(TAG, "Successfully added the local service."); if (onSuccess != null) onSuccess.call(); } @Override public void onFailure(int error) { Log.e(TAG, "Failed to create " + thisDevice.serviceName + " : Error Code: " + error); if (onFailure != null) onFailure.call(); } }); }
/** * This method will try to resolve why the wifip2p-state changed. If wifip2p was enbled * it will start broadcast a local service telling everyone around that they are * available to connect to. * * @param intent the intent that caused this method to be called */ private void handleStateChanged(Intent intent) { int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1); if( !(state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) ) { callback.onError(state); } else { // Create a string map containing information about your service. Map record = new HashMap(); record.put("listenport", String.valueOf(NetworkVariables.PORT)); record.put("servername", serverName); record.put("available", "visible"); // Service information. Pass it an instance name, service type // _protocol._transportlayer , and the map containing // information other devices will want once they connect to this one. WifiP2pDnsSdServiceInfo serviceInfo = WifiP2pDnsSdServiceInfo.newInstance(serverName, "_presence._tcp", record); // Add the local service, sending the service info, network channel, // and listener that will be used to indicate success or failure of // the request. mManager.addLocalService(mChannel, serviceInfo, new WifiP2pManager.ActionListener() { @Override public void onSuccess() {} @Override public void onFailure(int arg0) { debugHelper.log("Failed to add local service"); callback.onError(arg0); } }); } }
public NFGame(Context context, NFGameNotifyListener nFGameNotifyListener) { mContext = context; mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE); mWifiP2pManager = (WifiP2pManager) mContext.getSystemService(Context.WIFI_P2P_SERVICE); mChannel = mWifiP2pManager.initialize(mContext, mContext.getMainLooper(), null); mNFGameNotifyListener = nFGameNotifyListener; mNFGameBroadcastReceiver = new NFGameBroadcastReceiver(); mNFGameDnsSdServiceResponseListener = new NFGameDnsSdServiceResponseListener(); try { ApplicationInfo appInfo = mContext.getPackageManager().getApplicationInfo(mContext.getPackageName(), 0); appLabel = mContext.getPackageManager().getApplicationLabel(appInfo).toString(); } catch (NameNotFoundException e) { // e.printStackTrace(); } serviceType = mContext.getPackageName(); mWifiP2pDnsSdServiceInfo = WifiP2pDnsSdServiceInfo.newInstance(appLabel, serviceType, null); mWifiP2pDnsSdServiceRequest = WifiP2pDnsSdServiceRequest.newInstance(); mHandler = new Handler(); mDiscoveryingRunnable = new Runnable() { @Override public void run() { discoverPeers(); } }; peers = new ArrayList<WifiP2pDevice>(); servicePeers = new ArrayList<WifiP2pDevice>(); }
public void sendMessage(Message message) { // Create a string map containing information about your service. Map<String,String> record = new HashMap<String,String>(); record.put("dst", message.getDistance()+""); record.put("spd",+message.getVelocity()+""); record.put("msd_id", message.getId()+""); record.put("time", message.getTime().getTime()+""); record.put("axis", message.getAxis()+""); mServiceInfo = WifiP2pDnsSdServiceInfo.newInstance(SERVICE_NAME, "_presence._tcp", record); // Add the local service, sending the service info, network channel, // and listener that will be used to indicate success or failure of // the request. mManager.addLocalService(mChannel, mServiceInfo, new ActionListener() { @Override public void onSuccess() { // Toast.makeText(mContext, "Registo successo!", Toast.LENGTH_SHORT).show(); // Command successful! Code isn't necessarily needed here, // Unless you want to update the UI or add logging statements. } @Override public void onFailure(int arg0) { Toast.makeText(mContext, "Registo fail!", Toast.LENGTH_SHORT).show(); // Command failed. Check for P2P_UNSUPPORTED, ERROR, or BUSY } }); }
private void startRegistration() { // Create a string map containing information about your service. int SERVER_PORT = getAvaliablePort(); Map record = new HashMap(); record.put("listenport", String.valueOf(SERVER_PORT)); record.put("buddyname", "DynamixBridge_" + SERVER_PORT); record.put("available", "visible"); // Service information. Pass it an instance name, service type // _protocol._transportlayer , and the map containing // information other devices will want once they connect to this one. WifiP2pDnsSdServiceInfo serviceInfo = WifiP2pDnsSdServiceInfo.newInstance("_test", "_presence._tcp", record); // Add the local service, sending the service info, network channel, // and listener that will be used to indicate success or failure of // the request. mManager.addLocalService(mChannel, serviceInfo, new ActionListener() { @Override public void onSuccess() { // Command successful! Code isn't necessarily needed here, // Unless you want to update the UI or add logging statements. } @Override public void onFailure(int arg0) { // Command failed. Check for P2P_UNSUPPORTED, ERROR, or BUSY } }); }
@Override public void onSuccess() { final Map<String, String> record = new HashMap<String, String>(); record.put(TXTRECORD_PROP_AVAILABLE, "visible"); manager.addLocalService(channel, WifiP2pDnsSdServiceInfo.newInstance(SERVICE_INSTANCE, SERVICE_REG_TYPE, record), new LogActionListener("addLocalService")); }
/****************************Wi-Fi P2P for Service Discovery******************************/ private void startRegistration() { // Create a string map containing information about your service. Map<String,String> record = new HashMap<>(); record.put("listenport", String.valueOf("8080")); record.put("buddyname", Build.MODEL); record.put("available", "visible"); // Service information. Pass it an instance name, service type // _protocol._transportlayer , and the map containing // information other devices will want once they connect to this one. WifiP2pDnsSdServiceInfo serviceInfo = WifiP2pDnsSdServiceInfo.newInstance("_test", "_presence._tcp", record); // Add the local service, sending the service info, network channel, // and listener that will be used to indicate success or failure of // the request. mManager.addLocalService(mChannel, serviceInfo, new WifiP2pManager.ActionListener() { @Override public void onSuccess() { Log.e("addLocalService","onSuccess"); } @Override public void onFailure(int reason) { switch (reason){ case WifiP2pManager.ERROR: Log.e("addLocalService","ERROR"); break; case WifiP2pManager.P2P_UNSUPPORTED: Log.e("addLocalService","P2P_UNSUPPORTED"); break; case WifiP2pManager.BUSY: Log.e("addLocalService","BUSY"); break; case WifiP2pManager.NO_SERVICE_REQUESTS: Log.e("addLocalService","NO_SERVICE_REQUESTS"); break; } } }); }
/** * Setups an own local "susurrus"-service (which represents a chat room). */ public void setupLocalService(final CreateActivity feedbackActivity, final RoomModel roomModel) { feedbackActivity.registerWifiDialogUpdate(GROUP_CREATING); wifiDirectManager.createGroup(this.wifiDirectChannel, createGroupListener); // get the creator's username and add it // Service information. Pass it an instance name, service type // _protocol._transportlayer, and the map containing // information other devices will want once they connect to this one. this.roomInfo = WifiP2pDnsSdServiceInfo.newInstance(SERVICE_NAME, SERVICE_TYPE, roomModel.toHashMap()); // Add the local service, sending the service info, network channel, // and listener that will be used to indicate success or failure of // the request. wifiDirectManager.addLocalService(wifiDirectChannel, this.roomInfo, new WifiP2pManager.ActionListener() { @Override public void onSuccess() { Log.d(LOG_TAG, "... service created."); // send feedback feedbackActivity.registerWifiDialogUpdate(GROUP_CREATED); discoverLocalServices(); } @Override public void onFailure(int errorCode) { // command failed, check for P2P_UNSUPPORTED, ERROR, or BUSY if (errorCode == WifiP2pManager.P2P_UNSUPPORTED) { Log.d(LOG_TAG, "... service error code: P2P_UNSUPPORTED"); } else if (errorCode == WifiP2pManager.ERROR) { Log.d(LOG_TAG, "... service error code: ERROR"); } else if (errorCode == WifiP2pManager.BUSY) { Log.d(LOG_TAG, "... service error code: BUSY"); } feedbackActivity.registerWifiDialogUpdate(GROUP_ERROR); } }); }
private void createService(final SalutCallback onSuccess, final SalutCallback onFailure) { manager.clearLocalServices(channel, null); Log.d(TAG, "Starting " + thisDevice.serviceName + " Transport Protocol " + TTP); //Inject the listening port along with whatever else data that is going to be sent. thisDevice.txtRecord.put("LISTEN_PORT", String.valueOf(thisDevice.servicePort)); //Create a service info object will android will actually hand out to the clients. serviceInfo = WifiP2pDnsSdServiceInfo.newInstance(thisDevice.instanceName, TTP , thisDevice.txtRecord); //Register our service. The callbacks here just let us know if the service was registered correctly, //not necessarily whether or not we connected to a device. manager.addLocalService(channel, serviceInfo, new WifiP2pManager.ActionListener() { @Override public void onSuccess() { Log.v(TAG, "Successfully added the local service."); manager.createGroup(channel, new WifiP2pManager.ActionListener() { @Override public void onSuccess() { Log.v(TAG, "Successfully created group."); Log.d(TAG, "Successfully created " + thisDevice.serviceName + " service running on port " + thisDevice.servicePort); isRunningAsHost = true; if (onSuccess != null) { onSuccess.call(); } } @Override public void onFailure(int reason) { Log.e(TAG, "Failed to create group. Reason :" + reason); if (onFailure != null) onFailure.call(); } }); } @Override public void onFailure(int error) { Log.e(TAG, "Failed to create " + thisDevice.serviceName + " : Error Code: " + error); if (onFailure != null) onFailure.call(); } }); }
public void registerService() { // Create a string map containing information about your service. Map<String, String> userAttribMap = new HashMap<String, String>(); String regId = Registry.get(slpActivity, Expression.LOCATION_SELF); if (regId == null) { slpActivity.log("Not registered with Google Cloud Messaging, cannot share", true); return; } String userFriendlyName = PreferenceManager.getDefaultSharedPreferences(slpActivity).getString("name", null); if (userFriendlyName == null) { slpActivity.runOnUiThread(new Runnable() { @Override public void run() { slpActivity.log("Please set a name for your device", true); slpActivity.showDialog(slpActivity.DIALOG_SET_NAME); } }); return; } userAttribMap.put("name", userFriendlyName); userAttribMap.put("regId", regId); if (serviceInfo != null) { unregisterService(); } // Service information. Pass it an instance name, service type // _protocol._transportlayer , and the map containing // information other devices will want once they connect to this one. serviceInfo = WifiP2pDnsSdServiceInfo.newInstance("run2gether", "presence", userAttribMap); // Add the local service, sending the service info, network channel, // and listener that will be used to indicate success or failure of // the request. p2pManager.addLocalService(p2pChannel, serviceInfo, new WifiP2pManager.ActionListener() { @Override public void onSuccess() { Log.d(TAG, "service registered"); // Command successful! Code isn't necessarily needed here, // Unless you want to update the UI or add logging statements. } @Override public void onFailure(int arg0) { Log.e(TAG, "cannot register service"); // Command failed. Check for P2P_UNSUPPORTED, ERROR, or BUSY } }); }
@SuppressWarnings({"unchecked", "UnusedParameters"}) private void registerService(DeviceProfile profile) { HashMap record = new HashMap<>(); // record.put("listenport", String.valueOf(trunk.mainService)); //record.put("protocolVersion", "0"); //TODO: add actual version //record.put("deviceType", profile.getType().toString()); //record.put("mobileStatus", profile.getStatus().toString()); //record.put("congestion", String.valueOf(profile.getCongestion())); //record.put("hwServices", profile.getServices().toString()); WifiP2pDnsSdServiceInfo serviceInfo = WifiP2pDnsSdServiceInfo.newInstance("_Scatterbrain", "_presence._tcp",record); /* manager.removeLocalService(channel, serviceInfo, new WifiP2pManager.ActionListener() { @Override public void onSuccess() { Log.v(TAG, "Deregistered discovery service"); } @Override public void onFailure(int reason) { Log.e(TAG, "Failed to deregister discovery service"); } }); */ manager.addLocalService(channel, serviceInfo, new WifiP2pManager.ActionListener() { @Override public void onSuccess() { Log.v(TAG, "Successfully registered Scatterbrain service for discovery"); } @Override public void onFailure(int reason) { String reason_s; switch(reason) { case WifiP2pManager.P2P_UNSUPPORTED: reason_s = "P2P_UNSUPPORTED"; break; case WifiP2pManager.BUSY: reason_s = "P2P_BUSY"; break; case WifiP2pManager.ERROR: reason_s = "P2P_ERROR"; break; default: reason_s = "HUH?"; break; } Log.e(TAG, "Failed to register Scatterbrain discovery service: " + reason_s ); } }); }
public void addLocalService(String serviceInstance, String serviceRegType, Map<String, String> record) { WifiP2pDnsSdServiceInfo serviceInfo = WifiP2pDnsSdServiceInfo.newInstance(serviceInstance, serviceRegType, record); manager.addLocalService(channel, serviceInfo, new ActionListener() { public void onSuccess() { } public void onFailure(int error) { } }); }