Ejemplo n.º 1
0
        private static BluetoothRadio GetDefault()
        {
            BLUETOOTH_FIND_RADIO_PARAMS p    = BLUETOOTH_FIND_RADIO_PARAMS.Create();
            BLUETOOTH_RADIO_INFO        info = BLUETOOTH_RADIO_INFO.Create();
            IntPtr hRadio = IntPtr.Zero;

            IntPtr findHandle = NativeMethods.BluetoothFindFirstRadio(ref p, out hRadio);

            if (hRadio != IntPtr.Zero)
            {
                int result = NativeMethods.BluetoothGetRadioInfo(hRadio, ref info);
            }

            if (findHandle != IntPtr.Zero)
            {
                NativeMethods.BluetoothFindRadioClose(findHandle);
            }

            if (hRadio != IntPtr.Zero)
            {
                return(new BluetoothRadio(info, hRadio));
            }

            return(null);
        }
Ejemplo n.º 2
0
        public static bool BluetoothDisconnect(string serialNumber)
        {
            IntPtr         radioHandle     = IntPtr.Zero;
            SafeFileHandle bluetoothHandle = null;

            try
            {
                Debug.WriteLine("Attempting to disconnect bluetooth device.");

                //Get and parse the mac address
                byte[]   macAddressBytes = new byte[8];
                string[] macAddressSplit = { $"{serialNumber[0]}{serialNumber[1]}", $"{serialNumber[2]}{serialNumber[3]}", $"{serialNumber[4]}{serialNumber[5]}", $"{serialNumber[6]}{serialNumber[7]}", $"{serialNumber[8]}{serialNumber[9]}", $"{serialNumber[10]}{serialNumber[11]}" };
                for (int i = 0; i < 6; i++)
                {
                    macAddressBytes[5 - i] = Convert.ToByte(macAddressSplit[i], 16);
                }

                Debug.WriteLine("Disconnecting bluetooth device: " + serialNumber);

                //Disconnect the device from bluetooth
                BLUETOOTH_FIND_RADIO_PARAMS radioFindParams = new BLUETOOTH_FIND_RADIO_PARAMS();
                radioFindParams.dwSize = Marshal.SizeOf(radioFindParams);
                radioHandle            = BluetoothFindFirstRadio(ref radioFindParams, out bluetoothHandle);

                bool bluetoothDisconnected = false;
                while (!bluetoothDisconnected)
                {
                    bluetoothDisconnected = DeviceIoControl(bluetoothHandle, IoControlCodes.IOCTL_BTH_DISCONNECT_DEVICE, macAddressBytes, macAddressBytes.Length, null, 0, out int bytesWritten, IntPtr.Zero) && bytesWritten > 0;
                    if (!bluetoothDisconnected)
                    {
                        if (!BluetoothFindNextRadio(radioHandle, out bluetoothHandle))
                        {
                            bluetoothDisconnected = true;
                        }
                    }
                }

                Debug.WriteLine("Succesfully disconnected bluetooth: " + bluetoothDisconnected);
                return(bluetoothDisconnected);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Failed disconnecting bluetooth: " + ex.Message);
                return(false);
            }
            finally
            {
                if (radioHandle != IntPtr.Zero)
                {
                    BluetoothFindRadioClose(radioHandle);
                }
                if (bluetoothHandle != null)
                {
                    bluetoothHandle.Dispose();
                }
            }
        }
