BLEUtility.java 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package com.epson.mobilephone.common.ble.util;
  2. import android.bluetooth.BluetoothManager;
  3. import android.content.Context;
  4. public class BLEUtility {
  5. public static final int GATT_BUSY = 132;
  6. public static final int GATT_DB_FULL = 131;
  7. public static final int GATT_INTERNAL_ERROR = 129;
  8. public static final int GATT_WRONG_STATE = 130;
  9. public interface BleWorkCallback {
  10. void call(Object obj);
  11. }
  12. private BLEUtility() {
  13. }
  14. public static boolean isBLESupported(Context context) {
  15. return context.getPackageManager().hasSystemFeature("android.hardware.bluetooth_le");
  16. }
  17. public static BluetoothManager getManager(Context context) {
  18. return (BluetoothManager) context.getSystemService("bluetooth");
  19. }
  20. @NonNull
  21. public static String IntToHex2(int i) {
  22. return new String(new char[]{Character.forDigit((i >> 4) & 15, 16), Character.forDigit(i & 15, 16)}).toUpperCase();
  23. }
  24. public static boolean isThreadAliveParentGroup(String str) {
  25. if (str == null) {
  26. return false;
  27. }
  28. ThreadGroup parent = Thread.currentThread().getThreadGroup().getParent();
  29. Thread[] threadArr = new Thread[parent.activeCount()];
  30. parent.enumerate(threadArr, true);
  31. int length = threadArr.length;
  32. int i = 0;
  33. while (i < length) {
  34. Thread thread = threadArr[i];
  35. if (thread == null || !thread.getName().equals(str) || !thread.isAlive()) {
  36. i++;
  37. } else {
  38. EpLog.d("" + thread);
  39. return true;
  40. }
  41. }
  42. return false;
  43. }
  44. @Nullable
  45. public static Thread getThread(String str) {
  46. if (str == null) {
  47. return null;
  48. }
  49. Thread.currentThread();
  50. Thread[] threadArr = new Thread[Thread.activeCount()];
  51. Thread.enumerate(threadArr);
  52. int length = threadArr.length;
  53. int i = 0;
  54. while (i < length) {
  55. Thread thread = threadArr[i];
  56. if (thread == null || !thread.getName().equals(str)) {
  57. i++;
  58. } else {
  59. EpLog.m77i("find thread : " + str);
  60. return thread;
  61. }
  62. }
  63. return null;
  64. }
  65. public static boolean isThreadAlive(String str) {
  66. Thread thread = getThread(str);
  67. return thread != null && thread.isAlive();
  68. }
  69. }