2015-07-06 120 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(); 
     } 
    } 

是一般非常小心,不小心讓你的設備處於發現模式。 執行設備發現對於藍牙適配器來說是一個沉重的過程,並且會消耗大量資源。