Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mt.ant;
- import android.app.Activity;
- import android.bluetooth.BluetoothAdapter;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.support.v7.app.ActionBarActivity;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import android.widget.Toast;
- import mt.ant.AntPlus.AsyncHR;
- import mt.ant.BLE.BleHr;
- public class AddDevice extends ActionBarActivity {
- String bleId;
- String antId;
- boolean antScanHr = false;
- private TextView titleAnt;
- private TextView titleBle;
- private TextView titleGrs;
- private TextView idViewAnt;
- private TextView idViewBle;
- private TextView idViewGrs;
- Button bleBtn;
- Button antBtn;
- Button grsBtn;
- AsyncActivity asyncActivity;
- AsyncHR antHr;
- BleHr bleHr;
- public SharedPreferences sp;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_add_device);
- idViewAnt = (TextView) findViewById(R.id.deviceidAnt);
- titleAnt = (TextView) findViewById(R.id.titleAnt);
- idViewBle = (TextView) findViewById(R.id.deviceidBle);
- titleBle = (TextView) findViewById(R.id.titleBle);
- titleGrs = (TextView) findViewById(R.id.titleGrs);
- idViewGrs = (TextView) findViewById(R.id.deviceidGrs);
- bleBtn = (Button) findViewById(R.id.btnBle);
- antBtn = (Button) findViewById(R.id.btnAnt);
- grsBtn = (Button) findViewById(R.id.btnGrs);
- bleBtn.setOnClickListener( new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- activateBluetooth();
- bleHr.scanLeDevice(true, true);
- }
- });
- asyncActivity = new AsyncActivity();
- asyncActivity.doBindChannelService(this);
- antBtn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- antHr.requestAccessToPcc();
- //asyncActivity.addNewChannel(false);
- }
- });
- grsBtn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String id = ""+asyncActivity.newChannelInfo.broadcastData[0]+
- asyncActivity.newChannelInfo.broadcastData[1];
- Log.v("GrsID", id);
- setViewGrs(id);
- }
- });
- antHr = new AsyncHR(this, 0);
- bleHr = new BleHr(this, 0);
- antScanHr = true;
- sp = getSharedPreferences("values", Activity.MODE_PRIVATE);
- bleId = getDeviceID("bleHrID");
- antId = getDeviceID("antHrID");
- if(bleId != null){
- setViewBle(bleId);
- }
- if(antId != null){
- setViewAnt(antId);
- }
- }
- /**
- * Ask the user to enabling bluetooth
- */
- private void activateBluetooth(){
- BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
- if (!mBluetoothAdapter.isEnabled()){
- Intent intentBtEnabled = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
- int REQUEST_ENABLE_BT = 1;
- startActivityForResult(intentBtEnabled, REQUEST_ENABLE_BT);
- }
- }
- public void setViewGrs(final String id){
- lagreDeviceId("grsID", id);
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- titleGrs.setText("Globeracer sensor ID");
- idViewGrs.setText(id);
- Toast.makeText(getBaseContext(), "Lagret enhet: " + id, Toast.LENGTH_SHORT).show();
- }
- });
- }
- public void setViewAnt(final String id){
- lagreDeviceId("antHrID", id+"");
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- titleAnt.setText("Heartrate monitor ANT+");
- idViewAnt.setText(id);
- Toast.makeText(getBaseContext(), "Lagret enhet: " + id, Toast.LENGTH_SHORT).show();
- }
- });
- }
- public void setViewBle(final String id){
- lagreDeviceId("bleHrID", id);
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- titleBle.setText("Heartrate monitor BLE: ");
- idViewBle.setText(id);
- Toast.makeText(getBaseContext(), "Lagret enhet: " + id, Toast.LENGTH_SHORT).show();
- }
- });
- }
- public void lagreDeviceId(String key, String id){
- Log.v("Status", "Lagrer deviceID");
- SharedPreferences.Editor editor = sp.edit();
- editor.putString(key, id);
- editor.commit();
- }
- public String getDeviceID(String key){
- String id = sp.getString(key, null);
- return id;
- }
- @Override
- public void onDestroy(){
- super.onDestroy();
- antHr.close();
- asyncActivity.doUnbindChannelService();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement