@@ -139,35 +139,56 @@ private void setCharacteristicNotification(BluetoothGatt gatt,
139139 }
140140
141141 private void handleButtonEvent (byte [] data ) {
142+ if (data == null || data .length == 0 ) {
143+ return ;
144+ }
145+
142146 int type = data [0 ] & 0xFF ;
143147
144148 if (DEBUG ) Log .d (LOG_TAG , "BUTTON_EVENT raw: len=" + data .length +
145149 " data=" + Arrays .toString (data ));
146150
147- ButtonAction button = ButtonAction .fromType (type );
148- if (button != null ) {
149- switch (button .getAction ()) {
150- case DOWN :
151- mButtonDownTime = SystemClock .uptimeMillis ();
152- break ;
153- case UP :
154- if (!mButtonHadGesture ) {
155- SPenMode mode = SPenMode .valueOfPref (SettingsUtils .getSwitchPreference (
156- mContext , SettingsUtils .SPEN_MODE , "0" ));
157- // Can be used for e.g taking photos
158- sendEvent (mButtonDownTime , KeyEvent .ACTION_DOWN , mode .getButtonKey ());
159- sendEvent (KeyEvent .ACTION_UP , mode .getButtonKey ());
160- }
161- mButtonHadGesture = false ;
162- break ;
163- default :
164- break ;
151+ // Batched packets (newer firmware)
152+ if (type == 0xF0 && data .length >= 8 ) {
153+ // Motion records start at offset 2
154+ for (int i = 2 ; i + 5 < data .length ; i += 6 ) {
155+ MotionEvent move = MotionEvent .fromData (data , i );
156+ handleGesture (move );
165157 }
166158 return ;
167159 }
168160
169- MotionEvent move = MotionEvent .fromTypeData (type , data );
170- handleGesture (move );
161+ // Legacy motion handling (for old firmware)
162+ if (type == 0x0F && data .length >= 5 ) {
163+ MotionEvent move = MotionEvent .fromData (data , 0 );
164+ handleGesture (move );
165+ return ;
166+ }
167+
168+ // Button events (single-byte packets)
169+ ButtonAction button = ButtonAction .fromType (type );
170+ if (button == null ) {
171+ Log .w (LOG_TAG , "Unknown BUTTON_EVENT type: " + type );
172+ return ;
173+ }
174+
175+ switch (button .getAction ()) {
176+ case DOWN :
177+ mButtonDownTime = SystemClock .uptimeMillis ();
178+ break ;
179+ case UP :
180+ if (!mButtonHadGesture ) {
181+ SPenMode mode = SPenMode .valueOfPref (SettingsUtils .getSwitchPreference (
182+ mContext , SettingsUtils .SPEN_MODE , "0" ));
183+ // Can be used for e.g taking photos
184+ sendEvent (mButtonDownTime , KeyEvent .ACTION_DOWN , mode .getButtonKey ());
185+ sendEvent (KeyEvent .ACTION_UP , mode .getButtonKey ());
186+ }
187+ mButtonHadGesture = false ;
188+ break ;
189+ default :
190+ break ;
191+ }
171192 }
172193
173194 private void handleGesture (MotionEvent move ) {
0 commit comments