JM:I have a dream,期待和你一起去实现!
原文及译文
JM
网友:Mauricio Pastorini

        上下文是以下内容,我有一个Android应用程序,在特定设备连接到它时启动一个活动,并发送代码消息。该设备不是另一个Android应用程序,在这种情况下,问题会更容易。该设备是一个物联网设备...

推荐:想去Google查资料?(你懂的)想建站?想测试?想挂机?想折腾?想要高性价比的服务器?点我!!


如何在Andoid中的蓝牙BroadcastReceiver内创建蓝牙服务器套接字连接?(How to create a Bluetooth Server Socket Connection inside a bluetooth BroadcastReceiver in Andoid?)

上下文是以下内容,我有一个Android应用程序,在特定设备连接到它时启动一个活动,并发送代码消息。该设备不是另一个Android应用程序,在这种情况下,问题会更容易。该设备是物联网设备HC05。所以这个设备是在主模式下,它试图连接在这种情况下,从属的Android应用程序。简而言之,Android应用程序必须是从服务器作为服务器,因此它可以接收连接,而其他设备是主模式,而且android应用程序必须在广播接收器中检测设备,所以我不能直接使用BluetoothServerSocket与accept()方法,因为我不能运行一个线程。

我得到的最远是实现这个功能两次连接和断开蓝牙设备。所以,我的应用程序第一次启动广播接收器并检测BluetoothDevice,并在serverSocket.accept()中阻塞。然后,当我断开连接并再次连接蓝牙设备的第二次,我的应用程序成功返回一个连接的套接字,我可以tramitmit数据...是的!

这是我有的代码

public class BluetoothConnectionReceiver extends BroadcastReceiver {
    private ConnectedThread mConnectedThread;
    private BluetoothSocket btSocket = null;

    public BluetoothConnectionReceiver(){}

