Android的蓝牙:java.io.IOException异常:服务发现失败 [英] Android Bluetooth: java.io.IOException: Service discovery failed

查看:143
本文介绍了Android的蓝牙:java.io.IOException异常:服务发现失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发一个Android应用程序,它的图像传输从一台设备到另一台。接收到的图像将被显示在我的应用程序内的ImageView的。为了实现我的任务,我认为派位图的字节数组。我能拿到第一张图像上ImageView的。但是,当我点击按钮,发送另一个图像的应用程序无法发送该位图。它显示了我的异常产生java.io.IOException:服务fiscovery失败:要成功发送的任何形象,我需要为重新启动我的接收/远程设备上的应用程序即可。任何人都请提出一个解决方案,以每亩问题。在logcat中也被包括在下面。

code建立连接:

 私有类StartConnectionThread继承Thread {
    私人最终的BluetoothSocket的BluetoothSocket;
    私人最终BluetoothDevice类BluetoothDevice类;
    公共StartConnectionThread(BluetoothDevice类设备){
        的BluetoothSocket tempBluetoothSocket = NULL;
        BluetoothDevice类=设备;
        尝试
        {
            的System.out.println(UUID);
            tempBluetoothSocket = device.createRfcommSocketToServiceRecord(UUID);
        }
        赶上(IOException异常IOException异常)
        {

        }
        的BluetoothSocket = tempBluetoothSocket;
    }
    @覆盖
    公共无效的run(){
        // TODO自动生成方法存根
        bluetoothAdapter.cancelDiscovery();
        尝试
        {
            尝试 {
                视频下载(2000);
            }赶上(InterruptedException异常E){
                // TODO自动生成的catch块
                e.printStackTrace();
            }
            bluetoothSocket.connect();

        }
        赶上(IOException异常IOException异常)
        {
            的System.out.println(bluetoothSocketInThread失败);
            尝试
            {
                bluetoothSocket.close();
            }
            赶上(IOException异常cancelIOException)
            {

            }
            返回;
        }
        manageConnectedSocket(的BluetoothSocket);
    }
    公共无效取消()
    {
        尝试
        {
            bluetoothSocket.close();
        }
        赶上(IOException异常IOException异常)
        {

        }
    }
}
 

code接受连接:

 私有类AcceptConnectionThread继承Thread
{
    私人最终BluetoothServerSocket bluetoothServerSocket;
    公共AcceptConnectionThread(){
        // TODO自动生成构造函数存根
        的System.out.println(构造);
        BluetoothServerSocket tempBluetoothServerSocket = NULL;
        尝试
        {
            tempBluetoothServerSocket = bluetoothAdapter.listenUsingRfcommWithServiceRecord(我的纪念品,UUID);
        }
        赶上(IOException异常IOException异常)
        {
        }
        bluetoothServerSocket = tempBluetoothServerSocket;
    }
    @覆盖
    公共无效的run(){
        // TODO自动生成方法存根
        的BluetoothSocket的BluetoothSocket = NULL;
        而(真)
        {
            尝试
            {
                尝试 {
                    视频下载(2000);
                }赶上(InterruptedException异常E){
                    // TODO自动生成的catch块
                    e.printStackTrace();
                }
                的System.out.println(bluetoothServerSocket);
                如果(bluetoothServerSocket!= NULL)
                {
                    的BluetoothSocket = bluetoothServerSocket.accept();
                }
                的System.out.println(接受);
            }
            赶上(IOException异常IOException异常){
                打破;
            }
            如果(的BluetoothSocket!= NULL)
            {
                manageConnectedSocket(的BluetoothSocket);
                尝试 {
                    bluetoothServerSocket.close();
                }赶上(IOException异常E){
                    // TODO自动生成的catch块
                    e.printStackTrace();
                }
                打破;
            }
        }
    }
    公共无效取消()
    {
        尝试{
            bluetoothServerSocket.close();
        }
        赶上(IOException异常IOException异常){

        }
    }

}
 

code来管理连接:

 私有类ManageConnectedDevicesThread继承Thread
{
    私人最终的BluetoothSocket connectedBluetoothSocket;
    公共ManageConnectedDevicesThread(的BluetoothSocket插座){
        // TODO自动生成构造函数存根
        connectedBluetoothSocket =插座;
        InputStream的tempInputStream = NULL;
        OutputStream的tempOutputStream = NULL;
        尝试
        {
            tempInputStream = socket.getInputStream();
            tempOutputStream = socket.getOutputStream();
        }
        赶上(IOException异常IOException异常)
        {

        }
        为InputStream = tempInputStream;
        的OutputStream = tempOutputStream;
    }
    @覆盖
    公共无效的run(){
        // TODO自动生成方法存根
                    byte []的缓冲区=新的字节[1024 * 8]。
        诠释字节;
        而(真)
        {
            尝试
            {
                字节= inputStream.read(缓冲区);
                handler.obtainMessage(MESSAGE_READ,字节,-1​​,缓冲液).sendToTarget();
                的System.out.println(经理人);
            }
            赶上(IOException异常IOException异常)
            {
                的System.out.println(为处理程序:+ IOException异常);
                打破;
            }
        }
    }
    公共无效写入(字节[]字节)
    {
        尝试
        {
            outputStream.write(字节);
        }
        赶上(IOException异常IOException异常){
            的System.out.println(例外管理的连接线制tatement);
        }

    }


    公共无效的close()
    {
        尝试 {
            connectedBluetoothSocket.close();
        }赶上(IOException异常E){
            // TODO:处理异常
        }
    }
}
 

