【问题标题】:detecting all the bluetooth devices present in an area including the devices which are already paired检测区域中存在的所有蓝牙设备,包括已配对的设备
【发布时间】:2012-11-07 03:27:29
【问题描述】:

我愿意生成一个列表,其中包含手机附近所有启用蓝牙的设备的列表。我的代码发现了未配对的设备。有没有办法检索也配对的设备(如果它们落在附近)

发现附近所有设备的代码如下

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();
}

谁能帮我添加代码,甚至发现配对的设备。

谢谢,

【问题讨论】:

    标签: android android-emulator bluetooth android-sdk-2.1


    【解决方案1】:
    Set<BluetoothDevice> devices = adapter.getBondedDevices();
        for (BluetoothDevice device : devices) {
          tv.append(device);
        }
    

    我没有测试过这个。来自this tutorial

    【讨论】:

    • 当我使用上面的代码时,我仍然只得到配对的设备,而不是所有的设备......你能给我进一步了解这个问题吗
    猜你喜欢
    • 2021-04-26
    • 2013-05-16
    • 1970-01-01
    • 2015-01-18
    • 1970-01-01
    • 2012-12-25
    • 1970-01-01
    • 2016-09-26
    • 1970-01-01
    相关资源
    最近更新 更多