Robot Not a Dev? Buy Now
Not a Dev?

Misty Community Forum

Set network connection via Bluetooth example

Since we haven’t been able to get the companion apps working yet, I’m trying to set up the network connection on my Misty via the BlueTooth API. I downloaded an app call LightBlue and am able to connect to my Misty.

12 PM

I read the Bluetooth API docs (https://docs.mistyrobotics.io/apis/Advanced/ble/#setnetworkconnection ) but am not sure how to format the message. Can this be ASCII or should it be Hex? Could you guys give a formatted example of what I could write to send the message through LightBlue, if this is even possible?

1 Like

Hey Cameron!

There is a format for the messages. Because we are trying to support all Bluetooth LE devices (including Android 4.4 devices) the max amount of read/write bytes is 20. I’ll work through an example here with you.

Given this information:
SSID = “MyNetwork”
Password = “MyPassword”

In order to set the network connection we first need to build up the byte data. For now the format for network connection is this = “SSID,Password” so for our example here the string (before converting to bytes) should be:
“MyNetwork,MyPassword1”

Now we need to convert this string to bytes using UTF8 encoding (I used an online tool here, feel free to do this in code or however you might not want to type your actual password into that website)

C# psuedocode:

byte[] dataEncoded = Encoding.UTF8.GetBytes("MyNetwork,MyPassword");

byte[] dataActual = new byte[] 
{ 0x4D, 0x79, 0x4E, 0x65, 0x74, 0x77, 0x6F, 0x72, 0x6B, 0x2C, 0x4D, 0x79, 0x50, 0x61, 0x73, 0x73, 0x77, 0x6F, 0x72, 0x64 };

Above, the data encoded is the exact code that we use to convert the network name and password to bytes. The dataActual field is the actual byte values for our example.

After you connect to the BLE device, you need to discover the services. Specifically the Misty Api service with the Service Id = 1562C132-3A0D-4F39-9E67-A9A632E8D6AA as seen on our docs page. After you discover this service you can see that there are 3 characteristics, for now we just care about the read/write characteristics:
Write Characteristic = 3EE51024-7FDD-4D37-95AA-0A4B0E2D4F34
Read Characteristic = 418F52AB-10C6-42A6-9590-58CCCB818F64

The Write characteristic is how we send Api commands to Misty and the Read characteristic is how we read responses from Misty. So now that we have our characteristic and our byte array of network name and password we can send that information to Misty so it can be connected. In order for Misty to correctly process the command, we need to include a few extra bytes that let us know which command we want to execute. The SetNetwork connection command is 8. That number is represented as an unsigned short integer (2 bytes). So in bytes it is = new byte { 0x8, 0x0 }. Now this is a “Little Endian” conversion meaning that the least significant bit is first but Misty is setup to process everything as “Big Endian” over BLE so we need to reverse this such that 8 = new byte { 0x0, 0x8 }. Let’s redefine our data so we’re clear on what we have going forward:

CommandId = 8
CommandIdBytes = new byte[] { 0x0, 0x8 }
DataString = "MyNetwork,MyPassword"
DataBytes = new byte[] 
{ 0x4D, 0x79, 0x4E, 0x65, 0x74, 0x77, 0x6F, 0x72, 0x6B, 0x2C, 0x4D, 0x79, 0x50, 0x61, 0x73, 0x73, 0x77, 0x6F, 0x72, 0x64 };

Okay, so now we have to remember that the BLE Api can only handle 20 bytes at a time but we have to reserve 3 of these bytes 2 for the command id and 1 for the message type which indicates if the write value is Metadata, Data, Last Data packet or there is no data. For Misty to handle multiple transmissions we need to split this up into two transmissions but first we need to tell the api that we’re splitting it up into two transmissions so first we write what we call a “Metadata” packet which includes a command id, the message type of 1 converted to a single byte and the number of transmissions we are going to send to complete the request. So for the first transmission the data packet will look like:

Command id = new byte[] { 0x0, 0x8 }
Message Type = new byte[] { 0x1 }
# of transmissions (4-byte integer) = 2 = new byte[] { 0x0, 0x0, 0x0, 0x2 }

                                                           | command id | message type | # of transmissions |
byte[] metadataPacket = new byte[] { 0x0,       0x8,        0x1,             0x0, 0x0, 0x0, 0x2   }

So we can write that byte array to the bluetooth characteristic to initiate the request. Then we need to send the first data packet:

Command Id = 8 = new byte[] { 0x0, 0x8 }
Message Type = 2 (data) = new byte[] { 0x2 }
Data = new byte[] { 0x4D, 0x79, 0x4E, 0x65, 0x74, 0x77, 0x6F, 0x72, 0x6B, 0x2C, 0x4D, 0x79, 0x50, 0x61, 0x73, 0x73, 0x77 }

byte[] firstDataPacket = new byte[] { 0x0, 0x8, 0x2, 0x4D, 0x79, 0x4E, 0x65, 0x74, 0x77, 0x6F, 0x72, 0x6B, 0x2C, 0x4D, 0x79, 0x50, 0x61, 0x73, 0x73, 0x77 }

So we can send that first data packet to transmit the first 17 bytes of the network name and password. Then we need to sent the last little bit of the password:

Command Id = 8 = new byte[] { 0x0, 0x8 }
Message Type = 3 (end) = new byte[] { 0x3 }
Data = new byte[] { 0x6F, 0x72, 0x64 }

byte[] secondDataPacket = new byte[] { 0x0, 0x8, 0x3, 0x6F, 0x72, 0x64 }

We can send that last message. Notice the Message type is 3 now which signals this is the end of the transmission and now the api can assemble the previous bytes and connect to the network. This will vary based on the length of your network name and password. If your network name and password is more than 34 bytes, it will need 3 transmissions so you’ll have another transmission you need to send. I’ll see if I can download the LightBlue client and do a screen recording of me doing it here at the office and post it here. Let me know if that works for you. After you’ve send the last message you can read the “Read Characteristic” and if it responds with 0x0, 0x8, 0x3, 0x1 then it worked! (that last byte indicates the boolean true/false)

2 Likes