Request bluetooth to be discoverable on start.

This commit is contained in:
Felix Ableitner 2014-10-23 18:08:03 +03:00
parent 69f134b5bf
commit 4fe2d9ae45
1 changed files with 12 additions and 9 deletions

View File

@ -4,27 +4,30 @@ import android.app.Activity
import android.bluetooth.BluetoothAdapter
import android.content._
import android.os.Bundle
import android.view.{MenuItem, Menu}
import android.view.{Menu, MenuItem}
import android.widget.Toast
import com.nutomic.ensichat.R
import com.nutomic.ensichat.bluetooth.ChatService
/**
* Main activity, holds fragments and requests bluetooth to be enabled.
* Main activity, holds fragments and requests Bluetooth to be discoverable.
*/
class MainActivity extends Activity {
private final val RequestEnableBluetooth = 1
private val RequestSetDiscoverable = 1
/**
* Initializes layout, starts service and requests bluetooth.
* Initializes layout, starts service and requests Bluetooth to be discoverable.
*/
override def onCreate(savedInstanceState: Bundle): Unit = {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
startService(new Intent(this, classOf[ChatService]))
val intent: Intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
startActivityForResult(intent, RequestEnableBluetooth)
val intent: Intent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE)
intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 0)
startActivityForResult(intent, RequestSetDiscoverable)
}
/**
@ -36,12 +39,12 @@ class MainActivity extends Activity {
}
/**
* Exits with error if bluetooth was not enabled,
* Exits with error if bluetooth was not enabled/not set discoverable,
*/
override def onActivityResult(requestCode: Int, resultCode: Int, data: Intent): Unit = {
requestCode match {
case RequestEnableBluetooth =>
if (resultCode != Activity.RESULT_OK) {
case RequestSetDiscoverable =>
if (resultCode == Activity.RESULT_CANCELED) {
Toast.makeText(this, R.string.bluetooth_required, Toast.LENGTH_LONG).show()
finish()
}