Ejemplo n.º 3
0
        public static BLUETOOTH_ADDRESS?GetLocalBluetoothMacAddress()
        {
            IntPtr         radioHandle     = IntPtr.Zero;
            SafeFileHandle bluetoothHandle = null;

            try
            {
                BLUETOOTH_FIND_RADIO_PARAMS radioFindParams = new BLUETOOTH_FIND_RADIO_PARAMS();
                radioFindParams.dwSize = Marshal.SizeOf(radioFindParams);
                radioHandle            = BluetoothFindFirstRadio(ref radioFindParams, out bluetoothHandle);
                if (radioHandle == IntPtr.Zero)
                {
                    Debug.WriteLine("No bluetooth radio found to get mac address for.");
                    return(null);
                }

                BLUETOOTH_RADIO_INFO radioInfo = new BLUETOOTH_RADIO_INFO();
                radioInfo.dwSize = Marshal.SizeOf(radioInfo);
                if (BluetoothGetRadioInfo(radioHandle, ref radioInfo))
                {
                    Debug.WriteLine("Bluetooth local mac address: " + radioInfo.address.byte1 + ":" + radioInfo.address.byte2 + ":" + radioInfo.address.byte3 + ":" + radioInfo.address.byte4 + ":" + radioInfo.address.byte5 + ":" + radioInfo.address.byte6);
                    return(radioInfo.address);
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Failed to get local bluetooth mac address: " + ex.Message);
                return(null);
            }
            finally
            {
                if (radioHandle != IntPtr.Zero)
                {
                    BluetoothFindRadioClose(radioHandle);
                }
                if (bluetoothHandle != null)
                {
                    bluetoothHandle.Dispose();
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// コンピュータのBluetoothデバイスを取得します。
        /// </summary>
        private unsafe void radio_search()
        {
            IntPtr hrfind;

            var param = new BLUETOOTH_FIND_RADIO_PARAMS();

            param.dwSize = (UInt32)Marshal.SizeOf(param);
            if ((hrfind = MyBluetoothFindFirstRadio(ref param, out hradio)) != IntPtr.Zero)
            {
                rinfo.dwSize = (UInt32)Marshal.SizeOf(rinfo);
                MyBluetoothGetRadioInfo(hradio, ref rinfo);
            }
            else
            {
                throw new Exception("no bluetooth module");
            }
            MyBluetoothFindRadioClose(hrfind);
        }
Ejemplo n.º 5
0
        /// <summary>
        ///     Instruct host radio to disconnect a given remote device.
        /// </summary>
        /// <param name="device">The MAC address of the remote device.</param>
        public static void DisconnectRemoteDevice(PhysicalAddress device)
        {
            var radioParams = new BLUETOOTH_FIND_RADIO_PARAMS();

            radioParams.Init();

            var findHandle = BluetoothFindFirstRadio(ref radioParams, out var radioHandle);

            if (findHandle == IntPtr.Zero)
            {
                return;
            }

            var payloadSize = Marshal.SizeOf <ulong>();
            var payload = Marshal.AllocHGlobal(payloadSize);
            var raw = new byte[] { 0x00, 0x00 }.Concat(device.GetAddressBytes()).Reverse().ToArray();
            var value = (long)BitConverter.ToUInt64(raw, 0);

            Marshal.WriteInt64(payload, value);

            try
            {
                using (var safeHandle = new Kernel32.SafeObjectHandle(radioHandle))
                {
                    Kernel32.DeviceIoControl(
                        safeHandle,
                        unchecked ((int)IOCTL_BTH_DISCONNECT_DEVICE),
                        payload,
                        payloadSize,
                        IntPtr.Zero,
                        0,
                        out _,
                        IntPtr.Zero
                        );
                }
            }
            finally
            {
                Marshal.FreeHGlobal(payload);
            }

            BluetoothFindRadioClose(findHandle);
        }
Ejemplo n.º 6
0
        public bool DisconnectBT()
        {
            if (MACAddr != null)
            {
                Console.WriteLine("Trying to disonnect BT device");
                IntPtr btHandle = IntPtr.Zero;
                int    IOCTL_BTH_DISCONNECT_DEVICE = 0x41000c;

                byte[]   btAddr = new byte[8];
                string[] sbytes = MACAddr.Split(':');
                for (int i = 0; i < 6; i++)
                {
                    //parse hex byte in reverse order
                    btAddr[5 - i] = Convert.ToByte(sbytes[i], 16);
                }
                long lbtAddr = BitConverter.ToInt64(btAddr, 0);


                BLUETOOTH_FIND_RADIO_PARAMS p = new BLUETOOTH_FIND_RADIO_PARAMS();
                p.dwSize = Marshal.SizeOf(typeof(BLUETOOTH_FIND_RADIO_PARAMS));
                IntPtr searchHandle  = BluetoothFindFirstRadio(ref p, ref btHandle);
                int    bytesReturned = 0;
                bool   success       = false;
                while (!success && btHandle != IntPtr.Zero)
                {
                    success = DeviceIoControl(btHandle, IOCTL_BTH_DISCONNECT_DEVICE, ref lbtAddr, 8, IntPtr.Zero, 0, ref bytesReturned, IntPtr.Zero);
                    CloseHandle(btHandle);
                    if (!success)
                    {
                        if (!BluetoothFindNextRadio(searchHandle, ref btHandle))
                        {
                            btHandle = IntPtr.Zero;
                        }
                    }
                }
                BluetoothFindRadioClose(searchHandle);
                Console.WriteLine("Disconnect successul: " + success);
                return(success);
            }
            return(false);
        }
Ejemplo n.º 7
0
        private static void GetAllRadios(List <BluetoothRadio> radios)
        {
            BLUETOOTH_FIND_RADIO_PARAMS p    = BLUETOOTH_FIND_RADIO_PARAMS.Create();
            BLUETOOTH_RADIO_INFO        info = BLUETOOTH_RADIO_INFO.Create();
            IntPtr hRadio = IntPtr.Zero;

            IntPtr findHandle = NativeMethods.BluetoothFindFirstRadio(ref p, out hRadio);

            do
            {
                if (hRadio != IntPtr.Zero)
                {
                    int result = NativeMethods.BluetoothGetRadioInfo(hRadio, ref info);
                    radios.Add(new BluetoothRadio(info, hRadio));
                }
            }while (NativeMethods.BluetoothFindNextRadio(findHandle, out hRadio));

            if (findHandle != IntPtr.Zero)
            {
                NativeMethods.BluetoothFindRadioClose(findHandle);
            }
        }
Ejemplo n.º 8
0
        private static unsafe void radio_search()
        {
            IntPtr hrfind;

            BLUETOOTH_FIND_RADIO_PARAMS param = new BLUETOOTH_FIND_RADIO_PARAMS();

            param.dwSize = (UInt32)Marshal.SizeOf(param);
            if ((hrfind = MyBluetoothFindFirstRadio(ref param, out hradio)) != IntPtr.Zero)
            {
                rinfo.dwSize = (UInt32)Marshal.SizeOf(rinfo);
                MyBluetoothGetRadioInfo(hradio, ref rinfo);
                fixed(byte *addrbyte = rinfo.address.rgBytes)
                {
                    Console.WriteLine("bluetooth host = {5:X02}:{4:X02}:{3:X02}:{2:X02}:{1:X02}:{0:X02}",
                                      addrbyte[0], addrbyte[1], addrbyte[2], addrbyte[3], addrbyte[4], addrbyte[5]);
                }
            }
            else
            {
                Console.WriteLine("no bluetooth module");
            }
            MyBluetoothFindRadioClose(hrfind);
        }
Ejemplo n.º 9
0
 public static extern IntPtr BluetoothFindFirstRadio(BLUETOOTH_FIND_RADIO_PARAMS pbtfrp, ref IntPtr phRadio);
Ejemplo n.º 10
0
 private extern static IntPtr BluetoothFindFirstRadio(ref BLUETOOTH_FIND_RADIO_PARAMS pbtfrp, ref IntPtr phRadio);
Ejemplo n.º 11
0
 public static extern bool BluetoothFindNextRadio(
     ref BLUETOOTH_FIND_RADIO_PARAMS hFind,
     out IntPtr phRadio);
Ejemplo n.º 12
0
 internal extern static IntPtr BluetoothFindFirstRadio(ref BLUETOOTH_FIND_RADIO_PARAMS pbtfrp, ref IntPtr phRadio);
Ejemplo n.º 13
0
 public static extern IntPtr BluetoothFindFirstRadio(ref BLUETOOTH_FIND_RADIO_PARAMS pBtRadioParam, out SafeFileHandle phRadio);
Ejemplo n.º 14
0
        public bool DisconnectBT()
        {
            if (MACAddr != null)
            {
                Console.WriteLine("Trying to disonnect BT device");
                IntPtr btHandle = IntPtr.Zero;
                int IOCTL_BTH_DISCONNECT_DEVICE = 0x41000c;

                byte[] btAddr = new byte[8];
                string[] sbytes = MACAddr.Split(':');
                for (int i = 0; i < 6; i++)
                {
                    //parse hex byte in reverse order
                    btAddr[5 - i] = Convert.ToByte(sbytes[i], 16);
                }
                long lbtAddr = BitConverter.ToInt64(btAddr, 0);


                BLUETOOTH_FIND_RADIO_PARAMS p = new BLUETOOTH_FIND_RADIO_PARAMS();
                p.dwSize = Marshal.SizeOf(typeof(BLUETOOTH_FIND_RADIO_PARAMS));
                IntPtr searchHandle = BluetoothFindFirstRadio(ref p, ref btHandle);
                int bytesReturned = 0;
                bool success  = false;
                while (!success && btHandle != IntPtr.Zero)
                {
                    success = DeviceIoControl(btHandle, IOCTL_BTH_DISCONNECT_DEVICE, ref lbtAddr, 8, IntPtr.Zero, 0, ref bytesReturned, IntPtr.Zero);
                    CloseHandle(btHandle);
                    if (!success)
                        if (!BluetoothFindNextRadio(searchHandle, ref btHandle))
                            btHandle = IntPtr.Zero;

                }              
                BluetoothFindRadioClose(searchHandle);
                Console.WriteLine("Disconnect successul: "+success);
                return success;
            }
            return false;
        }
Ejemplo n.º 15
0
 BluetoothFindFirstRadio(ref BLUETOOTH_FIND_RADIO_PARAMS pbtfrp, out IntPtr phRadio);
Ejemplo n.º 16
0
 internal static extern IntPtr BluetoothFindFirstRadio(ref BLUETOOTH_FIND_RADIO_PARAMS pbtfrp, ref IntPtr phRadio);
Ejemplo n.º 17
0
 public extern static IntPtr MyBluetoothFindFirstRadio(
     ref BLUETOOTH_FIND_RADIO_PARAMS btfrp,
     out IntPtr hRadio
     );
Ejemplo n.º 18
0
 public static extern IntPtr BluetoothFindFirstRadio(
     ref BLUETOOTH_FIND_RADIO_PARAMS pbtfrp,
     out IntPtr phRadio);