Reference: https://github.com/ssh333/Monster-Party

Files

The main subject of this reverse-engineering effort is a Flutter app (怪兽派对.apk, arm64-v8a). The app scans for devices over Bluetooth and, once a device is successfully identified, adds it to the app’s device queue. The physical device we obtained has two DC motors, which the app can control separately by adjusting the PWM frequency.

Exploration

Unpacking with jadx shows that all the code is obfuscated and that the business logic is not in the Java layer. We can also see that the lib directory contains libflutter.so and libapp.so, where libapp.so holds the Dart 3.11 AOT business logic. In addition, libflutter_blue_plus_android.so is present — the native layer of the BLE plugin. That one is a library, so we won’t reverse it.

Because this app depends on Bluetooth and I don’t have a rooted phone on hand, dynamic debugging isn’t convenient, so I could only hunt for clues through string searches.

The searches pinned down a few key strings:

  • mix_device — the package name of the vendor’s in-house SDK
  • writeMotor — the motor command identifier
  • AES — this points to basic/v3; there is also v1 and v2 before it, and the v3 protocol uses AES encryption
  • write* — the command entry-point functions

This reveals that the device involves three kinds of protocol, using the v1, v2, and v3 protocols.

All three protocols involve a lot, so reverse engineering them is fairly difficult. Using Android HCI logs, I remote-controlled the toy with the original software and captured the logs. Log analysis confirmed:

  • Protocol: v2
  • Target device name: Ankni
  • GATT service: 00009000-0000-1000-8000-00805f9b34fb

This also gives a glimpse of part of the protocol design:

1
2
3
write characteristic: 0x9002
notify characteristic: 0x9001
vibration frame example: A0 90 01 XX / A0 98 02 XX YY

Native-layer Reverse Engineering

Next, let’s dig into the native layer.

After Flutter/Dart AOT compilation, all symbols are stripped. Plain disassembly only exposes addresses and registers, and class names, method names, and field offsets cannot be recovered. blutter recovers those symbols by parsing the object pool inside the Dart VM snapshot. blutter’s output is extremely high quality and very readable.

Key objects in the pp file:

1
[pp+0x5bb60] AnonymousClosure: (0x163ff8c), in [package:mix_device/src/bluetooth/v2/ble_device_v2.dart] BLEDeviceV2::_handleOperate (0x163f868)
1
2
// from _handleOperate (0x163f868)
LoadField field_93 ; comment: opCharacteristic (0x9001)
  • field_93 = 0x9001 = the op characteristic, Write With Response, used for reads and configuration writes

Also:

1
[pp+0x5bd00] AnonymousClosure: (0x160ae80), in [package:mix_device/src/bluetooth/v2/ble_device_v2.dart] BLEDeviceV2::_handleFunction (0x160a9f0)
1
2
3
// from _handleFunction (0x160a9f0)
LoadField field_97 ; comment: functionCharacteristic (0x9002)
withoutResponse: true
  • field_97 = 0x9002 = the function characteristic, Write Without Response, used for vibration/power-off

Both fields live in ble_device_v2.dart.

Analyzing this function lets us determine the frame structure:

1
2
3
4
byte[0] = CMD
byte[1] = (ACTION<<7) | (LEN<<3) // ACTION=1 means write, ACTION=0 means read
byte[2..] = PAYLOAD
// no CRC, no STX/ETX

Let’s analyze the 0xA0 command, which is our main recovery target; the main code is in writeMotor.

1
2
3
4
; AllocateArray r2=#0x2 (SMI tagged: 2>>1 = 1+1 = 2, but payload = [N] + pwms)
; payload[0] = N (number of motors, SMI-tag decoded)
; payload[1..N] = pwm values (strb in a loop)
; call _handleFunction(0xA0, payload)

SMI tag verification: in blutter’s comment, movz x1, #0x64 → logical 50 (0x64 >> 1 = 50, but 0x64 = 100 decimal). In fact, movz #0x64 at the battery clamp = 100, consistent with 0x64 = 100 decimal. The ÷2 for SMI is only needed when decoding with asr #1; byte payloads are written directly.

The resulting vibration frame is:

1
A0  [0x80|((N+1)<<3)]  N  pwm[0]  pwm[1] ... pwm[N-1]

Example: 1 motor at 50% → A0 90 01 32; stop → A0 90 01 00.

The response mechanism lives mainly in onValueReceived; taken together with handleOperate:

  • Reads go over 0x9001 (Write With Response), sending [cmd, 0x00] (header byte = 0 means read)
  • The device’s reply comes back via notify; onValueReceived matches the pending list by the cmd byte (firstWhereOrNull) and completes the Future
  • Timeout: timeoutCallback @ 0x16402a8, Future.delayed(raw 0x258 >> 1 = 300ms), no retry

