0

I want to get the ssid in scan result and upload to firebase, but there is nothing that gets put into the database. What am I missing in my code below?

private void uploadWifiList() {

    mWifiManager = (WifiManager) (WifiManager) getActivity().getApplicationContext().getSystemService(WIFI_SERVICE);
    mWifiManager.startScan();
    Toast.makeText(getContext(), "Scanning....", Toast.LENGTH_SHORT).show();

    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

    DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();

    ArrayList<String> wifis;
    wifis = new ArrayList<String>(); //initialize wifis

    List<ScanResult> scanResults = mWifiManager.getScanResults();
    wifis.clear();

    for (int i = 0; i < scanResults.size(); i++) {
        String rssid = scanResults.get(i).SSID;
        wifis.add(rssid);

        databaseReference.child("ssid").child(rssid).setValue(true);
    }

    ArrayList<String> userlist = new ArrayList<>();
    userlist.add(user.getDisplayName());

    DatabaseReference databaseReference1 = FirebaseDatabase.getInstance().getReference();
    for(String userlista : userlist) {
        databaseReference1.child("userlist").child(userlista).setValue(true);
    }
}
ivcubr
  • 1,988
  • 9
  • 20
  • 28
Caesar
  • 1
  • 1
  • If you print the value of `rssid` what does it show? – Frank van Puffelen Aug 18 '18 at 21:25
  • I want to show ssid in database, so rssid to get string ssid, what's wrong? Thank you. – Caesar Aug 19 '18 at 02:04
  • but database is nothing. Just user list can show. – Caesar Aug 19 '18 at 02:07
  • What is the value of `rssid`? Try to print it. Please responde with @. – Alex Mamo Aug 20 '18 at 09:49
  • @AlexMamo I try to get rssid, but the for loop is not work, how can i do? if for loop outside, I get D/ADebugTag: wifiList: null D/ADebugTag: wifis: [] scanResults: [] – Caesar Aug 20 '18 at 18:12
  • Use it only inside, not outside. If you want outside, check **[this](https://stackoverflow.com/questions/47847694/how-to-return-datasnapshot-value-as-a-result-of-a-method/47853774)** out. – Alex Mamo Aug 20 '18 at 18:14
  • @AlexMamo I check the wifiList.size is 0, and then for loop is not work, so I can not get the rssid value. – Caesar Aug 20 '18 at 21:05

0 Answers0