From b7110e2c37dee13e556617b1d35466353a47b2c7 Mon Sep 17 00:00:00 2001 From: Bjørn Mork Date: Thu, 30 May 2019 19:02:21 +0200 Subject: returning selected device works! MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Bjørn Mork --- .../no/mork/android/defogger/ScanListAdapter.java | 20 ++++++++++++++++++++ .../no/mork/android/defogger/ScannerActivity.java | 8 ++++++++ 2 files changed, 28 insertions(+) diff --git a/Defogger/src/no/mork/android/defogger/ScanListAdapter.java b/Defogger/src/no/mork/android/defogger/ScanListAdapter.java index cc1c4e5..e386056 100644 --- a/Defogger/src/no/mork/android/defogger/ScanListAdapter.java +++ b/Defogger/src/no/mork/android/defogger/ScanListAdapter.java @@ -2,18 +2,22 @@ package no.mork.android.defogger; import android.bluetooth.BluetoothDevice; import android.content.Context; +import android.util.Log; import android.view.LayoutInflater; +import android.view.View.OnClickListener; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import java.util.ArrayList; +import no.mork.android.defogger.ScannerActivity; // originally from https://developer.android.com/guide/topics/ui/layout/recyclerview // but converted to simpler ArrayAdapter using https://developer.android.com/guide/topics/ui/declaring-layout.html#FillingTheLayout public class ScanListAdapter extends BaseAdapter { + private static String msg = "Defogger Adapter: "; private ArrayList mObjects; private Context mCtx; private LayoutInflater mInflater; @@ -21,6 +25,7 @@ public class ScanListAdapter extends BaseAdapter { private int mTxtId; public ScanListAdapter(Context context, int resource, int textViewResourceId) { + mCtx = context; mObjects = new ArrayList(); mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mRes = resource; @@ -51,10 +56,25 @@ public class ScanListAdapter extends BaseAdapter { } ((TextView) convertView.findViewById(mTxtId)).setText(device.getAddress() + " - " + device.getName()); + + // react when selecting iteam + convertView.setOnClickListener(new OnClickListener() { + private BluetoothDevice ret = device; + private ScannerActivity c = (ScannerActivity)mCtx; + + @Override + public void onClick(View v) { + Log.d(msg, "ScanListAdapter: onClick() will return " + ret.getName()); + c.returnScanResult(ret); + } + }); + return convertView; } public void add(BluetoothDevice device) { + + // FIXME: Export methods to allow moving this test to the caller if (device.getName() != null && mObjects.indexOf(device) < 0) { // avoid duplicates and ignore nameless devices mObjects.add(device); notifyDataSetChanged(); diff --git a/Defogger/src/no/mork/android/defogger/ScannerActivity.java b/Defogger/src/no/mork/android/defogger/ScannerActivity.java index 72a85d1..bd4e2a9 100644 --- a/Defogger/src/no/mork/android/defogger/ScannerActivity.java +++ b/Defogger/src/no/mork/android/defogger/ScannerActivity.java @@ -98,6 +98,14 @@ public class ScannerActivity extends Activity implements Runnable { public void run() { stopScan(); } + + public void returnScanResult(BluetoothDevice device) { + Log.d(msg, "returnScanResult()"); + Intent intent = new Intent(); + intent.putExtra("scan_ret", device.toString()); + setResult(RESULT_OK, intent); + finish(); + } private void startScan() { btScanner = BluetoothAdapter.getDefaultAdapter().getBluetoothLeScanner(); -- cgit v1.2.3