0

closeBTBluetoothシリアルデバイスに接続するための簡単なAndroidアプリケーションを作成しました。Androidが接続されていない場合は、クラッシュのためにデバイスが範囲外にある可能性があることを追加したいと思います。

どうすればいいですか?このコードは正しいですか?

protected void onStart() {
    super.onStart();
    findBT(); //Check if bluettoth enable and paired devices

    try {
        openBT(); //open sockets,streams
    } catch (IOException e) {
        e.printStackTrace();
        closeBT();
    }
}
4

1 に答える 1

0

Try-catchアプリケーションロジック用ではありません!何かがうまくいかなかったときに何かをするためのものです!if-elseここで、のように使用したい

if (findBT() != null) { // I don't know what findBT does, but maybe it returns BT-devices
    try {
        openBT(); //open sockets,streams
    } catch (IOException e) {
        e.printStackTrace();
        // inform the user that a connection could not be established or something similar
    }
} else {
    // inform the user, that no BT-device was found.
}

closeBT()たとえば、ユーザーまたはアプリケーションが BT デバイスを切断することを決定したときに使用します。

于 2013-02-14T12:19:48.213 に答える