現在、Bluetooth ソケットからデータを読み取る必要がある Android アプリケーションに取り組んでいます。私が使用しているコードは次のとおりです。
runOnUiThread(new Runnable() {
public void run()
{
try{
ReadData();
}catch(Exception ex){}
}
});
public void ReadData() throws Exception{
try {
b1 = new StringBuilder();
stream = socket.getInputStream();
int intch;
String output = null;
String k2 = null;
byte[] data = new byte[10];
// read data from input stream if the end has not been reached
while ((intch = stream.read()) != -1) {
byte ch = (byte) intch;
b1.append(ByteToHexString(ch) +"/");
k++;
if(k == 20) // break the loop and display the output
{
output = decoder.Decode(b1.toString());
textView.setText(output);
k=0;
break;
}
}
// close the input stream, reader and socket
if (stream != null) {
try {stream.close();} catch (Exception e) {}
stream = null;
}
if (socket != null) {
try {socket.close();} catch (Exception e) {}
socket = null;
}
} catch (Exception e) {
}
}
ただし、Android デバイスでアプリケーションを実行すると、UI が自動的に更新されず、フリーズし続けます。UI フリーズの問題を解決する方法を知っている人はいますか? ループ終了後にデータを表示するのではなく、動的にUIにデータを表示したい。
事前にご協力いただきありがとうございます。
よろしく、
チャールズ