package com.github.ivbaranov.rxbluetooth.example;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import com.github.ivbaranov.rxbluetooth.RxBluetooth;
import com.github.ivbaranov.rxbluetooth.Action;
import java.util.ArrayList;
import java.util.List;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {
  private static final int REQUEST_ENABLE_BT = 1;

  private Button start;
  private Button stop;
  private ListView result;
  private RxBluetooth rxBluetooth;
  private Subscription deviceSubscription;
  private Subscription discoveryStartSubscription;
  private Subscription discoveryFinishSubscription;
  private Subscription bluetoothStateOnSubscription;
  private Subscription bluetoothStateOtherSubscription;
  private List<String> devices = new ArrayList<>();

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    start = (Button) findViewById(R.id.start);
    stop = (Button) findViewById(R.id.stop);
    result = (ListView) findViewById(R.id.result);

    rxBluetooth = new RxBluetooth(this);
    if (!rxBluetooth.isBluetoothEnabled()) {
      rxBluetooth.enableBluetooth(this, REQUEST_ENABLE_BT);
    }

    deviceSubscription = rxBluetooth.observeDevices()
        .observeOn(AndroidSchedulers.mainThread())
        .subscribeOn(Schedulers.computation())
        .subscribe(new Action1<BluetoothDevice>() {
          @Override public void call(BluetoothDevice bluetoothDevice) {
            addDevice(bluetoothDevice);
          }
        });

    discoveryStartSubscription = rxBluetooth.observeDiscovery()
        .observeOn(AndroidSchedulers.mainThread())
        .subscribeOn(Schedulers.computation())
        .filter(Action.isEqualTo(BluetoothAdapter.ACTION_DISCOVERY_STARTED))
        .subscribe(new Action1<String>() {
          @Override public void call(String action) {
            start.setText(R.string.button_searching);
          }
        });

    discoveryFinishSubscription = rxBluetooth.observeDiscovery()
        .observeOn(AndroidSchedulers.mainThread())
        .subscribeOn(Schedulers.computation())
        .filter(Action.isEqualTo(BluetoothAdapter.ACTION_DISCOVERY_FINISHED))
        .subscribe(new Action1<String>() {
          @Override public void call(String action) {
            start.setText(R.string.button_restart);
          }
        });

    bluetoothStateOnSubscription = rxBluetooth.observeBluetoothState()
        .observeOn(AndroidSchedulers.mainThread())
        .subscribeOn(Schedulers.computation())
        .filter(Action.isEqualTo(BluetoothAdapter.STATE_ON))
        .subscribe(new Action1<Integer>() {
          @Override public void call(Integer integer) {
            start.setBackgroundColor(getResources().getColor(R.color.colorActive));
          }
        });

    bluetoothStateOtherSubscription = rxBluetooth.observeBluetoothState()
        .observeOn(AndroidSchedulers.mainThread())
        .subscribeOn(Schedulers.computation())
        .filter(Action.isEqualTo(BluetoothAdapter.STATE_OFF, BluetoothAdapter.STATE_TURNING_OFF,
            BluetoothAdapter.STATE_TURNING_ON))
        .subscribe(new Action1<Integer>() {
          @Override public void call(Integer integer) {
            start.setBackgroundColor(getResources().getColor(R.color.colorInactive));
          }
        });

    start.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        devices.clear();
        setAdapter(devices);
        rxBluetooth.startDiscovery();
      }
    });
    stop.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        rxBluetooth.cancelDiscovery();
      }
    });
  }

  @Override protected void onDestroy() {
    super.onDestroy();
    if (rxBluetooth != null) {
      // Make sure we're not doing discovery anymore
      rxBluetooth.cancelDiscovery();
    }

    unsubscribe(deviceSubscription);
    unsubscribe(discoveryStartSubscription);
    unsubscribe(discoveryFinishSubscription);
    unsubscribe(bluetoothStateOnSubscription);
    unsubscribe(bluetoothStateOtherSubscription);
  }

  private void addDevice(BluetoothDevice device) {
    String deviceName;
    deviceName = device.getAddress();
    if (!TextUtils.isEmpty(device.getName())) {
      deviceName += " " + device.getName();
    }
    devices.add(deviceName);

    setAdapter(devices);
  }

  private void setAdapter(List<String> list) {
    int itemLayoutId = android.R.layout.simple_list_item_1;
    result.setAdapter(new ArrayAdapter<>(this, itemLayoutId, list));
  }

  private static void unsubscribe(Subscription subscription) {
    if (subscription != null && !subscription.isUnsubscribed()) {
      subscription.unsubscribe();
      subscription = null;
    }
  }
}

@@ -257,24 +257,55 @@
 .view.View;%0A
+import android.view.ViewGroup;%0A
 import andro
@@ -386,24 +386,57 @@
 t.ListView;%0A
+import android.widget.TextView;%0A%0A
 import com.g
@@ -1194,22 +1194,31 @@
 te List%3C
-String
+BluetoothDevice
 %3E device
@@ -4681,109 +4681,375 @@
     
-String deviceName;%0A    deviceName = device.getAddress();%0A    if (!TextUtils.isEmpty(device.getName())
+devices.add(device);%0A    setAdapter(devices);%0A  %7D%0A%0A  private void setAdapter(List%3CBluetoothDevice%3E list) %7B%0A    int itemLayoutId = android.R.layout.simple_list_item_1;%0A    result.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_2, android.R.id.text1, list) %7B%0A      @Override%0A      public View getView(int position, View convertView, ViewGroup parent
 ) %7B%0A
@@ -5058,285 +5058,553 @@
     
+ 
-deviceName += %22 %22 + device.getName(
+ View view = super.getView(position, convertView, parent);%0A%0A        BluetoothDevice device = devices.get(position
 );%0A    
-%7D%0A
     
-devices.add(deviceName);%0A%0A    setAdapter(devices);%0A  %7D%0A%0A  private void setAdapter(List%3CString%3E list) %7B%0A    int itemLayoutId = android.R.layout.simple_list_item_1;%0A    result.setAdapter(new ArrayAdapter%3C%3E(this, itemLayoutId, list)
+String devName = device.getName();%0A        String devAddress = device.getAddress();%0A%0A        if (TextUtils.isEmpty(devName)) %7B%0A          devName = %22NO NAME%22;%0A        %7D%0A        TextView text1 = (TextView) view.findViewById(android.R.id.text1);%0A        TextView text2 = (TextView) view.findViewById(android.R.id.text2);%0A%0A        text1.setText(devName);%0A        text2.setText(devAddress);%0A        return view;%0A      %7D%0A    %7D
 );%0A 

Update device list adapter to display names followed by addresses