使用 Google Glass 的无线 TCP 和 UDP
TCP & UDP Over Wireless with Google Glass
我已经为 Google Glass 开发了 TCP 和 UDP 应用程序。这是 UDP 的实现:
public class MainActivity extends Activity
{
private TextView mTextView;
private someRunnable mRun;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
mTextView = (TextView) findViewById(R.id.textView);
Handler mHandler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
Bundle bundle = msg.getData();
mTextView.setText(bundle.getString("message"));
super.handleMessage(msg);
}
};
try
{
mRun = new someRunnable(mHandler);
(new Thread(mRun)).start();
}
catch (IOException e)
{
Log.e("Error", "Unable to start Network activity");
Log.e("Error", e.getLocalizedMessage());
}
}
@Override
protected void onDestroy()
{
mRun.stopNetworkActivity();
super.onDestroy();
}
}
我有一个服务器每 .5 秒不断发送包含数字的数据包。 Glass 应用程序接收数据并将其显示在 TextView 上。我知道 UDP 不可靠,但即使使用 TCP,我也会得到随机的 "pauses",它会持续大约 4 秒,然后重新发送大量数据包 - 当然暂停,我的意思是 Glass 只是丢弃数据包强制服务器重新发送。或者更糟的是,UDP 只有 "drop and move on"。
进行进一步分析后,应用程序似乎仅在 LogCat 中显示以下内容时暂停:
01-05 16:22:43.812: W/BluetoothAdapter(951): getBluetoothService() called with no BluetoothManagerCallback
01-05 16:22:43.812: D/BluetoothSocket(951): connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[122]}
01-05 16:22:48.203: W/bt-sdp(951): SDP - Rcvd conn cnf with error: 0x4 CID 0x40
01-05 16:22:48.203: E/btif_sock_rfcomm(951): find_rfc_slot_by_id unable to find RFCOMM slot id: 1356
01-05 16:22:53.336: W/bt-sdp(951): SDP - Rcvd conn cnf with error: 0x4 CID 0x42
01-05 16:22:53.336: E/btif_sock_rfcomm(951): find_rfc_slot_by_id unable to find RFCOMM slot id: 1357
01-05 16:22:53.336: W/BluetoothMapClientService(951): connection failed
01-05 16:22:53.336: W/BluetoothMapClientService(951): java.io.IOException: read failed, socket might closed or timeout, read ret: -1
01-05 16:22:53.336: W/BluetoothMapClientService(951): at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:521)
01-05 16:22:53.336: W/BluetoothMapClientService(951): at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:532)
01-05 16:22:53.336: W/BluetoothMapClientService(951): at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:323)
01-05 16:22:53.336: W/BluetoothMapClientService(951): at com.android.bluetooth.map.BluetoothMapClientService.connect(BluetoothMapClientService.java:262)
01-05 16:22:53.336: W/BluetoothMapClientService(951): at com.android.bluetooth.map.BluetoothMapClientService.connect(BluetoothMapClientService.java:94)
01-05 16:22:53.336: W/BluetoothMapClientService(951): at android.bluetooth.IBluetoothMapClient$Stub.onTransact(IBluetoothMapClient.java:60)
01-05 16:22:53.336: W/BluetoothMapClientService(951): at android.os.Binder.execTransact(Binder.java:404)
01-05 16:22:53.336: W/BluetoothMapClientService(951): at dalvik.system.NativeStart.run(Native Method)
抛出IOException后,一切恢复正常,正常运行。
如果我通过蓝牙将 Glass 连接到我的 phone,上面的消息不会显示并且一切运行完美(没有暂停)。
为什么 BluetoothSocket 对我的应用程序有如此大的影响?除了在运行应用程序之前强制客户端通过蓝牙连接之外,我还能做些什么吗?
在 HERE
上查看此线程
顺便问一下,你的项目是开源的吗?
我已经为 Google Glass 开发了 TCP 和 UDP 应用程序。这是 UDP 的实现:
public class MainActivity extends Activity
{
private TextView mTextView;
private someRunnable mRun;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
mTextView = (TextView) findViewById(R.id.textView);
Handler mHandler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
Bundle bundle = msg.getData();
mTextView.setText(bundle.getString("message"));
super.handleMessage(msg);
}
};
try
{
mRun = new someRunnable(mHandler);
(new Thread(mRun)).start();
}
catch (IOException e)
{
Log.e("Error", "Unable to start Network activity");
Log.e("Error", e.getLocalizedMessage());
}
}
@Override
protected void onDestroy()
{
mRun.stopNetworkActivity();
super.onDestroy();
}
}
我有一个服务器每 .5 秒不断发送包含数字的数据包。 Glass 应用程序接收数据并将其显示在 TextView 上。我知道 UDP 不可靠,但即使使用 TCP,我也会得到随机的 "pauses",它会持续大约 4 秒,然后重新发送大量数据包 - 当然暂停,我的意思是 Glass 只是丢弃数据包强制服务器重新发送。或者更糟的是,UDP 只有 "drop and move on"。
进行进一步分析后,应用程序似乎仅在 LogCat 中显示以下内容时暂停:
01-05 16:22:43.812: W/BluetoothAdapter(951): getBluetoothService() called with no BluetoothManagerCallback
01-05 16:22:43.812: D/BluetoothSocket(951): connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[122]}
01-05 16:22:48.203: W/bt-sdp(951): SDP - Rcvd conn cnf with error: 0x4 CID 0x40
01-05 16:22:48.203: E/btif_sock_rfcomm(951): find_rfc_slot_by_id unable to find RFCOMM slot id: 1356
01-05 16:22:53.336: W/bt-sdp(951): SDP - Rcvd conn cnf with error: 0x4 CID 0x42
01-05 16:22:53.336: E/btif_sock_rfcomm(951): find_rfc_slot_by_id unable to find RFCOMM slot id: 1357
01-05 16:22:53.336: W/BluetoothMapClientService(951): connection failed
01-05 16:22:53.336: W/BluetoothMapClientService(951): java.io.IOException: read failed, socket might closed or timeout, read ret: -1
01-05 16:22:53.336: W/BluetoothMapClientService(951): at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:521)
01-05 16:22:53.336: W/BluetoothMapClientService(951): at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:532)
01-05 16:22:53.336: W/BluetoothMapClientService(951): at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:323)
01-05 16:22:53.336: W/BluetoothMapClientService(951): at com.android.bluetooth.map.BluetoothMapClientService.connect(BluetoothMapClientService.java:262)
01-05 16:22:53.336: W/BluetoothMapClientService(951): at com.android.bluetooth.map.BluetoothMapClientService.connect(BluetoothMapClientService.java:94)
01-05 16:22:53.336: W/BluetoothMapClientService(951): at android.bluetooth.IBluetoothMapClient$Stub.onTransact(IBluetoothMapClient.java:60)
01-05 16:22:53.336: W/BluetoothMapClientService(951): at android.os.Binder.execTransact(Binder.java:404)
01-05 16:22:53.336: W/BluetoothMapClientService(951): at dalvik.system.NativeStart.run(Native Method)
抛出IOException后,一切恢复正常,正常运行。
如果我通过蓝牙将 Glass 连接到我的 phone,上面的消息不会显示并且一切运行完美(没有暂停)。
为什么 BluetoothSocket 对我的应用程序有如此大的影响?除了在运行应用程序之前强制客户端通过蓝牙连接之外,我还能做些什么吗?
在 HERE
上查看此线程顺便问一下,你的项目是开源的吗?