6

我正在使用蓝牙设备(IOIO 开发板)。

当我的设备断开连接时,我想听。上面的代码可以正常工作,但不能立即识别。当我关闭我的蓝牙开发板时,我必须等待大约 16 秒,直到我的 Android 识别到连接丢失。

有人知道为什么吗?我听说这应该是一个内部的Android限制,连接不经常检查?有人知道如何编写一个线程来“ping”蓝牙设备(如果它仍然存在)吗?我认为它与 Android BluetoothChat 示例非常相似,但我无法自己修复它。

谢谢。

菲利克斯

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
    IntentFilter filter2 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
    this.registerReceiver(mReceiver, filter1);
    this.registerReceiver(mReceiver, filter2);

}

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
      @Override
      public void onReceive(Context context, Intent intent) {            
          String action = intent.getAction();
          BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

         if (BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) {
              //Device is about to disconnect
              Toast.makeText(context,"The device is about to disconnect" , Toast.LENGTH_LONG).show();
          }
          else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
              //Device has disconnected
              Toast.makeText(context,"Device has disconnected" , Toast.LENGTH_LONG).show();
          }           
       } 
   };
4

0 回答 0