Created
April 22, 2016 03:41
-
-
Save oilking143/36ac5433d9f98c6cf7080e9499b39797 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tw.com.acme_soft.mibandscanner; | |
import android.app.Activity; | |
import android.bluetooth.BluetoothDevice; | |
import android.util.Log; | |
import android.widget.Toast; | |
import com.zhaoxiaodan.miband.ActionCallback; | |
import com.zhaoxiaodan.miband.MiBand; | |
import com.zhaoxiaodan.miband.listeners.NotifyListener; | |
import java.io.UnsupportedEncodingException; | |
import java.math.BigInteger; | |
import tw.com.acme_soft.mibandscanner.interFace.contentToFragment; | |
/** | |
* Created by Oil on 2016/4/14. | |
*/ | |
public class utility { | |
private contentToFragment content; | |
public String parseHex(String temp) | |
{ | |
int ittegertemp=0; | |
String tmp1=temp.substring(0,1); | |
String tmp2=temp.substring(1,2); | |
String[] hex={"0","1","2","3","4","5","6","7","8","A","B","C","D","E","F"}; | |
for (int i=0;i<hex.length;i++) | |
{ | |
if(tmp1.equals(hex[i])) | |
{ | |
ittegertemp+=i*16; | |
} | |
if(tmp2.equals(hex[i])) | |
{ | |
ittegertemp+=i; | |
} | |
} | |
return Integer.toString(ittegertemp)+"°C"; | |
} | |
public void parseData(String data,final Activity act) | |
{ | |
String[] parsedData=new String[5]; | |
if (data.length() > 15) | |
{ | |
parsedData[0] = data.substring(10, 18); | |
parsedData[1] = data.substring(26, 30); | |
parsedData[2] = data.substring(22, 26); | |
parsedData[3] = data.substring(18, 22); | |
parsedData[4] = data.substring(8, 10); | |
switch (Integer.parseInt(parsedData[4])) { | |
case 0: | |
((TPMSApplication) act.getApplication()).setLFstatusVariable(parsedData); | |
Log.d("LF", ((TPMSApplication) act.getApplication()).getLFstatusVariable()[0]); | |
content.callOut(parsedData); | |
break; | |
case 1: | |
((TPMSApplication) act.getApplication()).setRFstatusVariable(parsedData); | |
Log.d("RF", ((TPMSApplication) act.getApplication()).getRFstatusVariable()[0]); | |
break; | |
case 2: | |
((TPMSApplication) act.getApplication()).setLBstatusVariable(parsedData); | |
Log.d("LB", ((TPMSApplication) act.getApplication()).getLBstatusVariable()[0]); | |
break; | |
case 3: | |
((TPMSApplication) act.getApplication()).setRBstatusVariable(parsedData); | |
Log.d("RB", ((TPMSApplication) act.getApplication()).getRBstatusVariable()[0]); | |
break; | |
} | |
} | |
} | |
public String[] getLFdata(final Activity act) | |
{ | |
String[] data=((TPMSApplication) act.getApplication()).getLFstatusVariable(); | |
return data; | |
} | |
public String[] getRFdata(final Activity act) | |
{ | |
String[] data=((TPMSApplication) act.getApplication()).getRFstatusVariable(); | |
return data; | |
} | |
public String[] getLBdata(final Activity act) | |
{ | |
String[] data=((TPMSApplication) act.getApplication()).getLBstatusVariable(); | |
return data; | |
} | |
public String[] getRBdata(final Activity act) | |
{ | |
String[] data=((TPMSApplication) act.getApplication()).getRBstatusVariable(); | |
return data; | |
} | |
public String parseBattery(String temp) | |
{ | |
int ittegertemp=0; | |
String tmp1=temp.substring(0,1); | |
String tmp2=temp.substring(1,2); | |
String[] hex={"0","1","2","3","4","5","6","7","8","A","B","C","D","E","F"}; | |
for (int i=0;i<hex.length;i++) | |
{ | |
if(tmp1.equals(hex[i])) | |
{ | |
ittegertemp+=i*16; | |
} | |
if(tmp2.equals(hex[i])) | |
{ | |
ittegertemp+=i; | |
} | |
} | |
String cal=Integer.toString(ittegertemp); | |
cal=cal.substring(0,1)+"."+cal.substring(1,2)+"V"; | |
return cal; | |
} | |
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray(); | |
public static String bytesToHex(byte[] bytes) { | |
char[] hexChars = new char[bytes.length * 2]; | |
for ( int j = 0; j < bytes.length; j++ ) { | |
int v = bytes[j] & 0xFF; | |
hexChars[j * 2] = hexArray[v >>> 4]; | |
hexChars[j * 2 + 1] = hexArray[v & 0x0F]; | |
} | |
return new String(hexChars); | |
} | |
public String byte2Hex(byte[] b) { | |
String result = ""; | |
for (int i=0 ; i<b.length ; i++) | |
result += Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 ); | |
return result; | |
} | |
public String string2Hex(String plainText, String charset) throws UnsupportedEncodingException { | |
return String.format("%040x", new BigInteger(1, plainText.getBytes(charset))); | |
} | |
public byte[] hex2Byte(String hexString) { | |
byte[] bytes = new byte[hexString.length() / 2]; | |
for (int i=0 ; i<bytes.length ; i++) | |
bytes[i] = (byte) Integer.parseInt(hexString.substring(2 * i, 2 * i + 2), 16); | |
return bytes; | |
} | |
public String hex2String(String hexString) { | |
StringBuilder str = new StringBuilder(); | |
for (int i=0 ; i<hexString.length() ; i+=2) | |
str.append((char) Integer.parseInt(hexString.substring(i, i + 2), 16)); | |
return str.toString(); | |
} | |
public byte[] hexStringToByteArray(String s) { | |
int len = s.length(); | |
byte[] data = new byte[len / 2]; | |
for (int i = 0; i < len; i += 2) { | |
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) | |
+ Character.digit(s.charAt(i+1), 16)); | |
} | |
return data; | |
} | |
public byte[] InttoByte(int Num, int ByteSize) { | |
byte[] OutByte = new byte[ByteSize]; | |
for (int i = 0; (i < 4) && (i < ByteSize); i++) { | |
OutByte[ByteSize - 1 - i] = (byte) (Num >> 8 * i & 0xFF); | |
} | |
return OutByte; | |
} | |
public void setcalloutListener(contentToFragment _content) | |
{ | |
content=_content; | |
} | |
public void setMiband(final MiBand miband , final Activity inneract,BluetoothDevice device) | |
{ | |
miband.connect(device, new ActionCallback() { | |
@Override | |
public void onSuccess(Object data) { | |
Log.d("TAG", "連線成功"); | |
inneract.runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
Toast.makeText(inneract, "已連線", Toast.LENGTH_SHORT).show(); | |
miband.setTPMSNotifyListener(new NotifyListener() { | |
@Override | |
public void onNotify(byte[] data) { | |
Log.d("dataTPMS:", utility.bytesToHex(data)); | |
parseData(utility.bytesToHex(data),inneract); | |
miband.SynchronizeTPMS(); | |
} | |
}); | |
} | |
}); | |
miband.setDisconnectedListener(new NotifyListener() { | |
@Override | |
public void onNotify(byte[] data) { | |
Log.d("TAG", "已斷線"); | |
inneract.runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
Toast.makeText(inneract, "已斷線", Toast.LENGTH_SHORT).show(); | |
} | |
}); | |
} | |
}); | |
} | |
@Override | |
public void onFail(int errorCode, String msg) { | |
Log.d("TAG", "連線失敗, ErrorCode: " + errorCode + ", MSG: " + msg); | |
inneract.runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
Toast.makeText(inneract, "連線失敗", Toast.LENGTH_SHORT).show(); | |
} | |
}); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment