BestOwl / MyPhone

Connect your mobile devices (Android/iOS/WindowsPhone) to PC
MIT License
191 stars 37 forks source link

MAP demo doesn't work with iPhone #3

Closed xtremehpx closed 3 years ago

xtremehpx commented 3 years ago

MAP demo does not seem to work with iPhone but Android Phone seems to work. Any suggestions?

System: Windows 10 Pro, 19042.110 Build + iPhone 11

iPhone Log:

 Selected: X’s iPhone    Bluetooth#Bluetooth30:e3:7a:62:d5:67-60:83:73:83:12:a6

Available RFComm services for this devices:
Bluetooth#Bluetooth30:e3:7a:62:d5:67-60:83:73:83:12:a6#RFCOMM:00000000:{00001132-0000-1000-8000-00805f9b34fb}
Bluetooth#Bluetooth30:e3:7a:62:d5:67-60:83:73:83:12:a6#RFCOMM:00000000:{02030302-1d19-415f-86f2-22a2106a0a77}
Bluetooth#Bluetooth30:e3:7a:62:d5:67-60:83:73:83:12:a6#RFCOMM:00000000:{00000000-deca-fade-deca-deafdecacafe}
Bluetooth#Bluetooth30:e3:7a:62:d5:67-60:83:73:83:12:a6#RFCOMM:00000000:{0000112f-0000-1000-8000-00805f9b34fb}
Bluetooth#Bluetooth30:e3:7a:62:d5:67-60:83:73:83:12:a6#RFCOMM:00000000:{0000111f-0000-1000-8000-00805f9b34fb}
Connecting...
80-00-1A-10-00-FF-FF-46-00-13-BB-58-2B-40-42-0C-11-DB-B0-DE-08-00-20-0C-9A-66
Returned package:
Opcode: Success
A0-00-32-10-00-0F-A0-46-00-13-BB-58-2B-40-42-0C-11-DB-B0-DE-08-00-20-0C-9A-66-CB-90-5D-20-07-4A-00-13-BB-58-2B-40-42-0C-11-DB-B0-DE-08-00-20-0C-9A-66
MNS: Start advertising MNS service
Wait for 1 seconds
MNS: Sending
82-00-4A-CB-00-00-00-01-42-00-25-78-2D-62-74-2F-4D-41-50-2D-4E-6F-74-69-66-69-63-61-74-69-6F-6E-52-65-67-69-73-74-72-61-74-69-6F-6E-00-4C-00-06-0E-01-01-48-00-04-30-46-00-13-BB-58-2B-40-42-0C-11-DB-B0-DE-08-00-20-0C-9A-66
Opcode: PutAlter
Returned packet:
D3-00-03
Opcode: 211
MNS: Failed to call SetNotificationRegistration
Enter any key to exit...

Android Log

Selected: Pixel 2 XL    Bluetooth#Bluetooth30:e3:7a:62:d5:67-08:d4:6a:1a:b4:e9

