13
votes

I'm trying to send a bMessage from C# code on a Win7 PC to a Samsung Note 2 phone. I have the 32feet Bluetooth library and am using Visual Studio 2013 Community Edition.

Here is the code that I currently have:

    string sendMessage = "";
    sendMessage = File.ReadAllText(file_path);

    BluetoothClient btClient = new BluetoothClient();
    btClient.Connect(btEp);
    Stream clientStream = btClient.GetStream();

    using (StreamWriter utfWriter = new StreamWriter(clientStream, Encoding.UTF8))
    {
        utfWriter.Write(sendMessage);
    }

btEp is a Bluetooth endpoint passed to the function to send the message and defined elsewhere as follows:

BluetoothEndPoint BtEp;

This is built using the variables:

BtEp = new BluetoothEndPoint(device.DeviceInfo, mapServiceId);

Both values are taken from the device at runtime and take the following values during execution:

-       mapServiceId    {00001132-0000-1000-8000-00805f9b34fb}  System.Guid
+       BtEp    {BC20A4164A8C:0000113200001000800000805f9b34fb} InTheHand.Net.BluetoothEndPoint

With BC20A4164A8C being the device address as reported by the device.

The connection to the phone appears to be working and accessing the correct service because when I execute the code the phone prompts me to ask if I am willing to accept a connection to the message server from my PC. This occurs when the statement 'btClient.Connect(btEp);' is executed.

To the best of my knowledge the text file that I read and send to the phone over the stream is a correctly formatted bMessage as defined by the Bluetooth MAP spec and verified elsewhere. It is constructed as follows:

BEGIN:BMSG
VERSION:1.0
STATUS:UNREAD
TYPE:SMS_GSM
FOLDER:TELECOM/MSG/OUTBOX
BEGIN:VCARD
    VERSION:2.1
    N:(redacted)
    TEL:(redacted)
END:VCARD
BEGIN:BENV
    BEGIN:VCARD
        VERSION:2.1
        N:(redacted)
        TEL:(redacted)
    END:VCARD
    BEGIN:BBODY
        ENCODING:G-7BIT
        LENGTH:47
        BEGIN:MSG
            This is a short message
        END:MSG
    END:BBODY
END:BENV
END:BMSG

I've replaced personally identifiable info with (redacted) but the actual text file contains proper names and numbers. I'm based in the UK so SMS_GSM should be correct as far as I know.

Any assistance would be greatly appreciated!

Update: I have installed a Bluetooth terminal on the target phone and verified that the message is being received by sending the same data to a virtual port on the device rather than to the Message Access Server. Not sure if it was an issue with the terminal program but the final line of the message was being appended to the front and missed from the end. I have added a line break to the end of the file which has fixed this issue but still no joy when I send the bMessage to the MAS.

I have tried a number of different stream writing approaches too:

1) removing the StreamWriter Encoding wrapper - same result 2) sending each byte in sequence from a loop - similar result, still no action on the phone.

If there are any Bluetooth comms experts out there I'd really appreciate some advice! Doesn't necessarily have to be Message Access Profile-specific nor, necessarily, C#.

I guess that my next step would be to see if I can find a modern car that has Bluetooth pairing for MAP services and can connect to the phone and maybe sniff the packets when using similar functionality???

03/02/16 - Have sniffed packets between my phone and a new car whilst sending/receiving messages in both directions and compared this to sniffed packets between my PC/code and phone (thanks Android! dev options natively allow BT packet logging and was able to dump this into Wireshark for analysis). My code is using BluetoothClient which operates over RFCOMM in the BT protocol stack, whereas the car uses L2CAP. L2CAP is a level below RFCOMM in the BT stack and 32Feet has an equivalent L2CapClient to operate at this level. Have implemented this but it throws an error. It turns out that although L2CAP is supported not all profiles that use it are and MAP appears to be one of those.

I'm going to answer this question to give possible options to anyone else who stumbles across it.

3
Cheers for the upvotes! Assume they are sympathy votes for expended bounty and still no answers? Much appreciated, nearly got my rep up to where it was before I offered the bounty! If I ever resolve this I'll update the post accordingly...fraggle

3 Answers

2
votes

Think I've established that doing this using 32Feet/C# is not going to be viable. If you want to access a phone's Bluetooth Message Access Server/Profile/Client (MAS MAP MAC) you cannot do so using the high level functions in the 32Feet.Net library. Instead you would need to use lower level code in C++ as detailed on MSDN:

https://msdn.microsoft.com/en-us/library/windows/hardware/ff536674%28v=vs.85%29.aspx

19/02/16 - I have managed to use the Bluetooth link to the Hands Free Profile on my mobile to initiate a call (this was something else I wanted to do but I mistakenly thought SMS would be easier!). I have used AT commands to achieve this. I then tried using AT commands to send SMS too but apparently my phone doesn't implement the AT commands for this in its implementation of HFP. So still no text messages.

I have a few packet sniffs of SMS over Bluetooth between my phone and a car so will look at how this is handled at some point. I do need to get the SMS sorted but can run with simple voice call initiation for the current stage of the project.

2
votes

I am trying to achieve the same things as the OP and I've stumbled on the same issues with the 32feet library.

However, I noticed that there is an application on MAC called Handsfree that implements dialing and sending messages through bluetooth. I used Android's Bluetooth HCI snoop log to get an insight how it works. The log can be downloaded here: https://www.dropbox.com/s/34ol5kd0o3oceib/btsnoop_hci.log?dl=0

The log contains an example of message sending. There is also a message notification sent by the phone. Hope this helps solving SMS sending issues.

2
votes

So, I did some more digging. I used blucat to scan for available services on my phone. Among the services there was: "MAP SMS/MMS" - btgoep://980D2E0CFB51:4. According the MAP Spec, MAP is a profile build on top of OBEX protocol. So, it makes sense to use an OBEX link to connect to this service. BTGOEP = bluetooth generic object exchange protocol. I tried to connect to MAP using the link above, using bluecove java API but I got a response code of 198, which means OBEX_HTTP_NOT_ACCEPTABLE.

Then I found this post. The last answer: You basically create two OBEX sessions: A Message Access Service (MAS) which then registers with the device to receive notifications using the Message Notification Service (MNS). Once you register, the device will open a MNS session with you and notify you of incoming messages. You'll also need to advertise your MNS service using Bluetooth SDP.

So I am thinking, I didn't connect properly to the MAP service and I got the response code 198. Maybe there is another way to connect to the MAP service, by registering with the phone somehow.