【发布时间】:2015-06-25 17:26:01
【问题描述】:
我正在尝试将我的应用连接到 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 Manifest 中有这些
<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() 时,它会以某种方式变为空。
这是预期的行为吗?我能做些什么来修复它? RN42 模块工作正常,因为我在 Play 商店中使用 RN 蓝牙聊天应用对其进行了测试。
如果有帮助,我在 Nexus 7 上使用 Android 5.1。
【问题讨论】:
-
我也面临类似的问题。所以请你能提供我的详细信息。你的问题怎么了?这个问题解决了吗?请提供示例代码。
标签: android bluetooth embedded