Available RFComm services for this devices:
Bluetooth#Bluetooth30:e3:7a:62:d5:67-08:d4:6a:1a:b4:e9#RFCOMM:00000000:{00001112-0000-1000-8000-00805f9b34fb}
Bluetooth#Bluetooth30:e3:7a:62:d5:67-08:d4:6a:1a:b4:e9#RFCOMM:00000000:{0000111f-0000-1000-8000-00805f9b34fb}
Bluetooth#Bluetooth30:e3:7a:62:d5:67-08:d4:6a:1a:b4:e9#RFCOMM:00000000:{00001132-0000-1000-8000-00805f9b34fb}
Bluetooth#Bluetooth30:e3:7a:62:d5:67-08:d4:6a:1a:b4:e9#RFCOMM:00000000:{0000112f-0000-1000-8000-00805f9b34fb}
Bluetooth#Bluetooth30:e3:7a:62:d5:67-08:d4:6a:1a:b4:e9#RFCOMM:00000000:{00001105-0000-1000-8000-00805f9b34fb}
Bluetooth#Bluetooth30:e3:7a:62:d5:67-08:d4:6a:1a:b4:e9#RFCOMM:00000000:{0000112d-0000-1000-8000-00805f9b34fb}
Connecting...
80-00-1A-10-00-FF-FF-46-00-13-BB-58-2B-40-42-0C-11-DB-B0-DE-08-00-20-0C-9A-66
Returned package:
Opcode: Success
A0-00-32-10-00-FF-FE-46-00-13-BB-58-2B-40-42-0C-11-DB-B0-DE-08-00-20-0C-9A-66-CB-01-00-00-00-4A-00-13-BB-58-2B-40-42-0C-11-DB-B0-DE-08-00-20-0C-9A-66
MNS: Start advertising MNS service
Wait for 1 seconds
MNS: Sending
82-00-4A-CB-00-00-00-01-42-00-25-78-2D-62-74-2F-4D-41-50-2D-4E-6F-74-69-66-69-63-61-74-69-6F-6E-52-65-67-69-73-74-72-61-74-69-6F-6E-00-4C-00-06-0E-01-01-48-00-04-30-46-00-13-BB-58-2B-40-42-0C-11-DB-B0-DE-08-00-20-0C-9A-66
Opcode: PutAlter
Returned packet:
A0-00-08-CB-01-00-00-00
Opcode: Success
Enter any key to exit...
Connection received
Stoping Advertising
Opcode: Connect

Unhandled exception. System.AggregateException: One or more errors occurred. (A method was called at an unexpected time. (0x8000000E))
 ---> System.InvalidOperationException: A method was called at an unexpected time. (0x8000000E)
   at Windows.Devices.Bluetooth.Rfcomm.RfcommServiceProvider.StopAdvertising()
   at MyPhone.Demo.MAP.Do() in D:\work\git\MyPhone\MyPhone.Demo\MAP.cs:line 84
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Threading.Tasks.Task.Wait()
   at MyPhone.Demo.MAP.Main(String[] args) in D:\work\git\MyPhone\MyPhone.Demo\MAP.cs:line 42
BestOwl commented 3 years ago

Sorry for the late reply. It's been a busy week.

This is a known issue. I guess it's because the MAP demo doesn't handle the ConnectionId. https://github.com/BestOwl/MyPhone/blob/9cb86e823e6932e8fce70c4eee34e2f895cd96a4/MyPhone.Demo/MAP.cs#L192

xtremehpx commented 3 years ago

Not sure if that is the root cause. I used ConnectionId from OBEX connection call and still getting Response Code "D3". And "D3" is not a valid MAP response code based on the specification document.

Get ConnectionId here:


OBEXConnectPacket packet = new OBEXConnectPacket();
var buf = packet.ToBuffer();
....

OBEXPacket retPacket = await OBEXPacket.ReadFromStream(_reader, packet);
if (retPacket != null)
{
    var bytes = retPacket.ToBuffer().ToArray();
    foreach (var header in retPacket.Headers)
    {
        if (header.HeaderId.Equals(HeaderId.ConnectionId))
        {
            ConnectionHeader = new Int32ValueHeader(HeaderId.ConnectionId, ((Int32ValueHeader)header).Value);
            break;
        }
    }
}

Set ConnectionId here:

OBEXPacket packet = new OBEXPacket(
                ConnectionHeader
                ,new StringValueHeader(HeaderId.Type, "x-bt/MAP-NotificationRegistration")
                ,new AppParamHeader(new AppParameter(AppParamTagId.NotificationStatus, 1))
                ,new BytesHeader(HeaderId.Body, 0x30)
                ,new BytesHeader(HeaderId.EndOfBody, 0x30)
                //,new BytesHeader(HeaderId.Target, MAS_UUID)
                );
            packet.Opcode = Opcode.PutAlter;
