我正在尝试将我的应用程序连接到RN42模块.

// Create a socket based on the application ID with a paired device

// Fetch the published UUIDs from the mbed and use the first one

bluetoothSocket = connectedDevice.createRfcommSocketToServiceRecord(connectedDevice.getUuids()[0].getUuid());

// Connect to the device

if (!bluetoothSocket.isConnected())

bluetoothSocket.connect();

// Create the input and output streams for sending/receiving messages

socketInput = bluetoothSocket.getInputStream();

socketOutput = bluetoothSocket.getOutputStream();

我在Android清单中有这些

调用bluetoothSocket.connect()时出现此错误.

Attempt to invoke virtual method 'void android.bluetooth.BluetoothSocket.connect()' on a null object reference

拨打此行后

bluetoothSocket = connectedDevice.createRfcommSocketToServiceRecord(connectedDevice.getUuids()[0].getUuid());

我已经使用Android Studio检查了变量bluetoothSocket,但它不是null.当调用bluetoothSocket.connect()时,它以某种方式变为null.

那是预期的行为吗?我该如何解决?我已经在Play商店中使用RN蓝牙聊天应用程序对其进行了测试,因此RN42模块可以正常工作.

如果有帮助,我使用的是Nexus 7上的Android 5.1.

解决方法:

我设法通过删除我的蓝牙连接代码,而以Android蓝牙聊天示例为基础来解决问题.我不知道确切的问题是什么,但“蓝牙聊天”示例设法解决了该问题.没有什么显而易见的突出之处,所以我最好的猜测是,这有些微妙.如果您遇到类似的问题,并且RN42与Android之间的连接很不稳定,请创建示例蓝牙聊天应用程序,然后重新使用该蓝牙连接代码.

少很多头痛!

标签:embedded,bluetooth,android

来源: https://codeday.me/bug/20191120/2044179.html

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