Пример #1
0
        internal static string FromBluetoothAddress(BluetoothAddress address)
        {
            var str = address.ToString("C");

            Debug.Assert(str == str.ToUpperInvariant());
            Debug.Assert(BluetoothAdapter.CheckBluetoothAddress(str), "Failed CheckBluetoothAddress: '" + str + "'");
            return(str);
        }
        private BluetoothDevice TryGetDevice(string mac)
        {
            bool isMac = BluetoothAdapter.CheckBluetoothAddress(mac);

            if (isMac)
            {
                return(_btAdapter.GetRemoteDevice(mac));
            }
            else
            {
                return(null);
            }
        }
Пример #3
0
        public bool pair(string strAddr, string strPsw)
        {
            bool             result           = false;
            BluetoothAdapter bluetoothAdapter = BluetoothAdapter.DefaultAdapter;

            bluetoothAdapter.CancelDiscovery();
            if (!bluetoothAdapter.IsEnabled)
            {
                bluetoothAdapter.Enable();
            }
            //检查蓝牙地址是否有效
            if (!BluetoothAdapter.CheckBluetoothAddress(strAddr))
            {
                Log.Debug("mylog", "devAdd un effient!");
            }
            BluetoothDevice device = bluetoothAdapter.GetRemoteDevice(strAddr);

            if (device.BondState != Bond.Bonded)
            {
                try
                {
                    Log.Debug("mylog", "NOT BOND_BONDED");
                    ClsUtils.setPin(device.Class, device, strPsw); // 手机和蓝牙采集器配对
                    ClsUtils.createBond(device.Class, device);
                    //ClsUtils.cancelPairingUserInput(device.Class, device);
                    result = true;
                }
                catch (Exception ex)
                {
                    Log.Debug("mylog", "setPiN failed!");
                }
            }
            else
            {
                try
                {
                    ClsUtils.createBond(device.Class, device);
                    ClsUtils.setPin(device.Class, device, strPsw); // 手机和蓝牙采集器配对
                    ClsUtils.createBond(device.Class, device);
                    result = true;
                }
                catch (Exception ex)
                {
                    Log.Debug("mylog", "setPiN failed!");
                }
            }
            return(result);
        }
Пример #4
0
        /// <summary>
        /// Выбор устройства по его MAC-адресу
        /// </summary>
        /// <param name="address">MAC-Адрес</param>
        public void SelectDeviceByAddress(string address)
        {
            //Сначала ищем прибор в списке найденых
            device = DeviceList.FirstOrDefault(d => d.Address == address);
            if (device != null)
            {
                return;
            }

            //Если не нашли, то создаём новый BluetoothDevice по MAC-адресу
            if (BluetoothAdapter.CheckBluetoothAddress(address))
            {
                device = BluetoothAdapter.DefaultAdapter.GetRemoteDevice(address);
                DeviceList.Add(device);
            }
            else
            {
                throw new ArgumentException("MAC-Адрес не валидный", nameof(address));
            }
        }
