打印可用蓝牙设备列表

问题描述:

我试图在打开活动时让我的应用打印可用蓝牙设备的列表。打印可用蓝牙设备列表

首先,我在设备上启用蓝牙:

public class Home extends AppCompatActivity { 

TextView textView3; 
private static final int REQUEST_ENABLE_BT = 1; 
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter(); 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_home); 
    textView3 = (TextView) findViewById(R.id.textView3); 

    if (btAdapter == null) { 
     textView3.append("\nBluetooth not supported. Aborting."); 
    } 
} 

然后我试图找到任何设备添加到适配器

private final BroadcastReceiver bReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     String action = intent.getAction(); 
     if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
      BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 

      //add device to the adapter 
      btAdapter.add(device.getName(), device.getAddress()); 
     } 
    } 
}; 

我知道应该有一个startDiscovery()调用的地方,但我很难理解其他在线答案。另外,我的btAdapter.add()由于某种原因未被识别。

任何帮助,将不胜感激!

我的btAdapter.add()由于某种原因未被识别。

因为在BluetoothAdapter中没有这样的方法。

我知道应该有一个startDiscovery()调用的地方,但我很难理解其他在线答案。

您可以调用startDiscovery,一旦您确定该设备支持蓝牙且Bluetooth设备已启用Bluetooth设备。