首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >RxBluetooth无法与设备建立连接

RxBluetooth无法与设备建立连接
EN

Stack Overflow用户
提问于 2018-06-04 00:48:09
回答 1查看 288关注 0票数 0

我想使用RxBluetooth在两个安卓设备之间传输一些文本。我可以扫描并查看附近的设备,但在尝试连接其中一个设备时,我遇到了以下故障:

D/ device实用程序: isSocketAllowedBySecurityPolicy start :device null

W/BluetoothAdapter:在没有BluetoothManagerCallback的情况下调用getBluetoothService()

D/蓝牙connect: connect(),SocketState:初始化,mPfd:{ParcelFileDescriptor: FileDescriptor65}

java.io.IOException:读取失败,套接字可能关闭或超时,读取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"/>

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-04 05:46:01

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

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

CommonsguyRxEcho (CommonsWare的评论中推荐的示例应用程序)帮助我认识到了这一点,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*/});

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50668724

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档