예제 #1
0
  @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();
      }
    }
  }
예제 #2
0
  @Override
  protected void onStop() {
    super.onStop();

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

    // Check Bluetooth availability on the device and set up the Bluetooth adapter
    bluetoothSerial.setup();
  }
예제 #4
0
 @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);
 }
예제 #5
0
 @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);
 }
예제 #6
0
 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();
 }
예제 #7
0
  @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;
    }
  }
예제 #8
0
  @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);
    }
  }
예제 #9
0
  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);
    }
  }
예제 #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);
  }
예제 #11
0
 @Override
 public void onBluetoothDeviceSelected(BluetoothDevice device) {
   // Connect to the selected remote Bluetooth device
   bluetoothSerial.connect(device);
 }