code重置连接:

 无效resetConnection()
    {
        如果(的InputStream!= NULL)
        {
            尝试 {
                inputStream.close();
            }赶上(IOException异常E){
                // TODO自动生成的catch块
                e.printStackTrace();
                        }
        }
        如果(OutputStream的!= NULL)
        {
            尝试 {
                outputStream.close();
            }赶上(IOException异常E){
                // TODO自动生成的catch块
                e.printStackTrace();
            }
        }
        如果(startConnectionThread!= NULL)
        {
            的System.out.println(开始瓦剌积极THA);
            startConnectionThread.cancel();
        }
        如果(acceptConnectionThread!= NULL)
        {
            的System.out.println(接受瓦剌积极THA);
            acceptConnectionThread.cancel();
        }
        如果(manageConnectedDevicesThread!= NULL)
        {
            的System.out.println(管理瓦剌积极THA);
            manageConnectedDevicesThread.close();
        }
    }

}
 

code的处理程序如下:

 私人最终处理程序处理程序=新的处理程序(){
公共无效的handleMessage(信息MSG){
    开关(msg.what){
    案例MESSAGE_READ:
        的System.out.println(MESSAGE_READ);
        字节[]的ReadBuf =(字节[])msg.obj;
        //从缓冲区中的有效字节构造字符串
        字符串readMessage =新的String(的ReadBuf,0,msg.arg1);
        byte []的B = readMessage.getBytes();
        位图bitmap1 = BitmapFactory.de codeByteArray(的ReadBuf,0,readBuf.length);
        imageView.setImageBitmap(bitmap1);
        打破;
    }
 

};

在logcat中显示以下内容:

  01-25 14:49:31.800:D / dalvikvm(9451):调试器分离;对象注册了1项
    01-25 14:49:38.380:V / BluetoothSocket.cpp(9451):initSocketNative
    01-25 14:49:38.380:V / BluetoothSocket.cpp(9451):...的fd 40创建(RFCOMM,LM = 26)
    01-25 14:49:38.380:V / BluetoothSocket.cpp(9451):initSocketFromFdNative
    01-25 14:49:40.420:D / BluetoothUtils(9451):isSocketAllowedBySecurityPolicy启动:设备空
    01-25 14:49:41.680:我/的System.out(9451):bluetoothSocketInThread失败
    01-25 14:49:41.680:V / BluetoothSocket.cpp(9451):abortNative
    01-25 14:49:41.680:V / BluetoothSocket.cpp(9451):... asocket_abort(40)完成
    01-25 14:49:41.680:V / BluetoothSocket.cpp(9451):destroyNative
    01-25 14:49:41.680:V / BluetoothSocket.cpp(9451):... asocket_destroy(40)完成
 

在此先感谢。

解决方案

也许你可以尝试添加了Thread.Sleep一秒钟?请参见这个讨论

  

我已经能够解决这个问题的唯一方法是通过添加   视频下载为关闭连接前一秒钟。

也看到的这个线程

  

我能够得到这个只运行分离的呼叫后,   findBT(); openBT();

     

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

     

但如果我把findBT()中的onCreate(),只是使用的按钮   openBT();它工作正常。

     

或者,如果我做了第二个按钮,每一个,它工作正常。

     

建议?

这是第二条评论摘录:

 设置pairedDevices = mBluetoothAdapter.getBondedDevices();
mmDevice = mBluetoothAdapter.getRemoteDevice(00:06:66:46:5A:91);
如果(pairedDevices.contains(mmDevice))
{
statusText.setText(蓝牙设备发现,地址:+ mmDevice.getAddress());
Log.d(ArduinoBT,BT配对);
}
 

     

在我进入我的蓝牙设备的地址。原来的code   找到设备,并返回的正确地址,但   mmSocket.connect();产生异常java.io.IOException异常:   服务发现失败

     

建议?

I'm trying to develop an Android application which transfers images from one device to another. The received image would then be shown on the ImageView inside my application. To achieve my task, I thought to send a byte array of the bitmap. I'm able to get the first image on the imageview. But, as soon as I click on the button to send another image the application fails to send the bitmap. It shows me an exception "java.io.IOException: Service fiscovery failed." To send any image successfully I need to restart my application on the receiving/remote device. Can anyone please suggest a solution to mu problem. The logcat has also been included below.

Code to establish the connection:

        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)
        {

        }
    }
}

Code to accept the connection:

        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){

        }
    }

}

Code to manage the connection:

        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
        }
    }
}

Code to reset the connection:

            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();
        }
    }

}

code for handler is shown below:

    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;
    }

};

The logcat shows the following:

    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

Thanks in advance.

解决方案

Maybe you can try adding thread.sleep for a second? See this discussion:

"The only way I've been able to fix the problem is by adding a thread.sleep for a second before closing the connection."

also see dan's two consecutive comments on this thread:

"I was able to get this to run only after separating the calls to findBT(); openBT();

Otherwise, mmSocket.connect(); throws an exception, "Service discovery failed"

but if I put findBT() in onCreate() and just use the button for openBT(); it works fine.

Or, if I make a second button, one for each, it works fine.

Suggestions?"

the excerpts from the second comment:

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");
}

where I entered the address of my Bluetooth device. The original code finds the device and returns the correct address, but mmSocket.connect(); generates an exception "java.io.IOException: Service discovery failed"

Suggestions?

这篇关于Android的蓝牙:java.io.IOException异常:服务发现失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