【问题标题】:Android Bluetooth: java.io.IOException: Service discovery failedAndroid蓝牙:java.io.IOException:服务发现失败
【发布时间】:2014-02-16 09:33:52
【问题描述】:

我正在尝试开发一个将图像从一台设备传输到另一台设备的 Android 应用程序。然后,接收到的图像将显示在我的应用程序内的 ImageView 上。为了完成我的任务,我想发送一个位图的字节数组。我能够在 imageview 上获得第一张图像。但是,只要我单击按钮发送另一个图像,应用程序就无法发送位图。它向我显示了一个异常“java.io.IOException: Service fiscovery failed.”要成功发送任何图像,我需要在接收/远程设备上重新启动我的应用程序。可以任何人请提出解决 mu 问题的方法。 logcat 也包含在下面。

建立连接的代码:

        private class StartConnectionThread extends Thread{
    private final BluetoothSocket bluetoothSocket;
    private final BluetoothDevice bluetoothDevice;
    public StartConnectionThread(BluetoothDevice device){
        BluetoothSocket tempBluetoothSocket=null;
        bluetoothDevice=device;
        try
        {
            System.out.println(uuid);
            tempBluetoothSocket=device.createRfcommSocketToServiceRecord(uuid);
        }
        catch(IOException ioException)
        {

        }
        bluetoothSocket=tempBluetoothSocket;
    }
    @Override
    public void run() {
        // TODO Auto-generated method stub
        bluetoothAdapter.cancelDiscovery();
        try
        {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            bluetoothSocket.connect();

        }
        catch(IOException ioException)
        {
            System.out.println("bluetoothSocketInThread failed");
            try
            {
                bluetoothSocket.close();
            }
            catch(IOException cancelIOException)
            {

            }
            return;
        }
        manageConnectedSocket(bluetoothSocket);
    }
    public void cancel()
    {
        try
        {
            bluetoothSocket.close();
        }
        catch(IOException ioException)
        {

        }
    }
}

接受连接的代码:

        private class AcceptConnectionThread extends Thread
{
    private final BluetoothServerSocket bluetoothServerSocket;
    public AcceptConnectionThread() {
        // TODO Auto-generated constructor stub
        System.out.println("constructor");
        BluetoothServerSocket tempBluetoothServerSocket=null;
        try
        {
            tempBluetoothServerSocket=bluetoothAdapter.listenUsingRfcommWithServiceRecord("My Souvenirs", uuid);
        }
        catch(IOException ioException)
        {
        }
        bluetoothServerSocket=tempBluetoothServerSocket;
    }
    @Override
    public void run() {
        // TODO Auto-generated method stub
        BluetoothSocket bluetoothSocket=null;
        while(true)
        {
            try
            {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println(bluetoothServerSocket);
                if(bluetoothServerSocket!=null)
                {
                    bluetoothSocket=bluetoothServerSocket.accept();
                }
                System.out.println("accept");
            }
            catch(IOException ioException){
                break;
            }
            if(bluetoothSocket!=null)
            {
                manageConnectedSocket(bluetoothSocket);
                try {
                    bluetoothServerSocket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                break;
            }
        }
    }
    public void cancel()
    {
        try{
            bluetoothServerSocket.close();
        }
        catch(IOException ioException){

        }
    }

}

管理连接的代码:

        private class ManageConnectedDevicesThread extends Thread
{
    private final BluetoothSocket connectedBluetoothSocket;
    public ManageConnectedDevicesThread(BluetoothSocket socket) {
        // TODO Auto-generated constructor stub
        connectedBluetoothSocket=socket;
        InputStream tempInputStream=null;
        OutputStream tempOutputStream=null;
        try
        {
            tempInputStream=socket.getInputStream();
            tempOutputStream=socket.getOutputStream();
        }
        catch(IOException ioException)
        {

        }
        inputStream=tempInputStream;
        outputStream=tempOutputStream;
    }
    @Override
    public void run() {
        // TODO Auto-generated method stub
                    byte[] buffer=new byte[1024*8];
        int bytes;
        while(true)
        {
            try
            {
                bytes=inputStream.read(buffer);
                handler.obtainMessage(MESSAGE_READ,bytes,-1,buffer).sendToTarget();
                System.out.println("handler");
            }
            catch(IOException ioException)
            {
                System.out.println("for handler:" +ioException);
                break;
            }
        }
    }
    public void write(byte[] bytes)
    {
        try
        {
            outputStream.write(bytes);
        }
        catch(IOException ioException){
            System.out.println("exception in wrie tatement of managing connections");
        }

    }


