How null pointer exception can be removed - android

I am trying to find the signal strength of Wifi but I m getting a null pointer exception.
While fetching the network informatiopns like SSID etc. Can anyone suggest me a solution how to remove the null pointer exception.
enter code here:
public class MyReciever extends BroadcastReceiver{
WifiManager wifi;
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
List<ScanResult> results=wifi.getScanResults();
ScanResult bestSignal=null;
for(ScanResult result:results)
{
if(bestSignal==null || WifiManager.compareSignalLevel(bestSignal.level, result.level)<0)
bestSignal=result;
}
String message=String.format("%s networks found. %s is the strongest", results.size(),bestSignal.SSID);
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
Log.d("Debug","onRecieve() message:" +message);
}
}
public class MainActivity extends Activity {
TextView textStatus;
WifiManager wifi;
BroadcastReceiver receiver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textStatus=(TextView)findViewById(R.id.textStatus);
wifi=(WifiManager)getSystemService(Context.WIFI_SERVICE);
WifiInfo info=wifi.getConnectionInfo();
textStatus.append("\n\nWifi Status: " +info.toString());
List<WifiConfiguration> configs=wifi.getConfiguredNetworks();
for(WifiConfiguration config:configs)
{
textStatus.append("\n\n" +config.toString());
}
if(receiver==null)
receiver = new MyReciever();
registerReceiver(receiver, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
Log.d("TAG", "onCreate()");
}
#Override
public void onStop() {
unregisterReceiver(receiver);
super.onStop();
}
}

Problem could be at String message=String.format("%s networks found. %s is the strongest", results.size(),bestSignal.SSID);
When there is no 'bestSignal' found, variable 'bestSignal' will be null and you are trying to execute bestSignal.SSID which might cause NPE.
Change you code like
if (bestSignal != null) {
String message=String.format("%s networks found. %s is the strongest", results.size(),bestSignal.SSID);
}
Hope it helps :)

Related

Getting notified about change in state of WiFi on android device?

I am aiming to build a android app which record everything going on with devices WiFi adapter. For example WiFi being turned on/off, device getting connected/moving out of range of a WiFi router, etc.
My app should be able to record these events as soon as the device is turned on. Clearing the app from RECENTS should not affect the ability of the app to record these events.
I have gone through BroadcastReceiver. It gets tied to the life cycle of the app and hence will not record the events once app gets cleared from RECENTS.
public class MainActivity extends Activity {
BroadcastReceiver mybroadcastReceiver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mybroadcastReceiver = new WifiBroadcastReceiver(this);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
registerReceiver(mybroadcastReceiver, intentFilter);
}
#Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mybroadcastReceiver);
}
}
public class WifiBroadcastReceiver extends BroadcastReceiver {
final String TAG = "WifiBroadcastReceiver";
final String desiredMacAddress = "02:17:1c:96:42:fa";
Activity activity;
WifiBroadcastReceiver(Activity activity) {
this.activity = activity;
}
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (WifiManager.SUPPLICANT_STATE_CHANGED_ACTION.equals(action)) {
SupplicantState state = intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE);
if (SupplicantState.isValidState(state) && state == SupplicantState.COMPLETED)
checkConnectedToDesiredWifi();
}
}
/** Detect you are connected to a specific network. */
private void checkConnectedToDesiredWifi() {
WifiManager myWifiManager = (WifiManager)activity.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = myWifiManager.getConnectionInfo();
if (wifiInfo != null) {
// get current router MAC address
String bssid = wifiInfo.getBSSID();
if (desiredMacAddress.equals(bssid))
Log.d(TAG, "Connected to " + bssid + " i.e., desiredMacAddress");
else
Log.d(TAG, "Connected to " + bssid + " not " + desiredMacAddress);
}
}
}
One solution could be to run the BroadcastReceiver from a Service that doesn't depend on the lifecycle of any Activity.
1) Declare the network state permission usage in the manifest
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
1) Create a Service class
public class WifiService extends Service {
private BroadcastReceiver mReceiver;
#Nullable
#Override
public IBinder onBind(Intent intent) {
// When creating the service, register a broadcast receiver
// to listen for network state changes
IntentFilter filter = new IntentFilter();
filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
mReceiver = new WifiReceiver();
registerReceiver(mReceiver, filter);
return null;
}
#Override
public boolean onUnbind(Intent intent) {
// Unregister the receiver when unbinding the service
unregisterReceiver(mReceiver);
mReceiver = null;
return super.onUnbind(intent);
}
}
2) Create a BroadcastReceiver
public class WifiReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
boolean isConnected = networkInfo != null &&
networkInfo.isConnectedOrConnecting();
if (isConnected) {
switch (networkInfo.getType()) {
case ConnectivityManager.TYPE_WIFI:
// Connected via wifi
checkConnectedToDesiredWifi();
break;
case ConnectivityManager.TYPE_ETHERNET:
// Connected via ethernet
break;
case ConnectivityManager.TYPE_MOBILE:
// Connected via mobile data
break;
}
}
}
private void checkConnectedToDesiredWifi() {
// ...
}
}
3) Declare the service in the manifest
<service android:name=".receivers.WifiService"
android:stopWithTask="false"
android:enabled="true"/>

