mirror of
https://github.com/Skysamara/airshot_a.git
synced 2025-12-06 06:16:05 +00:00
fix btConnect
This commit is contained in:
parent
41b849c86a
commit
353c34dbe3
@ -14,11 +14,11 @@ import kotlinx.android.synthetic.main.activity_main.*
|
||||
//
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
private val REQUEST_CODE_ENABLE_BT:Int = 1
|
||||
private val REQUEST_CODE_DISCOVERBLE_BT:Int = 2
|
||||
private val REQUEST_CODE_ENABLE_BT: Int = 1
|
||||
private val REQUEST_CODE_DISCOVERBLE_BT: Int = 2
|
||||
|
||||
// bluetooth adapter
|
||||
lateinit var bAdapter:BluetoothAdapter
|
||||
lateinit var bAdapter: BluetoothAdapter
|
||||
|
||||
@SuppressLint("MissingPermission")
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
@ -28,28 +28,25 @@ class MainActivity : AppCompatActivity() {
|
||||
// bluetooth adapter
|
||||
bAdapter = BluetoothAdapter.getDefaultAdapter()
|
||||
// check is on/off
|
||||
if(bAdapter == null){
|
||||
if (bAdapter == null) {
|
||||
bluetoothStatusTV.text = "Bluetooht is not available"
|
||||
}
|
||||
else{
|
||||
} else {
|
||||
bluetoothStatusTV.text = "Bluetooht is available"
|
||||
}
|
||||
// Статус bluetooth
|
||||
if (bAdapter.isEnabled){
|
||||
if (bAdapter.isEnabled) {
|
||||
bluetoothIv.setImageResource(R.drawable.ic_bluetooth_on)
|
||||
}
|
||||
else{
|
||||
} else {
|
||||
bluetoothIv.setImageResource(R.drawable.ic_bluetooth_off)
|
||||
}
|
||||
|
||||
// Включаем bluetooth
|
||||
// TODO Bluetooth включается, но сообщение и иконка неправильные
|
||||
turnOnBtn.setOnClickListener {
|
||||
if (bAdapter.isEnabled){
|
||||
if (bAdapter.isEnabled) {
|
||||
// Уже включен
|
||||
Toast.makeText(this, "Already on", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
else{
|
||||
} else {
|
||||
// Включаем
|
||||
val intent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
|
||||
startActivityForResult(intent, REQUEST_CODE_ENABLE_BT)
|
||||
@ -57,10 +54,9 @@ class MainActivity : AppCompatActivity() {
|
||||
}
|
||||
// Выключаем bluetooth
|
||||
turnOffBtn.setOnClickListener {
|
||||
if (!bAdapter.isEnabled){
|
||||
if (!bAdapter.isEnabled) {
|
||||
Toast.makeText(this, "Already off", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
else{
|
||||
} else {
|
||||
bAdapter.disable()
|
||||
bluetoothIv.setImageResource(R.drawable.ic_bluetooth_off)
|
||||
Toast.makeText(this, "Bluetooth turned off", Toast.LENGTH_SHORT).show()
|
||||
@ -69,38 +65,37 @@ class MainActivity : AppCompatActivity() {
|
||||
}
|
||||
// Ищем bluetooth
|
||||
discoverableBtn.setOnClickListener {
|
||||
if (!bAdapter.isDiscovering){
|
||||
if (!bAdapter.isDiscovering) {
|
||||
Toast.makeText(this, "Making Your devices discoverable", Toast.LENGTH_SHORT).show()
|
||||
val intent = Intent(Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE))
|
||||
startActivityForResult(intent, REQUEST_CODE_DISCOVERBLE_BT)
|
||||
}
|
||||
}
|
||||
// Список сопряженных устроств
|
||||
pairedBtn.setOnClickListener{
|
||||
if (bAdapter.isEnabled){
|
||||
pairedBtn.setOnClickListener {
|
||||
if (bAdapter.isEnabled) {
|
||||
pairedTv.text = "Paired devices"
|
||||
// получаем список сопряженных устройств
|
||||
val devices = bAdapter.bondedDevices
|
||||
for(device in devices){
|
||||
for (device in devices) {
|
||||
val deviceName = device.name
|
||||
val deviceAddress = device
|
||||
pairedTv.append("\nDevice: $deviceName , $device")
|
||||
}
|
||||
}
|
||||
else{
|
||||
} else {
|
||||
Toast.makeText(this, "Turn on bluetooth first", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||
when(requestCode){
|
||||
when (requestCode) {
|
||||
REQUEST_CODE_ENABLE_BT ->
|
||||
if (requestCode == Activity.RESULT_OK){
|
||||
// if (requestCode == Activity.RESULT_OK){
|
||||
if (resultCode == Activity.RESULT_OK) {
|
||||
bluetoothIv.setImageResource(R.drawable.ic_bluetooth_on) // 14:14
|
||||
Toast.makeText(this, "Bluetooth is on", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
else{
|
||||
} else {
|
||||
Toast.makeText(this, "Could not on bluetooth", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user