【发布时间】:2022-09-27 14:43:04
【问题描述】:
我正在尝试在 android studio 和 arduino 之间建立串行通信,但我有这个致命异常。
需要 android.permission.BLUETOOTH_CONNECT 权限 android.content.AttributionSource@71c08a09:适配器服务 getBondedDevices
我在清单中添加了权限,这是我的代码:
<?xml version=\"1.0\" encoding=\"utf-8\"?> <manifest xmlns:android=\"http://schemas.android.com/apk/res/android\" xmlns:tools=\"http://schemas.android.com/tools\" package=\"com.example.ovo\" > <uses-permission android:name=\"android.permission.BLUETOOTH_CONNECT\" /> <uses-permission android:name=\"android.permission.BLUETOOTH_SCAN\" /> <uses-permission android:name=\"android.permission.INTERNET\" /> <uses-permission android:name=\"android.permission.ACCESS_NETWORK_STATE\" /> <uses-permission android:name=\"android.permission.BLUETOOTH\" android:maxSdkVersion=\"30\" /> <uses-permission android:name=\"android.permission.BLUETOOTH_ADMIN\" android:maxSdkVersion=\"30\" /> <application android:allowBackup=\"true\" android:dataExtractionRules=\"@xml/data_extraction_rules\" android:fullBackupContent=\"@xml/backup_rules\" android:icon=\"@drawable/ic_hojita\" android:label=\"@string/app_name\" android:roundIcon=\"@drawable/ic_hojita\" android:supportsRtl=\"true\" android:theme=\"@style/Theme.OVO\" tools:targetApi=\"33\" > <activity android:name=\".WifiCredentialActivity\" android:exported=\"true\" android:configChanges=\"orientation\" android:screenOrientation=\"portrait\"> ...这是活动中的代码:
package com.example.ovo import android.Manifest import android.bluetooth.BluetoothDevice import android.content.pm.PackageManager import android.os.Bundle import android.util.Log import android.view.WindowManager import android.widget.Button import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import androidx.core.app.ActivityCompat import com.harrysoft.androidbluetoothserial.BluetoothManager import com.harrysoft.androidbluetoothserial.BluetoothSerialDevice import com.harrysoft.androidbluetoothserial.SimpleBluetoothDeviceInterface import com.harrysoft.androidbluetoothserial.SimpleBluetoothDeviceInterface.OnMessageReceivedListener import com.harrysoft.androidbluetoothserial.SimpleBluetoothDeviceInterface.OnMessageSentListener import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.schedulers.Schedulers class WifiCredentialActivity : AppCompatActivity() { val bluetoothManager: BluetoothManager = BluetoothManager.getInstance() private var deviceInterface: SimpleBluetoothDeviceInterface? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_wifi_credential) getWindow().setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN ) if (bluetoothManager == null) { // Bluetooth unavailable on this device :( tell the user Toast.makeText(this, \"Bluetooth not available.\", Toast.LENGTH_LONG) .show() // Replace context with your context instance. finish() } val pairedDevices: Collection<BluetoothDevice> = bluetoothManager.pairedDevicesList for (device in pairedDevices) { if (ActivityCompat.checkSelfPermission( this, Manifest.permission.BLUETOOTH_CONNECT ) != PackageManager.PERMISSION_GRANTED ) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return } Log.d(\"My Bluetooth App\", \"Device name: \" + device.name) Log.d(\"My Bluetooth App\", \"Device MAC Address: \" + device.address) // Here ill try to connect with the device } } private fun connectDevice(mac: String) { bluetoothManager.openSerialDevice(mac) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe({ connectedDevice: BluetoothSerialDevice -> onConnected(connectedDevice) }) { error: Throwable -> onError( error ) } } private fun onConnected(connectedDevice: BluetoothSerialDevice) { // You are now connected to this device! // Here you may want to retain an instance to your device: deviceInterface = connectedDevice.toSimpleDeviceInterface() // Listen to bluetooth events deviceInterface?.setListeners( OnMessageReceivedListener { message: String -> onMessageReceived(message) }, OnMessageSentListener { message: String -> onMessageSent(message) }, SimpleBluetoothDeviceInterface.OnErrorListener { error: Throwable -> onError( error ) }) // Let\'s send a message: deviceInterface?.sendMessage(\"Hello world!\") } private fun onMessageSent(message: String) { // We sent a message! Handle it here. Toast.makeText(this, \"Sent a message! Message was: $message\", Toast.LENGTH_LONG) .show() // Replace context with your context instance. } private fun onMessageReceived(message: String) { // We received a message! Handle it here. Toast.makeText(this, \"Received a message! Message was: $message\", Toast.LENGTH_LONG) .show() // Replace context with your context instance. } private fun onError(error: Throwable) { // Handle the error } }我正在使用这个库https://github.com/harry1453/android-bluetooth-serial,我的猜测是我必须调用 TODO 部分中所说的函数,但我不知道如何。
在此先感谢您,感谢您的帮助。