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 824efd0

Browse files
committed
connect to hardware agent via Bluetooth.
1 parent 2f318d0 commit 824efd0

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ interface JoystickContract {
1818

1919
@Throws(BluetoothNotSupportException::class)
2020
fun isBluetoothEnabled(): Boolean
21+
fun startBluetoothSettingActivity()
2122
fun startEnableBluetoothActivityForResult()
23+
fun connectToHardwareAgent()
2224
}
2325

2426
interface Presenter : BasePresenter {

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

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

44
import android.app.Activity
55
import android.bluetooth.BluetoothAdapter
6+
import android.bluetooth.BluetoothSocket
67
import android.content.ActivityNotFoundException
78
import android.content.Intent
89
import android.os.Bundle
@@ -16,6 +17,11 @@ import android.widget.Toast
1617

1718
import io.github.xei.police.R
1819
import io.github.xei.police.exception.BluetoothNotSupportException
20+
import java.io.InputStream
21+
import java.io.OutputStream
22+
import java.util.*
23+
import android.content.ComponentName
24+
import java.io.IOException
1925

2026

2127
/**
@@ -29,6 +35,10 @@ class JoystickFragment : Fragment(), JoystickContract.View, View.OnClickListener
2935
private const val REQUEST_CODE_ENABLE_BLUETOOTH = 100
3036
private const val REQUEST_CODE_SPEECH_RECOGNIZE = 200
3137

38+
private const val BLUETOOTH_NAME_HARDWARE_AGENT = "HC-06"
39+
// Standard SerialPortService ID
40+
private const val SPP_UUID_SERIAL_BOARD = "00001101-0000-1000-8000-00805f9b34fb"
41+
3242
fun newInstance() = JoystickFragment()
3343
}
3444

@@ -39,6 +49,11 @@ class JoystickFragment : Fragment(), JoystickContract.View, View.OnClickListener
3949
private lateinit var mUpArrowKeyImageButton: ImageButton
4050
private lateinit var mVoiceCommandImageButton: ImageButton
4151

52+
private var mBluetoothSocket: BluetoothSocket? = null
53+
private var mInputStream: InputStream? = null
54+
private var mOutputStream: OutputStream? = null
55+
private var mBuffer: String = ""
56+
4257

4358
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
4459
savedInstanceState: Bundle?): View? {
@@ -75,11 +90,46 @@ class JoystickFragment : Fragment(), JoystickContract.View, View.OnClickListener
7590
}
7691
}
7792

93+
override fun startBluetoothSettingActivity() {
94+
val intent = Intent(Intent.ACTION_MAIN, null)
95+
intent.addCategory(Intent.CATEGORY_LAUNCHER)
96+
intent.component = ComponentName("com.android.settings",
97+
"com.android.settings.bluetooth.BluetoothSettings")
98+
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
99+
startActivity(intent)
100+
}
101+
78102
override fun startEnableBluetoothActivityForResult() {
79103
val intent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
80104
startActivityForResult(intent, REQUEST_CODE_ENABLE_BLUETOOTH)
81105
}
82106

107+
override fun connectToHardwareAgent() {
108+
val hardwareAgent = BluetoothAdapter.getDefaultAdapter().bondedDevices.firstOrNull {it.name == BLUETOOTH_NAME_HARDWARE_AGENT}
109+
try {
110+
mBluetoothSocket = hardwareAgent!!.createRfcommSocketToServiceRecord(UUID.fromString(SPP_UUID_SERIAL_BOARD))
111+
mBluetoothSocket?.connect()
112+
mInputStream = mBluetoothSocket?.inputStream
113+
mOutputStream = mBluetoothSocket?.outputStream
114+
115+
showToast("Device connected to hardware agent.")
116+
117+
} catch (ioe: IOException) {
118+
showToast("Connection to hardware agent failed.")
119+
120+
// TODO: handle refresh logic
121+
activity?.finish()
122+
} catch (npe: NullPointerException) {
123+
// The device Bluetooth is not paired with the hardware agent.
124+
showToast("Please pair your device bluetooth with the hardware agent: $BLUETOOTH_NAME_HARDWARE_AGENT")
125+
startBluetoothSettingActivity()
126+
127+
// TODO: handle refresh logic
128+
activity?.finish()
129+
}
130+
131+
}
132+
83133
private fun startVoiceRecognitionActivityForResult() {
84134
val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
85135
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, javaClass.`package`.name)
@@ -108,8 +158,8 @@ class JoystickFragment : Fragment(), JoystickContract.View, View.OnClickListener
108158
when (requestCode) {
109159

110160
REQUEST_CODE_ENABLE_BLUETOOTH -> if (resultCode == Activity.RESULT_OK) {
111-
// TODO: connect to device
112-
// TODO: change LED to green
161+
connectToHardwareAgent()
162+
// TODO: change status LED color to green
113163
}
114164

115165
REQUEST_CODE_SPEECH_RECOGNIZE -> if (resultCode == Activity.RESULT_OK && data != null) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class JoystickPresenter(private val model: JoystickContract.Model, private val v
1616
if (view.isActive) {
1717
try {
1818
if (view.isBluetoothEnabled()) {
19-
// TODO: connect to device
19+
view.connectToHardwareAgent()
2020
} else {
2121
view.startEnableBluetoothActivityForResult()
2222
}

0 commit comments

Comments
(0)

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