我写了个app,要把Android手机连接到一个蓝牙设备上,可以通过打开一个BluetoothSocket这样做:
  1. Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
  2. socket = (BluetoothSocket) m.invoke(device, 1);
  3. socket.connect();
复制代码
但是我想知道怎么给这个socket的读写设置一个超时判断
中级工程师 2# sunxuhua 发表于 2012-2-23 10:02:40
在端口上写这个:
  1. try
  2. {
  3.     // Enviamos los bytes
  4.     DataOutputStream dOut = null;
  5.     dOut = new DataOutputStream(socket.getOutputStream());
  6.     // Send message
  7.     dOut.writeBytes(res);
  8.     dOut.flush();
  9. }
  10. catch (IOException e)
  11. {
  12.     Dialogs.showErrorDialog("Error al recuperar la fecha y hora del dispositivo Nonin.", this);
  13. }
复制代码
当响应可以访问的时候从端口上读:
  1. DataInputStream dIn = null;
  2. // We receive the answer
  3. try
  4. {
  5.      dIn = new DataInputStream(socket.getInputStream());
  6. }
  7. catch (IOException e)
  8. {
  9.     Dialogs.showAlertDialog("An exception occured during bluetooth io stream creation", this);
  10. }
  11. while (true)
  12. {
  13.     try
  14.     {
  15.          String data = dIn.readLine(); // readLine();
  16.          byte[] total = EncodingUtils.getBytes(data, "ASCII");
  17.          Dialogs.showInfoDialog("Mensaje ok: " + data.toString(), this);
  18.     }
  19.     catch (IOException e)
  20.     {
  21.           break;
  22.      }
  23. }
复制代码
您需要登录后才可以回帖 登录 | 注册

关于我们|手机版|Archiver|DEVDIV.COM ( 京ICP备07040843号 )  

GMT+8, 2012-5-19 16:43

Powered by DEVDIV.COM!

© 2010-2012 DEVDIV.COM Coummunity.

回顶部