Why websocket connection closes immediately...? - android

When i tried to create a websocket connection in android for implementing wamp using Autobahn, the connection is created and closed immediatley.
why this happening ....?
How to solve..........?
Part of my code is given below
private void start() {
// TODO Auto-generated method stub
final String wsuri = "ws://192.168.0.102:8080/ws";
if (!connection.isConnected()) {
Log.d("tag", "Not Connected");
}
connection.connect(wsuri, new ConnectionHandler() {
#Override
public void onOpen() {
// TODO Auto-generated method stub
Log.d("tag", "Connected to " + wsuri);
testPubSub();
}
#Override
public void onClose(int code, String reason) {
// TODO Auto-generated method stub
Log.d("tag", "disconnected");
}
});
}
protected void testPubSub() {
// TODO Auto-generated method stub
connection.subscribe(TOPIC, String.class, new EventHandler() {
#Override
public void onEvent(String topicUri, Object event) {
// TODO Auto-generated method stub
Log.d("tag", "Event Recieved");
}
});
}
Sorry for not posting the manifest file earlier. Its given below..
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.testwamp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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>

Most probably you didn't put INTERNET permission to the manifest.xml
<uses-permission android:name="android.permission.INTERNET" />

Related

LG Connect SDK issue with Android 7

I have an app that works fine with Android 6, but freezes on Android 7. All I have is added ConnectSDK by LG (http://www.svlconnectsdk.com/docs/1-6-0/android/). The app freezes as soon as I call start on DiscoveryManager's object. And I get the following stacktrace:
10-26 13:32:51.678: W/System(7569): ClassLoader referenced unknown path: /data/app/com.testconnectsdk.test-1/lib/arm64
10-26 13:32:51.774: W/System.err(7569): java.net.SocketException: Socket closed
10-26 13:32:51.774: W/System.err(7569): at java.net.PlainDatagramSocketImpl.receive0(Native Method)
10-26 13:32:51.774: W/System.err(7569): at java.net.AbstractPlainDatagramSocketImpl.receive(AbstractPlainDatagramSocketImpl.java:147)
10-26 13:32:51.774: W/System.err(7569): at java.net.DatagramSocket.receive(DatagramSocket.java:808)
10-26 13:32:51.774: W/System.err(7569): at com.connectsdk.discovery.provider.ssdp.SSDPClient.multicastReceive(SSDPClient.java:109)
10-26 13:32:51.774: W/System.err(7569): at com.connectsdk.discovery.provider.SSDPDiscoveryProvider$2.run(SSDPDiscoveryProvider.java:271)
10-26 13:32:51.774: W/System.err(7569): at java.lang.Thread.run(Thread.java:761)
The code is very simple :
public class MainActivity extends Activity implements DiscoveryManagerListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DiscoveryManager.init(getApplicationContext(), null);
final DiscoveryManager dm = DiscoveryManager.getInstance();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
dm.addListener(MainActivity.this);
}
}, 2000);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
dm.start();
}
}, 4000);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(MainActivity.this, OtherActivity.class);
startActivity(i);
}
}, 6000);
}
private static final String TAG = MainActivity.class.getName();
#Override
public void onDeviceAdded(DiscoveryManager manager, ConnectableDevice device) {
// TODO Auto-generated method stub
Log.d(TAG, "onDeviceAdded");
}
#Override
public void onDeviceUpdated(DiscoveryManager manager, ConnectableDevice device) {
// TODO Auto-generated method stub
Log.d(TAG, "onDeviceUpdated");
}
#Override
public void onDeviceRemoved(DiscoveryManager manager, ConnectableDevice device) {
// TODO Auto-generated method stub
Log.d(TAG, "onDeviceRemoved");
}
#Override
public void onDiscoveryFailed(DiscoveryManager manager, ServiceCommandError error) {
// TODO Auto-generated method stub
Log.d(TAG, "onDiscoveryFailed");
}
}
Any idea why is it crashing? Pasting my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testconnectsdk.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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>
<activity android:name="OtherActivity"></activity>
</application>
</manifest>
Kindly please help. Badly need it. Thanks

Android Studio Estimote Beacon Application

