2013-04-24 73 views
0

我是新來的android和我正在與藍牙功能的應用程序。我能夠設置藍牙適配器,獲取我自己的設備信息,但我無法使用startdiscovery發現藍牙設備。當我開始掃描時,它什麼都不做。Android上的藍牙:StartDiscovery無法正常工作。無法掃描設備

我使用的是onclicklistner開始掃描:

bt.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (!(bluetooth.isEnabled())) { 
        status = "Bluetooth is not Enabled."; 
        Toast.makeText(AddUser.this, status, Toast.LENGTH_LONG).show(); 
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
       startActivityForResult(enableBtIntent, 1); 

       } 
       else 
       { 

        scand(); 
       } 

      } 

這是我剛纔的「公共無效的onCreate」功能後,把onActivityResult功能:

protected void onActivityResult(int requestCode, int resultCode, Intent intent) { 
    System.out.println(resultCode); 
    if (resultCode == RESULT_CANCELED) { 
     status="Error Enabling bluetooth"; 
     Toast.makeText(AddUser.this, status, Toast.LENGTH_LONG).show(); 
    } else { 

      scand(); 
    } 



} 

這是我的scand功能,其中我是callng startdiscovery:

private void scand() 
{ 



    bluetooth.startDiscovery(); 
    Log.d("itmes", ""+items.size()); 
    item1 = new String[items.size()]; 
    item1 = items.toArray(item1); 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setTitle("Choose a device"); 
    builder.setItems(item1, new DialogInterface.OnClickListener() { 

     public void onClick(DialogInterface dialog, int item) { 
      Toast.makeText(getApplicationContext(), item1[item], Toast.LENGTH_SHORT).show(); 

     } 

    }); 

    AlertDialog alert = builder.create(); 

    alert.show(); 

} 

這是broadca stReceiver:

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

      if (BluetoothDevice.ACTION_FOUND.equals(action)) 
      { 
        Log.e("br", "--- device found ---"); 
       BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 

       items.add(device.getName()); 
      } 
      } 
     }; 

在爲廣播接收器上面的代碼中,我試圖把發現的設備名稱字符串中的ArrayList的「項目」。

我的OnCreate中functon內註冊的BroadcastReceiver這樣的:

filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
    registerReceiver(mReceiver, filter); 

我已經設置在androidmanifest文件藍牙權限。在上面的scand函數中,假設顯示已發現設備的列表,但它只顯示一個空標題的對話框。請告訴我如何正確使用startdiscovery和broadcastreceiver在alertdialog中顯示結果。

+0

是你試試藍牙設備在發現模式下掃描? – 2013-04-24 06:00:38

+0

是的,它們是可以發現的。 – 2013-04-24 06:04:45

+0

是使用startdiscovery和broadcastreceiver的正確方法嗎? – 2013-04-24 06:08:04

回答

1

startDiscovery()是異步的,你不會在通話結束後收到結果。移動代碼將對話框顯示爲一個函數let let public void showScanResult()並在您的onReceive中調用該函數。

+0

這可能不起作用,因爲我不確定你能否顯示對話框。但試試吧,我們從那裏開始。 – 2013-04-24 07:03:19

+0

Yesss,我用(bluetooth.isdiscovering){}緊接在startdiscovery調用之後。它會等到發現完成後,這是否是正確的方式?現在正在工作。感謝您的回覆。 :) – 2013-04-24 07:10:37

+0

那麼你不應該這樣做,因爲如果發現超過5秒鐘,你可能會得到ANR。先試試我的方法,然後我們從那裏開始。 – 2013-04-24 07:15:11

0

嗯,在我看來,當你將數組item1設置爲AlertDialog中顯示的內容時,你告訴Dialog顯示那個時刻數組中的項目(一個空數組),所以你會看到沒有元素。

我不確定你可以這樣做後,更新item1數組的內容,並期望AlertDialog被刷新。我不認爲這是這樣工作的(但是我從來沒有嘗試過)。

我做了類似的進入我的應用程序使用的東西一個ListView和一個適配器,所以當你通過適配器的ListView被刷新添加一個項目(你必須使用adapter.notifyDataSetChanged()反正):

protected ArrayList<BluetoothDevice> foundDevices 
         = new ArrayList<BluetoothDevice>(); 
private ListView foundDevicesListView; 
private ArrayAdapter<BluetoothDevice> adapter; 

foundDevicesListView = (ListView) findViewById(R.id.foundDevicesListView); 

adapter = new ArrayAdapter<BluetoothDevice>(this, 
      android.R.layout.simple_list_item_1, foundDevices); 
foundDevicesListView.setAdapter(adapter); 
// 
// Initialization, etc .... 


    BroadcastReceiver mReceiver = new BroadcastReceiver() { 
     public void onReceive(Context context, Intent intent) { 
      String action = intent.getAction(); 

      // When discovery finds a new device 
      if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
       // Get the BluetoothDevice object from the Intent 
       BluetoothDevice device= intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)); 
       if (!foundDevices.contains(device)) { 
        foundDevices.add(device); 
        adapter.notifyDataSetChanged(); 
       } 
      } 

      // When discovery cycle finished 
      if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { 
       if(foundDevices==null || foundDevices.isEmpty()){ 
        // Show not devices found message 
       } 
      } 
     } 
1
discovery() 
     { 
      IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
     this.registerReceiver(mReceiver, filter); 
     // Register for broadcasts when discovery has finished 
     filter = new IntentFilter(
       BluetoothAdapter.ACTION_DISCOVERY_FINISHED); 
     this.registerReceiver(mReceiver, filter); 

     // If we're already discovering, stop it 
     if (mBluetoothAdapter.isDiscovering()) { 
      mBluetoothAdapter.cancelDiscovery(); 
     } 
     // Request discover from BluetoothAdapter 
     mBluetoothAdapter.startDiscovery(); 

}

廣播接收器:

private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     String action = intent.getAction(); 
     // When discovery finds a device 
     try { 
      Broadcast=true; 
      if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
       // Get the BluetoothDevice object from the Intent 
       BluetoothDevice device = intent 
         .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 

       // If it's already paired, skip it, because it's been listed 
       // already 
       if (device.getBondState() !=BluetoothDevice.BOND_BONDED) { 

        near_device.add(device.getAddress()); 

       // When discovery is finished, change the Activity title 
      } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED 
        .equals(action)) { 
       scanResult(); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

}; 

scanresult() 
    { 

      mBluetoothAdapter.cancelDiscovery(); 
      receiverCheck = false; 
      // Unregister broadcast listeners 
      Home.this.unregisterReceiver(mReceiver); 
      // near device arraylist contain all device mac address 
    }