📄 bluetooth.cs
字号:
#region Using directives
using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Net.Sockets;
#endregion
namespace BluetoothEgova {
[Flags]
internal enum LookupFlags {
Containers = 2,
FlushCache = 0x1000,
ResService = 0x8000,
ReturnAddr = 0x100,
ReturnBlob = 0x200,
ReturnName = 0x10
}
public enum RadioMode {
PowerOff,
Connectable,
Discoverable
}
public class Bluetooth {
[DllImport("BthUtil.dll", SetLastError = true)]
public static extern int BthSetMode(RadioMode dwMode);
[DllImport("BthUtil.dll", SetLastError=true)]
public static extern int BthGetMode(ref RadioMode dwMode);
// 获取配对码请求
[DllImport("Btdrt.dll", SetLastError = true)]
public static extern int BthGetPINRequest(byte[] pba);
// 设置配对码
[DllImport("btdrt.dll", SetLastError = true)]
public static extern int BthSetPIN(byte[] pba, int cPinLength, byte[] ppin);
//创建ACL连接
[DllImport("Btdrt.dll", SetLastError = true)]
public static extern int BthCreateACLConnection(byte[] pbt, ref ushort phandle);
//配对码验证
[DllImport("Btdrt.dll", SetLastError = true)]
public static extern int BthAuthenticate(byte[] pbt);
//关闭连接
[DllImport("Btdrt.dll", SetLastError = true)]
public static extern int BthCloseConnection(ushort handle);
[DllImport("Btdrt.dll", SetLastError = true)]
public static extern int BthReadLocalAddr(byte[] pba);
//[DllImport("ws2.dll", EntryPoint = "WSALookupServiceBegin", SetLastError = true)]
//public static extern int WSALookupServiceBegin(byte[] pQuerySet, LookupFlags dwFlags, ref int lphLookup);
[DllImport("mscoree", EntryPoint="@339")]
public static extern int connect(int s, byte[] name, int namelen);
public Bluetooth() {
}
//获取本地设备的ID
public string GetDeviceID() {
byte[] pba = new byte[6];
BthReadLocalAddr(pba);
string text1 = "";
text1 = text1 + pba[5].ToString("X2") + ":";
text1 = text1 + pba[4].ToString("X2") + ":";
text1 = text1 + pba[3].ToString("X2") + ":";
text1 = text1 + pba[2].ToString("X2") + ":";
text1 = text1 + pba[1].ToString("X2") + ":";
return (text1 + pba[0].ToString("X2"));
}
//public void DiscoverDevices() {
// byte[] buffer1 = new byte[0x400];
// BitConverter.GetBytes(60).CopyTo(buffer1, 0);
// GCHandle handle1 = GCHandle.Alloc(blob1.ToByteArray(), GCHandleType.Pinned);
// IntPtr ptr1 = handle1.AddrOfPinnedObject();
// BitConverter.GetBytes((int)(ptr1.ToInt32() + 4)).CopyTo(buffer1, 0x38);
//}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -