【问题标题】:RxBluetooth unable to establish connection with deviceRxBluetooth 无法与设备建立连接
【发布时间】:2018-11-13 02:08:20
【问题描述】:

我想使用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"/>

谢谢。

【问题讨论】:

  • FWIW, this sample app 使用 RxBluetooth 在两个设备之间传输文本。

标签: android bluetooth rx-java2 rxbluetooth


【解决方案1】:

问题解决了。 我忘了observeBluetoothSocket()

        mRxBluetooth
            .observeBluetoothSocket("MyConnection", UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"))
            .subscribeOn(mSchedulerProvider.io())
            .observeOn(mSchedulerProvider.ui())
            .subscribe(this::observeIncomingMessages,
                    throwable -> {/*Handle error*/});

Commonsguy's RxEchoCommonsWare 评论中建议的示例应用程序)帮助我意识到了这一事实。 RxEcho 还帮助我发现了 RxJava2 Extras by David Moten 这使得从套接字读取数据非常方便:

        Strings.from(btSocket.getInputStream())
           .subscribeOn(mSchedulerProvider.io())
           .observeOn(mSchedulerProvider.ui())
           .subscribe(data -> {/* Post data to LiveData*/},
                   throwable -> {/* Handle error*/});

感谢您抽出宝贵时间阅读我的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    • 2013-04-12
    • 2018-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多