Failed to receive data over usb using Usbserial example

I am using following UsbSerial example from below link https://github.com/felHR85/SerialPortExample. I want receive data from over usb from the device shown in the photo.
Device is basically a counter machine which is sending counter data over serial port.
I am able to connect device and open port from it but unable to read data stream from it. Below is the code used. code is not giving any error
Mainactivity class
public class MainActivity extends AppCompatActivity {
/*
* Notifications from UsbService will be received here.
*/
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
switch (intent.getAction()) {
case UsbService.ACTION_USB_PERMISSION_GRANTED: // USB PERMISSION GRANTED
Toast.makeText(context, "USB Ready", Toast.LENGTH_SHORT).show();
break;
case UsbService.ACTION_USB_PERMISSION_NOT_GRANTED: // USB PERMISSION NOT GRANTED
Toast.makeText(context, "USB Permission not granted", Toast.LENGTH_SHORT).show();
break;
case UsbService.ACTION_NO_USB: // NO USB CONNECTED
Toast.makeText(context, "No USB connected", Toast.LENGTH_SHORT).show();
break;
case UsbService.ACTION_USB_DISCONNECTED: // USB DISCONNECTED
Toast.makeText(context, "USB disconnected", Toast.LENGTH_SHORT).show();
break;
case UsbService.ACTION_USB_NOT_SUPPORTED: // USB NOT SUPPORTED
Toast.makeText(context, "USB device not supported", Toast.LENGTH_SHORT).show();
break;
}
}
};
private UsbService usbService;
private TextView display;
private EditText editText;
private MyHandler mHandler;
private final ServiceConnection usbConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName arg0, IBinder arg1) {
usbService = ((UsbService.UsbBinder) arg1).getService();
usbService.setHandler(mHandler);
}
#Override
public void onServiceDisconnected(ComponentName arg0) {
usbService = null;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mHandler = new MyHandler(this);
display = (TextView) findViewById(R.id.textView1);
editText = (EditText) findViewById(R.id.editText1);
Button sendButton = (Button) findViewById(R.id.buttonSend);
sendButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!editText.getText().toString().equals("")) {
String data = editText.getText().toString();
if (usbService != null) { // if UsbService was correctly binded, Send data
display.append(data);
usbService.write(data.getBytes());
}
}
}
});
}
#Override
public void onResume() {
super.onResume();
setFilters(); // Start listening notifications from UsbService
startService(UsbService.class, usbConnection, null); // Start UsbService(if it was not started before) and Bind it
}
#Override
public void onPause() {
super.onPause();
unregisterReceiver(mUsbReceiver);
unbindService(usbConnection);
}
private void startService(Class<?> service, ServiceConnection serviceConnection, Bundle extras) {
if (!UsbService.SERVICE_CONNECTED) {
Intent startService = new Intent(this, service);
if (extras != null && !extras.isEmpty()) {
Set<String> keys = extras.keySet();
for (String key : keys) {
String extra = extras.getString(key);
startService.putExtra(key, extra);
}
}
startService(startService);
}
Intent bindingIntent = new Intent(this, service);
bindService(bindingIntent, serviceConnection, Context.BIND_AUTO_CREATE);
}
private void setFilters() {
IntentFilter filter = new IntentFilter();
filter.addAction(UsbService.ACTION_USB_PERMISSION_GRANTED);
filter.addAction(UsbService.ACTION_NO_USB);
filter.addAction(UsbService.ACTION_USB_DISCONNECTED);
filter.addAction(UsbService.ACTION_USB_NOT_SUPPORTED);
filter.addAction(UsbService.ACTION_USB_PERMISSION_NOT_GRANTED);
registerReceiver(mUsbReceiver, filter);
}
/*
* This handler will be passed to UsbService. Data received from serial port is displayed through this handler
*/
private static class MyHandler extends Handler {
private final WeakReference<MainActivity> mActivity;
public MyHandler(MainActivity activity) {
mActivity = new WeakReference<>(activity);
}
#Override
public void handleMessage(Message msg) {
mActivity.get().display.append("Handle:");
switch (msg.what) {
case UsbService.MESSAGE_FROM_SERIAL_PORT:
String data = (String) msg.obj;
mActivity.get().display.append(data);
break;
}
}
}
}
I know it's bit late, however just to help others who might come across similar issue, did you find solution to your problem? If not, I cannot see the other java file corresponding to the service (USBService.java) as described in the example referred by you. The same file contains following code snippet which you would like to debug to find out what's going wrong (could be a problem with byte to string conversion or so). Hope this helps.
/*
* Data received from serial port will be received here. Just populate onReceivedData with your code
* In this particular example. byte stream is converted to String and send to UI thread to
* be treated there.
*/
private UsbSerialInterface.UsbReadCallback mCallback = new UsbSerialInterface.UsbReadCallback()
{
#Override
public void onReceivedData(byte[] arg0)
{
try
{
String data = new String(arg0, "UTF-8");
if(mHandler != null)
mHandler.obtainMessage(MESSAGE_FROM_SERIAL_PORT,data).sendToTarget();
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
}
};

How to get wifi signal informations : strength, Link speed on android studio?

I used the folowing code but i still have problems, it doesn't return any informations :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initializeWiFiListener();
}
public void initializeWiFiListener() {
String connectivity_context = Context.WIFI_SERVICE;
final WifiManager wifi = (WifiManager) getSystemService(connectivity_context);
if (!wifi.isWifiEnabled()) {
if (wifi.getWifiState() != WifiManager.WIFI_STATE_ENABLING) {
wifi.setWifiEnabled(true);
}
}
registerReceiver(new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
WifiInfo info = wifi.getConnectionInfo();
String ssid = info.getSSID();
int rssi = info.getRssi();
int speed = info.getLinkSpeed();
TextView t1 = (TextView) findViewById(R.id.textView);
t1.setText(ssid + " " + Integer.toString(rssi) + " " + Integer.toString(speed));
}
}, new IntentFilter(WifiManager.RSSI_CHANGED_ACTION));
}
}
there is no informations about my connected wifi.
How to get wifi signal informations : strength, Link speed ?
Based on the documentation, call getConnectionInfo() on WifiManager, to get a WifiInfo object. On there, you have getRssi() for signal strength and getLinkSpeed() for link speed.