Sending MAP client Connection request to Phone MAP server:
80-00-1A-10-00-FF-FF-46-00-13-BB-58-2B-40-42-0C-11-DB-B0-DE-08-00-20-0C-9A-66
Opcode: Connect
Waiting reply packet:
Opcode: Success
Reply packet:
A0-00-32-10-00-0F-A0-46-00-13-BB-58-2B-40-42-0C-11-DB-B0-DE-08-00-20-0C-9A-66-CB-E0-65-00-1D-4A-00-13-BB-58-2B-40-42-0C-11-DB-B0-DE-08-00-20-0C-9A-66
ResponseCode: Success
Target: BB-58-2B-40-42-0C-11-DB-B0-DE-08-00-20-0C-9A-66
ConnectionId: E0-65-00-1D
Who: BB-58-2B-40-42-0C-11-DB-B0-DE-08-00-20-0C-9A-66
Sending MAP client notification registration request to Phone MAP server:
82-00-3B-CB-E0-65-00-1D-42-00-25-78-2D-62-74-2F-4D-41-50-2D-4E-6F-74-69-66-69-63-61-74-69-6F-6E-52-65-67-69-73-74-72-61-74-69-6F-6E-00-4C-00-06-0E-01-01-48-00-04-30-49-00-04-30
Opcode: PutAlter
Waiting reply packet:
Reply packet:
D3-00-03
ResponseCode: 211
Remote request failed.
xtremehpx commented 3 years ago

Looks like "D3" is from OBEX specification, No more than one Reliable OBEX Session shall exist at any time on each transport connection, which is equivalent to the L2CAP channel connection. If a CREATE SESSION command is issued while an active Reliable Session already exists, the operation shall be rejected with the Service Unavailable (0xD3) response code. If a CREATE SESSION command is issued while a suspended Reliable Session exists, the operation may be rejected with the Service Unavailable (0xD3) response code, or the suspended session may be removed and a new Reliable Session created.

xtremehpx commented 3 years ago

If connection id is NOT reversed, then the response code is no longer 0xD3 but 0x90. However, no valid data is return when we continue making the Get request.

https://github.com/BestOwl/MyPhone/blob/ede21d4dfa4c6452f52628aebce2da5aea309f4b/MyPhone.OBEX/OBEXHeader.cs#L67-L70

Sending RemoteNotificationRegister request
Sending request packet: 1
02-00-46-CB-1D-00-65-E0-42-00-25-78-2D-62-74-2F-4D-41-50-2D-4E-6F-74-69-66-69-63-61-74-69-6F-6E-52-65-67-69-73-74-72-61-74-69-6F-6E-00-4C-00-06-0E-01-01-46-00-13-BB-58-2B-40-42-0C-11-DB-B0-DE-08-00-20-0C-9A-66
Opcode: Put
Req packet sent. Waiting for reply...1
ReadFromStream:: Opcode: Continue
packet length: 3
Header size to read is zero.
Reply packet:
90-00-03
ResponseCode: Continue
No header returned.
Sending request packet: 2
02-00-03
Opcode: Put
Req packet sent. Waiting for reply...2
ReadFromStream:: Opcode: Continue
packet length: 3
Header size to read is zero.
...

I created additional MAS functions to test. However currently the only one worked is OBEX Connect request. My fork

BestOwl commented 3 years ago

Why is this issue closed? It has not been resolved.

xtremehpx commented 3 years ago

Why is this issue closed? It has not been resolved.

I was able to get remote notification to work by following changes in the Connection Id handling:

public override void FromBytes(byte[] bytes)
        {
            if (HeaderId.Equals(HeaderId.ConnectionId))
            {
                Value = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(bytes));
            }
            else
            {
                Value = BitConverter.ToInt32(bytes);
            }

        }

        public override byte[] ToBytes()
        {
            byte[] ret;

            if (HeaderId.Equals(HeaderId.ConnectionId))
            {
                ret = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(Value));
            }
            else
            {
                ret = BitConverter.GetBytes(Value);
            }
            return ret;
        }

my changes are available from my fork as well.

BTW, do you have suggestions for:

  1. how to actually receive and handle the Notification? and
  2. how to send message to phone?
BestOwl commented 3 years ago

