2015-07-06 82 views
0

嗨,这是我第一次发布。 我做了一个Android应用程序,只使用友好名称和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(); 
    } 

对不起,我打破了英语

回答

0

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

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

是一般非常小心,不小心让你的设备处于发现模式。 执行设备发现对于蓝牙适配器来说是一个沉重的过程,并且会消耗大量资源。