    public void close()
    {
        try {
            connectedBluetoothSocket.close();
        } catch (IOException e) {
            // TODO: handle exception
        }
    }
}

重置连接的代码:

            void resetConnection()  
    {
        if(inputStream!=null)
        {
            try {
                inputStream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                        }
        }
        if(outputStream!=null)
        {
            try {
                outputStream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(startConnectionThread!=null)
        {
            System.out.println("start wala active tha");
            startConnectionThread.cancel();
        }
        if(acceptConnectionThread!=null)    
        {
            System.out.println("accept wala active tha");
            acceptConnectionThread.cancel();
        }
        if(manageConnectedDevicesThread!=null)
        {
            System.out.println("manage wala active tha");
            manageConnectedDevicesThread.close();
        }
    }

}

处理程序代码如下所示:

    private final Handler handler=new Handler(){
public void handleMessage(Message msg) {
    switch (msg.what) {
    case MESSAGE_READ:
        System.out.println("MESSAGE_READ");
        byte[] readBuf = (byte[]) msg.obj;
        // construct a string from the valid bytes in the buffer
        String readMessage = new String(readBuf, 0, msg.arg1);
        byte[] b=readMessage.getBytes();
        Bitmap bitmap1=BitmapFactory.decodeByteArray(readBuf, 0, readBuf.length);
        imageView.setImageBitmap(bitmap1);
        break;
    }

};

logcat 显示如下:

    01-25 14:49:31.800: D/dalvikvm(9451): Debugger has detached; object registry had 1 entries
    01-25 14:49:38.380: V/BluetoothSocket.cpp(9451): initSocketNative
    01-25 14:49:38.380: V/BluetoothSocket.cpp(9451): ...fd 40 created (RFCOMM, lm = 26)
    01-25 14:49:38.380: V/BluetoothSocket.cpp(9451): initSocketFromFdNative
    01-25 14:49:40.420: D/BluetoothUtils(9451): isSocketAllowedBySecurityPolicy start : device null
    01-25 14:49:41.680: I/System.out(9451): bluetoothSocketInThread failed
    01-25 14:49:41.680: V/BluetoothSocket.cpp(9451): abortNative
    01-25 14:49:41.680: V/BluetoothSocket.cpp(9451): ...asocket_abort(40) complete
    01-25 14:49:41.680: V/BluetoothSocket.cpp(9451): destroyNative
    01-25 14:49:41.680: V/BluetoothSocket.cpp(9451): ...asocket_destroy(40) complete

提前致谢。

【问题讨论】:

  • 你能告诉我如何解决这个问题吗?我好绝望……
  • 抱歉回复晚了。由于服务连接需要几毫秒来执行,只需等待大约一秒钟左右,然后运行您的代码。然后系统会检测到连接的服务。
  • 谢谢你。我会试试的。

标签: android bluetooth inputstream serversocket outputstream


【解决方案1】:

也许您可以尝试添加 thread.sleep 一会儿?见this discussion

“我能够解决问题的唯一方法是添加一个 thread.sleep 在关闭连接之前等待一秒钟。”

还可以在this thread 上查看 dan 的两个连续 cmets:

“只有在将调用分开后,我才能让它运行 查找BT();打开BT();

否则,mmSocket.connect();抛出异常,“服务发现 失败”

但如果我将 findBT() 放入 onCreate() 并使用按钮 打开BT();它工作正常。

或者,如果我制作第二个按钮,每个按钮一个,它就可以正常工作。

建议?”

第二条评论的摘录:

Set pairedDevices = mBluetoothAdapter.getBondedDevices();
mmDevice = mBluetoothAdapter.getRemoteDevice(“00:06:66:46:5A:91″);
if (pairedDevices.contains(mmDevice))
{
statusText.setText(“Bluetooth Device Found, address: ” + mmDevice.getAddress() );
Log.d(“ArduinoBT”, “BT is paired”);
}

我在哪里输入了我的蓝牙设备的地址。原始代码 找到设备并返回正确的地址,但是 mmSocket.connect();生成异常“java.io.IOException: 服务发现失败”

建议?

【讨论】:

  • 谢谢,它解决了我的问题。现在我在同一个应用程序中面临另一个问题。我下一个问题的链接如下:stackoverflow.com/questions/21568601/… 希望 ypu 也能帮助我解决这个问题。再次感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-18
  • 1970-01-01
  • 1970-01-01
  • 2012-01-20
  • 2011-03-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多