private void updateView() { if (hrProvider == null) { scanButton.setEnabled(true); connectButton.setEnabled(false); connectButton.setText("Connect"); tvBTName.setText(""); tvHR.setText(""); return; } if (btName != null) { tvBTName.setText(btName); } else { tvBTName.setText(""); tvHR.setText(""); } if (hrProvider.isConnected()) { connectButton.setText("Disconnect"); connectButton.setEnabled(true); } else if (hrProvider.isConnecting()) { connectButton.setEnabled(false); connectButton.setText("Connecting"); } else { connectButton.setEnabled(btName == null ? false : true); connectButton.setText("Connect"); } }
private void close() { if (hrProvider != null) { log(hrProvider.getProviderName() + ".close()"); hrProvider.close(); hrProvider = null; } }
private void startScan() { log(hrProvider.getProviderName() + ".startScan()"); updateView(); deviceAdapter.deviceList.clear(); hrProvider.startScan(); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Scanning"); builder.setPositiveButton( "Connect", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { log(hrProvider.getProviderName() + ".stopScan()"); hrProvider.stopScan(); connect(); updateView(); dialog.dismiss(); } }); if (hrProvider.isBondingDevice()) { builder.setNeutralButton( "Pairing", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); Intent i = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS); startActivityForResult(i, 123); } }); } builder.setNegativeButton( "Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { log(hrProvider.getProviderName() + ".stopScan()"); hrProvider.stopScan(); load(); open(); dialog.dismiss(); updateView(); } }); builder.setSingleChoiceItems( deviceAdapter, -1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { HRDeviceRef hrDevice = deviceAdapter.deviceList.get(arg1); btAddress = hrDevice.getAddress(); btName = hrDevice.getName(); } }); builder.show(); }
private void open() { if (hrProvider != null && !hrProvider.isEnabled()) { if (hrProvider.startEnableIntent(this, 0) == true) { return; } hrProvider = null; } if (hrProvider != null) { log(hrProvider.getProviderName() + ".open(this)"); hrProvider.open(handler, this); } else { updateView(); } }
private void save() { Resources res = getResources(); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); Editor ed = prefs.edit(); ed.putString(res.getString(R.string.pref_bt_name), btName); ed.putString(res.getString(R.string.pref_bt_address), btAddress); ed.putString(res.getString(R.string.pref_bt_provider), hrProvider.getProviderName()); ed.commit(); }
@Override public void onConnectResult(boolean connectOK) { log(hrProvider.getProviderName() + "::onConnectResult(" + connectOK + ")"); if (connectOK) { save(); startTimer(); } else { } updateView(); }
@Override public void onOpenResult(boolean ok) { log(hrProvider.getProviderName() + "::onOpenResult(" + ok + ")"); if (mIsScanning) { mIsScanning = false; startScan(); return; } updateView(); }
@Override public void onScanResult(HRDeviceRef device) { log( hrProvider.getProviderName() + "::onScanResult(" + device.getAddress() + ", " + device.getName() + ")"); deviceAdapter.deviceList.add(device); deviceAdapter.notifyDataSetChanged(); }
protected void readHR() { if (hrProvider != null) { long age = hrProvider.getHRValueTimestamp(); int hrValue = hrProvider.getHRValue(); tvHR.setText(Integer.toString(hrValue)); if (age != lastTimestamp) { if (graphViewSeries == null) { timerStartTime = System.currentTimeMillis(); GraphViewData empty[] = {}; graphViewSeries = new GraphViewSeries(empty); graphView.addSeries(graphViewSeries); } graphViewListData.add(new GraphViewData((age - timerStartTime) / 1000, hrValue)); while (graphViewListData.size() > GRAPH_HISTORY_SECONDS) { graphViewListData.remove(0); } graphViewArrayData = graphViewListData.toArray(graphViewArrayData); graphViewSeries.resetData(graphViewArrayData); lastTimestamp = age; } } }
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == 0) { if (!hrProvider.isEnabled()) { log("Bluetooth not enabled!"); scanButton.setEnabled(false); connectButton.setEnabled(false); return; } load(); open(); return; } if (requestCode == 123) { startScan(); return; } }
void connect() { stopTimer(); if (hrProvider == null || btName == null || btAddress == null) { updateView(); return; } if (hrProvider.isConnecting() || hrProvider.isConnected()) { log(hrProvider.getProviderName() + ".disconnect()"); hrProvider.disconnect(); updateView(); return; } tvBTName.setText(getName()); tvHR.setText("?"); String name = btName; if (name == null || name.length() == 0) { name = btAddress; } log(hrProvider.getProviderName() + ".connect(" + name + ")"); hrProvider.connect(HRDeviceRef.create(btProviderName, btName, btAddress)); updateView(); }
@Override public void onCloseResult(boolean closeOK) { log(hrProvider.getProviderName() + "::onCloseResult(" + closeOK + ")"); }
@Override public void onDisconnectResult(boolean disconnectOK) { log(hrProvider.getProviderName() + "::onDisconnectResult(" + disconnectOK + ")"); }