1. Receive and handle the Notification

  1. Establish MNS service connection (SetNotificationRegistration)
  2. Once the MNS service connection is established, the MSE (phone) will send SendEvent function to MCE (PC) which contains the message handle when a new message arrives.
  3. MCE (PC) send GetMessage function to MSE(Phone) to get the message content
  4. Show the toast notification on MCE(PC) (For Windows 10/11, we can use this library, https://github.com/CommunityToolkit/WindowsCommunityToolkit/tree/main/Microsoft.Toolkit.Uwp.Notifications)
BestOwl commented 3 years ago

2. Send message to phone

Send PushMessage function to MSE(Phone)

xtremehpx commented 3 years ago

2. Send message to phone

Send PushMessage function to MSE(Phone)

I have been working on PushMessage for couple days and no success so far. Any working example by chance? My example is attached below.

In terms of getting the notification from the phone, I understand the high level flow. The demo code has 1 (2), 3 implemented but I don't see the GetMessage been triggered. No success so far.

PushMessage OBEX object payload with no success:

StringBuilder mssg = new StringBuilder();
            mssg.AppendLine("BEGIN:BMSG");
            mssg.AppendLine("VERSION: 1.0");
            mssg.AppendLine("STATUS: UNREAD");
            mssg.AppendLine("TYPE: SMS_GSM");
            mssg.AppendLine("FOLDER:telecom/msg/outbox");
            mssg.AppendLine("BEGIN: VCARD");
            mssg.AppendLine("VERSION:2.1");
            mssg.AppendLine("N: null");
            mssg.AppendLine("TEL: 1234567891");
            mssg.AppendLine("END: VCARD");
            mssg.AppendLine("BEGIN:BENV");
            mssg.AppendLine("BEGIN:BBODY");
            mssg.AppendLine("CHARSET:UTF - 8");
            mssg.AppendLine("LENGTH: 51");
            mssg.AppendLine("BEGIN: MSG");
            mssg.AppendLine("Joey Pigza Swallowed the Key;");
            mssg.AppendLine("END: MSG");
            mssg.AppendLine("END:BBODY");
            mssg.AppendLine("END:BENV");
            mssg.AppendLine("END:BMSG");

            OBEXPacket packet = new OBEXPacket(
                Opcode.PutAlter
                , ConnectionHeader
                , new TypeHeader("x-bt/message")
                , new NameHeader("telecom/msg/outbox")
                , new BodyHeader(HeaderId.Body, mssg.ToString())
                );
BestOwl commented 3 years ago

I will try to implement a working demo in the next few days. I will post updates here if I make any progress.

xtremehpx commented 3 years ago

I will try to implement a working demo in the next few days. I will post updates here if I make any progress.

Sounds good! I will update if I have success as well :)

xtremehpx commented 3 years ago

I fixed the bmessage format and it is working with the demo code. The only thing really need the fix is the Connection Id. I have not tested receiving notification yet though.

BestOwl commented 3 years ago

By building the MNS server on PC before sending the RemoteNotificationRegister request to MSE, I can now receive notifications.

MasObexConnect success is: True
**************************************************
MNS: Start advertising MNS service
Wait for 1 seconds
**************************************************
Sending RemoteNotificationRegister request
Sending request packet: 1
ConnectionId: 0A-90-5E-00
82-00-33-CB-0A-90-5E-00-42-00-25-78-2D-62-74-2F-4D-41-50-2D-4E-6F-74-69-66-69-63-61-74-69-6F-6E-52-65-67-69-73-74-72-61-74-69-6F-6E-00-4C-00-06-0E-01-01
Opcode: PutAlter
Req packet sent. Waiting for reply...1
ReadFromStream:: Opcode: Success
packet length: 3
Header size to read is zero.
Reply packet:
A0-00-03
ResponseCode: Success
No header returned.
Request returned success
RemoteNotificationRegister success is: True
Enter q to exit or other keys to try again...
Connection received
Stoping Advertising
ReadFromStream:: Opcode: Connect
packet length: 26
70
Opcode: Connect
ReadFromStream:: Opcode: PutAlter  #SendEvent from MSE
packet length: 243
66
76
73
Opcode: PutAlter

I can't push to your fork so I push https://github.com/BestOwl/MyPhone/commit/6f83fa583f50362fa190b6e5121717bee57125f6 to this branch

BestOwl commented 2 years ago

Let's move the discussion here https://github.com/BestOwl/MyPhone/pull/5 since this issue is closed.