    @Override
    public void onReceive(Context context, Intent intent) {
        //First time I connect the bluetooth device, this method is launched
        String action = intent.getAction();
        switch (action){
            case BluetoothDevice.ACTION_ACL_CONNECTED:
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
                BluetoothSocket socket = null;
                try{
                    BluetoothServerSocket serverSocket = adapter.listenUsingInsecureRfcommWithServiceRecord("MyApp", MY_UUID_SECURE);
                    socket = serverSocket.accept(); //The first launch blocks 

                    //The second time, when I disconnect and connect again the device, the code continues properly and I can correctly transmit the data
                    serverSocket.close();
                    try{
                       mConnectedThread = new ConnectedThread(socket);
                       mConnectedThread.start();
                    }catch (IOException ex){
                       ex.printStackTrace();
                     }
               } catch (IOException ex){
                   ex.printStackTrace();
               }

public class ConnectedThread extends Thread {
    private InputStream mmInStream;
    private OutputStream mmOutStream;
    private BluetoothSocket mSocket;

    public ConnectedThread(BluetoothSocket socket) throws IOException{
        InputStream tmpIn = null;
        OutputStream tmpOut = null;

        tmpIn = socket.getInputStream();
        tmpOut = socket.getOutputStream();

        mmInStream = tmpIn;
        mmOutStream = tmpOut;
        mSocket = socket;
    }

    // Other methods to receive and trasnmit data with the connected socket-
 }

事情是,我需要这样做只有一个连接从设备,而无需连接和断开设备。就像从设备上创建一个连接的套接字(就像BluetoothServerSocket与accept()方法一样),但是使用广播接收器给我的BluetoothDevice。

作为补充,我没有问题做独立开始与应用程序与设备设置在从属模式的连接

BluetoothSocket socket = device.createInsecureRfcommSocketToServiceRecord(BTMODULEUUID);
socket.connect();

但是,当我使用BroadcastReceiver提供的对象设备时,这并不适用于我。它抛出IOException:套接字连接读取失败的套接字可能关闭或超时也许是因为设备处于主模式。

我希望有人能够帮助我,因为我无法找到任何东西。


The context is the following, I have an Android App that launch an activity when a specific device connects to it, and send a code message. The device is not another android app, in such case the problem would be much easier. The device is an IoT device, the HC05. So this device is in master mode and it tries to connect with the android app that in this case is slave. In brief, the android app must be the slave as a server so it can receive connections, and the other device is in master mode, also, the android app has to detect the device in a broadcast receiver, so I cant use directly the BluetoothServerSocket with the accept() method as I cant run a thread.

The farthest I get is to implement this feature connecting and disconnecting the bluetooth device twice. So, the first time my app launch the broadcast receiver and detect the BluetoothDevice, and it blocks in serverSocket.accept(). Then, the second time when I disconnect and connect again the bluetooth device, my app success returning a connected socket which I can trasmit data... Yeah!

Here is the code that I have

public class BluetoothConnectionReceiver extends BroadcastReceiver {
    private ConnectedThread mConnectedThread;
    private BluetoothSocket btSocket = null;

    public BluetoothConnectionReceiver(){}

    @Override
    public void onReceive(Context context, Intent intent) {
        //First time I connect the bluetooth device, this method is launched
        String action = intent.getAction();
        switch (action){
            case BluetoothDevice.ACTION_ACL_CONNECTED:
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
                BluetoothSocket socket = null;
                try{
                    BluetoothServerSocket serverSocket = adapter.listenUsingInsecureRfcommWithServiceRecord("MyApp", MY_UUID_SECURE);
                    socket = serverSocket.accept(); //The first launch blocks 

                    //The second time, when I disconnect and connect again the device, the code continues properly and I can correctly transmit the data
                    serverSocket.close();
                    try{
                       mConnectedThread = new ConnectedThread(socket);
                       mConnectedThread.start();
                    }catch (IOException ex){
                       ex.printStackTrace();
                     }
               } catch (IOException ex){
                   ex.printStackTrace();
               }

public class ConnectedThread extends Thread {
    private InputStream mmInStream;
    private OutputStream mmOutStream;
    private BluetoothSocket mSocket;

    public ConnectedThread(BluetoothSocket socket) throws IOException{
        InputStream tmpIn = null;
        OutputStream tmpOut = null;

        tmpIn = socket.getInputStream();
        tmpOut = socket.getOutputStream();

        mmInStream = tmpIn;
        mmOutStream = tmpOut;
        mSocket = socket;
    }

    // Other methods to receive and trasnmit data with the connected socket-
 }

The thing is that I need to do this with only one connection from the device, without connecting and disconnecting the device. Something like creating a connected socket from the device (as the device does with the IOException: socket CONNECT read failed socket might closed or timeout method) but with the {mark2} that the broadcast receiver gives me.

As an addition, I had no problem doing independently starting the connection with the app with the device set in slave mode

BluetoothSocket socket = device.createInsecureRfcommSocketToServiceRecord(BTMODULEUUID);
socket.connect();

But this doesn't work for me when I use de object {mark2} that the BroadcastReceiver gave me. It throws {mark2} Maybe because the device is in master mode.

I hope someone could help me with this context that I cant find it any ware.


解决方案

目前暂无解决方案,可以加群交流,有解决方案后,第一时间通知你哦!或者看看下面的相关问题吧~

推荐:想去Google查资料?(你懂的)想建站?想测试?想挂机?想折腾?想要高性价比的服务器?点我!!


关于站长

JMJavaMethod的缩写,苦逼码农一个,一直想有番作为,奈何人老力衰,只能四处膜拜大佬以获得动力。已经从单机、局域网、互联网、混到了移动互联网,未来希望能在AI世界里继续混下去。这辈子有个终极目标:财务自由,心灵自由。

近期公告

一个人苦逼开发多日,网站终于正式上线,求关注!!! 一个人苦逼多日,终于正式上线,求关注!!!

找他  
猜你喜欢
想建站?想测试?
想挂机?想折腾?
想去Google查资料?(你懂的)
想要高性价比的服务器?
搬瓦工VPS - 性价最高的美国便宜VPS主机
友情链接

冀ICP备17016304号 版权所有 © JavaMethod.com All Rights Reserved,Theme by 拼图