Android 与 RN42 模块的蓝牙连接
Android Bluetooth Connectivity with RN42 Module
我正在尝试将我的应用程序连接到 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 清单中找到了这些
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
我在调用 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
,它不为空。当调用 bluetoothSocket.connect()
时,它会以某种方式变为 null。
这是预期的行为吗?我能做些什么来修复它? RN42 模块工作正常,因为我已经使用 Play 商店中的 RN 蓝牙聊天应用对其进行了测试。
如果有帮助,我在 Nexus 7 上使用 Android 5.1。
Seethis reference guide for the module(第 21 页).
这可能适用于也可能不适用于您的情况,但可能值得一试。当连接到 Android 设备时,他们对模块有特殊建议(分别为默认 UUID 和自定义 UUID)。
改为使用 createInsecureRfcommSocketToServiceRecord。不安全的套接字允许 RFCOMM 与未验证的配对设备通信。 RN42 或 KC2114 等嵌入式设备很难执行经过身份验证的配对,因为需要用户交互(数字比较、是-否响应)。 "Just Works" 自动配对不会产生经过身份验证的配对。 KC2114 支持自动认证配对(使用小技巧)和 Just Works 非认证配对。
我已经设法通过删除我的蓝牙连接代码并以 Android Bluetooth Chat
示例为基础来解决问题。我不知道确切的问题是什么,但蓝牙聊天示例设法解决了它。没有什么明显的突出,所以我最好的猜测是,这是一些微妙的东西。如果您遇到类似的问题并且 RN42 和 Android 之间的连接很不稳定,请创建一个示例蓝牙聊天应用程序并重新使用该蓝牙连接代码。
少了很多头痛! :)
我正在尝试将我的应用程序连接到 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 清单中找到了这些
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
我在调用 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
,它不为空。当调用 bluetoothSocket.connect()
时,它会以某种方式变为 null。
这是预期的行为吗?我能做些什么来修复它? RN42 模块工作正常,因为我已经使用 Play 商店中的 RN 蓝牙聊天应用对其进行了测试。
如果有帮助,我在 Nexus 7 上使用 Android 5.1。
Seethis reference guide for the module(第 21 页).
这可能适用于也可能不适用于您的情况,但可能值得一试。当连接到 Android 设备时,他们对模块有特殊建议(分别为默认 UUID 和自定义 UUID)。
改为使用 createInsecureRfcommSocketToServiceRecord。不安全的套接字允许 RFCOMM 与未验证的配对设备通信。 RN42 或 KC2114 等嵌入式设备很难执行经过身份验证的配对,因为需要用户交互(数字比较、是-否响应)。 "Just Works" 自动配对不会产生经过身份验证的配对。 KC2114 支持自动认证配对(使用小技巧)和 Just Works 非认证配对。
我已经设法通过删除我的蓝牙连接代码并以 Android Bluetooth Chat
示例为基础来解决问题。我不知道确切的问题是什么,但蓝牙聊天示例设法解决了它。没有什么明显的突出,所以我最好的猜测是,这是一些微妙的东西。如果您遇到类似的问题并且 RN42 和 Android 之间的连接很不稳定,请创建一个示例蓝牙聊天应用程序并重新使用该蓝牙连接代码。
少了很多头痛! :)