-1

Together I got the Services and Characteristics after I connected to my BLE device. With this code:

 if(gattServices == null)return;
    //loops through available GATT SERVICES
    for(BluetoothGattService gattService : gattServices){
        uuid = gattService.getUuid().toString();
        System.out.println("Service discovered: " + uuid);
        new ArrayList<HashMap<String, String>>();
        List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics();
        //loops through available characteristics
        for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics){
            final String charUuid = gattCharacteristic.getUuid().toString();
            System.out.println("Characteristic discovered: " + charUuid);

        }
    }
}

Now I want to display these Services and Characteristics in another Activity of my App, yet the Problem is that I don't know what's the best way of doing this. Can someone give me an advise, please?

Sergey Emeliyanov
  • 5,158
  • 6
  • 29
  • 52
Fab
  • 91
  • 8

1 Answers1

0

You can either make a Singleton pattern BLEManager that sets himself as the listener for everything and calls out to the listener that is currently subscribed, So make an interfaces:

public interface IBLEComListener {

/*/////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS.
*//////////////////////////////////////////////////////////////////////////////////////////
/**
 * To notify BLE connected.
 *
 * @param bluetoothDevice the instance of connected BluetoothDevice.
 */
void onBLEDeviceConnected(BluetoothDevice bluetoothDevice);
/**
 * To notify BLE disconnected.
 *
 * @param bluetoothDevice the instance of connected BluetoothDevice.
 */
void onBLEDeviceDisconnected(BluetoothDevice bluetoothDevice);
/**
 * To notify when unable to initialize Bluetooth.
 */
void onBLEUnableToInitializeBluetooth();
/**
 * To notify Services ready for Connected BLE Device.
 *
 * @param bluetoothDevice the instance of connected BluetoothDevice.
 */
void onBLEDeviceServicesReady(BluetoothDevice bluetoothDevice);
/**
 * To notify when service not found into BLE device.
 *
 * @param bluetoothDevice the instance of connected BluetoothDevice.
 */
void onServiceNotFound(BluetoothDevice bluetoothDevice);
/**
 * To notify when characteristic notification.
 *
 * @param bluetoothDevice the instance of connected BluetoothDevice.
 * @param bleMessageModelList the instance list of BLEMessageModel.
 */
void onBLECharacteristicNotificationReceived(BluetoothDevice bluetoothDevice, List<BLEMessageModel> bleMessageModelList);
/**
 * To notify when message arrived.
 *
 * @param bluetoothDevice the instance of connected BluetoothDevice.
 * @param characteristicDescriptorIdentifier the ENUM to identify the Characteristic or Descriptor.
 * @param bleMessageModelList the instance list of BLEMessageModel.
 */
void onBLEMessageReceived(BluetoothDevice bluetoothDevice, CharacteristicDescriptorIdentifier characteristicDescriptorIdentifier, List<BLEMessageModel> bleMessageModelList);
/**
 * To notify when message Sent.
 *
 * @param bluetoothDevice the instance of connected BluetoothDevice.
 * @param characteristicDescriptorIdentifier the ENUM to identify the Characteristic or Descriptor.
 */
void onBLEMessageSent(BluetoothDevice bluetoothDevice, CharacteristicDescriptorIdentifier characteristicDescriptorIdentifier);
/**
 * To notify when bluetooth off/disabled.
 */
void onBluetoothDisabled();
/**
 * To notify when bluetooth on/enabled.
 */
void onBluetoothEnabled();
 /**
 * To notify BLE devices discovered/updated.
 *
 * @param deviceModel the BLE device.
 */
    void onBLEDeviceDiscovered(DeviceModel deviceModel);


}

Then simply wire yourself up to your singleton for callbacks like MyBLEManager.getInstance(this, this) //context, listener

Then let the manager fire back your responses. Then in your service class that is handling the BLE connection just do like:

 @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            mServicesReadyBluetoothDeviceMap.clear();
            for(BluetoothDevice bluetoothDevice : gatt.getConnectedDevices()) {
                mServicesReadyBluetoothDeviceMap.put(bluetoothDevice.getAddress(), bluetoothDevice);
            }
            IBLEComListener comListener = A35BLEManager.getBLEComListener();
            if(comListener != null) {
                comListener.onBLEDeviceServicesReady(gatt.getDevice());
            }
        } else {
            A35Log.w(TAG, "onServicesDiscovered received: " + status);
        }
    }

Alternatively you could register a broadcast receiver and hand it out that way just as easily.

Sam
  • 5,342
  • 1
  • 23
  • 39