Bluetoothを使用して接続し、RFCOMMチャネルを介してデータを転送する2つのAndroidデバイスがあります。データを受信するデバイスは1つだけですが、他のデバイスはデータを送信します...
このコードを使用して、他のデバイスに接続し、RFCOMMチャネルのリッスンを開始できます。
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
socket = (BluetoothSocket) m.invoke(device, 2);
socket.connect();
class BasicThread implements Runnable{
public void run() {
try {
InputStream stream = socket.getInputStream();
BufferedReader r = new BufferedReader(new InputStreamReader(stream));
while (true){
Log.d("myapp", "now listening...");
latestLine = r.readLine();
Log.d("myapp", latestLine);
}
} catch (IOException e) {
}
}
}
new Thread(new BasicThread()).run();
他のデバイスを使用して、次のようなリスニングソケットを実装しました。
Method m = blue.getClass().getMethod("listenUsingRfcommOn", new Class[] { int.class });
BluetoothServerSocket socket = (BluetoothServerSocket) m.invoke(blue, 2);
BluetoothSocket sock = socket.accept();
Log.d("myapp", "Connected...\n\n\n\n\n\n\n\n");
OutputStream s = sock.getOutputStream();
final PrintWriter out = new PrintWriter(s);
これらは両方ともRFCOMMチャネル2で接続し、両方とも相互にSEEしますが、2番目のデバイスは常にBluetoothSocket sock = socket.accept();
何か助けはありますか?