蓝牙断开连接

时间:2014-11-27 08:52:24

标签: android bluetooth

我正在开发一款应用,通过蓝牙将智能手机与评估板配对。我在电话中找到了电路板,然后我将它们配对,但是当这种情况发生时,连接就会丢失。我试图将它们与其他应用程序配对并且效果很好,所以它必须是我的代码中的东西。这些是我的应用程序为了进行连接而遵循的步骤。

MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    /*Gets bluetooth adapter*/
    GlobalVar.mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    /*Resgisters receiver*/
    registerReceiver(bluetoothDeviceSelectedReceiver, new IntentFilter(
            "android.bluetooth.devicepicker.action.DEVICE_SELECTED"));
    /*Calls method to make conection and pair devices*/
    newConection();
}

public void newConection() {
    /*Our device does not support BT*/
    if (GlobalVar.mBluetoothAdapter == null) {
        Toast.makeText(this, R.string.bluetooth_not_available, Toast.LENGTH_LONG).show();
        finish();
        return;
    }
    /*Enables BT if is disabled*/
    if (!GlobalVar.mBluetoothAdapter.isEnabled()) {
        Intent enableBTIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBTIntent, REQUEST_ENABLE_BT);
        return;
    }
    /*If is already enabled, starts session*/
    else {
        GlobalVar.mConnections = new ManageConnection(MainActivity.this, this);
    }
    /*Opens activity to select device*/
    openSelectDevice();
}


/**Starts activity to select device*/
public void openSelectDevice() {
    startActivity(new Intent("android.bluetooth.devicepicker.action.LAUNCH")
    .putExtra("android.bluetooth.devicepicker.extra.NEED_AUTH", false)
    .putExtra("android.bluetooth.devicepicker.extra.FILTER_TYPE", 0)
    .setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS));
}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case REQUEST_ENABLE_BT:
            if (resultCode == RESULT_OK) {
                /*Starts session*/
                GlobalVar.mConnections = new ManageConnection(this, this);
                openSelectDevice();
            }
            break;
    }
}


/**Receiver to make somethign when a device is selected to pair*/
public BroadcastReceiver bluetoothDeviceSelectedReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        /*Gets device*/
        BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        GlobalVar.mConnections.cancelClient();
        GlobalVar.mConnections.startClient(device, MainActivity.this);
    }
};

ManageConnections:

/**Constructor*/
public ManageConnection(Context context, ConnectionListener connectionListener) {
    this.connectionListener = connectionListener;
}

public synchronized void startClient(BluetoothDevice device, ConnectionListener cnListener) {
    /*Cancel any thread attempting to make a connection*/
    if (mState == STATE_CONNECTING) {
        if (GlobalVar.clientConnectionThread != null) {
            GlobalVar.clientConnectionThread.cancel();
            GlobalVar.clientConnectionThread = null;
        }
    }
    /*Cancel any thread currently running a connection*/
    if (GlobalVar.connectedThread != null) {
        GlobalVar.connectedThread.cancel();
        GlobalVar.connectedThread = null;
    }
    /*Start the thread to connect with the given device*/
    GlobalVar.clientConnectionThread = new ClientConnectionThread(device, cnListener);
    GlobalVar.clientConnectionThread.start();
}

ClientConectionThread:

public ClientConnectionThread(BluetoothDevice device, ConnectionListener connectionListener) {
    this.connectionListener = connectionListener;
    BluetoothSocket socketTemp = null;

    try {
        socketTemp = device.createRfcommSocketToServiceRecord(GlobalVar.MY_UUID);
    } catch (IOException e) {
    }
    socket = socketTemp;
}

@Override
public void run() {
    try {
        socket.connect();
        connectionListener.onConnected(socket);
    } catch (IOException e) {
        try {
            socket.close();
        } catch (IOException e1) {}
    }
}

MannageConections:

