MultipeerConnectivity

Hi Forum, I'm pretty much a newbie really but am looking at usingTMSFMXNativeMultipeerConnectivity to simply send an image from 1 iPad to another and would like some guidance. I've looked at the demo but that's just really sending txt and the Manual isn't really to helpful. Any pointers would be appreciated.

Hi, 


You should be able to send the bitmap as a stream with the SendObject / SendObjectToAllPeers method. In the OnDidReceiveObject you can load the stream into a TBitmap object with LoadFromStream method. 

You can also send the image as a resource (file) with the SendResource / SendResourceToAllPeers and receive the resource in the OnDidReceiveResource event. There are additional parameters in the OnDidReceiveResource event that allow you to save the file to the documents folder and then you can load it in the TBitmap object with LoadFromFile instead of LoadFromStream with the first method.

Kind Regards, 
Pieter

Pieter Scheldeman2015-03-09 09:20:02

Thanks Pieter, Still having problems. Could you explain what I have done wrong here. Thank you.

procedure TForm1.Button1Click(Sender: TObject);

VAR

a:TObject;

ms: TMemoryStream;

begin

a:=TObject.create(Imageviewer1.Bitmap);

    ms := TMemoryStream.Create;

        ms.WriteComponent(a);

TMSFMXNativeMultipeerConnectivity1.SendResourceToAllPeers(a);

I have loaded a basic image into a TImageviewer component. Simply want to try and send it to all peers.

The code to accomplish this is


procedure TForm1.Button1Click(Sender: TObject);
var
  ms: TMemoryStream;
begin
  ms := TMemoryStream.Create;
  try
    Image1.Bitmap.SaveToStream(ms);
    TMSFMXNativeMultipeerConnectivity1.SendObjectToAllPeers(ms);
  finally
    ms.free;
  end;
end;

procedure TForm1.TMSFMXNativeMultipeerConnectivity1DidReceiveObject(
  Sender: TObject; AValue: TMemoryStream;
  APeer: TTMSFMXNativeMultipeerConnectivityPeer);
begin
  if Assigned(AValue) then
    Image1.Bitmap.LoadFromStream(AValue);
end;

But if you want to send additional information (to identify the object), I recommend to take a look at the Multipeer demo that is included in the distribution. There the TMyInfo class is created and sent as an object. It can contain any information as long as it's sent correctly.

Kind Regards, 
Pieter

Pieter Scheldeman2015-03-13 11:24:36

Here is the code from the demo to send an object to all peers:


  TMyInfo = class(TComponent)
  private
    FEmail: String;
    FLastName: String;
    FCompany: String;
    FPhone: String;
    FFirstName: String;
  public
    constructor Create(AFirstName, ALastName, AEmail, ACompany, APhone: String); reintroduce; overload;
    constructor Create; reintroduce; overload;
  published
    property FirstName: String read FFirstName write FFirstName;
    property LastName: String read FLastName write FLastName;
    property Email: String read FEmail write FEmail;
    property Company: String read FCompany write FCompany;
    property Phone: String read FPhone write FPhone;
  end;

implementation

procedure TForm1175.SendMyInfo;
var
  c: TMyInfo;
  ms: TMemoryStream;
begin
  try
    c := TMyInfo.Create(TMSFMXNativeUITextField2.Text, TMSFMXNativeUITextField3.Text,
      TMSFMXNativeUITextField4.Text, TMSFMXNativeUITextField5.Text, TMSFMXNativeUITextField6.Text);

    ms := TMemoryStream.Create;
    ms.WriteComponent(c);
    TMSFMXNativeMultipeerConnectivity1.SendObjectToAllPeers(ms);
    ShowMessageEx('My card info sent');
  finally
    if Assigned(ms) then
      ms.Free;
    if Assigned(c) then
      c.Free;
  end;
end;

procedure TForm1175.TMSFMXNativeMultipeerConnectivity1DidReceiveObject(
  Sender: TObject; AValue: TMemoryStream;
  APeer: TTMSFMXNativeMultipeerConnectivityPeer);
begin
  ReceiveFriendsInfo(AValue);
end;

procedure TForm1175.ReceiveFriendsInfo(AValue: TMemoryStream);
var
  c: TMyInfo;
begin
  if not Assigned(AValue) then
    Exit;

  try
    c := TMyInfo.Create;
    AValue.ReadComponent(c);
    TMSFMXNativeUITextField7.Text := c.Phone;
    TMSFMXNativeUITextField8.Text := c.Company;
    TMSFMXNativeUITextField9.Text := c.Email;
    TMSFMXNativeUITextField10.Text := c.LastName;
    TMSFMXNativeUITextField11.Text := c.FirstName;
    ShowMessageEx('Friends card info received');
    TMSFMXNativeUITabBarController1.SelectedItemIndex := 1
  finally
    if Assigned(c) then
      c.Free;
  end;
end;

Pieter Scheldeman2015-03-13 11:26:37

Thank you Pieter. I'll give it a try and let you know.