Пример #5
0
        private void StartScanningNew(ScanFilterOptions scanFilterOptions)
        {
            var hasFilter = scanFilterOptions?.HasFilter == true;
            List <ScanFilter> scanFilters = null;

            if (hasFilter)
            {
                scanFilters = new List <ScanFilter>();
                if (scanFilterOptions.HasServiceIds)
                {
                    foreach (var serviceUuid in scanFilterOptions.ServiceUuids)
                    {
                        var sfb = new ScanFilter.Builder();
                        sfb.SetServiceUuid(ParcelUuid.FromString(serviceUuid.ToString()));
                        scanFilters.Add(sfb.Build());
                    }
                }
                if (scanFilterOptions.HasServiceData)
                {
                    foreach (var serviceDataFilter in scanFilterOptions.ServiceDataFilters)
                    {
                        var sfb = new ScanFilter.Builder();
                        if (serviceDataFilter.ServiceDataMask == null)
                        {
                            sfb.SetServiceData(ParcelUuid.FromString(serviceDataFilter.ServiceDataUuid.ToString()), serviceDataFilter.ServiceData);
                        }
                        else
                        {
                            sfb.SetServiceData(ParcelUuid.FromString(serviceDataFilter.ServiceDataUuid.ToString()), serviceDataFilter.ServiceData, serviceDataFilter.ServiceDataMask);
                        }
                        scanFilters.Add(sfb.Build());
                    }
                }
                if (scanFilterOptions.HasManufacturerIds)
                {
                    foreach (var manufacturerDataFilter in scanFilterOptions.ManufacturerDataFilters)
                    {
                        var sfb = new ScanFilter.Builder();
                        if (manufacturerDataFilter.ManufacturerDataMask != null)
                        {
                            sfb.SetManufacturerData(manufacturerDataFilter.ManufacturerId, manufacturerDataFilter.ManufacturerData);
                        }
                        else
                        {
                            sfb.SetManufacturerData(manufacturerDataFilter.ManufacturerId, manufacturerDataFilter.ManufacturerData, manufacturerDataFilter.ManufacturerDataMask);
                        }
                        scanFilters.Add(sfb.Build());
                    }
                }
                if (scanFilterOptions.HasDeviceAddresses)
                {
                    foreach (var deviceAddress in scanFilterOptions.DeviceAddresses)
                    {
                        if (BluetoothAdapter.CheckBluetoothAddress(deviceAddress))
                        {
                            var sfb = new ScanFilter.Builder();
                            sfb.SetDeviceAddress(deviceAddress);
                            scanFilters.Add(sfb.Build());
                        }
                        else
                        {
                            Trace.Message($"Device address {deviceAddress} is invalid. The correct format is \"01:02:03:AB:CD:EF\"");
                        }
                    }
                }
                if (scanFilterOptions.HasDeviceNames)
                {
                    foreach (var deviceName in scanFilterOptions.DeviceNames)
                    {
                        var sfb = new ScanFilter.Builder();
                        sfb.SetDeviceName(deviceName);
                        scanFilters.Add(sfb.Build());
                    }
                }
            }

            var ssb = new ScanSettings.Builder();

            ssb.SetScanMode(ScanMode.ToNative());
            if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
            {
                // enable Bluetooth 5 Advertisement Extensions on Android 8.0 and above
                ssb.SetLegacy(false);
            }
            //ssb.SetCallbackType(ScanCallbackType.AllMatches);

            if (_bluetoothAdapter.BluetoothLeScanner != null)
            {
                Trace.Message($"Adapter >=21: Starting a scan for devices. ScanMode: {ScanMode}");
                if (hasFilter)
                {
                    if (scanFilterOptions.HasServiceIds)
                    {
                        Trace.Message($"Service UUID Scan Filters: {string.Join(", ", scanFilterOptions.ServiceUuids)}");
                    }
                    if (scanFilterOptions.HasServiceData)
                    {
                        Trace.Message($"Service Data Scan Filters: {string.Join(", ", scanFilterOptions.ServiceDataFilters.ToString())}");
                    }
                    if (scanFilterOptions.HasManufacturerIds)
                    {
                        Trace.Message($"Manufacturer Id Scan Filters: {string.Join(", ", scanFilterOptions.ManufacturerDataFilters.ToString())}");
                    }
                    if (scanFilterOptions.HasDeviceAddresses)
                    {
                        Trace.Message($"Device Address Scan Filters: {string.Join(", ", scanFilterOptions.DeviceAddresses)}");
                    }
                    if (scanFilterOptions.HasDeviceNames)
                    {
                        Trace.Message($"Device Name Scan Filters: {string.Join(", ", scanFilterOptions.DeviceNames)}");
                    }
                }
                _bluetoothAdapter.BluetoothLeScanner.StartScan(scanFilters, ssb.Build(), _api21ScanCallback);
            }
            else
            {
                Trace.Message("Adapter >= 21: Scan failed. Bluetooth is probably off");
            }
        }