Android lagging / instable bluetooth connection
我在使用不安全的 RFCOMM 连接连接到 WML-C46 AH 蓝牙芯片时遇到问题。我编写了一个只有相关代码的 android 应用程序来向您展示我的问题。
应用程序
该应用程序只有一个按钮。如果单击此按钮,则会启动蓝牙设备发现。发现完成后,应用程序连接到蓝牙设备(我只使用一个蓝牙设备进行测试,因此找不到任何其他设备)。然后它打开一个输入流来读取这个设备的数据。如果连接中断(抛出 ioexception)或再次单击按钮,则关闭连接(关闭所有线程、套接字和流)。如果您再次单击该按钮,则会启动新设备发现,依此类推...
问题
连接不正常。数据输入流似乎有点滞后,有时连接中断而没有任何明显的原因(IOException:软件导致连接中止或 IOException:再试一次)。它几乎是 android 蓝牙聊天示例的简化版本,它使用设备发现而不是配对设备和一个活动。
密码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | public class MainActivity extends Activity implements View.OnClickListener { BluetoothDevice btDevice; BluetoothSocket btSocket; InputStream inStream; OutputStream outStream; Object lock = new Object(); boolean connected = false; boolean canceled = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn = (Button) findViewById(R.id.button1); btn.setOnClickListener(this); IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); getApplicationContext().registerReceiver(btReceiver, filter); filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); getApplicationContext().registerReceiver(btReceiver, filter); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onClick(View arg0) { if (!connected) { connected = true; canceled = false; new Thread(new Runnable() { @Override public void run() { BluetoothAdapter btAdapter = BluetoothAdapter .getDefaultAdapter(); try { if (btAdapter.isDiscovering()) btAdapter.cancelDiscovery(); btAdapter.startDiscovery(); // block until device discovery has finished synchronized (lock) { try { lock.wait(); } catch (InterruptedException e) { Log.d("EXCEPTION","", e); } } btSocket = btDevice.createInsecureRfcommSocketToServiceRecord(UUID .fromString("00001101-0000-1000-8000-00805F9B34FB")); btSocket.connect(); inStream = btSocket.getInputStream(); outStream = btSocket.getOutputStream(); new Thread(new Runnable() { @Override public void run() { while (!canceled) { try { int in; while ((in = inStream.read()) != -1) { Log.d("Received new byte:", in +""); } } catch (IOException e) { Log.d("EXCEPTION IN LISTENER","", e); disconnect(); } } } }).start(); } catch (IOException e) { Log.d("","", e); } } }).start(); } else { disconnect(); } } private void disconnect() { connected = false; canceled = true; try { btSocket.close(); } catch (Exception e) { Log.d("EXCEPTION DISCONNECTING","", e); } try { inStream.close(); } catch (Exception e) { Log.d("EXCEPTION DISCONNECTING","", e); } try { outStream.close(); } catch (Exception e) { Log.d("EXCEPTION DISCONNECTING","", e); } btDevice = null; btSocket = null; inStream = null; outStream = null; } private final BroadcastReceiver btReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); try { if (BluetoothDevice.ACTION_FOUND.equals(action)) { btDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { synchronized (lock) { lock.notifyAll(); } } } catch (Exception e) { Log.d(this.getClass().getName(),"", e); } } }; } |
我只是使用阻塞方法或自己进行阻塞。因此,连接建立工作非常程序化。当我调整它时,我可能已经丢失了 android 示例的连接流程,但我找不到问题。
输出
建立连接:
1 2 3 4 5 6 | 01-16 16:52:46.234: V/BluetoothSocket.cpp(6961): initSocketNative 01-16 16:52:46.234: V/BluetoothSocket.cpp(6961): ...fd 47 created (RFCOMM, lm = 0) 01-16 16:52:46.234: V/BluetoothSocket.cpp(6961): initSocketFromFdNative 01-16 16:52:46.257: D/BluetoothUtils(6961): isSocketAllowedBySecurityPolicy start : device null 01-16 16:52:47.171: V/BluetoothSocket.cpp(6961): connectNative 01-16 16:52:47.570: V/BluetoothSocket.cpp(6961): ...connect(47, RFCOMM) = 0 (errno 115) |
连接中断:
1 2 3 4 5 6 | 01-16 16:53:38.539: D/EXCEPTION IN LISTENER(6961): java.io.IOException: Software caused connection abort 01-16 16:53:38.539: D/EXCEPTION IN LISTENER(6961): at android.bluetooth.BluetoothSocket.readNative(Native Method) 01-16 16:53:38.539: D/EXCEPTION IN LISTENER(6961): at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:388) 01-16 16:53:38.539: D/EXCEPTION IN LISTENER(6961): at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:60) 01-16 16:53:38.539: D/EXCEPTION IN LISTENER(6961): at com.example.bttest.MainActivity$2$1.run(MainActivity.java:100) 01-16 16:53:38.539: D/EXCEPTION IN LISTENER(6961): at java.lang.Thread.run(Thread.java:856) |
谢谢!
发现问题。蓝牙设备未按预期工作,并在几秒钟后关闭连接。然而,代码工作得很好。