Beispiel #1
0
        public bool Connect(BluetoothDevice device = null)
        {
            if (device == null)
            {
                device = this.device;
            }
            var rfSvcsRes = device.GetRfcommServices(BluetoothCacheMode.Uncached);

            foreach (var svc in rfSvcsRes.Services)
            {
                if (svc.ServiceId == RfcommServiceId.SerialPort)
                {
                    stream = svc.OpenStream();
                    break;
                }
            }
            return(stream != null);
        }
Beispiel #2
0
        public bool StartConnect()
        {
            if (_bluetoothDevice.ConnectionStatus == BluetoothConnectionStatus.Disconnected)
            {
                Debug.Log("Bluetooth Connected");
                var result   = _bluetoothDevice.GetRfcommServices(BluetoothCacheMode.Cached);
                var services = result.Services;


                /* find requested service and open connection*/
                foreach (var current in services)
                {
                    if (current.ServiceId == RfcommServiceId.SerialPort)
                    {
                        _stream = current.OpenStream();
                    }
                }

                return(true);
            }

            return(false);
        }
Beispiel #3
0
        //private NativeMethods.PFN_DEVICE_CALLBACK _callback;

        private async Task <DeviceInformation> DoPickSingleDeviceAsync(Rect selection, Placement placement)
        {
            NativeMethods.BLUETOOTH_SELECT_DEVICE_PARAMS sdp = new NativeMethods.BLUETOOTH_SELECT_DEVICE_PARAMS();
            sdp.dwSize     = Marshal.SizeOf(sdp);
            sdp.hwndParent = Process.GetCurrentProcess().MainWindowHandle;
            sdp.numDevices = 1;
            if (!string.IsNullOrEmpty(Appearance.Title))
            {
                sdp.info = Appearance.Title;
            }

            //defaults
            sdp.fShowAuthenticated = true;
            sdp.fShowUnknown       = false;

            List <int> codMasks = new List <int>();

            if (Filter.SupportedDeviceSelectors.Count > 0)
            {
                foreach (string filter in Filter.SupportedDeviceSelectors)
                {
                    var parts = filter.Split(':');
                    switch (parts[0])
                    {
                    case "bluetoothClassOfDevice":
                        int codMask = 0;
                        if (int.TryParse(parts[1], NumberStyles.HexNumber, null, out codMask))
                        {
                            codMasks.Add(codMask);
                            break;
                        }
                        break;

                    case "bluetoothPairingState":
                        bool pairingState = bool.Parse(parts[1]);
                        if (pairingState)
                        {
                            sdp.fShowAuthenticated = true;
                            sdp.fShowUnknown       = false;
                        }
                        else
                        {
                            sdp.fShowAuthenticated = false;
                            sdp.fShowUnknown       = true;
                        }
                        break;
                    }
                }
            }

            sdp.hwndParent = NativeMethods.GetForegroundWindow();

            if (codMasks.Count > 0)
            {
                // marshal the CODs to native memory
                sdp.numOfClasses      = codMasks.Count;
                sdp.prgClassOfDevices = Marshal.AllocHGlobal(8 * codMasks.Count);
                for (int i = 0; i < codMasks.Count; i++)
                {
                    Marshal.WriteInt32(IntPtr.Add(sdp.prgClassOfDevices, 8 * i), codMasks[i]);
                }
            }

            /*if (Filter.SupportedDeviceSelectors.Count > 0)
             * {
             *  _callback = new NativeMethods.PFN_DEVICE_CALLBACK(FilterDevices);
             *  sdp.pfnDeviceCallback = _callback;
             *  //sdp.pvParam = Marshal.AllocHGlobal(4);
             *  //Marshal.WriteInt32(sdp.pvParam, 1);
             * }*/

            bool success = NativeMethods.BluetoothSelectDevices(ref sdp);

            if (sdp.prgClassOfDevices != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(sdp.prgClassOfDevices);
                sdp.prgClassOfDevices = IntPtr.Zero;
                sdp.numOfClasses      = 0;
            }

            if (success)
            {
                BLUETOOTH_DEVICE_INFO info = Marshal.PtrToStructure <BLUETOOTH_DEVICE_INFO>(sdp.pDevices);
                NativeMethods.BluetoothSelectDevicesFree(ref sdp);

                foreach (string query in Filter.SupportedDeviceSelectors)
                {
                    var parts = query.Split(':');

                    if (parts[0] == "bluetoothService")
                    {
                        Guid serviceUuid = Guid.Parse(parts[1]);
                        foreach (Guid g in BluetoothDevice.GetRfcommServices(ref info))
                        {
                            if (g == serviceUuid)
                            {
                                return(new DeviceInformation(info, serviceUuid));
                            }
                        }

                        return(null);
                    }
                }

                return(new DeviceInformation(info));
            }

            return(null);
        }