我正在尝试使用免提配置文件 (HFP) 从 Windows 获取带有蓝牙 4.1 的耳机设备的电池电量。
我已经从我的 Android 手机中提取了蓝牙日志(能够获取电池电量),并且耳机正在使用 HFP 发送带有电池信息的 AT 命令(AT+IPHONEACCEV),这是从日志中提取的数据包之一(使用wireshark):
Frame 1122: 38 bytes on wire (304 bits), 38 bytes captured (304 bits)
Encapsulation type: Bluetooth H4 with linux header (99)
Arrival Time: Jul 16, 2020 12:18:16.449232000 Romance Daylight Time
[Time shift for this packet: 0.000000000 seconds]
Epoch Time: 1594894696.449232000 seconds
[Time delta from previous captured frame: 0.001885000 seconds]
[Time delta from previous displayed frame: 0.018263000 seconds]
[Time since reference or first frame: 21.363811000 seconds]
Frame Number: 1122
Frame Length: 38 bytes (304 bits)
Capture Length: 38 bytes (304 bits)
[Frame is marked: False]
[Frame is ignored: False]
Point-to-Point Direction: Received (1)
[Protocols in frame: bluetooth:hci_h4:bthci_acl:btl2cap:btrfcomm:bthfp]
Bluetooth
[Source: GNAudio_xxxxx (AA:BB:CC:DD:EE:FF)]
[Destination: SamsungE_xxxxx (FF:EE:DD:CC:BB:AA)]
Bluetooth HCI H4
[Direction: Rcvd (0x01)]
HCI Packet Type: ACL Data (0x02)
Bluetooth HCI ACL Packet
Bluetooth L2CAP Protocol
Bluetooth RFCOMM Protocol
Address: E/A flag: 1, C/R flag: 0, Direction: 0, Channel: 2
Control: Frame type: Unnumbered Information with Header check (UIH) (0xef), P/F flag: 0
Payload length: 25
Frame Check Sequence: 0xbf
Bluetooth HFP Profile
[Role: HS - Headset (2)]
AT Stream: AT+IPHONEACCEV=2,1,5,2,0\r
Command 0: +IPHONEACCEV
Command Line Prefix: AT
Command: +IPHONEACCEV (Apple Bluetooth Headset Battery Level Indication)
Type: Action Command (0x003d)
Parameters
Count: 2
Key: Battery Level (1)
Value: 5
Key: Dock State (2)
Value: 0
现在,我正在尝试使用 32feet (InTheHand.Net.Bluetooth) 库从我的 Windows C# 应用程序中的耳机获取此 AT 命令。为此,我尝试使用以下方式连接到耳机BluetoothService.Handsfree
:
BluetoothDevicePicker picker = new BluetoothDevicePicker();
BluetoothDeviceInfo device = await picker.PickSingleDeviceAsync();
BluetoothClient cli = new BluetoothClient();
//device.SetServiceState(BluetoothService.Handsfree, true);
cli.Connect(device.DeviceAddress, BluetoothService.Handsfree);
if (cli.Connected) MessageBox.Show("OK");
NetworkStream stream;
stream = cli.GetStream();
if (stream.CanRead)
{
byte[] buff = new byte[1024];
int n = 0;
StringBuilder str = new StringBuilder();
while (stream.DataAvailable)
{
n = await stream.ReadAsync(buff, 0, buff.Length);
str.AppendFormat("{0}", Encoding.ASCII.GetString(buff, 0, n));
}
MessageBox.Show(str.ToString());
}
执行代码时出现以下错误cli.Connect(device.DeviceAddress, BluetoothService.Handsfree);
:
System.Net.Sockets.SocketException: 'A socket operation failed because the destination host was down'
有了cli.Connect(device.DeviceAddress, BluetoothService.SerialPort);
它连接(所以主机没有关闭),但我没有从stream.ReadAsync
电话中得到任何回复。
我怎样才能做到这一点?谢谢!