I watched the articles and could not find anything, you can help (so that all device names are written into an array of strings)

BluetoothAdapter bluetooth; bluetooth = BluetoothAdapter.DefaultAdapter; if (bluetooth != null && bluetooth.IsEnabled) { List<String> device = new List<String>(); // и здесь требуется вписать в массив строк все устройства с включенным Bluetooth поблизости } 

I have no idea how to make BluetoothScanner , which will scan the space to search for available devices. Need to search for new devices, and not already paired

    1 answer 1

    Since I didn’t have Xamarin at hand, I’m writing the answer based on the documentation for the methods, classes and properties, and also based on the answers found.

    1) You need to get an instance of the BluetoothAdapter on the current device and check if it is enabled

    2) It is necessary to obtain all the physical devices that exist as paired devices with the current device, for this you need to use the BondedDevices collection from the adapter obtained earlier.

    Now the code that should help us:

     // получить адаптер по-умолчанию BluetoothAdapter adapter = BluetoothAdapter.DefaultAdapter; // проверяем, что у нас получен адаптер (есть Bluetooth) и он включен if (adapter != null && adapter.isEnabled) { // получим список связанных устройств ICollection<BluetoothDevice> devices = adapter.BondedDevices; // инициализируем пустой список строк имен устройств List<String> names = new List<String>(); // пройдем по списку устройств и будем заполнять список имен foreach (var device in devices) { // device - сопряженное устройство из BondedDevices names.Add(device.Name); // добавление имени } } 

    In addition to the answer, I will add a code that will also list new devices next to OnReceive other, for this you will need to write your overloaded OnReceive method in the class inheriting BroadcastReceiver :

     class BluetoothDeviceReceiver : BroadcastReceiver { // перегрузим метод `OnReceive` public override void OnReceive(Context context, Intent intent) { // это действие, которое будем выполнять (для нас пока будет только поиск) String action = intent.Action; if (action == BluetoothDevice.ActionFound) { // Получить устройство BluetoothDevice newDevice = (BluetoothDevice)intent.GetParcelableExtra(BluetoothDevice.ExtraDevice); // если устройство не сопряжено (его еще не было в BondedDevices) if (newDevice.BondState != Bond.Bonded) { // нужно учитывать, что переменная `names` должна быть в области видимости names.Add(newDevice.Name); // добавление имени } } // далее можно описать и другое действие, к примеру остановка поиска // это будет action = BluetoothAdapter.ActionDiscoveryFinished } } 

    Then, to use the written class, the following code is needed:

     // создадим фильтр с указанным дейсвтием - поиск IntentFilter filter = new IntentFilter(BluetoothDevice.ActionFound); // наш ранее описанный ресивер, для поиска по указанному фильтру BluetoothDeviceReceiver receiver = new BluetoothDeviceReceiver(); // зарегистрируем трансляцию(ресивер) и фильтр для обнаружения устройств RegisterReceiver(receiver, filter); // запустим поиск, это тот же адаптер из `BluetoothAdapter.DefaultAdapter` adapter.StartDiscovery(); 

    Also, in order to avoid glitches in the application, I recommend describing the action for the BluetoothAdapter.ActionDiscoveryFinished - it starts in the same way as BluetoothDevice.ActionFound , but only later. And do not forget to release the busy resource of the translator (receiver), through the method UnregisterReceiver . Additional sample code can be found here: Bluetooth Chat DeviceListActivity

    Links to useful sources:

    • I mean, the names of the devices next to our device (Bluetooth Scanner) are recorded in the array of strings - Alexey Papkov
    • @ AlekseyPapkov add to the question what you already have in order to understand more specifically the question. As time will, I will edit in response - Denis Bubnov 6:49 pm
    • Style code - brackets! - Flippy