How to find the devices in the range by using bluetooth?

后端 未结 3 1694
广开言路
广开言路 2021-02-03 14:17

I am new to android.I want to develop an application to find the devices in the range by using Bluetooth programmatically.If any one has idea please give some sample code to me.

3条回答
  •  孤独总比滥情好
    2021-02-03 14:49

    Create Broad cast receiver something like the following and add the device information

        private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
    ArrayList arl = new ArrayList();    
                // 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);
                    // If it's already paired, skip it, because it's been listed already
                    HashMap deviceMap = new HashMap();
                    deviceMap.put(device.getName(), device.getAddress());
                    arl.add(deviceMap);
    
                // When discovery is finished, change the Activity title
                } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                    setProgressBarIndeterminateVisibility(false);
                    setTitle(R.string.select_device);
                    if (mNewDevicesArrayAdapter.getCount() == 0) {
                        String noDevices = getResources().getText(R.string.none_found).toString();
                        mNewDevicesArrayAdapter.add(noDevices);
                    }
                }
            }
    
    
        };
    

提交回复
热议问题