From cb438934b6c7a67a598f580c0d7141986dd0c860 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Sat, 21 Oct 2017 13:50:19 +0200 Subject: [PATCH] WIP: Pairing --- app/src/main/AndroidManifest.xml | 6 + .../PumpCombo/activities/PairingActivity.java | 232 ++++++++++++++++++ .../res/layout/combo_pairing_activity.xml | 22 ++ app/src/main/res/xml/pref_combo.xml | 4 +- 4 files changed, 262 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/activities/PairingActivity.java create mode 100644 app/src/main/res/layout/combo_pairing_activity.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 8420b26768..3cdb12f6f9 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -58,6 +58,12 @@ + + + + + + mDevices = new ArrayList<>(); + ; + + private BluetoothAdapter mBluetoothAdapter = null; + private BluetoothLeScanner mBluetoothLeScanner = null; + + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.danars_blescanner_activity); + + mListAdapter = new PairingActivity.ListAdapter(); + + listView = (ListView) findViewById(R.id.danars_blescanner_listview); + listView.setEmptyView(findViewById(R.id.danars_blescanner_nodevice)); + listView.setAdapter(mListAdapter); + + initView(); + } + + private void initView() { + mContext = getApplicationContext(); + + BluetoothManager bluetoothManager = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE); + mBluetoothAdapter = bluetoothManager.getAdapter(); + mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner(); + + // MIKE: test mBluetoothLeScanner for null (bt disabled) + + mListAdapter.notifyDataSetChanged(); + + } + + @Override + protected void onResume() { + super.onResume(); + + startScan(); + } + + @Override + protected void onPause() { + super.onPause(); + + stopScan(); + } + + private void startScan() { + mBluetoothLeScanner.startScan(mBleScanCallback); + } + + private void stopScan() { + mBluetoothLeScanner.stopScan(mBleScanCallback); + } + + private void addBleDevice(BluetoothDevice device) { + if (device == null || device.getName() == null || device.getName().equals("")) { + return; + } + PairingActivity.BluetoothDeviceItem item = new PairingActivity.BluetoothDeviceItem(device); + if (!isSNCheck(device.getName()) || mDevices.contains(item)) { + return; + } + + mDevices.add(item); + new Handler().post(new Runnable() { + public void run() { + mListAdapter.notifyDataSetChanged(); + } + }); + } + + private ScanCallback mBleScanCallback = new ScanCallback() { + @Override + public void onScanResult(int callbackType, ScanResult result) { + addBleDevice(result.getDevice()); + } + }; + + class ListAdapter extends BaseAdapter { + + @Override + public int getCount() { + return mDevices.size(); + } + + @Override + public PairingActivity.BluetoothDeviceItem getItem(int i) { + return mDevices.get(i); + } + + @Override + public long getItemId(int i) { + return 0; + } + + @Override + public View getView(int i, View convertView, ViewGroup parent) { + View v = convertView; + PairingActivity.ListAdapter.ViewHolder holder; + + if (v == null) { + v = View.inflate(mContext, R.layout.danars_blescanner_item, null); + holder = new PairingActivity.ListAdapter.ViewHolder(v); + v.setTag(holder); + } else { + holder = (PairingActivity.ListAdapter.ViewHolder) v.getTag(); + } + + PairingActivity.BluetoothDeviceItem item = getItem(i); + holder.setData(i, item); + return v; + } + + private class ViewHolder implements View.OnClickListener { + private PairingActivity.BluetoothDeviceItem item = null; + + private TextView mName = null; + private TextView mAddress = null; + + public ViewHolder(View v) { + mName = (TextView) v.findViewById(R.id.ble_name); + mAddress = (TextView) v.findViewById(R.id.ble_address); + + v.setOnClickListener(PairingActivity.ListAdapter.ViewHolder.this); + } + + @Override + public void onClick(View v) { + SP.putString(R.string.key_danars_address, item.device.getAddress()); + SP.putString(R.string.key_danars_name, mName.getText().toString()); + MainApp.bus().post(new EventDanaRSDeviceChange()); + finish(); + } + + public void setData(int pos, PairingActivity.BluetoothDeviceItem data) { + if (data != null) { + try { + String tTitle = data.device.getName(); + if (tTitle == null || tTitle.equals("")) { + tTitle = "(unknown)"; + } else if (tTitle.length() > 10) { + tTitle = tTitle.substring(0, 10); + } + mName.setText(tTitle); + + mAddress.setText(data.device.getAddress()); + + item = data; + } catch (Exception e) { + } + } + } + } + } + + // + private class BluetoothDeviceItem { + private BluetoothDevice device; + + public BluetoothDeviceItem(BluetoothDevice device) { + super(); + this.device = device; + } + + @Override + public boolean equals(Object o) { + if (device == null || o == null || !(o instanceof PairingActivity.BluetoothDeviceItem)) { + return false; + } + PairingActivity.BluetoothDeviceItem checkItem = (PairingActivity.BluetoothDeviceItem) o; + if (checkItem.device == null) { + return false; + } + return stringEquals(device.getAddress(), checkItem.device.getAddress()); + } + + public boolean stringEquals(String arg1, String arg2) { + try { + return arg1.equals(arg2); + } catch (Exception e) { + return false; + } + } + } + + public static boolean isSNCheck(String sn) { + String regex = "^([a-zA-Z]{3})([0-9]{5})([a-zA-Z]{2})$"; + Pattern p = Pattern.compile(regex); + Matcher m = p.matcher(sn); + + return m.matches(); + } +} diff --git a/app/src/main/res/layout/combo_pairing_activity.xml b/app/src/main/res/layout/combo_pairing_activity.xml new file mode 100644 index 0000000000..b7e55133b1 --- /dev/null +++ b/app/src/main/res/layout/combo_pairing_activity.xml @@ -0,0 +1,22 @@ + + + + + + + + diff --git a/app/src/main/res/xml/pref_combo.xml b/app/src/main/res/xml/pref_combo.xml index 287082bfac..c53fe01812 100644 --- a/app/src/main/res/xml/pref_combo.xml +++ b/app/src/main/res/xml/pref_combo.xml @@ -5,9 +5,9 @@ android:title="@string/combopump_settings"> - +