Reputation: 169
So I tri to set button color. it something like this:
if (D) {Log.d(TAG, "color =" + bytes);};
int c = Color.argb(bytes[4], bytes[3], bytes[2], bytes[1]);
btnStart.setBackgroundColor(c);
if (D) {Log.d(TAG, "color " + bytes[4]+ bytes[3]+bytes[2]+ bytes[1]);};
break;
i get the following output to LogCat: color = [B@40534710 color -1-1-1-1 how this is happening? i am expecting to see some other values in the array, not -1...
Here goes the full code
mainHandler=new Handler(){
public void handleMessage(Message msg) {
switch (msg.what){
case TOAST:
Bundle bundle = msg.getData();
String string = bundle.getString("myKey");
Toast.makeText(getApplicationContext(), string, Toast.LENGTH_SHORT).show();
break;
case NEW_SMS:
if (D) {Log.d(TAG, "newSms recieved");}
byte[] bytes =(byte[]) msg.obj;
switch (bytes[0]){
case SMS_TEXT:
bytes = shiftArrayToLeft(bytes);
String readMessage = new String(bytes, 0, msg.arg1);
txtView.setText(readMessage);
break;
case SMS_COLOR:
if (D) {Log.d(TAG, "color =" + bytes);};
//LinearLayout lLayout = (LinearLayout) findViewById(R.id.lLayout);
int c = Color.argb(bytes[4], bytes[3], bytes[2], bytes[1]);
btnStart.setBackgroundColor(c);
if (D) {Log.d(TAG, "color " + bytes[4]+ bytes[3]+bytes[2]+ bytes[1]);};
break;
}
}
}};
this is handler that handles the bluetooth message
Upvotes: 0
Views: 100
Reputation: 2456
What is the type of your bytes
array? If it is an array of byte[]
then you have a problem, because byte
s are signed integers with a range from -128 to 127, while the Color.argb()
constructor expects 4 int
s in the range of 0 to 255. This means that if any elements of your byte array contain a negative value, the Color.argb()
call will fail. The documentation says:
These component values should be [0..255], but there is no range check performed, so if they are out of range, the returned color is undefined.
Anyway, there is no unsigned byte type in Java so you have to manually ensure the values are converted from -128 to 127 range to integers in the 0 to 255 range. Something like this should work:
int c = Color.argb(((int)bytes[4]) % 256, ((int)bytes[3]) % 256, ((int)bytes[2]) % 256, ((int)bytes[1]) % 256);
There may be a more elegant solution but this will at least confirm whether that is your problem.
Upvotes: 1