I am making an app on Android studio. My app makes a bluetooth connection to an RFDuino microcontroller board. I want to add in a notification that will notify the user when they lose bluetooth signal. I want the notification to use vibration and/or sound. I have tried several methods, but can't get them to compile. Most recently, I used TaskStackBuilder, but I keep getting errors. For some reason, it doesn't recognize some of the methods in TaskStackBuilder. Any ideas or simple notification code?
C:\Users\Trisha\AndroidStudioProjects\SmartB3\app\src\main\java\smartb\smartb3\MainActivity.java
Error:(44, 32) error: identifier expected
Error:(44, 48) error: identifier expected
Error:(46, 31) error: identifier expected
Error:(46, 44) error: identifier expected
Error:(48, 30) error: identifier expected
Error:(48, 50) error: identifier expected
package smartb.smartb3;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.smartb.smartb3.R;
public class MainActivity extends Activity implements BluetoothAdapter.LeScanCallback {
// State machine
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("SMARTB")
.setContentText("You left your USB!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
/* Adds the back stack for the Intent (but not the Intent itself) */
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
final private static int STATE_BLUETOOTH_OFF = 1;
final private static int STATE_DISCONNECTED = 2;
final private static int STATE_CONNECTING = 3;
final private static int STATE_CONNECTED = 4;
private int state;
private boolean scanStarted;
private boolean scanning;
private BluetoothAdapter bluetoothAdapter;
private BluetoothDevice bluetoothDevice;
private RFduinoService rfduinoService;
private Button enableBluetoothButton;
private TextView scanStatusText;
private Button scanButton;
private TextView deviceInfoText;
private TextView connectionStatusText;
private Button connectButton;
private EditData valueEdit;
private Button sendZeroButton;
private Button sendValueButton;
private Button clearButton;
private LinearLayout dataLayout;
final BluetoothAdapter.LeScanCallback callback;
private final BroadcastReceiver bluetoothStateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
if (state == BluetoothAdapter.STATE_ON) {
upgradeState(STATE_DISCONNECTED);
} else if (state == BluetoothAdapter.STATE_OFF) {
downgradeState(STATE_BLUETOOTH_OFF);
}
}
};
private final BroadcastReceiver scanModeReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
scanning = (bluetoothAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_NONE);
scanStarted &= scanning;
updateUi();
}
};
private final ServiceConnection rfduinoServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
rfduinoService = ((RFduinoService.LocalBinder) service).getService();
if (rfduinoService.initialize()) {
if (rfduinoService.connect(bluetoothDevice.getAddress())) {
upgradeState(STATE_CONNECTING);
}
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
rfduinoService = null;
downgradeState(STATE_DISCONNECTED);
// mId allows you to update the notification later on.
int mId = 0;
mNotificationManager.notify(mId, mBuilder.build());
}
};
private final BroadcastReceiver rfduinoReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (RFduinoService.ACTION_CONNECTED.equals(action)) {
upgradeState(STATE_CONNECTED);
} else if (RFduinoService.ACTION_DISCONNECTED.equals(action)) {
downgradeState(STATE_DISCONNECTED);
} else if (RFduinoService.ACTION_DATA_AVAILABLE.equals(action)) {
addData(intent.getByteArrayExtra(RFduinoService.EXTRA_DATA));
}
}
};
public MainActivity() {
callback = null;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// Bluetooth
enableBluetoothButton = (Button) findViewById(R.id.enableBluetooth);
enableBluetoothButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
enableBluetoothButton.setEnabled(false);
enableBluetoothButton.setText(
bluetoothAdapter.enable() ? "Enabling bluetooth..." : "Enable failed!");
}
});
// Find Device
scanStatusText = (TextView) findViewById(R.id.scanStatus);
scanButton = (Button) findViewById(R.id.scan);
scanButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
scanStarted = true;
bluetoothAdapter.startLeScan(callback);
}
});
// Device Info
deviceInfoText = (TextView) findViewById(R.id.deviceInfo);
// Connect Device
connectionStatusText = (TextView) findViewById(R.id.connectionStatus);
connectButton = (Button) findViewById(R.id.connect);
connectButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setEnabled(false);
connectionStatusText.setText("Connecting...");
Intent rfduinoIntent = new Intent(MainActivity.this, RFduinoService.class);
bindService(rfduinoIntent, rfduinoServiceConnection, BIND_AUTO_CREATE);
}
});
// Send
valueEdit = (EditData) findViewById(R.id.value);
valueEdit.setImeOptions();
valueEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEND) {
sendValueButton.callOnClick();
return true;
}
return false;
}
});
sendZeroButton = (Button) findViewById(R.id.sendZero);
sendZeroButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rfduinoService.send(new byte[]{0});
}
});
sendValueButton = (Button) findViewById(R.id.sendValue);
sendValueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rfduinoService.send(valueEdit.getData());
}
});
// Receive
clearButton = (Button) findViewById(R.id.clearData);
clearButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dataLayout.removeAllViews();
}
});
dataLayout = (LinearLayout) findViewById(R.id.dataLayout);
}
@Override
protected void onStart() {
super.onStart();
registerReceiver(scanModeReceiver, new IntentFilter(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED));
registerReceiver(bluetoothStateReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
registerReceiver(rfduinoReceiver, RFduinoService.getIntentFilter());
updateState(bluetoothAdapter.isEnabled() ? STATE_DISCONNECTED : STATE_BLUETOOTH_OFF);
}
@Override
protected void onStop() {
super.onStop();
bluetoothAdapter.stopLeScan(this);
unregisterReceiver(scanModeReceiver);
unregisterReceiver(bluetoothStateReceiver);
unregisterReceiver(rfduinoReceiver);
}
private void upgradeState(int newState) {
if (newState > state) {
updateState(newState);
}
}
private void downgradeState(int newState) {
if (newState < state) {
updateState(newState);
}
}
private void updateState(int newState) {
state = newState;
updateUi();
}
private void updateUi() {
// Enable Bluetooth
boolean on = state > STATE_BLUETOOTH_OFF;
enableBluetoothButton.setEnabled(!on);
enableBluetoothButton.setText(on ? "Bluetooth enabled" : "Enable Bluetooth");
scanButton.setEnabled(on);
// Scan
if (scanStarted && scanning) {
scanStatusText.setText("Scanning...");
scanButton.setText("Stop Scan");
scanButton.setEnabled(true);
} else if (scanStarted) {
scanStatusText.setText("Scan started...");
scanButton.setEnabled(false);
} else {
scanStatusText.setText("");
scanButton.setText("Scan");
scanButton.setEnabled(true);
}
// Connect
boolean connected = false;
String connectionText = "Disconnected";
if (state == STATE_CONNECTING) {
connectionText = "Connecting...";
} else if (state == STATE_CONNECTED) {
connected = true;
connectionText = "Connected";
}
connectionStatusText.setText(connectionText);
connectButton.setEnabled(bluetoothDevice != null && state == STATE_DISCONNECTED);
// Send
sendZeroButton.setEnabled(connected);
sendValueButton.setEnabled(connected);
}
private void addData(byte[] data) {
View view = getLayoutInflater().inflate(android.R.layout.simple_list_item_2, dataLayout, false);
TextView text1 = (TextView) view.findViewById(android.R.id.text1);
text1.setText(HexAsciiHelper.bytesToHex(data));
String ascii = HexAsciiHelper.bytesToAsciiMaybe(data);
if (ascii != null) {
TextView text2 = (TextView) view.findViewById(android.R.id.text2);
text2.setText(ascii);
}
dataLayout.addView(
view, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
}
@Override
public void onLeScan(BluetoothDevice device, final int rssi, final byte[] scanRecord) {
bluetoothAdapter.stopLeScan(this);
bluetoothDevice = device;
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
deviceInfoText.setText(
BluetoothHelper.getDeviceInfoText(bluetoothDevice, rssi, scanRecord));
updateUi();
}
});
}
}
I think you need to learn how your code works first.
You are writing code outside of a method. I suggest you learn the basis of Java and then start by learning Android.
Your problem in this case is that the following lines should be in a method like this:
public void sendNotification() {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("SMARTB")
.setContentText("You left your USB!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
/* Adds the back stack for the Intent (but not the Intent itself) */
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
}
And call this method where you need it.