discoverServices method

Future<List<BluetoothService>> discoverServices()

Discovers services offered by the remote device as well as their characteristics and descriptors

Implementation

Future<List<BluetoothService>> discoverServices() async {
  final s = await state.first;
  if (s != BluetoothDeviceState.connected) {
    return Future.error(new Exception(
        'Cannot discoverServices while device is not connected. State == $s'));
  }
  var response = FlutterBlue.instance._methodStream
      .where((m) => m.method == "DiscoverServicesResult")
      .map((m) => m.arguments)
      .map((buffer) => new protos.DiscoverServicesResult.fromBuffer(buffer))
      .where((p) => p.remoteId == id.toString())
      .map((p) => p.services)
      .map((s) => s.map((p) => new BluetoothService.fromProto(p)).toList())
      .first
      .then((list) {
    _services.add(list);
    _isDiscoveringServices.add(false);
    return list;
  });

  await FlutterBlue.instance._channel
      .invokeMethod('discoverServices', id.toString());

  _isDiscoveringServices.add(true);

  return response;
}