如何获取所有附近可发现的蓝牙设备的列表?

问题描述:

我想在我的应用程序中获取所有附近可发现的蓝牙设备列表,但我没有得到任何列表。我的deviceDiscovery()方法执行得很好,但onCreate()中的BroadcastReceiver中的onReceive()没有得到任何响应。谁能告诉我为什么这样?这里是我的代码:如何获取所有附近可发现的蓝牙设备的列表?

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener{ 


    BluetoothAdapter ba; 
    ToggleButton tb; 
    ArrayAdapter aa; 
    ListView l; 
    private static final int ENABLE_REQUEST=1; 
    private static final int DISCOVERABLE_REQUEST=2; 
    private static final int DISCOVERABLE_DURATION=10; 


    BroadcastReceiver br=new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 


      String action=intent.getAction(); 

      if(BluetoothDevice.ACTION_FOUND.equals(action)) 
      { 
       BluetoothDevice bd= intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
       aa.add(bd.getName()+" "+bd.getAddress()); 


      } 
     } 
    }; 






    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     tb= (ToggleButton) findViewById(R.id.toggle); 
aa=new ArrayAdapter(this,android.R.layout.simple_list_item_1); 
     l= (ListView) findViewById(R.id.list); 
     l.setAdapter(aa); 


     ba= BluetoothAdapter.getDefaultAdapter(); 

     tb.setOnClickListener(this); 
    } 




    @Override 
    public void onClick(View v) { 

     aa.clear(); 

     ToggleButton tb= (ToggleButton) v; 

     if(ba==null) { 
      Toast.makeText(this, "This feature is not supported", Toast.LENGTH_SHORT).show(); 
      tb.setChecked(false); 
     } 
     else 
     { 
      if(tb.isChecked()) 
      { 
       if(!ba.isEnabled()) 
       { 
        Intent i=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
        startActivityForResult(i,ENABLE_REQUEST); 
       } 
       else 
       { 
        Toast.makeText(this, "Your device is already enabled", Toast.LENGTH_SHORT).show(); 
        deviceDiscovery(); 
        makeDiscoverable(); 
       } 
      } 
      else 
      { 
       aa.clear(); 
       ba.disable(); 
       Toast.makeText(this, "Your device is now disabled", Toast.LENGTH_SHORT).show(); 
      } 
     } 



    } 








    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

     switch(requestCode) 
     { 
      case ENABLE_REQUEST:if(resultCode==RESULT_OK) 
      { 
       Toast.makeText(this, "Bluetooth Enabled", Toast.LENGTH_SHORT).show(); 
       deviceDiscovery(); 
       makeDiscoverable(); 
      } 
      else 
      { 
       Toast.makeText(this, "Bluetooth Enabling Rejected", Toast.LENGTH_SHORT).show(); 
       tb.setChecked(false); 
      } 
       break; 

      case DISCOVERABLE_REQUEST:if(resultCode==DISCOVERABLE_DURATION) 
      { 
       Toast.makeText(this, "Your device is discoverable by other devices", Toast.LENGTH_SHORT).show(); 
      } 
      else 
      { 
       Toast.makeText(this, "Your device is not discoverable", Toast.LENGTH_SHORT).show(); 
      } 
       break; 
     } 
    } 


    private void makeDiscoverable() { 
     Intent i=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); 
     i.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,DISCOVERABLE_DURATION); 
     startActivityForResult(i,DISCOVERABLE_REQUEST); 
    } 



    private void deviceDiscovery() 
    { 
if(ba.startDiscovery()) 
{ 
    IntentFilter filter=new IntentFilter(BluetoothDevice.ACTION_FOUND); 

    this.registerReceiver(br,filter); 
    Toast.makeText(this, "Discovering other bluetooth devices", Toast.LENGTH_SHORT).show(); 
} 
else 
{ 
    Toast.makeText(this, "Discovery failed to start", Toast.LENGTH_SHORT).show(); 
} 

    } 


} 

MainActivity.xml

<ToggleButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textOn="On" 
     android:textOff="Off" 
     android:id="@+id/toggle" 
     /> 

    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/list" 
     android:layout_below="@+id/toggle"/> 

AndroidManifest.xml中

<uses-permission android:name="android.permission.BLUETOOTH"/> 
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> 
+0

您正在测试哪种Android版本? –

+0

@VladMatvienko Android版本6.0 –

+0

从6.0你必须申报的位置权限以及使用蓝牙。 –

添加filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);和运行项目。您可以阅读更多Getting the address and names of all available bluetooth devices in android