im trying to make basic beacon application in android studio. I just want to scan beacons and list them into the screen. Here are my codes. I took them from somewhere.
public class MainActivity extends ActionBarActivity {
private static final String ESTIMOTE_PROXIMITY_UUID = "B9407F30-F5F8-466E-AFF9-25556B57FE6D";
private static final Region ALL_ESTIMOTE_BEACONS = new Region("regionId", null, null, null);
private BeaconManager beaconManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
beaconManager.setRangingListener(new BeaconManager.RangingListener() {
#Override public void onBeaconsDiscovered(Region region, List<Beacon> beacons) {
Log.d("TAG", "Ranged beacons: " + beacons);
}
});
}
#Override
protected void onStart() {
super.onStart();
beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
#Override public void onServiceReady() {
try {
beaconManager.startRanging(ALL_ESTIMOTE_BEACONS);
} catch (RemoteException e) {
Log.e("TAG","Cannot start ranging", e);
}
}
});
}
#Override
protected void onStop() {
super.onStop();
try {
beaconManager.stopRanging(ALL_ESTIMOTE_BEACONS);
} catch (RemoteException e) {
Log.e("TAG", "Cannot stop but it does not matter now", e);
}
}
#Override
protected void onDestroy() {
super.onDestroy();
beaconManager.disconnect();
}
}
Here is my manifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.oem.estimote_ibeacon_app" >
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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>
<service
android:name="com.estimote.sdk.service.BeaconService"
android:exported="false"/>
</application>
</manifest>
When i opened application it says "stopped" please help. I have beacons for test. Where is my mistake? Thank you.
it seems that ALL_ESTIMOTE_BEACONS in
beaconManager.startRanging(ALL_ESTIMOTE_BEACONS);
is null.
enter a valid regionid instead of "regionid" in
private static final Region ALL_ESTIMOTE_BEACONS = new Region("regionId", null, null, null);

How to remotely invoke a device admin policy?

What I want to do, have a [BroadcastReceiver] that listens to any incoming SMS, that when the incoming SMS has a certain keyword, it will pass over to device admin and invoke the required policy.
I have created a BroadcastReceiver that listen to SMS and pop a toast, and it works fine. Then I created a device admin app following this guide: http://marakana.com/s/post/1291/android_device_policy_administration_tutorial, and its working as it should. But when I include that SMS receiver together with device admin, it crashes whenever an sms comes in. Also, I'm unable to pass the string that I want to use from the SMS receiver to my device admin app in order to invoke the required policy.
This is my device admin
public class AppPolicyActivity extends Activity implements OnCheckedChangeListener {
static final int ACTIVATION_REQUEST = 47;
DevicePolicyManager mDPM;
ComponentName appPolicy;
ToggleButton toggleBtn;
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity_layout);
toggleBtn = (ToggleButton) findViewById(R.id.toggleBtn);
toggleBtn.setOnCheckedChangeListener(this);
mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
appPolicy = new ComponentName(this, AppPolicyReceiver.class);
// The commented code below made my app crash during installation
/* Intent intent;
try {
if (getIntent() != null) {
intent = getIntent();
String keyword = intent.getStringExtra("keyword");
if (keyword.equals("LOCK")) {
receivedSMS(keyword);
} else if (keyword.equals("WIPE")) {
receivedSMS(keyword);
}
keyword = "";
intent = null;
finish();
}
} catch (Exception e) {} */
}
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, appPolicy);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Activate the application to use its features");
startActivityForResult(intent, ACTIVATION_REQUEST);
} else {
mDPM.removeActiveAdmin(appPolicy);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
switch (requestCode) {
case ACTIVATION_REQUEST:
if (resultCode == Activity.RESULT_OK) {
toggleBtn.setChecked(true);
} else {
toggleBtn.setChecked(false);
}
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
public void receivedSMS(String keyword) {
// Lock
if (keyword.equals("LOCK")) {
mDPM.lockNow();
}
// Wipe/delete
else if (keyword.equals("WIPE")) {
mDPM.wipeData(ACTIVATION_REQUEST);
}
}
}
This is the device admin receiver class
public class AppPolicyReceiver extends DeviceAdminReceiver {
#Override
public void onDisabled(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, R.string.disabletext, Toast.LENGTH_SHORT).show();
super.onDisabled(context, intent);
}
#Override
public void onEnabled(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, R.string.enabletext, Toast.LENGTH_SHORT).show();
super.onEnabled(context, intent);
}
}
this is my manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.fyp.mobilesecurity"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<application android:label="#string/app_name"
android:icon="#drawable/ic_launcher"
android:theme="#style/AppTheme">
<activity android:name="AppPolicyActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!-- appPolicyReceiver -->
<receiver android:permission="android.permission.BIND_DEVICE_ADMIN" android:name="AppPolicyReceiver">
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/>
<action android:name="android.app.action.DEVICE_ADMIN_DISABLED"/>
</intent-filter>
<meta-data android:resource="#xml/app_admin"
android:name="android.app.device_admin"/>
</receiver>
<!-- SMSReceiver -->
<receiver android:enabled="true" android:name="SMSReceiver" android:exported="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
and this is from log, as you can see my receiver is not even started.
11-17 21:06:52.300: E/AndroidRuntime(375): java.lang.RuntimeException: Unable to start receiver com.fyp.mobilesecurity.SMSReceiver: java.lang.NullPointerException
I just realize that its returning an exception, so I enclosed my receiver in try/catch block, and now its working fine.
You'll need to see what's causing the NullPointerException in SMSReceiver. The stacktrace will have the line number.

