Hallo,
Ich versuche grade eine grundlegene Bluetooth infrastruktur zu bauen, aber die Official docs zum Classic scan scheinen nicht zu funktionieren.
Spezifisch, liefert der Broadcast receiver beim BluetoothDevice.ACTION_FOUND keine neue Geräte, auch wenn welche in der Umgebung da sind und sind auch mit anderen Geräten Discovered.
Unten ist mein code. Über irgendeine Hilfe würde ich mich freuen.
Java: MainActivity.kt
package com.example.ble
import android.app.Activity
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.AdapterView
import android.widget.ArrayAdapter
import kotlinx.android.synthetic.main.activity_main.*
import org.jetbrains.anko.toast
class MainActivity : AppCompatActivity() {
private var defaultAdapter: BluetoothAdapter? = null
lateinit var pairedDevices: Set<BluetoothDevice>
val REQUEST_ENABLE_BLUETOOTH = 1
companion object {
const val EXTRA_ADDRESS: String = "Device_address"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
defaultAdapter = BluetoothAdapter.getDefaultAdapter()
if (defaultAdapter == null) {
toast("Dieses Gerät verfügt über keine Bluetooth Adapter. Sie können diese App nicht benutzen")
return
}
if (!defaultAdapter!!.isEnabled) {
val enableBluetoothIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
startActivityForResult(enableBluetoothIntent, REQUEST_ENABLE_BLUETOOTH)
}
searchButton.setOnClickListener { startDiscovery() }
val filter = IntentFilter(BluetoothDevice.ACTION_FOUND)
Log.i("receiver", "registering receiver")
registerReceiver(receiver, filter)
}
private val receiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
toast("maybe i find new device who know")
when (intent.action) {
BluetoothDevice.ACTION_FOUND -> {
// Discovery has found a device. Get the BluetoothDevice
// object and its info from the Intent.
val device: BluetoothDevice? =
intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)
val deviceName = device?.name
val deviceHardwareAddress = device?.address // MAC address
Log.i("BLUETOOTH DEVICE", "NEW DEVICE CAUGHT $deviceName")
toast("device $deviceName was found")
}
BluetoothAdapter.ACTION_DISCOVERY_FINISHED -> {
context.unregisterReceiver(this)
}
}
}
}
private fun startDiscovery() {
if (defaultAdapter!!.isDiscovering) {
toast("scan already in progress")
} else {
toast("starting discovery")
defaultAdapter!!.startDiscovery();
}
}
private fun pairedDeviceList() {
pairedDevices = defaultAdapter!!.bondedDevices
val list: ArrayList<BluetoothDevice> = ArrayList()
if (!pairedDevices.isEmpty()) {
for (device: BluetoothDevice in pairedDevices) {
list.add(device)
Log.i("device", "" + device)
}
} else {
toast("keine verbundene Geräte gefunden.")
}
val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, list)
select_device_list.adapter = adapter
select_device_list.onItemClickListener = AdapterView.OnItemClickListener { _, _, position, l ->
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 (defaultAdapter!!.isEnabled) {
toast("Bluetooth wurde angemacht")
} else {
toast("Bluetooth wurde deaktiviert")
}
} else if (resultCode == Activity.RESULT_CANCELED) {
toast("Bluetooth kann von System aus nicht aktiviert werden")
}
}
}
}
Alles anzeigen