Advertisement
Guest User

Untitled

a guest
Sep 27th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.18 KB | None | 0 0
  1. public class BluetoothOperationModule {
  2.  
  3.     private static final String TAG = BluetoothOperationModule.class.getSimpleName();
  4.     private static UUID serviceUUID;
  5.     private static UUID characteristicUUID;
  6.     private static BluetoothDevice device;
  7.     private static Context context;
  8.     private static BluetoothOperationCallback callback;
  9.     private static BluetoothGatt bluetoothGatt;
  10.     private static int connectionState;
  11.     private static BluetoothGattCharacteristic characteristic;
  12.     private static BluetoothGattCharacteristic charRX;
  13.  
  14.     public interface BluetoothOperationCallback {
  15.         void onServicesDiscovered();
  16.  
  17.         void onServicesNotSupported();
  18.  
  19.         void onConnected();
  20.  
  21.         void onDisconnected();
  22.  
  23.         void onMessageWritten(BluetoothGattCharacteristic characteristic);
  24.     }
  25.  
  26.     private static final BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
  27.  
  28.         @Override
  29.         public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
  30.             TestLog.writeLog(TAG + " onConnectionStateChange");
  31.             if (newState == BluetoothProfile.STATE_CONNECTED) {
  32.                 connectionState = ConnectionState.STATE_CONNECTED;
  33.                 TestLog.writeLog("Connected to GATT server.");
  34.                 if (null == bluetoothGatt && null != gatt) {
  35.                     checkEquals(gatt);
  36.                     bluetoothGatt = gatt;
  37.                 }
  38.                 if (null != bluetoothGatt) {
  39.                     bluetoothGatt.discoverServices();
  40.                 }
  41.                 callback.onConnected();
  42.             } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
  43.                 TestLog.writeLog("Disconnected from GATT server.");
  44.                 finishModule();
  45.                 connectionState = ConnectionState.STATE_DISCONNECTED;
  46.                 callback.onDisconnected();
  47.             }
  48.         }
  49.  
  50.         @Override
  51.         public void onServicesDiscovered(BluetoothGatt gatt, int status) {
  52.             TestLog.writeLog(TAG + " onServicesDiscovered");
  53.             checkEquals(gatt);
  54.             bluetoothGatt = gatt;
  55.             if (status == BluetoothGatt.GATT_SUCCESS) {
  56.                 TestLog.writeLog("Service Discovered with status success");
  57.                 if (!saveCharacteristic()) {
  58.                     disconnect();
  59.                 }
  60.                 callback.onServicesDiscovered();
  61. //                } else {
  62. //                    callback.onServicesNotSupported();
  63. //                }
  64.             }
  65.         }
  66.  
  67.         @Override
  68.         public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
  69.             checkEquals(gatt);
  70.             bluetoothGatt = gatt;
  71.             TestLog.writeLog(new Date().toString() + "onCharacteristicWrite");
  72.             if (status == BluetoothGatt.GATT_SUCCESS) {
  73.                 callback.onMessageWritten(characteristic);
  74.                 TestLog.writeLog("onCharacteristicWrite: s");
  75.             } else {
  76.                 TestLog.writeLog("onCharacteristicWrite received: " + status);
  77.                 TestLog.writeLog("onCharacteristicWrite: f");
  78.             }
  79.         }
  80.  
  81.         @Override
  82.         public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
  83.             checkEquals(gatt);
  84.             bluetoothGatt = gatt;
  85.             TestLog.writeLog("onCharacteristicChanged");
  86.             super.onCharacteristicChanged(gatt, characteristic);
  87.         }
  88.     };
  89.  
  90.     private static void checkEquals(BluetoothGatt gatt) {
  91. //        if(bluetoothGatt == null){
  92. //            TestLog.writeLog("Try replace bluetoothGatt. Current Gat == null");
  93. //        }else {
  94. //            TestLog.writeLog("Try replace bluetoothGatt. Equals: " + String.valueOf(bluetoothGatt.equals(gatt)));
  95. //        }
  96.     }
  97.  
  98.     private static boolean saveCharacteristic() {
  99.         BluetoothGattService gattService = bluetoothGatt.getService(serviceUUID);
  100.         TestLog.writeLog("SaveCharacteristic gattService != null = " + String.valueOf(gattService != null));
  101.         if (gattService != null) {
  102.             characteristic = gattService.getCharacteristic(characteristicUUID);
  103.             if(characteristic == null){
  104.                 return false;
  105.             }
  106.             charRX = gattService.getCharacteristic(characteristicUUID);
  107.             setCharacteristicNotification(charRX, true);
  108.  
  109.             TestLog.writeLog("SaveCharacteristic characteristic != null = " + String.valueOf(characteristic != null));
  110.             if (characteristic != null) {
  111.                 return true;
  112.             }
  113.         }
  114.         return false;
  115.     }
  116.  
  117.     public static void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) {
  118.         TestLog.writeLog(TAG + " setCharacteristicNotification");
  119.         if(!BuildConfig.DEBUG) {
  120.             if (bluetoothGatt == null) {
  121.                 return;
  122.             }
  123.             bluetoothGatt.setCharacteristicNotification(characteristic, enabled);
  124.             if (DeviceConfig.UUID_HM_RX_TX.equals(characteristic.getUuid())) {
  125.  
  126.                 BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(DeviceConfig.CLIENT_CHARACTERISTIC_CONFIG));
  127.                 descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
  128.                 bluetoothGatt.writeDescriptor(descriptor);
  129.                 if (2 != characteristic.getWriteType()) {
  130.                     characteristic.setWriteType(2);
  131.                 }
  132.             }
  133.         }
  134.     }
  135.  
  136.     public static void setServiceUUID(UUID serviceUUID) {
  137.         BluetoothOperationModule.serviceUUID = serviceUUID;
  138.     }
  139.  
  140.     public static void setCharacteristicUUID(UUID characteristicUUID) {
  141.         BluetoothOperationModule.characteristicUUID = characteristicUUID;
  142.     }
  143.  
  144.     public static void connect(BluetoothDevice device, Context context, BluetoothOperationCallback callback) {
  145.         if(BuildConfig.DEBUG) {
  146.             TestLog.writeLog("lib: debug");
  147.         }else{
  148.             TestLog.writeLog("lib: release");
  149.         }
  150.         TestLog.writeLog("Trying to create a new connection.");
  151.         BluetoothOperationModule.device = device;
  152.         BluetoothOperationModule.context = context;
  153.         BluetoothOperationModule.callback = callback;
  154.         TestLog.writeLog("Device: " + device.toString() + " " + device.getName() + " " + device.getAddress());
  155.  
  156.         bluetoothGatt = device.connectGatt(context, false, gattCallback);
  157.         connectionState = ConnectionState.STATE_CONNECTING;
  158.     }
  159.  
  160.     public static void disconnect() {
  161.         if (bluetoothGatt != null) {
  162.             TestLog.writeLog(TAG + "Trying to disconnect.");
  163.             bluetoothGatt.disconnect();
  164.         }
  165.     }
  166.  
  167.     public static void close() {
  168.         if (bluetoothGatt == null) {
  169.             return;
  170.         }
  171.         bluetoothGatt.close();
  172.         bluetoothGatt = null;
  173.     }
  174.  
  175.     public static int writeCharacteristic(byte[] message) {
  176.         //TODO whet test - commit it
  177.         if(!BuildConfig.DEBUG) {
  178.             if (connectionState == ConnectionState.STATE_CONNECTING ||
  179.                     connectionState == ConnectionState.STATE_DISCONNECTED ||
  180.                     bluetoothGatt == null) {
  181.                 TestLog.writeLog("close writeCharacteristic");
  182.                 return WriteCommandResult.RESULT_NO_CONNECTION;
  183.             }
  184.             boolean result;
  185.             if (characteristic == null) {
  186.                 if (!saveCharacteristic()) {
  187.                     disconnect();
  188.                     return WriteCommandResult.RESULT_NO_CONNECTION;
  189.                 }
  190.             }
  191.             characteristic.setValue(message);
  192.             result = bluetoothGatt.writeCharacteristic(characteristic);
  193.             TestLog.writeLog("Write command: " + new String(message) + " res: " + String.valueOf(result));
  194.             return result ? WriteCommandResult.RESULT_OK : WriteCommandResult.RESULT_CANCEL;
  195.         }else{
  196.             return WriteCommandResult.RESULT_OK;
  197.         }
  198.     }
  199.  
  200.     public static void finishModule() {
  201.         TestLog.writeLog("finishModule");
  202.         disconnect();
  203.         close();
  204.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement