-
Notifications
You must be signed in to change notification settings - Fork 0
/
bt-checks.ts
88 lines (76 loc) · 2.7 KB
/
bt-checks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import noble from "@abandonware/noble";
import { connectToPrinter } from "./bluetooth";
async function exploreFlowControl(
peripheral: noble.Peripheral,
characteristic: noble.Characteristic
) {
try {
// Get the current MTU
console.log("Attempting to get MTU...");
// @ts-ignore - mtu exists but isn't in types
const currentMtu = peripheral.mtu || "unknown";
console.log(`Current MTU: ${currentMtu}`);
// Try to request a larger MTU
console.log("\nAttempting to request larger MTU...");
try {
// @ts-ignore - exchangeMtu exists but isn't in types
await peripheral.exchangeMtuAsync(512);
// @ts-ignore
console.log(`New MTU: ${peripheral.mtu}`);
} catch (error) {
console.log("MTU negotiation not supported or failed:", error);
}
// Get write properties
console.log("\nCharacteristic properties:");
console.log(characteristic.properties);
// Check write types supported
if (characteristic.properties.includes("write")) {
console.log("Supports Write With Response");
}
if (characteristic.properties.includes("writeWithoutResponse")) {
console.log("Supports Write Without Response");
}
// Get maximum write length
// @ts-ignore - maxWriteLength exists but isn't in types
const maxWrite = characteristic.maxWriteLength || "unknown";
console.log(`\nMaximum write length: ${maxWrite}`);
// Test different write modes
const testData = Buffer.from([0x1b, 0x40]); // Simple init command
console.log("\nTesting write with response...");
try {
await characteristic.writeAsync(testData, true);
console.log("Write with response succeeded");
} catch (error) {
console.log("Write with response failed:", error);
}
console.log("\nTesting write without response...");
try {
await characteristic.writeAsync(testData, false);
console.log("Write without response succeeded");
} catch (error) {
console.log("Write without response failed:", error);
}
} catch (error) {
console.error("Error during flow control exploration:", error);
}
}
async function main() {
try {
console.log("Connecting to printer...");
const { peripheral, writeCharacteristic } = await connectToPrinter();
console.log("\nExploring flow control options...");
await exploreFlowControl(peripheral, writeCharacteristic);
console.log("\nPress Enter to disconnect...");
await new Promise((resolve) => {
process.stdin.once("data", () => {
resolve();
});
});
await peripheral.disconnectAsync();
console.log("Disconnected from printer");
} catch (error) {
console.error("Error:", error.message);
}
process.exit();
}
main();