The internal logic of onValueReceived is:

1
2
3
4
data[0] = cmd
data[1] >> 7 = ACTION (0 = reply to a read, 1 = device-initiated push)
(data[1] >> 3) & 0xF = LEN
verify data.length == LEN + 2, otherwise drop

Device-initiated push events (Action = 1):

  • 0x14: battery level, payload[0] = 0–100
  • 0x15: heartbeat (device-initiated; no app-side heartbeat needed)
  • 0xA1: button event → onReceivedKeyData, type byte 0x10/0x11/0x20/0x30/0x60

Next let’s look at the operation queue GlobalBLEOperationQueue; the file is global_ble_operation_queue.dart. The following facts can be established:

  • Global singleton; all GATT operations are strictly serialized (_isProcessing mutex)
  • execute @ 0x16078d8 → _enqueue @ 0x16085dc → _processQueue @ 0x1607ba4
  • Priority ordering: sorted by priority int on insertion, lowest first
  • 3 s watchdog (Timer, 0x1607dc0); on timeout it marks the entry cancelled and does not retry

Then a potential bug emerged: when the underlying BLE future never settles, the 3 s watchdog does not advance the queue.

The data models live in mix_model.dart, including MixDeviceMotorLevel:

1
2
3
4
field_7 (offset 0x8) = b0, default 0
field_f (offset 0x10) = b1, default 100
toData() → [b0, b1]
fromData(data) → b0=data[0], b1=data[1]

This is used by the 0x17 motor strength configuration command.

The PWM generation pipeline is in motor_pwm_transformer.dart:

  • defaultSinglePwmRange = (min=10, max=100)
  • defaultPwmRanges (3 presets): (10,50), (20,80), (40,100), selected by gesture class
  • applyMotorRange: 0 → 0; non-zero → clamped to [max(min,10), min(max,200)]
  • Two-toy mapping:
    • _buildSyncPwmByIndex: aligns by index, padding with the trailing value when short
    • _buildInterleavePwmByAverage: the secondary device runs the inverse of the primary device’s average (100 - avg)

We were also able to find the v3 protocol. A shallow analysis: the v3 protocol is very complex. It calls the backend POST /basic/v3/auth/signature/secret to obtain a per-device unique key, and the frame contents appear to involve AES encryption.

The main functional points are listed below:

Feature Location
Outbound frame construction (function path) BLEDeviceV2._handleFunction
@ 0x160a9f0
Outbound frame construction + op send (op path) BLEDeviceV2._handleOperate
@ 0x163f868
; _dispatchOperateWithTimeout
@ 0x163f7c0
Vibration BLEDeviceV2.writeMotor
@ 0x17e1108
(cmd 0xA0)
Notification parsing + dispatch BLEDeviceV2.onValueReceived
@ 0x162bff0
; button parsing onReceivedKeyData
@ 0x162c…
Read-response timeout (300 ms) timeoutCallback
@ 0x16402a8
(Future.delayed
, raw 0x258
→300)
Init / discovery / MTU / notifications init
@ 0x1634d34
; initCharacteristics
@ 0x16350e8
; _initMtu
@ 0x163580c
Characteristic fields field_97
= 0x9002 (write/function); field_93
= 0x9001 (op)
Queue GlobalBLEOperationQueue.execute
@ 0x16078d8
; _processQueue
@ 0x1607ba4
; timeout 3 s @ 0x1607dc0
Models MixDeviceMotorLevel
(mix_model.dart
@ 0x163c8fc
); PWM MotorPwmTransformer
(motor_pwm_transformer.dart
)

Reconstruction

GATT Profile

Characteristics are located solely by their 16-bit UUID — the app flattens all discovered characteristics (across every service) into a single Map<uuidString, characteristic> and looks them up by UUID. No parent-service UUID filtering is applied, so only the characteristic UUID matters. (16-bit UUIDs are expanded using the SIG base UUID 0000XXXX-0000-1000-8000-00805f9b34fb.)

UUID (16-bit) Code name (field) Role How the app writes here Notifications
0x9002 “function” (field_97
)
Real-time function commands (vibration 0xA0
, power-off 0xA2
, extend 0xA3
)
Write Without Response (fire-and-forget) Enabled
0x9001 “op” (field_93
)
Operation commands and queries (all reads + configuration writes: 0x01…0x18
)
Write With Response (waits for notification) Enabled
0x2902 CCCD descriptor Enables notifications on both characteristics — —

