This commit is contained in:
skysamara 2022-09-05 17:46:38 +03:00
parent f92f27c647
commit 29f1dc6177
2 changed files with 43 additions and 19 deletions

View File

@ -4,9 +4,8 @@
package="ru.panorobot.airshot"> package="ru.panorobot.airshot">
<uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" /> <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<application <application
android:allowBackup="true" android:allowBackup="true"

View File

@ -3,6 +3,9 @@ package ru.panorobot.airshot
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.Activity import android.app.Activity
import android.bluetooth.BluetoothAdapter import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothManager
import android.content.Context
import android.content.Intent import android.content.Intent
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle import android.os.Bundle
@ -17,32 +20,38 @@ class MainActivity : AppCompatActivity() {
private val REQUEST_CODE_ENABLE_BT: Int = 1 private val REQUEST_CODE_ENABLE_BT: Int = 1
private val REQUEST_CODE_DISCOVERBLE_BT: Int = 2 private val REQUEST_CODE_DISCOVERBLE_BT: Int = 2
// bluetooth adapter lateinit var btAdapter: BluetoothAdapter
lateinit var bAdapter: BluetoothAdapter
@SuppressLint("MissingPermission")
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
// bluetooth adapter init() // Инициализируем bluetooth adapter
bAdapter = BluetoothAdapter.getDefaultAdapter() checkBluetoothStatus()
setListeners()
getPairedDevices()
}
private fun checkBluetoothStatus() {
// check is on/off // check is on/off
if (bAdapter == null) { if (btAdapter == null) {
bluetoothStatusTV.text = "Bluetooht is not available" bluetoothStatusTV.text = "Bluetooht is not available"
} else { } else {
bluetoothStatusTV.text = "Bluetooht is available" bluetoothStatusTV.text = "Bluetooht is available"
} }
// Статус bluetooth // Статус bluetooth
if (bAdapter.isEnabled) { if (btAdapter.isEnabled) {
bluetoothIv.setImageResource(R.drawable.ic_bluetooth_on) bluetoothIv.setImageResource(R.drawable.ic_bluetooth_on)
} else { } else {
bluetoothIv.setImageResource(R.drawable.ic_bluetooth_off) bluetoothIv.setImageResource(R.drawable.ic_bluetooth_off)
} }
}
@SuppressLint("MissingPermission")
private fun MainActivity.setListeners() {
// Включаем bluetooth // Включаем bluetooth
turnOnBtn.setOnClickListener { turnOnBtn.setOnClickListener {
if (bAdapter.isEnabled) { if (btAdapter.isEnabled) {
// Уже включен // Уже включен
Toast.makeText(this, "Already on", Toast.LENGTH_SHORT).show() Toast.makeText(this, "Already on", Toast.LENGTH_SHORT).show()
} else { } else {
@ -53,10 +62,10 @@ class MainActivity : AppCompatActivity() {
} }
// Выключаем bluetooth // Выключаем bluetooth
turnOffBtn.setOnClickListener { turnOffBtn.setOnClickListener {
if (!bAdapter.isEnabled) { if (!btAdapter.isEnabled) {
Toast.makeText(this, "Already off", Toast.LENGTH_SHORT).show() Toast.makeText(this, "Already off", Toast.LENGTH_SHORT).show()
} else { } else {
bAdapter.disable() btAdapter.disable()
bluetoothIv.setImageResource(R.drawable.ic_bluetooth_off) bluetoothIv.setImageResource(R.drawable.ic_bluetooth_off)
Toast.makeText(this, "Bluetooth turned off", Toast.LENGTH_SHORT).show() Toast.makeText(this, "Bluetooth turned off", Toast.LENGTH_SHORT).show()
} }
@ -64,18 +73,28 @@ class MainActivity : AppCompatActivity() {
} }
// Ищем bluetooth // Ищем bluetooth
discoverableBtn.setOnClickListener { discoverableBtn.setOnClickListener {
if (!bAdapter.isDiscovering) { if (!btAdapter.isDiscovering) {
Toast.makeText(this, "Making Your devices discoverable", Toast.LENGTH_SHORT).show() Toast.makeText(this, "Making Your devices discoverable", Toast.LENGTH_SHORT).show()
val intent = Intent(Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE)) val intent = Intent(Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE))
startActivityForResult(intent, REQUEST_CODE_DISCOVERBLE_BT) startActivityForResult(intent, REQUEST_CODE_DISCOVERBLE_BT)
} }
} }
// Переходим на окно мишени
shootBtn.setOnClickListener {
//TODO Проверить, что bluetooth включен и подключена мишень
startShooting()
}
}
@SuppressLint("MissingPermission")
private fun getPairedDevices() {
// Список сопряженных устроств // Список сопряженных устроств
pairedBtn.setOnClickListener { pairedBtn.setOnClickListener {
if (bAdapter.isEnabled) { if (btAdapter.isEnabled) {
pairedTv.text = "Paired devices" pairedTv.text = "Paired devices"
// получаем список сопряженных устройств // получаем список сопряженных устройств
val devices = bAdapter.bondedDevices val devices = btAdapter.bondedDevices
for (device in devices) { for (device in devices) {
val deviceName = device.name val deviceName = device.name
val deviceAddress = device val deviceAddress = device
@ -85,12 +104,18 @@ class MainActivity : AppCompatActivity() {
Toast.makeText(this, "Turn on bluetooth first", Toast.LENGTH_SHORT).show() Toast.makeText(this, "Turn on bluetooth first", Toast.LENGTH_SHORT).show()
} }
} }
}
shootBtn.setOnClickListener { private fun init(){
//TODO Проверить, что bluetooth включен и подключена мишень val btManager = getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
startShooting() btAdapter = btManager.adapter
} }
@SuppressLint("MissingPermission") //TODO Что это?
private fun getPaireddevices(){
val pairedDevices: Set<BluetoothDevice>? = btAdapter?.bondedDevices
} }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {