summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBjørn Mork <bjorn@mork.no>2019-06-02 15:34:24 +0200
committerBjørn Mork <bjorn@mork.no>2019-06-02 15:34:24 +0200
commit4ecb36e982deeb68c55a36a3b81fb419462139a8 (patch)
treee8d7401df32daf5dbddb10a12a10d01a5b053dc7
parent74a5814dff40adc22e8a11dcacd9170a3cbb5361 (diff)
simplify scan adapater to arrayadapter
Signed-off-by: Bjørn Mork <bjorn@mork.no>
-rw-r--r--Defogger/src/no/mork/android/defogger/IpCamActivity.java38
-rw-r--r--Defogger/src/no/mork/android/defogger/MainActivity.java1
-rw-r--r--Defogger/src/no/mork/android/defogger/ScannerActivity.java63
3 files changed, 72 insertions, 30 deletions
diff --git a/Defogger/src/no/mork/android/defogger/IpCamActivity.java b/Defogger/src/no/mork/android/defogger/IpCamActivity.java
index b99cf29..60ff2c6 100644
--- a/Defogger/src/no/mork/android/defogger/IpCamActivity.java
+++ b/Defogger/src/no/mork/android/defogger/IpCamActivity.java
@@ -32,6 +32,7 @@ import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
+import android.widget.Toast;
import java.lang.StringBuilder;
import java.nio.charset.StandardCharsets;
@@ -49,6 +50,7 @@ public class IpCamActivity extends Activity {
private BluetoothGatt mGatt;
private BluetoothGattService ipcamService;
+ private BluetoothDevice device;
private String pincode;
private ArrayDeque<BluetoothGattCharacteristic> readQ;
private ArrayDeque<BluetoothGattCharacteristic> writeQ;
@@ -66,12 +68,10 @@ public class IpCamActivity extends Activity {
// Get the Intent that started this activity and extract parameters
Intent intent = getIntent();
pincode = intent.getStringExtra("pincode");
- BluetoothDevice dev = intent.getExtras().getParcelable("btdevice");
- if (pincode == null || dev == null)
+ device = intent.getExtras().getParcelable("btdevice");
+ if (pincode == null || device == null)
finish();
- connectDevice(dev);
-
EditText cmd = (EditText) findViewById(R.id.command);
cmd.setOnEditorActionListener(new OnEditorActionListener() {
@Override
@@ -88,6 +88,7 @@ public class IpCamActivity extends Activity {
@Override
protected void onResume() {
super.onResume();
+ connectDevice(device);
}
// utilities
@@ -131,7 +132,7 @@ public class IpCamActivity extends Activity {
if (!gatt.discoverServices())
setStatus("Falied to start service discovery");
} else {
- disconnectDevice();
+ disconnectDevice("Connection to " + gatt.getDevice().getName() + " failed");
}
}
@@ -139,16 +140,14 @@ public class IpCamActivity extends Activity {
Log.d(msg, "onServicesDiscovered()");
if (status != BluetoothGatt.GATT_SUCCESS) {
- setStatus("Failed to discover services");
- disconnectDevice();
+ disconnectDevice("Failed to discover services on " + gatt.getDevice().getName());
return;
}
// get the IPCam service
// ipcamService = gatt.getService(UUID.fromString("0000d001-0000-1000-8000-00805f9b34fb"));
ipcamService = gatt.getService(UUIDfromInt(0xd001));
if (ipcamService == null) {
- setStatus(gatt.getDevice().getName() + " does not support the IPCam GATT service");
- disconnectDevice();
+ disconnectDevice(gatt.getDevice().getName() + " does not support the IPCam GATT service");
return;
}
@@ -297,11 +296,11 @@ public class IpCamActivity extends Activity {
}
private class NetAdapter extends ArrayAdapter<String> {
- private int res;
+ private int resource;
public NetAdapter(Context context, int resource, String[] networks) {
super(context, resource, networks);
- res = resource;
+ this.resource = resource;
}
@Override
@@ -312,7 +311,7 @@ public class IpCamActivity extends Activity {
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
- convertView = LayoutInflater.from(getContext()).inflate(res, parent, false);
+ convertView = LayoutInflater.from(getContext()).inflate(resource, parent, false);
}
// Lookup view for data population
@@ -364,16 +363,21 @@ public class IpCamActivity extends Activity {
mGatt = device.connectGatt(this, true, gattClientCallback);
}
- private void disconnectDevice() {
+ private void disconnectDevice(String reason) {
+ // we're finishing, so we either havt to return the reason to the parent or flash it like this
+ runOnUiThread(new Runnable() {
+ public void run() {
+ Toast.makeText(getApplicationContext(), reason, Toast.LENGTH_LONG).show();
+ }});
+
// reset status to default
connected = false;
locked = true;
wifilink = false;
- if (mGatt == null)
- return;
- Log.d(msg, "disconnectDevice() " + mGatt.getDevice().getAddress());
- mGatt.close();
+ if (mGatt != null)
+ mGatt.close();
+ Log.d(msg, "disconnectDevice() " + device.getAddress() + " with reason: " + reason);
finish();
}
diff --git a/Defogger/src/no/mork/android/defogger/MainActivity.java b/Defogger/src/no/mork/android/defogger/MainActivity.java
index c9f037c..34cc562 100644
--- a/Defogger/src/no/mork/android/defogger/MainActivity.java
+++ b/Defogger/src/no/mork/android/defogger/MainActivity.java
@@ -3,7 +3,6 @@
* Copyright (c) 2019 Bjørn Mork <bjorn@mork.no>
*/
-
package no.mork.android.defogger;
import android.app.Activity;
diff --git a/Defogger/src/no/mork/android/defogger/ScannerActivity.java b/Defogger/src/no/mork/android/defogger/ScannerActivity.java
index b669e0b..af2871c 100644
--- a/Defogger/src/no/mork/android/defogger/ScannerActivity.java
+++ b/Defogger/src/no/mork/android/defogger/ScannerActivity.java
@@ -18,9 +18,14 @@ import android.os.Bundle;
import android.os.Handler;
import android.os.ParcelUuid;
import android.util.Log;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.View.OnClickListener;
+import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
+import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
@@ -33,13 +38,13 @@ public class ScannerActivity extends Activity implements Runnable {
private BluetoothLeScanner btScanner;
private boolean mScanning;
private ScanCallback leScanCallback;
- private ScanListAdapter scanlistAdapter;
+ private ScanAdapter scanlistAdapter;
private class BtleScanCallback extends ScanCallback {
- private ScanListAdapter mScanResults;
+ private ScanAdapter mScanResults;
- BtleScanCallback(ScanListAdapter scanResults) {
+ BtleScanCallback(ScanAdapter scanResults) {
mScanResults = scanResults;
}
@@ -64,34 +69,68 @@ public class ScannerActivity extends Activity implements Runnable {
private void addScanResult(ScanResult result) {
BluetoothDevice device = result.getDevice();
+
+ /* filter result manually, since the filter API is dysfunctional */
+ if (device.getName() == null || mScanResults.getPosition(device) >=0) // avoid duplicates and ignore nameless devices
+ return;
+
+ /* FIXME: further filtering on camera service */
mScanResults.add(device);
}
};
+ private class ScanAdapter extends ArrayAdapter<BluetoothDevice> {
+ private ScannerActivity ctx;
+ private int resource;
+
+ public ScanAdapter(Context context, int resource) {
+ super(context, resource);
+ ctx = (ScannerActivity)context;
+ this.resource = resource;
+ }
+
+ @Override
+ public View getView(int position, View convertView, ViewGroup parent) {
+ BluetoothDevice device = getItem(position);
+
+ if (convertView == null) {
+ convertView = LayoutInflater.from(ctx).inflate(resource, parent, false);
+ }
+
+ TextView txt = (TextView) convertView.findViewById(R.id.scanitem);
+ txt.setText(device.getAddress() + " - " + device.getName());
+
+ // react when selecting iteam
+ convertView.setOnClickListener(new OnClickListener() {
+ private BluetoothDevice ret = device;
+
+ @Override
+ public void onClick(View v) {
+ Log.d(msg, "ScanListAdapter: onClick() will return " + ret.getName());
+ ctx.returnScanResult(ret);
+ }
+ });
+
+ return convertView;
+ }
+ }
+
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scanner);
ListView listView = (ListView) findViewById(R.id.scanlist_view);
- scanlistAdapter = new ScanListAdapter(this, R.layout.item_scan, R.id.scanitem);
+ scanlistAdapter = new ScanAdapter(this, R.layout.item_scan);
listView.setAdapter(scanlistAdapter);
leScanCallback = new BtleScanCallback(scanlistAdapter);
-
- CharSequence text = "Hello toast!";
-
- Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
- Intent intent = new Intent();
- intent.putExtra("scan_ret", "This data is returned when scan activity is finished.");
- setResult(RESULT_OK, intent);
}
@Override
protected void onResume() {
super.onResume();
startScan();
- //finish();
}
@Override