如何在Win7中从C#.Net中断开蓝牙设备
问题描述:
我想从我的c#.Net应用程序中断开蓝牙设备,该应用程序在Win 7 x64上运行。如何在Win7中从C#.Net中断开蓝牙设备
我知道MS在.Net上提供很少的功能调节BT。
我搜了32feet.Net,发现了如何连接,发现,获取信息......但没有断开连接(我错过了什么?)。
然后,我在Msdn IOCTL_BTH_DISCONNECT_DEVICE上找到。问题是我无法理解如何调用它。 它似乎我应该使用平台调用DeviceIOControl,但我恐怕没有足够的.Net技能来自己建立这个。
这里是我的时刻,其中:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
using System.IO;
namespace BtDisco
{
class Program
{
const int IOCTL_BTH_DISCONNECT_DEVICE = 0x41000c;
[DllImport("Kernel32.dll", SetLastError = false, CharSet = CharSet.Auto)]
public static extern bool DeviceIoControl(
Microsoft.Win32.SafeHandles.SafeFileHandle hDevice,
uint dwIoControlCode,
[MarshalAs(UnmanagedType.AsAny)] [In] object InBuffer,
uint nInBufferSize,
[MarshalAs(UnmanagedType.AsAny)] [Out] object OutBuffer,
uint nOutBufferSize,
ref uint pBytesReturned,
[In] ref System.Threading.NativeOverlapped Overlapped
);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern SafeFileHandle CreateFile(
string lpFileName,
[MarshalAs(UnmanagedType.U4)] FileAccess dwDesiredAccess,
[MarshalAs(UnmanagedType.U4)] FileShare dwShareMode,
IntPtr lpSecurityAttributes,
[MarshalAs(UnmanagedType.U4)] FileMode dwCreationDisposition,
[MarshalAs(UnmanagedType.U4)] FileAttributes dwFlagsAndAttributes,
IntPtr hTemplateFile);
static void Main(string[] args)
{
//http://msdn.microsoft.com/en-us/library/windows/desktop/aa363216(v=vs.85).aspx
//hDev = Use CreateFile
SafeFileHandle _hdev = CreateFileR(...);
DeviceIoControl(hDev, IOCTL_BTH_DISCONNECT_DEVICE, char[] btAddr, btAddr.Length(), result, result.Length(), ref getCnt, IntPtr.Zero);
}
}
}
可能有人好心地帮我完成这个?
答
最后,我有它自己的工作!
我在InTheHand.Net代码中搜索了一下,最终了解了如何制作它!
下面是一些工作的代码(你需要InTheHand.Net,如果你想使用它):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Globalization;
using System.Text;
using InTheHand.Net;
using InTheHand.Net.Bluetooth;
using System.Runtime.InteropServices;
namespace BTDisco2
{
class Program
{
const int IOCTL_BTH_DISCONNECT_DEVICE = 0x41000c;
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
internal static extern bool DeviceIoControl(
IntPtr hDevice,
uint dwIoControlCode,
ref long InBuffer,
int nInBufferSize,
IntPtr OutBuffer,
int nOutBufferSize,
out int pBytesReturned,
IntPtr lpOverlapped);
static void Main(string[] args)
{
var r = BluetoothRadio.PrimaryRadio;
var h = r.Handle;
long btAddr = BluetoothAddress.Parse("00:1b:3d:0d:ac:31").ToInt64();
int bytesReturned = 0;
var success = DeviceIoControl(h,
IOCTL_BTH_DISCONNECT_DEVICE,
ref btAddr, 8,
IntPtr.Zero, 0, out bytesReturned, IntPtr.Zero);
if (!success)
{
int gle = Marshal.GetLastWin32Error();
Console.WriteLine(string.Format(CultureInfo.InvariantCulture, "failure: {0} = 0x{0:X}.", gle));
}
else
{
Console.WriteLine("Success !");
}
while (!Console.KeyAvailable) System.Threading.Thread.Sleep(200);
}
}
}
干得好!我开始想知道它们是否可以从用户模式中调用,但显然如此。 – alanjmcf 2013-02-12 22:32:07
就我所知,在类型BluetoothClient的obj下还有一个Dispose()方法,它也具有Connect()。不知道这是你想要的。 – Mehrad 2014-03-25 05:54:03