@@ -3,6 +3,7 @@ package io.github.xei.police.joystick
33
44import android.app.Activity
55import android.bluetooth.BluetoothAdapter
6+ import android.bluetooth.BluetoothSocket
67import android.content.ActivityNotFoundException
78import android.content.Intent
89import android.os.Bundle
@@ -16,6 +17,11 @@ import android.widget.Toast
1617
1718import io.github.xei.police.R
1819import 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 ) {
0 commit comments