Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BluetoothOperationModule {
- private static final String TAG = BluetoothOperationModule.class.getSimpleName();
- private static UUID serviceUUID;
- private static UUID characteristicUUID;
- private static BluetoothDevice device;
- private static Context context;
- private static BluetoothOperationCallback callback;
- private static BluetoothGatt bluetoothGatt;
- private static int connectionState;
- private static BluetoothGattCharacteristic characteristic;
- private static BluetoothGattCharacteristic charRX;
- public interface BluetoothOperationCallback {
- void onServicesDiscovered();
- void onServicesNotSupported();
- void onConnected();
- void onDisconnected();
- void onMessageWritten(BluetoothGattCharacteristic characteristic);
- }
- private static final BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
- @Override
- public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
- TestLog.writeLog(TAG + " onConnectionStateChange");
- if (newState == BluetoothProfile.STATE_CONNECTED) {
- connectionState = ConnectionState.STATE_CONNECTED;
- TestLog.writeLog("Connected to GATT server.");
- if (null == bluetoothGatt && null != gatt) {
- checkEquals(gatt);
- bluetoothGatt = gatt;
- }
- if (null != bluetoothGatt) {
- bluetoothGatt.discoverServices();
- }
- callback.onConnected();
- } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
- TestLog.writeLog("Disconnected from GATT server.");
- finishModule();
- connectionState = ConnectionState.STATE_DISCONNECTED;
- callback.onDisconnected();
- }
- }
- @Override
- public void onServicesDiscovered(BluetoothGatt gatt, int status) {
- TestLog.writeLog(TAG + " onServicesDiscovered");
- checkEquals(gatt);
- bluetoothGatt = gatt;
- if (status == BluetoothGatt.GATT_SUCCESS) {
- TestLog.writeLog("Service Discovered with status success");
- if (!saveCharacteristic()) {
- disconnect();
- }
- callback.onServicesDiscovered();
- // } else {
- // callback.onServicesNotSupported();
- // }
- }
- }
- @Override
- public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
- checkEquals(gatt);
- bluetoothGatt = gatt;
- TestLog.writeLog(new Date().toString() + "onCharacteristicWrite");
- if (status == BluetoothGatt.GATT_SUCCESS) {
- callback.onMessageWritten(characteristic);
- TestLog.writeLog("onCharacteristicWrite: s");
- } else {
- TestLog.writeLog("onCharacteristicWrite received: " + status);
- TestLog.writeLog("onCharacteristicWrite: f");
- }
- }
- @Override
- public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
- checkEquals(gatt);
- bluetoothGatt = gatt;
- TestLog.writeLog("onCharacteristicChanged");
- super.onCharacteristicChanged(gatt, characteristic);
- }
- };
- private static void checkEquals(BluetoothGatt gatt) {
- // if(bluetoothGatt == null){
- // TestLog.writeLog("Try replace bluetoothGatt. Current Gat == null");
- // }else {
- // TestLog.writeLog("Try replace bluetoothGatt. Equals: " + String.valueOf(bluetoothGatt.equals(gatt)));
- // }
- }
- private static boolean saveCharacteristic() {
- BluetoothGattService gattService = bluetoothGatt.getService(serviceUUID);
- TestLog.writeLog("SaveCharacteristic gattService != null = " + String.valueOf(gattService != null));
- if (gattService != null) {
- characteristic = gattService.getCharacteristic(characteristicUUID);
- if(characteristic == null){
- return false;
- }
- charRX = gattService.getCharacteristic(characteristicUUID);
- setCharacteristicNotification(charRX, true);
- TestLog.writeLog("SaveCharacteristic characteristic != null = " + String.valueOf(characteristic != null));
- if (characteristic != null) {
- return true;
- }
- }
- return false;
- }
- public static void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) {
- TestLog.writeLog(TAG + " setCharacteristicNotification");
- if(!BuildConfig.DEBUG) {
- if (bluetoothGatt == null) {
- return;
- }
- bluetoothGatt.setCharacteristicNotification(characteristic, enabled);
- if (DeviceConfig.UUID_HM_RX_TX.equals(characteristic.getUuid())) {
- BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(DeviceConfig.CLIENT_CHARACTERISTIC_CONFIG));
- descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
- bluetoothGatt.writeDescriptor(descriptor);
- if (2 != characteristic.getWriteType()) {
- characteristic.setWriteType(2);
- }
- }
- }
- }
- public static void setServiceUUID(UUID serviceUUID) {
- BluetoothOperationModule.serviceUUID = serviceUUID;
- }
- public static void setCharacteristicUUID(UUID characteristicUUID) {
- BluetoothOperationModule.characteristicUUID = characteristicUUID;
- }
- public static void connect(BluetoothDevice device, Context context, BluetoothOperationCallback callback) {
- if(BuildConfig.DEBUG) {
- TestLog.writeLog("lib: debug");
- }else{
- TestLog.writeLog("lib: release");
- }
- TestLog.writeLog("Trying to create a new connection.");
- BluetoothOperationModule.device = device;
- BluetoothOperationModule.context = context;
- BluetoothOperationModule.callback = callback;
- TestLog.writeLog("Device: " + device.toString() + " " + device.getName() + " " + device.getAddress());
- bluetoothGatt = device.connectGatt(context, false, gattCallback);
- connectionState = ConnectionState.STATE_CONNECTING;
- }
- public static void disconnect() {
- if (bluetoothGatt != null) {
- TestLog.writeLog(TAG + "Trying to disconnect.");
- bluetoothGatt.disconnect();
- }
- }
- public static void close() {
- if (bluetoothGatt == null) {
- return;
- }
- bluetoothGatt.close();
- bluetoothGatt = null;
- }
- public static int writeCharacteristic(byte[] message) {
- //TODO whet test - commit it
- if(!BuildConfig.DEBUG) {
- if (connectionState == ConnectionState.STATE_CONNECTING ||
- connectionState == ConnectionState.STATE_DISCONNECTED ||
- bluetoothGatt == null) {
- TestLog.writeLog("close writeCharacteristic");
- return WriteCommandResult.RESULT_NO_CONNECTION;
- }
- boolean result;
- if (characteristic == null) {
- if (!saveCharacteristic()) {
- disconnect();
- return WriteCommandResult.RESULT_NO_CONNECTION;
- }
- }
- characteristic.setValue(message);
- result = bluetoothGatt.writeCharacteristic(characteristic);
- TestLog.writeLog("Write command: " + new String(message) + " res: " + String.valueOf(result));
- return result ? WriteCommandResult.RESULT_OK : WriteCommandResult.RESULT_CANCEL;
- }else{
- return WriteCommandResult.RESULT_OK;
- }
- }
- public static void finishModule() {
- TestLog.writeLog("finishModule");
- disconnect();
- close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement