MainActivity.kt
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.WindowCompat
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.content_main)
}
}
MainFragment.kt
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothManager
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.neco_desarrollo.bluetoothmoduletest.databinding.FragmentMainBinding
import com.neco_dev.bt_def.BluetoothConstants
import com.neco_dev.bt_def.bluetooth.BluetoothController
class MainFragment : Fragment(), BluetoothController.Listener {
private lateinit var bluetoothController: BluetoothController
private lateinit var btAdapter: BluetoothAdapter
private lateinit var binding: FragmentMainBinding
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentMainBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initBtAdapter()
val pref = activity?.getSharedPreferences(
BluetoothConstants.PREFERENCES, Context.MODE_PRIVATE)
val mac = pref?.getString(BluetoothConstants.MAC, "")
bluetoothController = BluetoothController(btAdapter)
binding.apply {
btList.setOnClickListener {
findNavController().navigate(R.id.listFragment)
}
connect.setOnClickListener {
bluetoothController.connect(mac ?: "", this@MainFragment)
}
takeOn.setOnClickListener {
bluetoothController.sendMessage("A")
}
takeOff.setOnClickListener {
bluetoothController.sendMessage("B")
}
}
}
private fun initBtAdapter(){
val bManager = activity?.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
btAdapter = bManager.adapter
}
override fun onReceive(message: String) {
activity?.runOnUiThread {
binding.tvMessage.text = message
}
}
override fun onDestroy() {
super.onDestroy()
bluetoothController.closeConnection()
}
}
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<fragment
android:id="@+id/nav_host_fragment_content_main"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
fragment_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<TextView
android:id="@+id/tvMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="72dp"
android:text="TextView"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/takeOff"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Off"
app:layout_constraintBottom_toTopOf="@+id/takeOn"
app:layout_constraintEnd_toEndOf="@+id/takeOn"
app:layout_constraintStart_toStartOf="@+id/takeOn" />
<Button
android:id="@+id/connect"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Connect"
app:layout_constraintBottom_toTopOf="@+id/btList"
app:layout_constraintEnd_toEndOf="@+id/btList"
app:layout_constraintStart_toStartOf="@+id/btList" />
<Button
android:id="@+id/btList"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Device list"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/takeOn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="On"
app:layout_constraintBottom_toTopOf="@+id/connect"
app:layout_constraintEnd_toEndOf="@+id/connect"
app:layout_constraintStart_toStartOf="@+id/connect" />
</androidx.constraintlayout.widget.ConstraintLayout>
nav_graph.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph"
app:startDestination="@id/mainFragment">
<fragment
android:id="@+id/mainFragment"
android:name="com.neco_desarrollo.bluetoothmoduletest.MainFragment"
tools:layout="@layout/fragment_main"/>
<fragment
android:id="@+id/listFragment"
android:name="com.neco_dev.bt_def.DeviceListFragment"
tools:layout="@layout/fragment_list"/>
</navigation>
можно ли скачать целый проект BluetoothModulDef, с app и библиотекой bt_def?