Starting intentservice over adb fails

When I try to start an IntentService over adb i get tho following error
E/AndroidRuntime( 2418): java.lang.RuntimeException: Unable to instantiate service
com.myCompany.MyPackage.Service: java.lang.InstantiationException:
com.myCompany.MyPackage.Service
when I start a normal service ovre adb, everithing works fine.
What are the reason a cannot start an intentservice over adb?
public class NCService extends IntentService {
public NCService(String name) {
super(name);
// TODO Auto-generated constructor stub
}
NetworkStateReceiver nsr;
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "onStartCommand", 0).show();
nsr = new NetworkStateReceiver();
return START_STICKY;
}
#Override
public void onCreate() {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "onCreate", 0).show();
super.onCreate();
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "onDestroy", 0).show();
super.onDestroy();
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
}
}
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<service android:name=".NCService" />
<receiver android:name=".NetworkStateReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
</application>
Regards
Simon
Your service need a default no-arg constructor, otherwise Android does not know what argument pass as name parameter:
public NCService() {
super("MyNCService");
}

Android addProximityAlert does not fire

I tried to implement the addProximityAlert method, but it does not fire. Here is my code:
SampleProximityAlert
public class SampleProximityAlert extends Activity {
/** Called when the activity is first created. */
protected LocationManager locationManager;
protected Location location;
public static Location pLocation;
protected Intent intent;
protected PendingIntent pIntent;
protected GeoPoint point;
public float distance;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
locationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
try {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 1000, 1,
new ProximityListener());
} catch (RuntimeException e) {
e.printStackTrace();
}
// point = new GeoPoint(49868004, 8626971);
try {
pLocation = new Location(location.getProvider());
} catch (RuntimeException e) {
e.printStackTrace();
}
Log.i("PROVIDER", location.getProvider());
pLocation.setLatitude(49.868004);
pLocation.setLongitude(8.626971);
distance = methodeDistance(pLocation);
Log.i("DISTANCE", String.valueOf(distance));
Log.i("POSITION",
String.valueOf(location.getLatitude()) + ","
+ String.valueOf(location.getLongitude()));
intent = new Intent(this, ProximityAlert.class);
intent.setAction("ProximityAlert");
pIntent = PendingIntent.getService(this, 0, intent, 0);
locationManager.addProximityAlert(49.868004, 8.626971, 429, 50000,
pIntent);
}
public float methodeDistance(Location mLocation) {
float mDistance = location.distanceTo(pLocation);
return mDistance;
}
}
ProximityAlert
public class ProximityAlert extends Service {
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Log.i("Alert", "fired");
return null;
}
#Override
public void onCreate() {
super.onCreate();
Log.i("ONCREATE", "create ProximityAlert as Service");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("ONSTARTCOMMAND", "OnStartCommand");
return super.onStartCommand(intent, flags, startId);
}
}
ProximityListener
public class ProximityListener implements LocationListener {
String DEBUG_TAG = "ProximityListener";
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
Log.d(DEBUG_TAG, location.toString());
// tvD.setText(String.valueOf(location.distanceTo(pLocation)));
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
XML Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tsystems.proximityAlert"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".SampleProximityAlert"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".ProximityAlert">
<intent-filter>
<action android:name="ProximityAlert"/>
<category android:name="com.proaktiveOrtung"/>
</intent-filter>
</service>
<service android:name=".ProximityAlert"></service>
</application>
<uses-sdk android:minSdkVersion="8" />
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tsystems.proximityAlert"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".SampleProximityAlert"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".ProximityAlert">
<intent-filter>
<action android:name="ProximityAlert"/>
<category android:name="com.proaktiveOrtung"/>
</intent-filter>
</service>
<service android:name=".ProximityAlert"></service>
</application>
<uses-sdk android:minSdkVersion="8" />
</manifest>
Sorry I selected not the complete code.
when you add a ProximityAlert you have to pass a PendingIntent to the LocationManager, these PendingIntent will be fired on a broadcastIntent and i dont see in your code when, where you register a receiver for that PendingIntent.
Now a question... why do you write a service if you add the ProximityAlert on an Activity?

Categories

Resources