JSR82连接:未创建与蓝牙套接字的连接(失败或中止)

时间:2014-08-14 09:15:55

标签: android android-bluetooth jsr82

因此,有很多与此问题相关的SO帖子 -

其他帖子 -

尝试从反射到没有反思的所有事情,但没有效果 -

if(!mDeviceAddress.equals("") && BluetoothAdapter.checkBluetoothAddress(mDeviceAddress))
{
    Log.i(TAG, "Remote Device Name  "+mDeviceName);

    bdDevice = mBluetoothAdapter.getRemoteDevice(mDeviceAddress);
    getConnected(bdDevice);
}


@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public void getConnected(BluetoothDevice bdDevice)
{           
    if(bdDevice == null)
    {
        setSetting("STATUS", "Disconnected");
        Toast.makeText(getActivity(), 
        "Unable to get Remote Device!", Toast.LENGTH_SHORT).show();
        return;
    }
    else
    {
        Log.i(TAG, "Connecting Address--"+ bdDevice.getAddress());

        boolean isConnected = createInsecureRfcommSocket(bdDevice, 1);          

        if(!isConnected) 
        {
            for(int i=2;i<4;i++)
            {  
                if(!isConnected) 
                    isConnected = createInsecureRfcommSocket(bdDevice, i); 
                else
                    break;
            }
        }

    if(isConnected)
    {
        Log.i(TAG, "Connected Socket");
        setSetting("STATUS", "Connected");

        mConnectedThread = new ConnectedThread(socket);
        mConnectedThread.start();
        timeSyncCommand();
        mConnectedThread.writeByte(runCommand);

        startTime = System.currentTimeMillis();
    }
    else
    {
         try
         {
             socket = bdDevice.
             createInsecureRfcommSocketToServiceRecord(
             UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
         }   
         catch(IOException io)
         {
            Toast.makeText(getActivity(), "Socket Create -" 
            + io.toString() , Toast.LENGTH_SHORT).show();
         }
         try
         {
            mBluetoothAdapter.cancelDiscovery();
            socket.connect();
         }
         catch(IOException io)
         {
            Log.i(TAG, "Socket Connect -"+io.toString());
         }

         if(socket.isConnected())
         {
            Log.i(TAG, "Connected Socket");
            setSetting("STATUS", "Connected");

            mConnectedThread = new ConnectedThread(socket);
            mConnectedThread.start();
            timeSyncCommand();
            mConnectedThread.writeByte(runCommand);

            startTime = System.currentTimeMillis();
         }
         else
         {
            Log.i(TAG, "Disconnected Socket");
            setSetting("STATUS", "Disconnected");
         }

       }    
     }
    }


    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public boolean createInsecureRfcommSocket(BluetoothDevice bdDevice, int i)
     {
         try 
        {
            Log.i(TAG,
            "Creating RFCOMM socket using reflection with Object "+i);
            //socket = bdDevice.createRfcommSocketToServiceRecord(my_UUID);
            Method m = bdDevice.getClass().
            getMethod("createInsecureRfcommSocket", new Class[] {int.class});
            socket = (BluetoothSocket) m.invoke(bdDevice, i);
            mBluetoothAdapter.cancelDiscovery();

            Log.i(TAG,"Attempt to connect to a remote device");
            socket.connect();
        }
        catch(IOException e)
        {
            setSetting("STATUS", "Disconnected");
            Log.i(TAG,"Exception raised "+e.getMessage());

            try 
            {
                socket.close();
                Log.i(TAG,
                "Cannot connect with address "+bdDevice.getAddress());
                e.printStackTrace();
            } 
            catch (IOException e1) 
            {
                Log.i(TAG,"Socket not closed");
                e1.printStackTrace();
            }   
        }
        catch (NoSuchMethodException e1) 
        {
            Log.i(TAG,"NoSuchMethodException");

            e1.printStackTrace();
        }
        catch (InvocationTargetException e2) 
        {
            Log.i(TAG,"InvocationTargetException");

            e2.printStackTrace();
        }
        catch (IllegalAccessException e3) 
        {
            Log.i(TAG,"IllegalAccessException");

            e3.printStackTrace();
        }
        catch (NullPointerException e4) 
        {
            Log.i(TAG,"NullPointerException");

            e4.printStackTrace();
        }
    }

如果您仔细查看上述代码,则会注意到 -

1) 尝试使用端口1进行反射

2) 如果1失败,则尝试用端口2进行反射

3) 如果2失败,则尝试用端口3进行反射

4) 如果3失败则尝试不带反射

简而言之,使用了一切,但都没有用。

我的例外追踪 -

Remote Device Name  RN-IAP-E281
Connecting Address--00:06:68:4D:E2:81
Creating RFCOMM socket using reflection with Object 1
Attempt to connect to a remote device
Exception raised [JSR82] connect: Connection is not created (failed or aborted).
Cannot connect with address 00:06:68:4D:E2:81
 Creating RFCOMM socket using reflection with Object 2
 Attempt to connect to a remote device
Exception raised [JSR82] connect: Connection is not created (failed or aborted).
Cannot connect with address 00:06:68:4D:E2:81
Creating RFCOMM socket using reflection with Object 3
Attempt to connect to a remote device
Exception raised [JSR82] connect: Connection is not created (failed or aborted).
Cannot connect with address 00:06:68:4D:E2:81
Disconnected Socket
------onReceive BroadcastReceiver------
Received Bluetooth Disconnected Request
------Returned from broadcast after disconnect------

任何帮助将不胜感激!

0 个答案:

没有答案