package com.khmelenko.lab.mibanddemo;

import android.app.Activity;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.le.ScanResult;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.khmelenko.lab.miband.MiBand;

import java.util.ArrayList;
import java.util.HashMap;

import rx.functions.Action1;
import timber.log.Timber;

public class ScanActivity extends Activity {
    private MiBand miband;

    HashMap<String, BluetoothDevice> devices = new HashMap<>();
    private ArrayAdapter<String> mAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.scan);

        miband = new MiBand(this);

        mAdapter = new ArrayAdapter<>(this, R.layout.item, new ArrayList<>());

        Button startScanButton = (Button) findViewById(R.id.starScanButton);
        startScanButton.setOnClickListener(v -> {
            Timber.d("Scanning started...");
            miband.startScan()
                    .subscribe(handleScanResult(),
                            Throwable::printStackTrace);
        });

        findViewById(R.id.stopScanButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Timber.d("Stop scanning...");
                miband.stopScan().subscribe(handleScanResult());
            }
        });


        ListView lv = (ListView) findViewById(R.id.listView);
        lv.setAdapter(mAdapter);
        lv.setOnItemClickListener((parent, view, position, id) -> {
            String item = ((TextView) view).getText().toString();
            if (devices.containsKey(item)) {

                miband.stopScan().subscribe(handleScanResult());

                BluetoothDevice device = devices.get(item);
                Intent intent = new Intent();
                intent.putExtra("device", device);
                intent.setClass(ScanActivity.this, MainActivity.class);
                ScanActivity.this.startActivity(intent);
                ScanActivity.this.finish();
            }
        });

    }

    /**
     * Handles the result of scanning
     *
     * @return Action handler
     */
    private Action1<ScanResult> handleScanResult() {
        return result -> {
            BluetoothDevice device = result.getDevice();
            Timber.d("Scan results: name:" + device.getName() + ",uuid:"
                    + device.getUuids() + ",add:"
                    + device.getAddress() + ",type:"
                    + device.getType() + ",bondState:"
                    + device.getBondState() + ",rssi:" + result.getRssi());

            String item = device.getName() + "|" + device.getAddress();
            if (!devices.containsKey(item)) {
                devices.put(item, device);
                mAdapter.add(item);
            }
        };
    }
}

@@ -204,34 +204,8 @@
 le;%0A
-import android.view.View;%0A
 impo
@@ -400,16 +400,41 @@
 ayList;%0A
+import java.util.Arrays;%0A
 import j
@@ -459,17 +459,27 @@
 %0Aimport 
-r
+io.reactive
 x.functi
@@ -482,23 +482,24 @@
 nctions.
-Action1
+Consumer
 ;%0Aimport
@@ -519,16 +519,75 @@
 imber;%0A%0A
+/**%0A * Scanner activity%0A *%0A * @author Dmytro Khmelenko%0A */%0A
 public c
@@ -623,16 +623,17 @@
 ivity %7B%0A
+%0A
     priv
@@ -644,18 +644,19 @@
 MiBand m
-ib
+MiB
 and;%0A%0A  
@@ -657,16 +657,24 @@
 d;%0A%0A    
+private 
 HashMap%3C
@@ -698,17 +698,18 @@
 Device%3E 
-d
+mD
 evices =
@@ -935,18 +935,19 @@
        m
-ib
+MiB
 and = ne
@@ -1224,26 +1224,27 @@
            m
-ib
+MiB
 and.startSca
@@ -1433,105 +1433,15 @@
 ner(
-new View.OnClickListener() %7B%0A            @Override%0A            public void onClick(View v) %7B%0A    
+v -%3E %7B%0A
     
@@ -1482,39 +1482,36 @@
 %22);%0A            
-    mib
+mMiB
 and.stopScan().s
@@ -1500,32 +1500,53 @@
 iBand.stopScan()
+%0A                    
 .subscribe(handl
@@ -1561,30 +1561,16 @@
 ult());%0A
-            %7D%0A
         
@@ -1820,17 +1820,18 @@
     if (
-d
+mD
 evices.c
@@ -1868,18 +1868,19 @@
        m
-ib
+MiB
 and.stop
@@ -1885,16 +1885,41 @@
 opScan()
+%0A                        
 .subscri
@@ -1984,17 +1984,18 @@
 evice = 
-d
+mD
 evices.g
@@ -2296,25 +2296,24 @@
         %7D);%0A
-%0A
     %7D%0A%0A    /
@@ -2414,15 +2414,16 @@
 ate 
-Action1
+Consumer
 %3CSca
@@ -2623,32 +2623,48 @@
               + 
+Arrays.toString(
 device.getUuids(
@@ -2664,16 +2664,17 @@
 tUuids()
+)
  + %22,add
@@ -2950,17 +2950,18 @@
    if (!
-d
+mD
 evices.c
@@ -2996,17 +2996,18 @@
         
-d
+mD
 evices.p

Fix errors in scan activity