@Override
  protected void onResume() {
    super.onResume();

    // Open a Bluetooth serial port and get ready to establish a connection
    if (bluetoothSerial.checkBluetooth() && bluetoothSerial.isBluetoothEnabled()) {
      if (!bluetoothSerial.isConnected()) {
        bluetoothSerial.start();
      }
    }
  }
  @Override
  protected void onStop() {
    super.onStop();

    // Disconnect from the remote device and close the serial port
    bluetoothSerial.stop();
  }
  @Override
  protected void onStart() {
    super.onStart();

    // Check Bluetooth availability on the device and set up the Bluetooth adapter
    bluetoothSerial.setup();
  }
 @Override
 public void onBluetoothSerialRead(String message) {
   // Print the incoming message on the terminal screen
   tvTerminal.append(
       getString(
           R.string.terminal_message_template, bluetoothSerial.getConnectedDeviceName(), message));
   svTerminal.post(scrollTerminalToBottom);
 }
 @Override
 public void onBluetoothSerialWrite(String message) {
   // Print the outgoing message on the terminal screen
   tvTerminal.append(
       getString(
           R.string.terminal_message_template, bluetoothSerial.getLocalAdapterName(), message));
   svTerminal.post(scrollTerminalToBottom);
 }
 private void showDeviceListDialog() {
   // Display dialog for selecting a remote Bluetooth device
   BluetoothDeviceListDialog dialog = new BluetoothDeviceListDialog(this);
   dialog.setOnDeviceSelectedListener(this);
   dialog.setTitle(R.string.paired_devices);
   dialog.setDevices(bluetoothSerial.getPairedDevices());
   dialog.showAddress(true);
   dialog.useDarkTheme(true);
   dialog.show();
 }
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
      case REQUEST_ENABLE_BLUETOOTH:
        // Set up Bluetooth serial port when Bluetooth adapter is turned on
        if (resultCode == Activity.RESULT_OK) {
          bluetoothSerial.setup();
        }
        break;
    }
  }
  @Override
  public void invalidateOptionsMenu() {
    if (bluetoothSerial == null) return;

    // Show or hide the "Connect" and "Disconnect" buttons on the app bar
    if (bluetoothSerial.isConnected()) {
      if (actionConnect != null) actionConnect.setVisible(false);
      if (actionDisconnect != null) actionDisconnect.setVisible(true);
    } else {
      if (actionConnect != null) actionConnect.setVisible(true);
      if (actionDisconnect != null) actionDisconnect.setVisible(false);
    }
  }
  private void updateBluetoothState() {
    // Get the current Bluetooth state
    final int state;
    if (bluetoothSerial != null) state = bluetoothSerial.getState();
    else state = BluetoothSerial.STATE_DISCONNECTED;

    // Display the current state on the app bar as the subtitle
    String subtitle;
    switch (state) {
      case BluetoothSerial.STATE_CONNECTING:
        subtitle = getString(R.string.status_connecting);
        break;
      case BluetoothSerial.STATE_CONNECTED:
        subtitle = getString(R.string.status_connected, bluetoothSerial.getConnectedDeviceName());
        break;
      default:
        subtitle = getString(R.string.status_disconnected);
        break;
    }

    if (getSupportActionBar() != null) {
      getSupportActionBar().setSubtitle(subtitle);
    }
  }
Esempio n. 10
0
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_connect) {
      showDeviceListDialog();
      return true;
    } else if (id == R.id.action_disconnect) {
      bluetoothSerial.stop();
      return true;
    } else if (id == R.id.action_crlf) {
      crlf = !item.isChecked();
      item.setChecked(crlf);
      return true;
    }

    return super.onOptionsItemSelected(item);
  }
Esempio n. 11
0
 @Override
 public void onBluetoothDeviceSelected(BluetoothDevice device) {
   // Connect to the selected remote Bluetooth device
   bluetoothSerial.connect(device);
 }