我愿意生成一个列表,其中列出了手机附近所有支持蓝牙功能的设备。我发现的代码没有配对的设备。有没有办法检索也配对的设备(如果它们落在附近)
发现附近所有设备的代码如下
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bluetooth_connection);
final TextView tv=(TextView)findViewById(R.id.textView1);
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
final BroadcastReceiver mReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action))
{
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(
BluetoothDevice.EXTRA_DEVICE);
tv.append(device.getName() + "-"+ device.getAddress()+"\n");
}
}
};
String aDiscoverable = BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE;
startActivityForResult(new Intent(aDiscoverable),DISCOVERY_REQUEST);
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
mBluetoothAdapter.startDiscovery();
}
任何人都可以帮助我添加甚至发现配对设备的代码。
谢谢,
答案 0 :(得分:0)
Set<BluetoothDevice> devices = adapter.getBondedDevices();
for (BluetoothDevice device : devices) {
tv.append(device);
}
我没有测试过这个。它来自this tutorial。