Search code examples
ioslibgdxmultipeer-connectivityrobovm

Bug in receiving data using MPC framework in libgdx game


We're trying to make a multipeer connection between two devices using MPC framework in libgdx game.

What generally we've done successfully:

  1. Devices are connecting, session is establishing correctly.
  2. After session is established nearBrowser and nearAdvertiser stop doing their stuff.
  3. Then we do transition to the game scene. In the new scene one device can send a message to another.
  4. DidReceiveData method from Session Delegate is called and there we've got right messages for both devices.
  5. After this we send to libgdx message for updating content (in main gdx thread).

BUT after a while when some device received data it immediately crashes. Sometimes it happens on 10th receiving, sometimes after 200th. Crash appears only on the device that received message. It doesn't matter how long they are connected. Crash appears after all methods have done their work with data. So we don't know where exactly error happens.

// MCSession delegate method

public void didReceiveData(MCSession session, NSData data, MCPeerID peerID) {

 //there we make userInfoData

 // 
 DispatchQueue.getMainQueue().async(new Runnable() {
  @Override
  public void run() {
        NSNotificationCenter.getDefaultCenter().postNotification(new NSString("didReceiveData"), null,  userInfoData);
  }
  });

}

// Register observer in NSNotificationCenter
// NSNotificationCenter.getDefaultCenter().addObserver(this, Selector.register("updateDataWithNotification:"), new NSString("didReceiveData"), null);

// This method is called when device has received new data

@Method
private void updateDataWithNotification(NSNotification notification){

 userInfoDict = notification.getUserInfo();
 data = (NSData) userInfoDict.get(new NSString("data"));
 strBytes =  new String(data.getBytes());

 // i'm not sure this Gdx.app.postRunnable is really needed         
 Gdx.app.postRunnable(new Runnable() {
    @Override
    public void run() { 
  SBGlobalMessanger.getInstance().readBluetoothMessage(BluetoothData.RC_MESSAGE, strBytes);
    }
 });
}

The questions are: Where is the bug? And how can we fix it?


Solution

  • The problem was in robovm plugin. In debug mode it made build that crushed. After making release build bug disappeared. The thing i have learned after working with robovm + libgdx is if you have strange bug just make a release build. It seems that this kind of bugs was eliminated with the last release of robovm 1.3 (i haven't try it out yet).