Я пытаюсь разработать BLE-код для приложения Android, используя несколько примеров, но я постоянно отключаю состояние BLE.Я использую планшет Amazon FireFly с самыми современными операционными системами и вижу все устройства BLE при сканировании устройств Bluetooth в меню настроек.Но я не вижу никаких устройств BLE при запуске этого кода.
«if (! M_bluetoothAdapter !!. IsEnabled ())» всегда возвращает ложное условие и никогда не выполняет сканирование устройства.
Есть ли что-то еще, что мне нужно сделать при ложном условии, чтобы это утверждение включило Bluetooth на планшете?
Код для действия
package com.example.blesandpit
import android.app.Activity
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import java.sql.Types.NULL
import android.bluetooth.BluetoothA2dp
import android.bluetooth.BluetoothManager
import android.content.Context
import android.widget.AdapterView
import android.widget.ArrayAdapter
import kotlinx.android.synthetic.main.activity_main.*
import android.util.Log
import androidx.core.app.ComponentActivity.ExtraData
import androidx.core.content.ContextCompat.getSystemService
import android.icu.lang.UCharacter.GraphemeClusterBreak.T
class MainActivity : AppCompatActivity() {
private var m_bluetoothAdapter: BluetoothAdapter? = null
private lateinit var m_pairedDevices: Set<BluetoothDevice>
private val REQUEST_ENABLE_BLUETOOTH = 1
companion object {
val EXTRA_ADDRESS: String = "Device_address"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
m_bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
if(m_bluetoothAdapter == null) {
Toast.makeText( this,"This device doesn't support bluetooth", Toast.LENGTH_LONG).show()
return
}
else{
Toast.makeText( this,"This device supports bluetooth", Toast.LENGTH_LONG).show()
}
// ref : /5152941/android-bluetooth-vklychit
if(!m_bluetoothAdapter!!.isEnabled()) {
val enableBluetoothIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
startActivityForResult(enableBluetoothIntent, REQUEST_ENABLE_BLUETOOTH)
Toast.makeText( this,"Bluetooth is enabled", Toast.LENGTH_LONG).show()
}else
{
Toast.makeText( this,"Bluetooth is disabled(1)", Toast.LENGTH_LONG).show()
}
select_device_refresh.setOnClickListener{ pairedDeviceList() }
}
private fun pairedDeviceList() {
m_pairedDevices = m_bluetoothAdapter!!.bondedDevices
val list : ArrayList<BluetoothDevice> = ArrayList()
if (!m_pairedDevices.isEmpty()) {
for (device: BluetoothDevice in m_pairedDevices) {
list.add(device)
Log.i("device", ""+device)
}
} else {
Toast.makeText( this,"No paired bluetooth devices found", Toast.LENGTH_LONG).show()
}
val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, list)
select_device_list.adapter = adapter
select_device_list.onItemClickListener = AdapterView.OnItemClickListener { _, _, position, _ ->
val device: BluetoothDevice = list[position]
val address: String = device.address
val intent = Intent(this, ControlActivity::class.java)
intent.putExtra(EXTRA_ADDRESS, address)
startActivity(intent)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_ENABLE_BLUETOOTH) {
if (resultCode == Activity.RESULT_OK) {
if (m_bluetoothAdapter!!.isEnabled) {
Toast.makeText( this,"Bluetooth has been enabled(2)", Toast.LENGTH_LONG).show()
} else {
Toast.makeText( this,"Bluetooth has been disabled(2)", Toast.LENGTH_LONG).show()
}
} else if (resultCode == Activity.RESULT_CANCELED) {
Toast.makeText( this,"Bluetooth enabling has been canceled(2)", Toast.LENGTH_LONG).show()
}
}
}
}
Это файл манифеста ...
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.blesandpit">
<!--
Reference
https://developer.android.com/guide/topics/connectivity/bluetooth-le
-->
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".control_layout"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>