android rn框架 蓝牙,带有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清单中有这些

调用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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值