Java Question Library for this piece of code

bluedude

Well-Known Member
Licensed User
Longtime User
Hi,

How hard would it be to create a library for below piece of code?

AndroidManifest.xml
Since DICE+ uses Bluetooth technology, you need to get permission from the system to access the BT radio.

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

MainActivity
Before any methods starts, create the following variables. Where the most important variable in our project is the API Key. With this key you can authenticate communication with DICE+

public static final int[] developerKey = new int[] {0x83, 0xed, 0x60, 0x0e, 0x5d, 0x31, 0x8f, 0xe7};
private Die dicePlus;
TextView TVResult;

DiceScanningListener
After we have our basic variables, we need to register our listeners so we can receive all the events regarding our DICE+. We start of by creating our DiceScanningListener. While writing the code DiceScanningListener scanningListener = new DiceScanningListener(), Android Studio will complete the code for you with all the related methods.

onNewDie is the method that interests us. It will be executed each time the SDK finds a DICE+. When this method is fired we want to save our dice in the object that we created at the beginning of our class and start the connection process.

DiceScanningListener scanningListener = new DiceScanningListener() {
@Override
public void onNewDie(Die die) {
dicePlus = die;
DiceController.connect(dicePlus);
}

@Override
public void onScanStarted() {}

@Override
public void onScanFailed() {
BluetoothManipulator.startScan();
}

@Override
public void onScanFinished() {
if(dicePlus == null) {
BluetoothManipulator.startScan();
}
}
};

DiceConnectionListener
We started the connection process and now we need to know when DICE+ gets connected. For this we add the DiceConnectionListener and wait for the onConnectionEstablished to fire. Once that happens we know that everything went ok. Now we can ask DICE+ to send us information from the sensor. In this case we subscribe for each roll.

DiceConnectionListener connectionListener = new DiceConnectionListener() {
@Override
public void onConnectionEstablished(Die die) {
Log.d(TAG, "DICE+ Connected");

// Signing up for roll events
DiceController.subscribeRolls(dicePlus);
}

@Override
public void onConnectionFailed(Die die, Exception e) {
Log.d(TAG, "Connection failed", e);

dicePlus = null;

BluetoothManipulator.startScan();
}

@Override
public void onConnectionLost(Die die) {
Log.d(TAG, "Connection lost");

dicePlus = null;

BluetoothManipulator.startScan();
}
};

DiceResponseListener
Now that we asked DICE+ to send us some specific information. We need to tell our code to listen to those messages. Since we asked for each roll, in the DiceResponseListener we add the method onRoll. Now, every time your throw DICE+, the method onRoll will be executed.

DiceResponseListener responseListener = new DiceResponseAdapter() {
@Override
public void onRoll(Die die, RollData rollData, Exception e) {
super.onRoll(die, rollData, e);

Log.d(“DICEPlus”, "Roll: " + rollData.face);
}
};

One important thing
Since onRoll is working on a separate thread, to update the main interface for example, you need to access the main thread using the following solution. Just past following code after the Log method which is inside onRoll. Remember also that you will need to set your variable or object to final or you won’t be able to access it on another thread.

final int face = rollData.face;

MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
TVResult.setText(""+face);
}
});

How to handle the connectivity with DICE+
It is really important to remember that DICE+ needs to connect and disconnect form your game in the most reliable way possible. That is why you should fire up all your listeners and start scanning in the onResume method.

@Override
protected void onResume() {
super.onResume();

TVResult = (TextView) findViewById(R.id.TVResult);

// Initiating
BluetoothManipulator.initiate(this);
DiceController.initiate(developerKey);

// Listen to all the state occurring during the discovering process of DICE+
BluetoothManipulator.registerDiceScanningListener(scanningListener);

// When connecting to DICE+ you get two responses: a good one and a bad one ;)
DiceController.registerDiceConnectionListener(connectionListener);

// Attaching to DICE+ events that we subscribed to.
DiceController.registerDiceResponseListener(responseListener);

// Scan for a DICE+
BluetoothManipulator.startScan();
}

In the onStop method you should always unregister your listeners, disconnect from DICE+ and delete your dice object. Using this two methods will guarantee you the best effect.

@Override
protected void onStop() {
super.onStop();

Log.d(TAG, "onStop");

DiceController.unregisterDiceConnectionListener(connectionListener);
BluetoothManipulator.unregisterDiceScanningListener(scanningListener);
DiceController.unregisterDiceResponseListener(responseListener);

DiceController.disconnectDie(dicePlus);
dicePlus = null;
}
 

bocker77

Active Member
Licensed User
Longtime User
Bluedude,

Have you had any luck in communicating with the Dice+ device? If so can you provide me with any information on how you did it?

Thanks
 
Top