打印可用蓝牙设备列表

时间:2016-03-01 23:58:44

标签: java android bluetooth

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

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

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()由于某种原因无法识别。

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

  

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

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

  

我知道某处应该有一个startDiscovery()调用,但我无法在线理解其他答案。

一旦确定设备支持蓝牙,并且设备设置中的蓝牙已开启,您就可以调用startDiscovery。