【问题标题】:How to make the android Bluetooth keep scanning如何让安卓蓝牙一直扫描
【发布时间】:2015-07-06 04:27:22
【问题描述】:

您好,这是我第一次发帖。 我正在制作一个仅通过使用友好名称和 mac 地址来发现蓝牙设备的 android 应用程序。未配对。

我面临的问题是我的设备在开始扫描大约 10 到 12 秒后无法检测到进入该范围的新设备。

final BroadcastReceiver bReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        // When discovery finds a device
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
             // Get the BluetoothDevice object from the Intent
             BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
             // add the name and the MAC address of the object to the arrayAdapter
            // System.out.println(device.getAddress());

             for(int i=0;i<listmac.size();i++)
             {
                 if(listmac.get(i).equals(device.getAddress())){
                    System.out.println(listname.get(i));
                    myListView.setItemChecked(i, true);
                    }
             } 
        }
    }
};

public void find(View view) {
       if (myBluetoothAdapter.isDiscovering()) {
           // the button is pressed when it discovers, so cancel the discovery
           myBluetoothAdapter.cancelDiscovery();
       }
       else {
            myBluetoothAdapter.startDiscovery();

            registerReceiver(bReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
            isBreceiverRegisted = true;
        }    
   }

 public void off(){
      myBluetoothAdapter.disable();

      Toast.makeText(getApplicationContext(),"Bluetooth turned off",
              Toast.LENGTH_LONG).show();
   }

对不起,我的英语不好

【问题讨论】:

    标签: android bluetooth


    【解决方案1】:

    仅仅是因为发现任务有超时防止无休止的搜索。 为了能够检测到发现已结束,您需要添加此意图

    if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
          // When discovery is finished , start discovery again  
             myBluetoothAdapter.startDiscovery(); 
          } 
        } 
    

    通常要非常小心,以免意外将您的设备置于发现模式。 执行设备发现对于蓝牙适配器来说是一个繁重的过程,并且会消耗大量资源。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-03
      • 1970-01-01
      • 1970-01-01
      • 2018-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多