テキストベースのATコマンドを使用して単純なBluetooth周辺機器と通信するblackberryアプリを作成しています-モデムに似ています...イベントリスナーを使用してblackberryでのみ動作させることができます。したがって、通信は非同期になります。
ただし、これは単純なデバイスであり、同時アクセスを制御する必要があるため、ブロッキング コールのみを使用したいと考えています。
待機/通知を使用して通信をブロッキングに変換しようとする次のコードがあります。しかし、実行すると、getStringValue が完了するまで notifyResults は実行されません。つまり、遅延に関係なく常にタイムアウトします。
btCon オブジェクトは、すでに別のスレッドで実行されています。
スレッド化で明らかな何かが欠けていると確信しています。誰か親切に指摘してくれませんか?
ありがとう
また、notifyAll が IllegalMonitorStateException で爆発することも追加する必要があります。
以前、単純なブール値フラグと待機ループで試しました。しかし、同じ問題が存在しました。notifyResult は、getStringValue が完了するまで実行されません。
public class BTCommand implements ResultListener{
String cmd;
private BluetoothClient btCon;
private String result;
public BTCommand (String cmd){
this.cmd=cmd;
btCon = BluetoothClient.getInstance();
btCon.addListener(this);
System.out.println("[BTCL] BTCommand init");
}
public String getStringValue(){
result = "TIMEOUT";
btCon.sendCommand(cmd);
System.out.println("[BTCL] BTCommand getStringValue sent and waiting");
synchronized (result){
try {
result.wait(5000);
} catch (InterruptedException e) {
System.out.println("[BTCL] BTCommand getStringValue interrupted");
}
}//sync
System.out.println("[BTCL] BTCommand getStringValue result="+result);
return result;
}
public void notifyResults(String cmd) {
if(cmd.equalsIgnoreCase(this.cmd)){
synchronized(result){
result = btCon.getHash(cmd);
System.out.println("[BTCL] BTCommand resultReady: "+cmd+"="+result);
result.notifyAll();
}//sync
}
}
}