public synchronized void startConnected(BluetoothSocket socket, Activity activity, ConnectionListener cnListener) {
    /*Cancel the thread that completed the connection*/
    if (GlobalVar.clientConnectionThread != null) {
        GlobalVar.clientConnectionThread.cancel();
        GlobalVar.clientConnectionThread = null;
    }
    /*Cancel any thread currently running a connection*/
    if (GlobalVar.connectedThread != null) {
        GlobalVar.connectedThread.cancel();
        GlobalVar.connectedThread = null;
    }
    /*Cancel the accept thread because we only want to connect to one device*/
    if (GlobalVar.serverConnectionThread != null) {
        GlobalVar.serverConnectionThread.cancel();
        GlobalVar.serverConnectionThread = null;
    }
    /*Start the thread to manage the connection and perform transmissions*/
    GlobalVar.connectedThread = new ConnectedThread(socket, activity, cnListener);
    GlobalVar.connectedThread.start();
}

ConnectedThread:

public ConnectedThread(BluetoothSocket socket, Activity activity, ConnectionListener connectionListener) {
    this.socket = socket;
    this.activity = activity;
    this.connectionListener = connectionListener;

    InputStream inTemp = null;
    OutputStream outTemp = null;

    try {
        inTemp = socket.getInputStream();
        outTemp = socket.getOutputStream();
    } catch (IOException e) {
    }
    inputStream = inTemp;
    outputStream = outTemp;
}

@Override
public void run() {
    byte[] buffer = new byte[1024];
    int readed;

    while (true) {
        try {
            readed = inputStream.read(buffer);  //IN THIS POINT JUMPS TO THE CATCH STATEMENT
            if (readed > 0) {
                final byte[] temp = new byte [readed];
                System.arraycopy(buffer, 0, temp, 0, readed);

                /*Shows on UI*/
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        connectionListener.msgRead(temp);
                    }
                });
            }
        } catch (IOException e) {
            connectionListener.onDisconnected("Error when reading: " +e.getMessage());
            break;
        }
    }
}

所以,在上面的行中指示跳转到catch语句,这就是我在logCat上得到的结果:

11-27 09:49:24.438: W/BluetoothAdapter(5297): getBluetoothService() called with no BluetoothManagerCallback
11-27 09:49:24.438: D/BluetoothSocket(5297): connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[46]}
11-27 09:49:24.458: I/Adreno200-EGL(5297): <qeglDrvAPI_eglInitialize:265>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB_2.5.5.04.02.02.092.059_msm8960_JB_2.5.5_CL3896081_release_AU (CL3896081)
11-27 09:49:24.458: I/Adreno200-EGL(5297): Build Date: 06/25/13 Tue
11-27 09:49:24.458: I/Adreno200-EGL(5297): Local Branch: 
11-27 09:49:24.458: I/Adreno200-EGL(5297): Remote Branch: quic/jb_2.5.5
11-27 09:49:24.458: I/Adreno200-EGL(5297): Local Patches: NONE
11-27 09:49:24.458: I/Adreno200-EGL(5297): Reconstruct Branch: AU_LINUX_ANDROID_JB_2.5.5.04.02.02.092.059 +  NOTHING
11-27 09:49:24.528: D/OpenGLRenderer(5297): Enabling debug mode 0
11-27 09:49:24.658: I/Timeline(5297): Timeline: Activity_idle id: android.os.BinderProxy@41d0d7c8 time:2279177
11-27 09:49:25.769: E/BluetoothSocket(5297): IOException while reading the InputStream
11-27 09:49:25.779: D/onDisconnected(5297): Error when reading: bt socket closed, read return: -1

2 个答案:

答案 0 :(得分:0)

在连接完成之前,您似乎正在尝试read() inputStream

The documentation of getInputStream()告诉它:

  

[...]对该流的操作将抛出IOException,直到关联的套接字连接

仅在ConnectedThread返回后启动socket.connect()

答案 1 :(得分:0)

synchronized (ClientConnectionThread.this) {
    GlobalVar.clientConnectionThread = null;
}