Electrocardiogram (ECG) signals contain a significant amount of subtle information that can be used to detect some types of heart dysfunction. The widespread availability of digital ECG and the algorithmic paradigm of the long short-term memory (LSTM) network present an opportunity to substantially improve the accuracy and scalability of automated ECG analysis. However, the number of hidden units and initial learning rate of an LSTM neural network for ECG classification are currently preset based on prior knowledge, which causes the model to reach a sub-optimal state. In this study, an automated ECG detection and classification method using a bidirectional LSTM (BiLSTM) network modified by Bayesian optimization is developed. Bayesian optimization is used to optimize the two hyperparameters of the BiLSTM network: the initial learning rate and the number of hidden layers. By classifying five ECG signals in the MIT-BIH arrhythmia database, the accuracy of the modified network reaches 99.00%, which is 0.86% higher than that before optimization. The results demonstrate that Bayesian optimization can be an effective approach to improving the quality of classifiers based on deep learning. The presented approach can also be considered for generalization to other quasi-periodical biometric signal-based classification tasks in future studies, which may have practical applications.