* This is the main Activity that displays the current chat session.
*/
public class AltosDroid extends Activity {
- // Debugging
- private static final String TAG = "AltosDroid";
- private static final boolean D = true;
+ // Debugging
+ private static final String TAG = "AltosDroid";
+ private static final boolean D = true;
// Message types sent from the BluetoothChatService Handler
public static final int MESSAGE_STATE_CHANGE = 1;
public static final String DEVICE_NAME = "device_name";
public static final String TOAST = "toast";
- // Intent request codes
- private static final int REQUEST_CONNECT_DEVICE = 1;
- private static final int REQUEST_ENABLE_BT = 2;
- // Layout Views
- private TextView mTitle;
- private TextView mSerialView;
private EditText mOutEditText;
private Button mSendButton;
- // Name of the connected device
- private String mConnectedDeviceName = null;
// String buffer for outgoing messages
private StringBuffer mOutStringBuffer;
- // Local Bluetooth adapter
- private BluetoothAdapter mBluetoothAdapter = null;
// Member object for the chat services
private BluetoothChatService mChatService = null;
-
+ // Intent request codes
+ private static final int REQUEST_CONNECT_DEVICE = 1;
+ private static final int REQUEST_ENABLE_BT = 2;
+
+ // Layout Views
+ private TextView mTitle;
+ private TextView mSerialView;
+ // Name of the connected device
+ private String mConnectedDeviceName = null;
+ // Local Bluetooth adapter
+ private BluetoothAdapter mBluetoothAdapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- if(D) Log.e(TAG, "+++ ON CREATE +++");
-
- // Set up the window layout
- requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
- setContentView(R.layout.main);
- getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
+ super.onCreate(savedInstanceState);
+ if(D) Log.e(TAG, "+++ ON CREATE +++");
- // Set up the custom title
- mTitle = (TextView) findViewById(R.id.title_left_text);
- mTitle.setText(R.string.app_name);
- mTitle = (TextView) findViewById(R.id.title_right_text);
+ // Set up the window layout
+ requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
+ setContentView(R.layout.main);
+ getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
// Get local Bluetooth adapter
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
+ // Set up the custom title
+ mTitle = (TextView) findViewById(R.id.title_left_text);
+ mTitle.setText(R.string.app_name);
+ mTitle = (TextView) findViewById(R.id.title_right_text);
// If the adapter is null, then Bluetooth is not supported
- if (mBluetoothAdapter == null) {
- Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
- finish();
- return;
- }
+ if (mBluetoothAdapter == null) {
+ Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
+ finish();
+ return;
+ }
}
@Override
public void onStart() {
- super.onStart();
- if(D) Log.e(TAG, "++ ON START ++");
-
- // If BT is not on, request that it be enabled.
- // setupChat() will then be called during onActivityResult
- if (!mBluetoothAdapter.isEnabled()) {
- Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
- startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
- // Otherwise, setup the chat session
- } else {
+ super.onStart();
+ if(D) Log.e(TAG, "++ ON START ++");
+
+ if (!mBluetoothAdapter.isEnabled()) {
+ Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
+ startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
+ } else {
if (mChatService == null) setupChat();
- }
+ }
}
@Override
public synchronized void onResume() {
- super.onResume();
- if(D) Log.e(TAG, "+ ON RESUME +");
+ super.onResume();
+ if(D) Log.e(TAG, "+ ON RESUME +");
- // Performing this check in onResume() covers the case in which BT was
- // not enabled during onStart(), so we were paused to enable it...
- // onResume() will be called when ACTION_REQUEST_ENABLE activity returns.
+ // Performing this check in onResume() covers the case in which BT was
+ // not enabled during onStart(), so we were paused to enable it...
+ // onResume() will be called when ACTION_REQUEST_ENABLE activity returns.
if (mChatService != null) {
// Only if the state is STATE_NONE, do we know that we haven't started already
if (mChatService.getState() == BluetoothChatService.STATE_NONE) {
@Override
public synchronized void onPause() {
- super.onPause();
- if(D) Log.e(TAG, "- ON PAUSE -");
+ super.onPause();
+ if(D) Log.e(TAG, "- ON PAUSE -");
}
@Override
public void onStop() {
- super.onStop();
- if(D) Log.e(TAG, "-- ON STOP --");
+ super.onStop();
+ if(D) Log.e(TAG, "-- ON STOP --");
}
@Override
public void onDestroy() {
- super.onDestroy();
+ super.onDestroy();
// Stop the Bluetooth chat services
if (mChatService != null) mChatService.stop();
- if(D) Log.e(TAG, "--- ON DESTROY ---");
+ if(D) Log.e(TAG, "--- ON DESTROY ---");
}
private void setupChat() {
- Log.d(TAG, "setupChat()");
+ Log.d(TAG, "setupChat()");
mSerialView = (TextView) findViewById(R.id.in);
mSerialView.setMovementMethod(new ScrollingMovementMethod());
mSerialView.setClickable(false);
mSerialView.setLongClickable(false);
- // Initialize the compose field with a listener for the return key
- mOutEditText = (EditText) findViewById(R.id.edit_text_out);
- mOutEditText.setOnEditorActionListener(mWriteListener);
+ // Initialize the compose field with a listener for the return key
+ mOutEditText = (EditText) findViewById(R.id.edit_text_out);
+ mOutEditText.setOnEditorActionListener(mWriteListener);
- // Initialize the send button with a listener that for click events
- mSendButton = (Button) findViewById(R.id.button_send);
- mSendButton.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
+ // Initialize the send button with a listener that for click events
+ mSendButton = (Button) findViewById(R.id.button_send);
+ mSendButton.setOnClickListener(new OnClickListener() {
+ public void onClick(View v) {
// Send a message using content of the edit text widget
- TextView view = (TextView) findViewById(R.id.edit_text_out);
- String message = view.getText().toString();
+ TextView view = (TextView) findViewById(R.id.edit_text_out);
+ String message = view.getText().toString();
sendMessage(message);
- }
- });
+ }
+ });
- // Initialize the BluetoothChatService to perform bluetooth connections
- mChatService = new BluetoothChatService(this, mHandler);
+ // Initialize the BluetoothChatService to perform bluetooth connections
+ mChatService = new BluetoothChatService(this, mHandler);
- // Initialize the buffer for outgoing messages
- mOutStringBuffer = new StringBuffer("");
+ // Initialize the buffer for outgoing messages
+ mOutStringBuffer = new StringBuffer("");
}
/**
- * Sends a message.
- * @param message A string of text to send.
- */
+ * Sends a message.
+ * @param message A string of text to send.
+ */
+ /*
private void sendMessage(String message) {
- // Check that we're actually connected before trying anything
- if (mChatService.getState() != BluetoothChatService.STATE_CONNECTED) {
- Toast.makeText(this, R.string.not_connected, Toast.LENGTH_SHORT).show();
- return;
- }
+ // Check that we're actually connected before trying anything
+ if (mChatService.getState() != BluetoothChatService.STATE_CONNECTED) {
+ Toast.makeText(this, R.string.not_connected, Toast.LENGTH_SHORT).show();
+ return;
+ }
- // Check that there's actually something to send
- if (message.length() > 0) {
- // Get the message bytes and tell the BluetoothChatService to write
- byte[] send = message.getBytes();
- mChatService.write(send);
+ // Check that there's actually something to send
+ if (message.length() > 0) {
+ // Get the message bytes and tell the BluetoothChatService to write
+ byte[] send = message.getBytes();
+ mChatService.write(send);
+
+ // Reset out string buffer to zero and clear the edit text field
+ mOutStringBuffer.setLength(0);
+ mOutEditText.setText(mOutStringBuffer);
+ }
+ }
- // Reset out string buffer to zero and clear the edit text field
- mOutStringBuffer.setLength(0);
- mOutEditText.setText(mOutStringBuffer);
- }
- }
// The action listener for the EditText widget, to listen for the return key
private TextView.OnEditorActionListener mWriteListener =
- new TextView.OnEditorActionListener() {
- public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
- // If the action is a key-up event on the return key, send the message
- if (actionId == EditorInfo.IME_NULL && event.getAction() == KeyEvent.ACTION_UP) {
- String message = view.getText().toString();
- sendMessage(message);
- }
- if(D) Log.i(TAG, "END onEditorAction");
+ new TextView.OnEditorActionListener() {
+ public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
+ // If the action is a key-up event on the return key, send the message
+ if (actionId == EditorInfo.IME_NULL && event.getAction() == KeyEvent.ACTION_UP) {
+ String message = view.getText().toString();
+ sendMessage(message);
+ }
+ if(D) Log.i(TAG, "END onEditorAction");
return true;
}
};
};
public void onActivityResult(int requestCode, int resultCode, Intent data) {
- if(D) Log.d(TAG, "onActivityResult " + resultCode);
- switch (requestCode) {
- case REQUEST_CONNECT_DEVICE:
- // When DeviceListActivity returns with a device to connect to
- if (resultCode == Activity.RESULT_OK) {
- connectDevice(data);
- }
- break;
- case REQUEST_ENABLE_BT:
- // When the request to enable Bluetooth returns
- if (resultCode == Activity.RESULT_OK) {
- // Bluetooth is now enabled, so set up a chat session
+ if(D) Log.d(TAG, "onActivityResult " + resultCode);
+ switch (requestCode) {
+ case REQUEST_CONNECT_DEVICE:
+ // When DeviceListActivity returns with a device to connect to
+ if (resultCode == Activity.RESULT_OK) {
+ connectDevice(data);
+ }
+ break;
+ case REQUEST_ENABLE_BT:
+ // When the request to enable Bluetooth returns
+ if (resultCode == Activity.RESULT_OK) {
+ // Bluetooth is now enabled, so set up a chat session
setupChat();
- } else {
- // User did not enable Bluetooth or an error occured
- Log.d(TAG, "BT not enabled");
- Toast.makeText(this, R.string.bt_not_enabled_leaving, Toast.LENGTH_SHORT).show();
- finish();
- }
- }
+ } else {
+ // User did not enable Bluetooth or an error occured
+ Log.d(TAG, "BT not enabled");
+ stopService(new Intent(AltosDroid.this, TelemetryService.class));
+ Toast.makeText(this, R.string.bt_not_enabled_leaving, Toast.LENGTH_SHORT).show();
+ finish();
+ }
+ }
}
private void connectDevice(Intent data) {
- // Get the device MAC address
- String address = data.getExtras()
- .getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
- // Get the BLuetoothDevice object
- BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
- // Attempt to connect to the device
+ // Get the device MAC address
+ String address = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
+ // Get the BLuetoothDevice object
+ BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
+ // Attempt to connect to the device
mChatService.connect(device);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
- MenuInflater inflater = getMenuInflater();
- inflater.inflate(R.menu.option_menu, menu);
- return true;
+ MenuInflater inflater = getMenuInflater();
+ inflater.inflate(R.menu.option_menu, menu);
+ return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
- Intent serverIntent = null;
- switch (item.getItemId()) {
+ Intent serverIntent = null;
+ switch (item.getItemId()) {
case R.id.telemetry_service_control:
serverIntent = new Intent(this, TelemetryServiceActivities.Controller.class);
startActivity(serverIntent);
serverIntent = new Intent(this, TelemetryServiceActivities.Binding.class);
startActivity(serverIntent);
return true;
- case R.id.connect_scan:
- // Launch the DeviceListActivity to see devices and do scan
- serverIntent = new Intent(this, DeviceListActivity.class);
- startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);
- return true;
- }
- return false;
+ case R.id.connect_scan:
+ // Launch the DeviceListActivity to see devices and do scan
+ serverIntent = new Intent(this, DeviceListActivity.class);
+ startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);
+ return true;
+ }
+ return false;
}
}
* Activity in the result Intent.
*/
public class DeviceListActivity extends Activity {
- // Debugging
- private static final String TAG = "DeviceListActivity";
- private static final boolean D = true;
-
- // Return Intent extra
- public static String EXTRA_DEVICE_ADDRESS = "device_address";
-
- // Member fields
- private BluetoothAdapter mBtAdapter;
- private ArrayAdapter<String> mPairedDevicesArrayAdapter;
- private ArrayAdapter<String> mNewDevicesArrayAdapter;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- // Setup the window
- requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
- setContentView(R.layout.device_list);
-
- // Set result CANCELED incase the user backs out
- setResult(Activity.RESULT_CANCELED);
-
- // Initialize the button to perform device discovery
- Button scanButton = (Button) findViewById(R.id.button_scan);
- scanButton.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- doDiscovery();
- v.setVisibility(View.GONE);
- }
- });
-
- // Initialize array adapters. One for already paired devices and
- // one for newly discovered devices
- mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);
- mNewDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);
-
- // Find and set up the ListView for paired devices
- ListView pairedListView = (ListView) findViewById(R.id.paired_devices);
- pairedListView.setAdapter(mPairedDevicesArrayAdapter);
- pairedListView.setOnItemClickListener(mDeviceClickListener);
-
- // Find and set up the ListView for newly discovered devices
- ListView newDevicesListView = (ListView) findViewById(R.id.new_devices);
- newDevicesListView.setAdapter(mNewDevicesArrayAdapter);
- newDevicesListView.setOnItemClickListener(mDeviceClickListener);
-
- // Register for broadcasts when a device is discovered
- IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
- this.registerReceiver(mReceiver, filter);
-
- // Register for broadcasts when discovery has finished
- filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
- this.registerReceiver(mReceiver, filter);
-
- // Get the local Bluetooth adapter
- mBtAdapter = BluetoothAdapter.getDefaultAdapter();
-
- // Get a set of currently paired devices
- Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();
-
- // If there are paired devices, add each one to the ArrayAdapter
- if (pairedDevices.size() > 0) {
- findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);
- for (BluetoothDevice device : pairedDevices) {
- mPairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
- }
- } else {
- String noDevices = getResources().getText(R.string.none_paired).toString();
- mPairedDevicesArrayAdapter.add(noDevices);
- }
- }
-
- @Override
- protected void onDestroy() {
- super.onDestroy();
-
- // Make sure we're not doing discovery anymore
- if (mBtAdapter != null) {
- mBtAdapter.cancelDiscovery();
- }
-
- // Unregister broadcast listeners
- this.unregisterReceiver(mReceiver);
- }
-
- /**
- * Start device discover with the BluetoothAdapter
- */
- private void doDiscovery() {
- if (D) Log.d(TAG, "doDiscovery()");
-
- // Indicate scanning in the title
- setProgressBarIndeterminateVisibility(true);
- setTitle(R.string.scanning);
-
- // Turn on sub-title for new devices
- findViewById(R.id.title_new_devices).setVisibility(View.VISIBLE);
-
- // If we're already discovering, stop it
- if (mBtAdapter.isDiscovering()) {
- mBtAdapter.cancelDiscovery();
- }
-
- // Request discover from BluetoothAdapter
- mBtAdapter.startDiscovery();
- }
-
- // The on-click listener for all devices in the ListViews
- private OnItemClickListener mDeviceClickListener = new OnItemClickListener() {
- public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) {
- // Cancel discovery because it's costly and we're about to connect
- mBtAdapter.cancelDiscovery();
-
- // Get the device MAC address, which is the last 17 chars in the View
- String info = ((TextView) v).getText().toString();
- String address = info.substring(info.length() - 17);
-
- // Create the result Intent and include the MAC address
- Intent intent = new Intent();
- intent.putExtra(EXTRA_DEVICE_ADDRESS, address);
-
- // Set result and finish this Activity
- setResult(Activity.RESULT_OK, intent);
- finish();
- }
- };
-
- // The BroadcastReceiver that listens for discovered devices and
- // changes the title when discovery is finished
- private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
-
- // When discovery finds a device
- if (BluetoothDevice.ACTION_FOUND.equals(action)) {
- // Get the BluetoothDevice object from the Intent
- BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
- // If it's already paired, skip it, because it's been listed already
- if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
- mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
- }
- // When discovery is finished, change the Activity title
- } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
- setProgressBarIndeterminateVisibility(false);
- setTitle(R.string.select_device);
- if (mNewDevicesArrayAdapter.getCount() == 0) {
- String noDevices = getResources().getText(R.string.none_found).toString();
- mNewDevicesArrayAdapter.add(noDevices);
- }
- }
- }
- };
+ // Debugging
+ private static final String TAG = "DeviceListActivity";
+ private static final boolean D = true;
+
+ // Return Intent extra
+ public static String EXTRA_DEVICE_ADDRESS = "device_address";
+
+ // Member fields
+ private BluetoothAdapter mBtAdapter;
+ private ArrayAdapter<String> mPairedDevicesArrayAdapter;
+ private ArrayAdapter<String> mNewDevicesArrayAdapter;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ // Setup the window
+ requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
+ setContentView(R.layout.device_list);
+
+ // Set result CANCELED incase the user backs out
+ setResult(Activity.RESULT_CANCELED);
+
+ // Initialize the button to perform device discovery
+ Button scanButton = (Button) findViewById(R.id.button_scan);
+ scanButton.setOnClickListener(new OnClickListener() {
+ public void onClick(View v) {
+ doDiscovery();
+ v.setVisibility(View.GONE);
+ }
+ });
+
+ // Initialize array adapters. One for already paired devices and
+ // one for newly discovered devices
+ mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);
+ mNewDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);
+
+ // Find and set up the ListView for paired devices
+ ListView pairedListView = (ListView) findViewById(R.id.paired_devices);
+ pairedListView.setAdapter(mPairedDevicesArrayAdapter);
+ pairedListView.setOnItemClickListener(mDeviceClickListener);
+
+ // Find and set up the ListView for newly discovered devices
+ ListView newDevicesListView = (ListView) findViewById(R.id.new_devices);
+ newDevicesListView.setAdapter(mNewDevicesArrayAdapter);
+ newDevicesListView.setOnItemClickListener(mDeviceClickListener);
+
+ // Register for broadcasts when a device is discovered
+ IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
+ this.registerReceiver(mReceiver, filter);
+
+ // Register for broadcasts when discovery has finished
+ filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
+ this.registerReceiver(mReceiver, filter);
+
+ // Get the local Bluetooth adapter
+ mBtAdapter = BluetoothAdapter.getDefaultAdapter();
+
+ // Get a set of currently paired devices
+ Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();
+
+ // If there are paired devices, add each one to the ArrayAdapter
+ if (pairedDevices.size() > 0) {
+ findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);
+ for (BluetoothDevice device : pairedDevices) {
+ mPairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
+ }
+ } else {
+ String noDevices = getResources().getText(R.string.none_paired).toString();
+ mPairedDevicesArrayAdapter.add(noDevices);
+ }
+ }
+
+ @Override
+ protected void onDestroy() {
+ super.onDestroy();
+
+ // Make sure we're not doing discovery anymore
+ if (mBtAdapter != null) {
+ mBtAdapter.cancelDiscovery();
+ }
+
+ // Unregister broadcast listeners
+ this.unregisterReceiver(mReceiver);
+ }
+
+ /**
+ * Start device discover with the BluetoothAdapter
+ */
+ private void doDiscovery() {
+ if (D) Log.d(TAG, "doDiscovery()");
+
+ // Indicate scanning in the title
+ setProgressBarIndeterminateVisibility(true);
+ setTitle(R.string.scanning);
+
+ // Turn on sub-title for new devices
+ findViewById(R.id.title_new_devices).setVisibility(View.VISIBLE);
+
+ // If we're already discovering, stop it
+ if (mBtAdapter.isDiscovering()) {
+ mBtAdapter.cancelDiscovery();
+ }
+
+ // Request discover from BluetoothAdapter
+ mBtAdapter.startDiscovery();
+ }
+
+ // The on-click listener for all devices in the ListViews
+ private OnItemClickListener mDeviceClickListener = new OnItemClickListener() {
+ public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) {
+ // Cancel discovery because it's costly and we're about to connect
+ mBtAdapter.cancelDiscovery();
+
+ // Get the device MAC address, which is the last 17 chars in the View
+ String info = ((TextView) v).getText().toString();
+ String address = info.substring(info.length() - 17);
+
+ // Create the result Intent and include the MAC address
+ Intent intent = new Intent();
+ intent.putExtra(EXTRA_DEVICE_ADDRESS, address);
+
+ // Set result and finish this Activity
+ setResult(Activity.RESULT_OK, intent);
+ finish();
+ }
+ };
+
+ // The BroadcastReceiver that listens for discovered devices and
+ // changes the title when discovery is finished
+ private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ String action = intent.getAction();
+
+ // When discovery finds a device
+ if (BluetoothDevice.ACTION_FOUND.equals(action)) {
+ // Get the BluetoothDevice object from the Intent
+ BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
+ // If it's already paired, skip it, because it's been listed already
+ if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
+ mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
+ }
+ // When discovery is finished, change the Activity title
+ } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
+ setProgressBarIndeterminateVisibility(false);
+ setTitle(R.string.select_device);
+ if (mNewDevicesArrayAdapter.getCount() == 0) {
+ String noDevices = getResources().getText(R.string.none_found).toString();
+ mNewDevicesArrayAdapter.add(noDevices);
+ }
+ }
+ }
+ };
}
public class TelemetryService extends Service {
- private NotificationManager mNM;
-
- // Unique Identification Number for the Notification.
- // We use it on Notification start, and to cancel it.
- private int NOTIFICATION = R.string.telemetry_service_label;
/**
* Class for clients to access. Because we know this service always
return TelemetryService.this;
}
}
+ // Unique Identification Number for the Notification.
+ // We use it on Notification start, and to cancel it.
+ private int NOTIFICATION = R.string.telemetry_service_label;
+ private NotificationManager mNM;
- @Override
+ @Override
public void onCreate() {
- // Create a reference to the NotificationManager so that we can update our notifcation text later
- mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
- }
+ // Create a reference to the NotificationManager so that we can update our notifcation text later
+ mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
+ }
- @Override
+ @Override
public int onStartCommand(Intent intent, int flags, int startId) {
- Log.i("TelemetryService", "Received start id " + startId + ": " + intent);
+ Log.i("TelemetryService", "Received start id " + startId + ": " + intent);
- CharSequence text = getText(R.string.telemetry_service_started);
+ CharSequence text = getText(R.string.telemetry_service_started);
- // Create notification to be displayed while the service runs
- Notification notification = new Notification(R.drawable.am_status_c, text, 0);
+ // Create notification to be displayed while the service runs
+ Notification notification = new Notification(R.drawable.am_status_c, text, 0);
- // The PendingIntent to launch our activity if the user selects this notification
- PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
+ // The PendingIntent to launch our activity if the user selects this notification
+ PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, TelemetryServiceActivities.Controller.class), 0);
- // Set the info for the views that show in the notification panel.
- notification.setLatestEventInfo(this, getText(R.string.telemetry_service_label), text, contentIntent);
+ // Set the info for the views that show in the notification panel.
+ notification.setLatestEventInfo(this, getText(R.string.telemetry_service_label), text, contentIntent);
- // Set the notification to be in the "Ongoing" section.
- notification.flags |= Notification.FLAG_ONGOING_EVENT;
+ // Set the notification to be in the "Ongoing" section.
+ notification.flags |= Notification.FLAG_ONGOING_EVENT;
- // Move us into the foreground.
- startForeground(NOTIFICATION, notification);
+ // Move us into the foreground.
+ startForeground(NOTIFICATION, notification);
- // We want this service to continue running until it is explicitly
- // stopped, so return sticky.
- return START_STICKY;
- }
+ // We want this service to continue running until it is explicitly
+ // stopped, so return sticky.
+ return START_STICKY;
+ }
- @Override
+ @Override
public void onDestroy() {
- // Demote us from the foreground, and cancel the persistent notification.
- stopForeground(true);
- // Tell the user we stopped.
- Toast.makeText(this, R.string.telemetry_service_stopped, Toast.LENGTH_SHORT).show();
- }
- @Override
+ // Demote us from the foreground, and cancel the persistent notification.
+ stopForeground(true);
+
+ // Tell the user we stopped.
+ Toast.makeText(this, R.string.telemetry_service_stopped, Toast.LENGTH_SHORT).show();
+ }
+
+ @Override
public IBinder onBind(Intent intent) {
return mBinder;
- }
+ }
// This is the object that receives interactions from clients. See
// RemoteService for a more complete example.