蓝牙 - 设备类型设置: Class of Device

在电脑或手机上,搜寻和连接蓝牙设备时,不同的蓝牙设备显示的图标是不同的,比如搜到或连接上的设备是一个蓝牙键盘,显示的就会是键盘图标,如果搜索到的设备是一个手柄,显示的就是一个手柄图标。

显示的图标是根据搜到或连接上的设备类型信息而显示的,这个信息就是COD(class of device)信息。这个信息是在Discovery过程中搜索周围的蓝牙设备时,搜索方从被搜到的设备那里得到的。然后依据COD来判断设备类型,决定显示什么样的图标。

在进行蓝牙产品开发时,通过设置这个信息,就可以显示跟你的产品相匹配的图标。注意这个COD信息主要使用在BR/EDR经典蓝牙上。对于BLE,一般使用的的是广播数据中有两个字节的appearance,而不是三个字节的COD信息。

具体的Class of Device信息介绍如下。

COD信息由四部分组成:

- A Major Service Classes bitfield // 主要服务类型

- A Major Device Class enumerated value // 主要设备类型

- The Minor Device Classes // 次要设备类型

- A fixed value of 0b00 in the two least significant bits // 固定的最低两位bit的值为0

主要设备类型不同,次要设备类型值的含义也不同。

COD信息的结构定义如下:

Major Service Classes

Last Modified: 2022-05-25

Bit 

Class of Device Major Service Class

13

Limited Discoverable Mode

14

LE audio

15

Reserved for future use

16

Positioning (Location identification)

17

Networking (LAN, Ad hoc, ...)

18

Rendering (Printing, Speakers, ...)

19

Capturing (Scanner, Microphone, ...)

20

Object Transfer (vInbox, vFolder, ...)

21

Audio (Speaker, Microphone, Headset service, ...)

22

Telephony (Cordless telephony, Modem, Headset service, ...)

23

Information (WEBserver, WAPserver, ...)

这个是每个位表示一种类型。

Major Device Classes

12

11

10

9

8

Major Device Class

0

0

0

0

0

Miscellaneous

0

0

0

0

1

Computer (desktop, notebook, PDA, organizer, ...)

0

0

0

1

0

Phone (cellular, cordless, pay phone, modem, ...)

0

0

0

1

1

LAN/Network Access point

0

0

1

0

0

Audio/Video (headset, speaker, stereo, video display, VCR, ...)

0

0

1

0

1

Peripheral (mouse, joystick, keyboard, ...)

0

0

1

1

0

Imaging (printer, scanner, camera, display, ...)

0

0

1

1

1

Wearable

0

1

0

0

0

Toy

0

1

0

0

1

Health

1

1

1

1

1

Uncategorized: device code not specified

Minor Device Class field – Computer Major Class

如果主要设备类型是电脑,次要设备类型信息如下:

7

6

5

4

3

2

Minor Device Class

0

0

0

0

0

0

Uncategorized, code for device not assigned

0

0

0

0

0

1

Desktop workstation

0

0

0

0

1

0

Serverclass computer

0

0

0

0

1

1

Laptop

0

0

0

1

0

0

Handheld PC/PDA (clamshell)

0

0

0

1

0

1

Palmsize PC/PDA

0

0

0

1

1

0

Wearable computer (watch size)

0

0

0

1

1

1

Tablet

Minor Device Class field – Phone Major Class

如果主要设备类型是手机,次要设备类型信息如下:

7

6

5

4

3

2

Minor Device Class

0

0

0

0

0

0

Uncategorized, code for device not assigned

0

0

0

0

0

1

Cellular

0

0

0

0

1

0

Cordless

0

0

0

0

1

1

Smartphone

0

0

0

1

0

0

Wired modem or voice gateway

0

0

0

1

0

1

Common ISDN access 

Minor Device Class field – LAN/Network Access point Major Class

7

6

5

Minor Device Class

0

0

0

Fully available

0

0

1

1% to 17% utilized

0

1

0

17% to 33% utilized

0

1

1

33% to 50% utilized

1

0

0

50% to 67% utilized

1

0

1

67% to 83% utilized

1

1

0

83% to 99% utilized

1

1

1

No service available

4

3

2

Minor Device Class

0

0

0

Uncategorized (use this value if no others apply)

Minor Device Class field – Audio/Video Major Class

7

6

5

4

3

2

Minor Device Class

0

0

0

0

0

0

Uncategorized, code not assigned

0

0

0

0

0

1

Wearable Headset Device

0

0

0

0

1

0

Handsfree Device

0

0

0

0

1

1

(Reserved)

0

0

0

1

0

0

Microphone

0

0

0

1

0

1

Loudspeaker

0

0

0

1

1

0

Headphones

0

0

0

1

1

1

Portable Audio

0

0

1

0

0

0

Car Audio

0

0

1

0

0

1

Settop box

0

0

1

0

1

0

HiFi Audio Device

0

0

1

0

1

1

VCR

0

0

1

1

0

0

Video Camera

0

0

1

1

0

1

Camcorder

0

0

1

1

1

0

Video Monitor

0

0

