Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b0dc1f1

Browse files
committed
check bluetooth enable.
1 parent 7d22b76 commit b0dc1f1

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

‎controller-application/Police/app/src/main/java/io/github/xei/police/joystick/JoystickFragment.kt‎

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package io.github.xei.police.joystick
22

33

44
import android.app.Activity
5+
import android.bluetooth.BluetoothAdapter
56
import android.content.ActivityNotFoundException
67
import android.content.Intent
78
import android.os.Bundle
@@ -14,6 +15,7 @@ import android.widget.ImageButton
1415
import android.widget.Toast
1516

1617
import io.github.xei.police.R
18+
import io.github.xei.police.exception.BluetoothNotSupportException
1719

1820

1921
/**
@@ -24,7 +26,8 @@ import io.github.xei.police.R
2426
class JoystickFragment : Fragment(), JoystickContract.View, View.OnClickListener {
2527

2628
companion object {
27-
private const val REQUEST_CODE_SPEECH_RECOGNIZE = 100
29+
private const val REQUEST_CODE_ENABLE_BLUETOOTH = 100
30+
private const val REQUEST_CODE_SPEECH_RECOGNIZE = 200
2831

2932
fun newInstance() = JoystickFragment()
3033
}
@@ -33,31 +36,50 @@ class JoystickFragment : Fragment(), JoystickContract.View, View.OnClickListener
3336
override val isActive: Boolean
3437
get() = isAdded
3538

39+
private lateinit var mUpArrowKeyImageButton: ImageButton
3640
private lateinit var mVoiceCommandImageButton: ImageButton
3741

38-
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
42+
43+
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
3944
savedInstanceState: Bundle?): View? {
4045

41-
val rootView = inflater!!.inflate(R.layout.fragment_joystick, container, false)
46+
val rootView = inflater.inflate(R.layout.fragment_joystick, container, false)
4247

4348
findViews(rootView)
4449
setOnClickListeners()
4550

51+
presenter.start()
52+
4653
return rootView
4754
}
4855

4956
override fun findViews(rootView: View) {
57+
mUpArrowKeyImageButton = rootView.findViewById(R.id.fragmentJoystick_imageButton_upArrowButton)
5058
mVoiceCommandImageButton = rootView.findViewById(R.id.layoutFragmentJoystickCenter_imageButton_voiceCommand)
5159
}
5260

5361
override fun setOnClickListeners() {
62+
mUpArrowKeyImageButton.setOnClickListener(this)
5463
mVoiceCommandImageButton.setOnClickListener(this)
5564
}
5665

5766
override fun showToast(text: String) {
5867
Toast.makeText(context, text, Toast.LENGTH_SHORT).show()
5968
}
6069

70+
override fun isBluetoothEnabled(): Boolean {
71+
try {
72+
return BluetoothAdapter.getDefaultAdapter().isEnabled
73+
} catch (npe: NullPointerException) {
74+
throw BluetoothNotSupportException()
75+
}
76+
}
77+
78+
override fun startEnableBluetoothActivityForResult() {
79+
val intent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
80+
startActivityForResult(intent, REQUEST_CODE_ENABLE_BLUETOOTH)
81+
}
82+
6183
private fun startVoiceRecognitionActivityForResult() {
6284
val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
6385
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, javaClass.`package`.name)
@@ -84,6 +106,12 @@ class JoystickFragment : Fragment(), JoystickContract.View, View.OnClickListener
84106
super.onActivityResult(requestCode, resultCode, data)
85107

86108
when (requestCode) {
109+
110+
REQUEST_CODE_ENABLE_BLUETOOTH -> if (resultCode == Activity.RESULT_OK) {
111+
// TODO: connect to device
112+
// TODO: change LED to green
113+
}
114+
87115
REQUEST_CODE_SPEECH_RECOGNIZE -> if (resultCode == Activity.RESULT_OK && data != null) {
88116
val recognizedCommand = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)
89117
presenter.performVoiceCommand(recognizedCommand[0])

‎controller-application/Police/app/src/main/java/io/github/xei/police/joystick/JoystickPresenter.kt‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.github.xei.police.joystick
22

3+
import io.github.xei.police.exception.BluetoothNotSupportException
4+
35
/**
46
* Created by hamidreza on 3/23/18.
57
*/
@@ -11,7 +13,18 @@ class JoystickPresenter(private val model: JoystickContract.Model, private val v
1113
}
1214

1315
override fun start() {
16+
if (view.isActive) {
17+
try {
18+
if (view.isBluetoothEnabled()) {
19+
// TODO: connect to device
20+
} else {
21+
view.startEnableBluetoothActivityForResult()
22+
}
23+
} catch (bnse: BluetoothNotSupportException) {
24+
view.showToast(bnse.message.toString())
25+
}
1426

27+
}
1528
}
1629

1730
override fun performVoiceCommand(recognizedCommand: String) {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /