問題。 Android アプリの Bluetooth 2 SPP 接続を介したデータ ストリームが約 30 秒後にハングする
環境。 Atmega 1284P マイクロコントローラーによって制御されている Bluetooth 2 RN-41 モジュールにクライアントとして Android 4.3 (第 1 世代 nexus 7) を実行している Android タブレットを接続しようとしています。マイクロコントローラは USART ラインを介して Bluetooth モジュールにデータをプッシュしているだけで、モジュールへの接続に成功し、そのデータの一部を Android アプリケーションでストリーミングしています。ただし、約 30 秒後にデータ ストリームが停止することに気付きました (接続は持続しているように見えますが)。これは私を夢中にさせており、何が悪いのかよくわかりません。私は初心者の Android 開発者です。助けてくれてありがとう!
アンドロイドコード。コード量を短縮するために、else といくつかのキャッチを取り除き、コードを論理的なコメント セクションに分割しようとしました。私はこれに慣れていないので、私のコードがばかげている場合は申し訳ありません!
private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
private BluetoothSocket btSocket = null;
private OutputStream outStream = null;
private InputStream inStream = null;
private BluetoothDevice pairedScale = null;
private UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); // found this at https://stackoverflow.com/questions/4632524/how-to-find-the-uuid-of-serial-port-bluetooth-device
private String MAC_ADDR;
private boolean isBTConnected = false;
...
public void connectToBT() {
/* * *
* Find Bluetooth device from list of paired devices
* * */
if (mBluetoothAdapter.isEnabled()) {
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
MAC_ADDR = macSpinner.getItemAtPosition(macSpinner.getSelectedItemPosition()).toString();
for (BluetoothDevice device : pairedDevices) {
Log.v("HP", "Iterating through paired BT devices for a match");
if (MAC_ADDR.equals(device.toString())) {
pairedScale = device;
Toast.makeText(MainActivity.this, device.toString(), Toast.LENGTH_LONG).show();
Log.v("HP", "Found a BT device match: " + device.toString());
changeDisplayText("\nUsing paired device: " + device.toString(), true);
break;
}
}
}
/* * *
* Spawn new thread and attempt to connect
* * */
// If socket created successfully, spawn new thread
if (btSocket != null) {
new Thread(new Runnable() {
public void run() {
isConnecting = true;
// try to connect
try {
btSocket.connect();
} catch (IOException e) {
e.printStackTrace();
Log.e("HP", "Couldn't connect socket");
}
/* * *
* Create IO Streams
* * */
// create IO stream
if (btSocket.isConnected()) {
Log.v("HP", "Congratulations! Socket connected");
isBTConnected = true;
try {
inStream = btSocket.getInputStream();
outStream = btSocket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
Log.e("HP", "Couldn't create io stream");
}
} else {
Log.e("HP", "Couldn't connect socket");
}
// we have our io streams, now get data
if (isBTConnected && inStream != null) {
Log.v("HP", "Attempting to get data from inStream");
runOnUiThread(new Runnable() {
public void run() {
changeDisplayText("\nConnection successful!" +
"\nStreaming Data...", true);
}
});
Log.v("HP", "Starting data stream...");
// read from the input stream
byte[] scaleData = new byte[512];
byte[] allScaleData;
String hpFileName = "streamedData_" + epoch.toString() + ".dat";
FileOutputStream fOutStream = null;
/* * *
* Create file to store data
* * */
// external storage
File myDir = new File(Environment.getExternalStorageDirectory().toString() + "/streamedData");
myDir.mkdirs();
File file = new File(myDir, hpFileName);
Log.v("HP", "Attempted to make file");
try {
fOutStream = new FileOutputStream(file);
Log.v("HP", "Made file");
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.e("HP", "Error: Couldn't make file");
}
/* * *
* Stream data here
* * */
Integer bytesRead;
while (true) {
try {
bytesRead = inStream.read(scaleData);
Log.d("HP", "Read " + bytesRead.toString() + " bytes");
fOutStream.write(Arrays.copyOfRange(scaleData, 0, bytesRead.intValue()));
} catch (IOException e) {
Log.d("HP", "Done reading bytes");
try {
btSocket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
break;
}
}
try {
fOutStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
clearBTConnection();
isConnecting = false;
}
}).start();
}
}
}
リサーチ。運が悪い記事をいくつか読んだ。例: への Android Bluetooth SPP 接続が数秒後に切断されたように見える