I have been trying to implement an single choice dialog where you select one of the TxPower options and depending the option you select (-23dBm-> decimal=0) you'll have one value or other. What I want to do is to transform that int value to hex value and then call a function I created to transform it into byte and return the final value. I would like to call this function because it is generally called by the program.
Here is the code of the single choice dialog:
fields.writeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
UUID uuid = mCharacteristic.getUuid();
if (uuid.equals(BleDefinedUUIDs.Characteristic.TxPower)){
final CharSequence[] choice = {"-23dBm","-6dBm","0dBm","4dBm"};
new AlertDialog.Builder(v.getContext())
.setSingleChoiceItems(choice, 0, null)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
int selectedPosition = ((AlertDialog)dialog).getListView().getCheckedItemPosition();
if (selectedPosition == 0) {
String hex = Integer.toHexString(selectedPosition).toLowerCase(Locale.getDefault());
byte[] dataToWrite = parseHexStringToBytes(hex);
mBleWrapper.writeDataToCharacteristic(mCharacteristic, dataToWrite);
}
if (selectedPosition == 1) {
String hex = Integer.toHexString(selectedPosition).toLowerCase(Locale.getDefault());
byte[] dataToWrite = parseHexStringToBytes(hex);
mBleWrapper.writeDataToCharacteristic(mCharacteristic, dataToWrite);
}
if (selectedPosition == 2) {
String hex = Integer.toHexString(selectedPosition).toLowerCase(Locale.getDefault());
byte[] dataToWrite = parseHexStringToBytes(hex);
mBleWrapper.writeDataToCharacteristic(mCharacteristic, dataToWrite);
}
if (selectedPosition == 3) {
String hex = Integer.toHexString(selectedPosition).toLowerCase(Locale.getDefault());
byte[] dataToWrite = parseHexStringToBytes(hex);
mBleWrapper.writeDataToCharacteristic(mCharacteristic, dataToWrite);
}
}
})
.show();
}
}
});
And here is the function parseHexStringToBytes which transforms the returns the final byte value:
public byte[] parseHexStringToBytes(final String hex) {
String tmp = hex.substring(2).replaceAll("[^[0-9][a-f]]", "");
byte[] bytes = new byte[tmp.length() / 2]; // every two letters in the string are one byte finally
String part = "";
for(int i = 0; i < bytes.length; ++i) {
part = "0x" + tmp.substring(i*2, i*2+2);
bytes[i] = Long.decode(part).byteValue();
}
return bytes;
}
And finally, this is the logcat of the error itself:
07-25 16:38:56.467: E/AndroidRuntime(7326): FATAL EXCEPTION: main
07-25 16:38:56.467: E/AndroidRuntime(7326): Process: org.bluetooth.bledemo, PID: 7326
07-25 16:38:56.467: E/AndroidRuntime(7326): java.lang.StringIndexOutOfBoundsException: length=1; index=2
07-25 16:38:56.467: E/AndroidRuntime(7326): at java.lang.String.indexAndLength(String.java:584)
07-25 16:38:56.467: E/AndroidRuntime(7326): at java.lang.String.substring(String.java:1449)
07-25 16:38:56.467: E/AndroidRuntime(7326): at org.bluetooth.bledemo.CharacteristicDetailsAdapter.parseHexStringToBytes(CharacteristicDetailsAdapter.java:102)
07-25 16:38:56.467: E/AndroidRuntime(7326): at org.bluetooth.bledemo.CharacteristicDetailsAdapter$2$1.onClick(CharacteristicDetailsAdapter.java:171)
07-25 16:38:56.467: E/AndroidRuntime(7326): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
07-25 16:38:56.467: E/AndroidRuntime(7326): at android.os.Handler.dispatchMessage(Handler.java:102)
07-25 16:38:56.467: E/AndroidRuntime(7326): at android.os.Looper.loop(Looper.java:136)
07-25 16:38:56.467: E/AndroidRuntime(7326): at android.app.ActivityThread.main(ActivityThread.java:5579)
07-25 16:38:56.467: E/AndroidRuntime(7326): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 16:38:56.467: E/AndroidRuntime(7326): at java.lang.reflect.Method.invoke(Method.java:515)
07-25 16:38:56.467: E/AndroidRuntime(7326): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
07-25 16:38:56.467: E/AndroidRuntime(7326): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
07-25 16:38:56.467: E/AndroidRuntime(7326): at dalvik.system.NativeStart.main(Native Method)
Note: The line 171 corresponds to calling the parseHexStringToBytes when the selectposition ==1 and the line 102 is the String tmp from the function parseHexStringToBytes.
If you require any further information, please let me know but I seriously need someone to help me out with this problem