android - BluetoothDevice.setPin 未连接
问题描述
我似乎无法让我的应用程序连接到蓝牙设备并以编程方式设置 PIN。
我试过不中止广播,但没有帮助。如果我不实现BroadcastReceiver
,它会提示用户输入 PIN 并成功配对。但是,如果我尝试直接设置 PIN,它永远不会起作用。而且我什至尝试将byteArray
字符集指定为 UTF-8,但没有任何效果。我什至尝试过手动设置byteArray
,但仍然没有。
另外,我已经在 Manifest 中声明了所有必要的权限:
- BLUETOOTH_ADMIN
- 蓝牙
- BLUETOOTH_CONNECT
- BLUETOOTH_SCAN
- ACCESS_COARSE_LOCATION
- ACCESS_FINE_LOCATION
- ACCESS_BACKGROUND_LOCATION
我究竟做错了什么?
class MainActivity: AppCompatActivity() {
private var broadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(p0: Context?, p1: Intent?) {
var device: BluetoothDevice? = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)
device?.setPin("1234".toByteArray())
this.abortBroadcast()
}}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.devicelist)
ActivityCompat.requestPermissions(this,arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),1)
registerReceiver(broadcastReceiver, IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST))
findViewById<Button>(R.id.button3).setOnClickListener{
var ble = BluetoothAdapter.getDefaultAdapter()
var dev = ble.getRemoteDevice("00:20:04:32:8A:EE")
dev.createBond()
}
}}
解决方案
推荐阅读
- linux - 在 Bash 中用字符串替换命令
- json - JMeter 中的 POST 给出“无法识别的令牌 'json'”
- python - 如何在 Selenium 中从主页移动到下一页以提取帖子的完整信息
- javascript - 如何在 JavaScript 中将字符串拆分为数组?
- docker - 使用 Skopeo 检查本地图像
- javascript - 从对象数组返回最小值
- powerapps - 在 Powerapps 门户中找不到 Common Data Service 的依赖项
- android - 当我尝试删除 List Element-Android Studio 时应用程序崩溃
- python - Yfinance Pandas 挑选原始或 fmt 值
- mysql - 如何优化循环内的laravel作业