@Override public boolean onCreateOptionsMenu(final Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); // get a reference to the bluetooth action button bluetoothMenutItem = menu.findItem(R.id.action_bluetooth); // and put the right view on it bluetoothMenutItem.setActionView(R.layout.animated_menu_item); // set the correct initial state setBluetoothState(BLUETOOTH_DISCONNECTED); // get access to the image view ImageView imageView = (ImageView) bluetoothMenutItem.getActionView().findViewById(R.id.animated_menu_item_action); // define an action imageView.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { (new Thread( new Runnable() { @Override public void run() { toast("Reconnecting Bluetooth ..."); stopBluetooth(); reloadBluetooth(); } })) .start(); } }); return true; }
@Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) { // Device has disconnected // only resume if this activity is also visible if (visible) { // stop reading if (device != null) { device.stopAndJoin(); } // inform user setTitle(TAG + " - disconnected"); setBluetoothState(BLUETOOTH_DISCONNECTED); Toast.makeText( MainActivity.this.getBaseContext(), "Bluetooth connection lost!", Toast.LENGTH_LONG) .show(); // try to reconnect onResume(); } } }
@Override protected void onCreate(Bundle savedInstanceState) { // always create an instance // dataLogger = DataLogger.getInstance(); dataLogger = new DataLogger(); debug("MainActivity: onCreate"); instance = this; getWindow().requestFeature(Window.FEATURE_ACTION_BAR); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // navigation bar appSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager()); actionBar = getSupportActionBar(); actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE); // actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); viewPager = (ViewPager) findViewById(R.id.main); viewPager.setAdapter(appSectionsPagerAdapter); viewPager.setOnPageChangeListener( new ViewPager.SimpleOnPageChangeListener() { @Override public void onPageSelected(int position) { // actionBar.setSelectedNavigationItem(position); updateActionBar(); } }); updateActionBar(); /* for (int i = 0; i < appSectionsPagerAdapter.getCount(); i++) { actionBar.addTab( actionBar.newTab() .setText(appSectionsPagerAdapter.getPageTitle(i)) .setTabListener(MainActivity.this)); } */ // load the initial "main" fragment // loadFragement(new MainFragment()); setTitle(TAG + " - not connected"); setBluetoothState(BLUETOOTH_DISCONNECTED); // tabs // final ActionBar actionBar = getSupportActionBar(); // Specify that tabs should be displayed in the action bar. // open the database CanzeDataSource.getInstance(getBaseContext()).open(); // cleanup CanzeDataSource.getInstance().cleanUp(); // setup cleaning (once every hour) Runnable cleanUpRunnable = new Runnable() { @Override public void run() { CanzeDataSource.getInstance().cleanUp(); } }; Handler handler = new Handler(); handler.postDelayed(cleanUpRunnable, 60 * 1000); // register for bluetooth changes IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED); this.registerReceiver(broadcastReceiver, intentFilter); // configure Bluetooth manager BluetoothManager.getInstance() .setBluetoothEvent( new BluetoothEvent() { @Override public void onBeforeConnect() { setBluetoothState(BLUETOOTH_SEARCH); } @Override public void onAfterConnect(BluetoothSocket bluetoothSocket) { device.init(visible); device.registerFilters(); // set title debug("MainActivity: onAfterConnect > set title"); runOnUiThread( new Runnable() { @Override public void run() { setTitle( TAG + " - connected to <" + bluetoothDeviceName + "@" + bluetoothDeviceAddress + ">"); setBluetoothState(BLUETOOTH_CONNECTED); } }); } @Override public void onBeforeDisconnect(BluetoothSocket bluetoothSocket) {} @Override public void onAfterDisconnect() { runOnUiThread( new Runnable() { @Override public void run() { setTitle(TAG + " - disconnected"); } }); } }); // detect hardware status int BT_STATE = BluetoothManager.getInstance().getHardwareState(); if (BT_STATE == BluetoothManager.STATE_BLUETOOTH_NOT_AVAILABLE) Toast.makeText( this.getBaseContext(), "Sorry, but your device doesn't seem to have Bluetooth support!", Toast.LENGTH_LONG) .show(); else if (BT_STATE == BluetoothManager.STATE_BLUETOOTH_NOT_ACTIVE) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, 1); } // load settings // - includes the reader // - includes the decoder // loadSettings(); --> done in onResume // load fields from static code debug("Loaded fields: " + fields.size()); // load fields // final SharedPreferences settings = getSharedPreferences(PREFERENCES_FILE, 0); (new Thread( new Runnable() { @Override public void run() { debug("Loading fields last field values from database"); for (int i = 0; i < fields.size(); i++) { Field field = fields.get(i); field.setCalculatedValue(CanzeDataSource.getInstance().getLast(field.getSID())); // debug("MainActivity: Setting "+field.getSID()+" = "+field.getValue()); // f.setValue(settings.getFloat(f.getUniqueID(), 0)); } debug("Loading fields last field values from database (done)"); } })) .start(); }