Note: the naming in the code is counter-intuitive — the “function” characteristic (0x9002) carries vibration data, while the “op” characteristic (0x9001) carries control/query commands. setNotifyValue(true) is applied to both, and both feed into the same onValueReceived parser.

Connection and Initialization

BLEDeviceV2.init() runs after the BLE link is established:

  1. discoverServices() (through the queue).
  2. await Future.delayed(25 ms) ← stabilization wait.
  3. requestMtu() (Android). The negotiated MTU is stored/logged (communicate mtu = …). Frames are small (≤ about 18 bytes), so MTU is not a control constraint.
  4. await Future.delayed(50 ms) ← stabilization wait.
  5. Build the uuid → characteristic map; assert that it contains "9002" and "9001". If either is missing → "init characteristics failed".
  6. initCharacteristics(): setNotifyValue(true) on both 0x9001 and 0x9002 (writing the CCCD 0x2902), and register a value-change handler (onValueReceived) on both.
  7. The service layer then performs device bring-up (polymorphic, order approximate): toy verification 0x00, device-info reads (0x01 deviceID, 0x02 firmware, 0x03 MAC, 0x04 SN, 0x06 productID, 0x05 variantID, 0x09 hardwareID), and device initialization 0x0B. Failure here → session "init_failed" / "设备初始化失败:无法完成初始化流程" (device initialization failed: could not complete the initialization flow).

v2 has no encryption gate (unlike v3). The 0x00 verification is a lightweight handshake; no server-issued key is needed to control the device. Once notifications are enabled, you can send vibration frames on 0x9002.

Frame Format

Every v2 frame — outbound and inbound — is:

1
2
3
4
byte[0] = CMD                              // command code
byte[1] = (ACTION << 7) | (LEN << 3) // header byte
byte[2 .. 2+LEN-1] = PAYLOAD // LEN bytes
total length = LEN + 2

Bit layout of the header byte byte[1]:

1
2
3
4
5
6
7
8
bit:   7    6 5 4 3    2 1 0
[A] [ L L L L ] [0 0 0]
| | └─ reserved (always 0)
| └─ LEN = payload length, 0..15 (bits 6..3)
└─ ACTION/direction flag (bit 7):
outbound WRITE/command → 1 (header = 0x80 | (LEN<<3))
outbound READ request → 0 (header = LEN<<3 )
inbound from the device → the device sets it: 0 = reply to a read, 1 = device push
  • No checksum / CRC. No STX/ETX.
  • Inbound validation: the parser requires total >= 2 and total == LEN + 2; otherwise it logs "on op received invalid length data" and drops the frame.
  • Since LEN is 4 bits, the per-frame payload is ≤ 15 bytes.

There are two transmission paths:

Path Used by Characteristic Write type Wait for a reply?
_handleFunction(cmd, data) 0xA0
vibration, 0xA2
power-off, 0xA3
extend
0x9002 Write Without Response No (fire-and-forget)
_handleOperate(OPModel)
via _dispatchOperateWithTimeout
All reads + configuration writes (0x01…0x18
)
0x9001 Write With Response Yes — match the notification, 300 ms timeout

Complete Command Set

CMD codes (from the in-code label map). Direction: ↑ app→device, ↓ device→app.

CMD Name Direction Path/Notes
0x00 Toy verification ↕ op path; connection handshake
0x01 Device ID ↑request/↓response read; response payload = 4 bytes → uint32
0x02 Firmware version ↑/↓ read
0x03 MAC address ↑/↓ read
0x04 SN ↑/↓ read
0x05 Variant ID ↑/↓ read
0x06 Product ID ↑/↓ read
0x07 Combo product ID ↑/↓ read
0x08 Combo variant ID ↑/↓ read
0x09 Hardware ID ↑/↓ read
0x0A Clear data ↑ op write
0x0B Device initialization ↑ op write; connection
0x10 Travel lock ↕ op write; payload = TravelLockModel.toData()
(about 1 byte)
0x11 Power-on heating parameters ↑ op write; payload = HeatingModel.toData()
(multi-byte)
0x12 Light brightness ↑ op write; payload = [level]
(1 byte)
0x13 Set heating state ↑ op write; payload = [on?1:0]
0x14 Battery ↑request/↓push battery%=payload[0]
0x15 Heartbeat ↓ device push
0x17 Motor level (strength configuration) ↕ op; payload = MixDeviceMotorLevel.toData()
= [b0, b1]
, default [0,100]
0x18 Extended peripheral status ↕ op
0xA0 Write vibration ↑ function path
0xA1 Button/key ↓ device push → MixDeviceKeyEvent
0xA2 Power off ↑ function path; payload = empty → on the wire A2 80
0xA3 Work-unit extension ↑ function path; payload = a caller-supplied List<int>
(pass-through)
0xA4 Pressure-report toggle ↑ op write; payload = [on?1:0]
0xA5 Sensor-report toggle ↑ op write; payload = [on?1:0]

