2013-04-24 735 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 
    }