我想使用RxBluetooth在两个Android设备之间传输一些文字。 我能够扫描并查看附近的设备,但在尝试连接其中一个设备时,我面临以下故障:
D / BluetoothUtils: isSocketAllowedBySecurityPolicy start:device null
W / BluetoothAdapter: getBluetoothService()调用,没有BluetoothManagerCallback
D / BluetoothSocket: connect(),SocketState:INIT,mPfd:{ParcelFileDescriptor:FileDescriptor [65]}
java.io.IOException:读取失败,socket可能关闭或超时,读取ret:-1
这是我为了与选定的BluetoothDevice
:
mRxBluetooth.observeConnectDevice(bluetoothDevice, UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"))
.subscribeOn(mSchedulerProvider.io())
.observeOn(mSchedulerProvider.ui())
.subscribe(new Consumer<BluetoothSocket>() {
@Override
public void accept(BluetoothSocket bluetoothSocket) throws Exception {
// Unable to reach here.
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
// I reach this point with the message:
// java.io.IOException: read failed, socket might closed or timeout, read ret: -1
}
})
两台设备都启用了蓝牙功能,因为我可以从另一台设备上发现蓝牙。
我使用的权限是:
<uses-feature
android:name="android.hardware.bluetooth_le"
android:required="true"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
谢谢。
答案 0 :(得分:0)
问题解决了。
我忘了observeBluetoothSocket()
:
mRxBluetooth
.observeBluetoothSocket("MyConnection", UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"))
.subscribeOn(mSchedulerProvider.io())
.observeOn(mSchedulerProvider.ui())
.subscribe(this::observeIncomingMessages,
throwable -> {/*Handle error*/});
Commonsguy的RxEcho( CommonsWare 评论中建议的示例应用)帮助我意识到了这一点。 RxEcho还帮我发现RxJava2 Extras David Moten,这使得从套接字读取数据变得非常方便:
Strings.from(btSocket.getInputStream())
.subscribeOn(mSchedulerProvider.io())
.observeOn(mSchedulerProvider.ui())
.subscribe(data -> {/* Post data to LiveData*/},
throwable -> {/* Handle error*/});
感谢您抽出宝贵时间阅读我的问题。