Android Sim Change Detect, error in Log.e

I have error when refer to LogCat. I have store the storedsimcard(1st) and compare with currentsimcard(2nd), if sim serial is different, logcat will print out sim changed. But i had problem with my service when i run it. Below is my code
LogCat showing
Tag SimSerial:: 8944110065486249080
Tag Current Sim Serial:: 8944110065486249080
Tag Sim Status Sim no changed !!!
Tag SimSerial:: 8944110065486249080
Tag Current Sim Serial:: 8944110065486249080
Tag Sim Status Sim changed !!!
First part is correct, but the second part Sim Status should no "Sim no changed" as well.
Does anyone know where is the error ?
BootCompleteReceiver
public class BootCompleteReceiver extends BroadcastReceiver {
Context context;
SharedPreferences settings;
public static final String PREFS_NAME = "MyPrefsFile";
#Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, MyService.class);
context.startService(service);
}
}
MyService
public class MyService extends Service {
String storedSimSerial;
String currentSimSerial;
#Override
public void onCreate() {
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
TelephonyManager telephoneMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
storedSimSerial = telephoneMgr.getSimSerialNumber();
Log.e("SimSerial::",storedSimSerial);
TelephonyManager tmMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
currentSimSerial = tmMgr.getSimSerialNumber();
Log.e("Current Sim Serial::",currentSimSerial);
if(currentSimSerial==storedSimSerial)
{
Log.e("Sim Status","Sim no changed !!!");
}
else
Log.e("Sim Status","Sim changed !!!");
return Service.START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Destroy", Toast.LENGTH_LONG).show();
}
#Override
public IBinder onBind(Intent arg0) {
return null;
}
}
Write your if like this:
if(currentSimSerial.equals(storedSimSerial))
{
Log.e("Sim Status","Sim no changed !!!");
}
else
Log.e("Sim Status","Sim changed !!!");
When you use == you compare Object references, not content which rarely works for Strings.

BroadcastReceiver not being called after activity resume