Among these, 0xA0 is the most important: it sets the instantaneous strength of each motor. The payload for this CMD is:

1
2
3
payload = [ N, pwm_0, pwm_1, ..., pwm_{N-1} ]
N = number of motors (payload[0])
pwm_i = strength of motor i, integer 0..100

The complete on-air frame for a Write Without Response to 0x9002:

1
2
3
4
5
[0] = 0xA0
[1] = 0x80 | ((N+1) << 3) // ACTION=1 (write); LEN = N+1 (count byte + N pwm bytes)
[2] = N
[3 + i] = pwm_i // i = 0..N-1
total = N + 3 bytes

For example:

Intent Bytes (hex)
1 motor @ 50 A0 90 01 32
1 motor @ 100 A0 90 01 64
1 motor OFF A0 90 01 00
2 motors @ (100, 0) A0 98 02 64 00
2 motors OFF A0 98 02 00 00
3 motors @ (30,60,90) A0 A0 03 1E 3C 5A

Rules

At runtime the following rules must be observed for control to succeed:

  • Deduplication: the app drops a frame identical to the previously sent frame (_isSamePwm). If you re-send the same PWM, it may be a no-op at the app layer — send on change; the device holds the state of the last command until a new frame or power-off. To stop, send zeros (A0 … 00 …) or 0xA2.
  • Value range: pwm_i ∈ [0,100]. In the app’s own pipeline, an active motor is floored to 10 (values 1–9 are raised to 10; 0 stays 0) and clamped to the device’s per-motor [min,max] (default max 100; the format tolerates up to 200, but no factory default exceeds 100). If you drive the device directly, you can send any value 0–100; the device firmware is expected to accept 0–100.
  • Cadence: the waveform/playback engine ticks at a high rate (nominally about 8 ms, raw 16 on the internal timer) and calls the send path on every tick, but the serial queue (§11) + the dedup gate determine the actual over-the-air rate. In practice: send only on PWM change; there is no artificial minimum interval in the queue, but keep BLE reasonable (≤ about 50–60 Hz).
  • Written as Write Without Response → no ACK, lowest latency

Reference Pseudocode

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
WRITE_CHAR  = 0x9002   # function: vibration/power-off/extend, Write Without Response
OP_CHAR = 0x9001 # op: reads/config, Write With Response
CCCD = 0x2902

def frame(cmd, payload=b"", write=True):
assert len(payload) <= 15
action = 1 if write else 0
return bytes([cmd & 0xFF, (action << 7) | (len(payload) << 3)]) + bytes(payload)

# --- vibration ---
def vibrate(motors): # motors = [pwm0, pwm1, ...], each 0..100
payload = bytes([len(motors)]) + bytes(max(0, min(100, p)) for p in motors)
ble_write(WRITE_CHAR, frame(0xA0, payload, write=True), response=False)

def stop(n_motors): vibrate([0]*n_motors)
def power_off(): ble_write(WRITE_CHAR, frame(0xA2, b"", write=True), response=False)

# --- reads (op path) ---
async def read(cmd, timeout_ms=300):
fut = pending.register(cmd) # match the response by cmd
ble_write(OP_CHAR, frame(cmd, b"", write=False), response=True) # [cmd, 0x00]
return await fut.timeout(timeout_ms)

# --- notification parser (registered on both 0x9001 and 0x9002) ---
def on_notify(data):
if len(data) < 2: return
cmd = data[0]
action = data[1] >> 7
length = (data[1] >> 3) & 0x0F
if len(data) != length + 2: return # invalid frame
payload = data[2:2+length]
if action == 0: # reply to a read
pending.resolve(cmd, payload)
else: # device push
if cmd == 0x14: battery = payload[0] # 0..100
elif cmd == 0x15: on_heartbeat(payload)
elif cmd == 0xA1: on_key(payload)
# ... 0x18 / 0xA4 / 0xA5

The connection sequence to follow:

connect → discoverServices → sleep 25ms → requestMtu → sleep 50ms → enable notify (CCCD) on 0x9001 & 0x9002 → (optional) verify 0x00 + read info → ready

Then stream vibrate frames whenever there is a change.