1

1

1

1

Video Display and Loudspeaker

0

1

0

0

0

0

Video Conferencing

0

1

0

0

0

1

(Reserved)

0

1

0

0

1

0

Gaming/Toy

Minor Device Class field – Peripheral Major Class

7

6

Minor Device Class

0

0

Uncategorized, code not assigned

0

1

Keyboard

1

0

Pointing device

1

1

Combo Keyboard/Pointing device

5

4

3

2

Minor Device Class

0

0

0

0

Uncategorized, code not assigned

0

0

0

1

Joystick

0

0

1

0

Gamepad

0

0

1

1

Remote control

0

1

0

0

Sensing device

0

1

0

1

Digitizer tablet

0

1

1

0

Card Reader (e.g. SIM Card Reader)

0

1

1

1

Digital Pen

1

0

0

0

Handheld scanner for barcodes, RFID, etc.

1

0

0

1

Handheld gestural input device (e.g., “wand” form factor)

Minor Device Class field – Imaging Major Class

7

6

5

4

Minor Device Class

X

X

X

1

Display

X

X

1

X

Camera

X

1

X

X

Scanner

1

X

X

X

Printer

3

2

Minor Device Class

0

0

Uncategorized, default

Minor Device Class field – Wearable Major Class

7

6

5

4

3

2

Minor Device Class

0

0

0

0

0

1

Wristwatch

0

0

0

0

1

0

Pager

0

0

0

0

1

1

Jacket

0

0

0

1

0

0

Helmet

0

0

0

1

0

1

Glasses

Minor Device Class field – Toy Major Class

7

6

5

4

3

2

Minor Device Class

0

0

0

0

0

1

Robot

0

0

0

0

1

0

Vehicle

0

0

0

0

1

1

Doll/Action figure

0

0

0

1

0

0

Controller

0

0

0

1

0

1

Game

Minor Device Class field – Health Major Class

7

6

5

4

3

2

Minor Device Class

0

0

0

0

0

0

Undefined

0

0

0

0

0

1

Blood Pressure Monitor

0

0

0

0

1

0

Thermometer

0

0

0

0

1

1

Weighing Scale

0

0

0

1

0

0

Glucose Meter

0

0

0

1

0

1

Pulse Oximeter

0

0

0

1

1

0

Heart/Pulse Rate Monitor

0

0

0

1

1

1

Health Data Display

0

0

1

0

0

0

Step Counter

0

0

1

0

0

1

Body Composition Analyzer

0

0

1

0

1

0

Peak Flow Monitor

0

0

1

0

1

1

Medication Monitor

0

0

1

1

0

0

Knee Prosthesis

0

0

1

1

0

1

Ankle Prosthesis

0

0

1

1

1

0

Generic Health Manager

0

0

1

1

1

1

Personal Mobility Device

举个例子,比如我手里拿的是一把带camera的扫描枪,使用蓝牙的SPP串口协议连接电脑,可以传输数据包括图像,它的COD就是:

MAJOR_SERVICE_CLASS = 0x40 // Capturing (Scanner, Microphone, ...)

MAJOR_DEVICE_CLASS = 0x06  //  Imaging (printer, scanner, camera, display, ...)

MINOR_DEVICE_CLASS = 0x18  //  Camera + Scanner

Byte2:0x08

Byte1:0x06

Byte0:0x60

如果我拿的是一个蓝牙键盘:

MAJOR_SERVICE_CLASS = 0x00 // 无

MAJOR_DEVICE_CLASS = 0x05  //  Peripheral (mouse, joystick, keyboard, ...)

MINOR_DEVICE_CLASS = 0x10  //  Keyboard

Byte2:0x00

Byte1:0x05

Byte0:0x40

关于COD信息的操作的宏:

typedef  struct _tagClass_of_Device_t

{

   Byte_t Class_of_Device0;

   Byte_t Class_of_Device1;

   Byte_t Class_of_Device2;

} Class_of_Device_t;

#define GET_CLASS_OF_DEVICE_FORMAT_TYPE(_x)                     \

   ((Byte_t)((_x).Class_of_Device0 & 0x03))

#define SET_CLASS_OF_DEVICE_FORMAT_TYPE(_x, _y)                 \

   ((_x).Class_of_Device0 = ((_x).Class_of_Device0 & 0xFC) | ((_y) & 0x03))

#define GET_MAJOR_SERVICE_CLASS(_x)                                   \

  ((Word_t)(((_x).Class_of_Device2 << 0x03) | ((_x).Class_of_Device1 >> 0x05)))

#define SET_MAJOR_SERVICE_CLASS(_x, _y)                               \

{                                                                     \

   (_x).Class_of_Device2  = (Byte_t)(((_y) & 0x7FF) >> 0x03);         \

   (_x).Class_of_Device1 &= (Byte_t)0x1F;                             \

   (_x).Class_of_Device1 |= (Byte_t)((((_y) & 0xFF) << 0x05) & 0xFF); \

}

#define GET_MAJOR_DEVICE_CLASS(_x)                                    \

   ((Byte_t)((_x).Class_of_Device1 & 0x1F))