First of all I'm a novice to android, so this could probably be a silly issue, nevertheless I've already spent a couple of days trying to get to a solution.
I'm trying to build a wifi module for localization purpouses, so I wrote a BroadcastReceiver in order to handle the wifi scanning and the localization. The application works and does its (quite simple at this stage) job, with anu kind of issues both when I change the orientation of the screen and when I hit the back button on my Desire HD and then open the application again. But when I hit the HOME key, going to the main screen, and then enter again my app the Broadcast Receiver seems not to work anymore, and if I close the application I get an error message.
Here's the code, partially adapted from here.
public class WiFiDemo extends Activity implements OnClickListener {
private static final String TAG = "WiFiDemo";
WifiManager wifi;
BroadcastReceiver receiver;
WifiManager.WifiLock lock;
boolean wifiPrevState;
boolean scanON = false;
String header;
TextView textStatus;
Button buttonScan;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Setup UI
textStatus = (TextView) findViewById(R.id.textStatus);
buttonScan = (Button) findViewById(R.id.buttonScan);
buttonScan.setOnClickListener(this);
// Setup WiFi
if (wifi == null){
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
}
//checking WiFi status, enabling it if needed and locking it.
wifiPrevState = wifi.isWifiEnabled();
wifi.setWifiEnabled(true);
if (lock == null){
lock = wifi.createWifiLock("lock");
}
lock.acquire();
// Get WiFi status
WifiInfo info = wifi.getConnectionInfo();
header="\n\nWiFi Status: \n" + info.toString() + "\n\nAvailable nets:";
textStatus.append(header);
// Register Broadcast Receiver
if (receiver == null)
receiver = new WiFiScanReceiver(this);
registerReceiver(receiver, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
Log.d(TAG, "onCreate()");
}
/*
#Override
protected void onPause(){
super.onPause();
wifi.setWifiEnabled(wifiPrevState);
lock.release();
unregisterReceiver(receiver);
Log.d(TAG, "onPause()");
}
#Override
protected void onResume(){
super.onResume();
wifi.setWifiEnabled(true);
lock.acquire();
registerReceiver(receiver, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
Log.d(TAG, "onResume()");
}
*/
#Override
public void onStop() {
super.onStop();
wifi.setWifiEnabled(wifiPrevState);
lock.release();
unregisterReceiver(receiver);
}
public void onClick(View view) {
Toast.makeText(this, "On Click Clicked. Toast to that!!!",
Toast.LENGTH_LONG).show();
if (view.getId() == R.id.buttonScan) {
Log.d(TAG, "onClick() wifi.startScan()");
scanON = !scanON;
wifi.startScan();
}
}
}
And this is the BroadcastReceiver
public class WiFiScanReceiver extends BroadcastReceiver {
private static final String TAG = "WiFiScanReceiver";
WiFiDemo wifiDemo;
ScanResult storedBest;
public WiFiScanReceiver(WiFiDemo wifiDemo) {
super();
this.wifiDemo = wifiDemo;
storedBest = null;
}
#Override
public void onReceive(Context c, Intent intent) {
List<ScanResult> results = wifiDemo.wifi.getScanResults();
ScanResult bestSignal = null;
wifiDemo.textStatus.setText(wifiDemo.header);
for (ScanResult result : results) {
if (bestSignal == null
|| WifiManager.compareSignalLevel(bestSignal.level, result.level) < 0)
bestSignal = result;
wifiDemo.textStatus.append("\n\n" + result.toString());
}
if ( storedBest == null || ((bestSignal.SSID.compareTo(storedBest.SSID)!=0) && bestSignal.level>-50)){
storedBest = bestSignal;
String locationMessage = String.format("You are near %s's Access Point",
bestSignal.SSID);
Toast.makeText(wifiDemo, locationMessage, Toast.LENGTH_LONG).show();
}
String message = String.format("%s networks found. %s is the strongest. Its level is %s",
results.size(), bestSignal.SSID, bestSignal.level);
if (wifiDemo.scanON) wifiDemo.wifi.startScan();
Log.d(TAG, "onReceive() message: " + message);
}
}
When posting, its best to post the error message that you are getting so we know the problem you are having.
That being said, the reason it probably isn't working is because you unregister your receiver in onStop and you only register your receiver in onCreate. You should typically do these type of calls in life cycle stages that match.
onCreate/onDestroy
onStart/onStop
onResume/onPause.
To fix your problem, try registering your receiver in onStart instead of onCreate.
Your onResume() method is commented out...
Are you sure that's the correct IntentFilter?

Categories

Resources