Closed. This question needs to be more focused。它当前不接受答案。
想改善这个问题吗?更新问题,使其仅关注editing this post的一个问题。
在10个月前关闭。
Improve this question
想改善这个问题吗?更新问题,使其仅关注editing this post的一个问题。
在10个月前关闭。
Improve this question
class BluetoothClass : AppCompatActivity() {
var mBluetoothAdapter: BluetoothAdapter? = null
lateinit var m_pairedDevices: Set<BluetoothDevice>
var discover_btn: Button? = null
var deviceHardwareAddress :String?=null
val REQUEST_ENABLE_BLUETOOTH = 1
val deviceList: ArrayList<String> = ArrayList(
val TAG = "BluetoothManager2"
val bmanager:BluetoothManagerClass=BluetoothManagerClass()
var btnEnableDisable_Discoverable: Button? = null
var btnONOFF: Button? = null
var mBTDevices = java.util.ArrayList<BluetoothDevice>()
var mDeviceListAdapter: BluetoothListAdapter? = null
var listview_discover: ListView? = null
var deviceName = String()
companion object {
val EXTRA_ADDRESS: String = "Device_address"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.bluetooth_access)
listview_discover = findViewById<View>(R.id.discover_bluetoot_list) as ListView
discover_btn = findViewById<View>(R.id.discover_button) as Button
btnONOFF = findViewById(R.id.onoffdiscover_button)
btnEnableDisable_Discoverable =
findViewById(R.id.enablediscover_button)
bmanager.mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
mBTDevices = java.util.ArrayList()
select_device_refresh.setOnClickListener { pairedDeviceList()
}
btnONOFF!!.setOnClickListener {
bmanager.enableDisableBT()
Log.d(this.TAG, "onClick: enabling/disabling bluetooth.")
}
}
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 (bmanager.mBluetoothAdapter!!.isEnabled) {
Toast.makeText(this, "Bluetooth has been enabled", Toast.LENGTH_SHORT)
.show();
} else {
Toast.makeText(this, "Bluetooth has been disabled", Toast.LENGTH_SHORT)
.show();
}
} else if (resultCode == Activity.RESULT_CANCELED) {
Toast.makeText(this, "Bluetooth enabling has been canceled", Toast.LENGTH_SHORT)
.show();
}
}
}
fun btnEnableDisable_Discoverable(view: View?) {
Log.d(
this.TAG,
"btnEnableDisable_Discoverable: Making device discoverable for 300 seconds."
)
bmanager.enalbedisablediscovery()
}
@RequiresApi(Build.VERSION_CODES.M)
fun checkBTPermissions() {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
var permissionCheck =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
checkSelfPermission("Manifest.permission.ACCESS_FINE_LOCATION")
} else {
TODO("VERSION.SDK_INT < M")
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
permissionCheck += checkSelfPermission("Manifest.permission.ACCESS_COARSE_LOCATION")
} else {
TODO("VERSION.SDK_INT < M")
}
if (permissionCheck != 0) {
requestPermissions(
arrayOf(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
), 1001
) //Any number
}
} else {
Log.d(
this.TAG,
"checkBTPermissions: No need to check permissions. SDK version < LOLLIPOP."
)
}
}
@RequiresApi(Build.VERSION_CODES.M)
fun btnDiscover(view: View?) {
Log.d(this.TAG, "btnDiscover: Looking for unpaired devices.")
bmanager.discovering()
bmanager.startdiscovering()
}
fun pairedDeviceList() {
m_pairedDevices = bmanager.mBluetoothAdapter!!.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_SHORT)
.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.name.toString()
val intent = Intent(this, BluetoothControl::class.java)
intent.putExtra(BluetoothClass.EXTRA_ADDRESS, address)
startActivity(intent)
}
}
}
val TAG = "BluetoothManager"
var btnEnableDisable_Discoverable: Button? = null
var btnONOFF: Button? = null
var mDeviceListAdapter: BluetoothListAdapter? = null
var listview_discover: ListView? = null
private var myApiClass: BluetoothClass =
BluetoothClass()
var deviceName = String()
companion object {
val EXTRA_ADDRESS: String = "Device_address"
}
@RequiresApi(Build.VERSION_CODES.M)
fun discovering() {
if (this.mBluetoothAdapter!!.isDiscovering) {
this.mBluetoothAdapter!!.cancelDiscovery()
Log.d(this.TAG, "btnDiscover: Canceling discovery.")
//check BT permissions in manifest
myApiClass.checkBTPermissions()
this.mBluetoothAdapter!!.startDiscovery()
val discoverDevicesIntent = IntentFilter(BluetoothDevice.ACTION_FOUND)
myApiClass.registerReceiver(mBroadcastDiscovernewDevice, discoverDevicesIntent)
}
}
@RequiresApi(Build.VERSION_CODES.M)
fun startdiscovering(){
if (!this.mBluetoothAdapter!!.isDiscovering) { //check BT permissions in manifest
myApiClass.checkBTPermissions()
this.mBluetoothAdapter!!.startDiscovery()
val discoverDevicesIntent = IntentFilter(BluetoothDevice.ACTION_FOUND)
myApiClass.registerReceiver(mBroadcastDiscovernewDevice, discoverDevicesIntent)
}
}
fun enalbedisablediscovery(){
val discoverableIntent = Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE)
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300)
myApiClass. startActivity(discoverableIntent)
val intentFilter = IntentFilter(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED)
myApiClass.registerReceiver(mBroadcastDiscoverable, intentFilter)
}
fun enableDisableBT() {
if (this.mBluetoothAdapter == null) {
Log.d(this.TAG, "enableDisableBT: Does not have BT capabilities.")
}
if (!this.mBluetoothAdapter!!.isEnabled) {
Log.d(this.TAG, "enableDisableBT: enabling BT.")
val enableBTIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
myApiClass.startActivity(enableBTIntent)
val BTIntent = IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED)
myApiClass.registerReceiver(mBroadcastBluetoothOnOff, BTIntent)
}
if (this.mBluetoothAdapter!!.isEnabled) {
Log.d(this.TAG, "enableDisableBT: disabling BT.")
this.mBluetoothAdapter!!.disable()
val BTIntent = IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED)
myApiClass.registerReceiver(mBroadcastBluetoothOnOff, BTIntent)
}
}
private val mBroadcastBluetoothOnOff: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val action = intent.action
// When discovery finds a device
if (action == BluetoothAdapter.ACTION_STATE_CHANGED) {
val state =
intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)
when (state) {
BluetoothAdapter.STATE_OFF ->
Log.d(ContentValues.TAG, "onReceive: STATE OFF")
BluetoothAdapter.STATE_TURNING_OFF -> Log.d(
ContentValues.TAG,
"mBroadcastReceiver1: STATE TURNING OFF"
)
BluetoothAdapter.STATE_ON -> Log.d(
ContentValues.TAG,
"mBroadcastReceiver1: STATE ON"
)
BluetoothAdapter.STATE_TURNING_ON -> Log.d(
ContentValues.TAG,
"mBroadcastReceiver1: STATE TURNING ON"
)
}
}
}
}
val mBroadcastDiscovernewDevice: BroadcastReceiver = object : BroadcastReceiver() {
val arrayList = ArrayList<String>()
var deviceName = String()
override fun onReceive(context: Context, intent: Intent) {
val action = intent.action
Log.d(ContentValues.TAG, "onReceive: ACTION FOUND.")
if (action == BluetoothDevice.ACTION_FOUND) {
val device =
intent.getParcelableExtra<BluetoothDevice>(BluetoothDevice.EXTRA_DEVICE)
Log.d(
ContentValues.TAG,
"onReceive: " + device.name + ": " + device.address
)
deviceName = device.name
val deviceHardwareAddress = device.address
arrayList.add(deviceName)
var mBTDevices = java.util.ArrayList<BluetoothDevice>()
mBTDevices = java.util.ArrayList()
var mDeviceListAdapter: BluetoothListAdapter? = null
var listview_discover: ListView? = null
mDeviceListAdapter =
BluetoothListAdapter(context, R.layout.device_adapter_view, mBTDevices)
listview_discover!!.setAdapter(mDeviceListAdapter)
mBTDevices.add(device)
}
}
}
val mBroadcastDiscoverable: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val action = intent.action
if (action == BluetoothAdapter.ACTION_SCAN_MODE_CHANGED) {
val mode =
intent.getIntExtra(BluetoothAdapter.EXTRA_SCAN_MODE, BluetoothAdapter.ERROR)
when (mode) {
BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE -> Log.d(
ContentValues.TAG,
"mBroadcastReceiver2: Discoverability Enabled."
)
BluetoothAdapter.SCAN_MODE_CONNECTABLE -> Log.d(
ContentValues.TAG,
"mBroadcastReceiver2: Discoverability Disabled. Able to receive connections."
)
BluetoothAdapter.SCAN_MODE_NONE -> Log.d(
ContentValues.TAG,
"mBroadcastReceiver2: Discoverability Disabled. Not able to receive connections."
)
BluetoothAdapter.STATE_CONNECTING -> Log.d(
ContentValues.TAG,
"mBroadcastReceiver2: Connecting...."
)
BluetoothAdapter.STATE_CONNECTED -> Log.d(
ContentValues.TAG,
"mBroadcastReceiver2: Connected."
)
}
}
}
}
最佳答案
您的BluetoothClass
会在其init中实例化BluetoothManagerClass
实例。您的BluetoothManagerClass
会在其初始化中实例化BluetoothClass
。这会导致无限循环,直到堆栈空间用完为止。BluetoothClass
是-Activity
,无论如何您都不应该自己实例化 Activity 。考虑例如将您的BluetoothClass
实例引用作为参数传递给BluetoothManagerClass
。
10-02 06:46