#define SET_MAJOR_DEVICE_CLASS(_x, _y)                                \

{                                                                     \

   (_x).Class_of_Device1 &= 0xE0;                                     \

   (_x).Class_of_Device1 |= (Byte_t)((_y) & 0x1F);                    \

}

#define GET_MINOR_DEVICE_CLASS(_x)                                    \

   ((Byte_t)((_x).Class_of_Device0 >> 0x02))

#define SET_MINOR_DEVICE_CLASS(_x, _y)                                \

{                                                                     \

   (_x).Class_of_Device0 &= (Byte_t)0x03;                             \

   (_x).Class_of_Device0 |= (Byte_t)(((_y) << 0x02) & 0xFC);          \

}

#define ASSIGN_CLASS_OF_DEVICE(_dest, _x, _y, _z) \

{                                                 \

   (_dest).Class_of_Device0 = (_z);               \

   (_dest).Class_of_Device1 = (_y);               \

   (_dest).Class_of_Device2 = (_x);               \

参考:

Requesting Assigned Numbers – Bluetooth SIG Inc

2.8 Class of Device

 

  • 7
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 你好!这是一个在 C# 中检测蓝牙设备电量的示例脚本。 首先,你需要在你的项目中添加一个引用 to the System.Management namespace。然后,你可以使用下面的代码来检测蓝牙设备的电量: ``` using System.Management; // Replace "BluetoothDeviceName" with the actual name of your Bluetooth device. string deviceName = "BluetoothDeviceName"; // Query WMI for the battery status of the Bluetooth device. ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Battery WHERE DeviceID='" + deviceName + "'"); foreach (ManagementObject queryObj in searcher.Get()) { // The percentage of battery life remaining. int batteryLife = Convert.ToInt32(queryObj["EstimatedChargeRemaining"]); Console.WriteLine("Battery life: " + batteryLife + "%"); } ``` 希望这能帮到你! ### 回答2: 使用C#编写一个脚本来检测蓝牙设备的电量,你需要使用Windows.Devices.Bluetooth命名空间下的相关类和方法。 首先,你需要确保你的项目已经引用了Windows.Devices.Bluetooth的程序集。 然后,你可以使用以下步骤来编写脚本: 1. 导入所需的命名空间: ``` using System; using Windows.Devices.Bluetooth; using Windows.Devices.Bluetooth.GenericAttributeProfile; ``` 2. 创建一个BluetoothLEDevice实例,用于表示要检测的蓝牙设备: ``` BluetoothLEDevice bluetoothDevice = await BluetoothLEDevice.FromIdAsync(deviceId); ``` 注意:deviceId是蓝牙设备的唯一标识符,在使用前需要提前获取。 3. 获取蓝牙设备的GATT服务: ``` GattDeviceServicesResult services = await bluetoothDevice.GetGattServicesAsync(); ``` 4. 遍历服务列表,找到包含电池服务的服务: ``` foreach (var service in services.Services) { if (service.Uuid == GattServiceUuids.Battery) { GattCharacteristicsResult characteristics = await service.GetCharacteristicsAsync(); foreach (var characteristic in characteristics.Characteristics) { if (characteristic.Uuid == GattCharacteristicUuids.BatteryLevel) { // 获取电池电量 GattReadResult result = await charactericstic.ReadValueAsync(); var reader = DataReader.FromBuffer(result.Value); byte batteryLevel = reader.ReadByte(); Console.WriteLine("蓝牙设备的电量为:" + batteryLevel + "%"); } } } } ``` 这样,你就可以使用C#编写一个检测蓝牙设备电量的脚本。当执行此脚本时,它将连接到蓝牙设备并读取其电池电量。请确保在使用之前适当处理异常情况,例如设备连接失败或电池电量特征不存在等情况。 ### 回答3: 要使用C#编写一个检测蓝牙设备电量的脚本,首先需要使用System.Net.Sockets命名空间中的BluetoothClient类来实现与蓝牙设备的通信。以下是一个简单的示例代码: ```csharp using System; using InTheHand.Net.Sockets; public class BatteryChecker { public static void Main() { BluetoothClient bluetoothClient = new BluetoothClient(); var pairedDevices = bluetoothClient.DiscoverDevices(); foreach (var device in pairedDevices) { if (device.DeviceName.Contains("蓝牙设备名称")) { byte[] rfcomm = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x35, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; var batteryLevel = bluetoothClient.GetBatteryLevel(device.DeviceAddress, BluetoothCommandStatus.Error, rfcomm); Console.WriteLine("蓝牙设备电量:" + batteryLevel.ToString() + "%"); } } } } ``` 在上述代码中,我们使用BluetoothClient的DiscoverDevices方法获取已配对的蓝牙设备列表,并通过遍历列表来找到我们想要检测电量的设备。在找到目标设备后,通过BluetoothClient的GetBatteryLevel方法来获取蓝牙设备的电量。最后,我们将电量打印到控制台。 请注意,以上代码只是一个简单的示例,实际操作时可能需要根据蓝牙设备的特定协议和通信方式进行相应的调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜流冰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值