Saturday, February 10, 2018

An Update to the Ibeacon Scanner App for Android

I have found a time to review the source code of the Ibeacon Scanner App and it would be easy to add the feature to include scanning Eddystone Beacons. To do this, the file DeviceScanActivity.java needs to be modified to detect the 3 Eddystone frames(UID, TLM, URL).

Here is the portion of the file that needs to be modified:

public void onLeScan(final BluetoothDevice device, final int rssi, final byte[]                             scanRecord) {
           int startByte = 2;
           boolean patternFound = false;
           while (startByte <= 5) {
           if ( ((int) scanRecord[startByte + 2] & 0xff) == 0x02 && //Identifies an iBeacon
                //Identifies correct data length
                ((int) scanRecord[startByte + 3] & 0xff) == 0x15) {
                 patternFound = true;
                 break;
             }
                startByte++;
            }
        if (patternFound) {
            //Convert to hex String
           byte[] uuidBytes = new byte[16];
           System.arraycopy(scanRecord, startByte+4, uuidBytes, 0, 16);
          String hexString = bytesToHex(uuidBytes);

        //Here is your UUID
       String uuid = hexString.substring(0,8) + "-" +
                             hexString.substring(8,12) + "-" +
                             hexString.substring(12,16) + "-" +
                             hexString.substring(16,20) + "-" +
                             hexString.substring(20,32);
       mUUID[mElems]=uuid;
       mRSSI[mElems]= String.valueOf(rssi);
       mElems++;
     //Here is your Major value
        int major = (scanRecord[startByte+20] & 0xff) * 0x100 +                                                                    (scanRecord[startByte+21] & 0xff);

     //Here is your Minor value
        int minor = (scanRecord[startByte+22] & 0xff) * 0x100 +                                                                     (scanRecord[startByte+23] & 0xff);
       runOnUiThread(new Runnable() {
     

The variable  patternFound was used to detect the ibeacon pattern and the scanRecord variable currently contains the data being processed and which is being examined whether it contains the ibeacon pattern. And from this, we can already insert the code to identify the current contents of scanRecord if it is an Eddystone pattern.


Friday, January 26, 2018

A Sample Android App for My Beacon Management System

I was very busy lately with my own project(I am currently developing a Beacon Management System). Part of this system is to detect the current configuration settings of the beacon and to do this, the app must be able to detect the beacon, connect to it and read the services and characteristics of that beacon and store the received value on a MySql database.

I am using MIT App Inventor 2 to develop the app and today, I just succeeded in creating the app(the BLE part) and here is the portion of the app I just created(I created a small app as a demo).

The initial screen looks like below:



When the "Scan" button is clicked/tapped, the app begins to scan for available devices and display all found devices on a list view:
Initially, the background color of the listview is light yellow-orange, when the listed device was clicked/tapped, the background color becomes dark gray and the "click" even is triggered. The "click" event will connect to the selected device and when successful, the labels below the buttons get updated. And the clock object not visible on the screen gets activated and after 5 seconds its "timer" event is triggered and will read the instance id of the connected device. I was able to determine the instance id of the beacon from nrfConnect app screen below, highlighted in red rectangle, the value is in hex 0x00-00-00-00-00-25 this is equal to 37 in decimal.


And here is the screenshot after the clock "timer" was executed:


The rssi of the device gets updated each time its rssi changes and the label gets updated too. When using this app, the device must be in connectable mode so that its services and characteristics can be accessed and red.

The other two buttons are obviously labeled to correspond to its corresponding functions which is to disbable the phone from scanning other devices and disconnect to the beacon when connected.

You can download the app here. Remember that this app is tested for Minew Beacons only.

Sunday, December 31, 2017

Part II: Using Eddystones as Land Mines in a Simulated Military Game

In case you missed the first part, here is the link: Part I

The High Level Implementation



Based on the above picture, we have 4 eddystone beacons at hand and our main goal is to arrange them so that the enemy will have a very hard time in surviving by avoiding the landmines which when he stays within range, of the landmine, it explodes. Arranging the landmines can have a lot of possibilities. This problem is almost the same as the Traveling Salesman Problem but our goal is to generate a solution where the salesman has to travel the longest possible time. Yes, there should be a way for the salesman(enemy) to at least survive the test.


Figure 1: shows the high level solution applying Simulated Annealing Algorithm. It has mentioned the different algorithms that will be used. 

Further Reading:
The 2 Dimensional Random Pattern Generator Algorithm may be adapted to generate the arrangement of the eddystone beacons.
The different algorithm for solving the maze pattern can be found in wikipedia


Wednesday, December 27, 2017

Eddystone URL Encoding : A Beginner's Guide

For beginners who need to know how to convert url into UINT8_T, This can be a very helpful information:

To begin with, beginners need to know exactly what the URL frame contains and here is the details:

The first 9 bytes of the frame are just prefix. To check the content of the prefix value, see the picture below:


The 10th and 11th byte is constant having the value "AAFE". And starting on the 12th byte is the actual eddystone frame which is broken down into the following:

Frame Specification

Byte offsetFieldDescription
0Frame TypeValue = 0x10
1TX PowerCalibrated Tx power at 0 m
2URL SchemeEncoded Scheme Prefix
3+Encoded URLLength 1-17

The URL prefix have constant designated values:

URL Scheme Prefix

The URL Scheme Prefix byte defines the identifier scheme, an optional prefix and how the remainder of the URL is encoded.
DecimalHexExpansion
00x00http://www.
10x01https://www.
20x02http://
30x03https://

The URL Expansion also have constant values:
DecimalHexExpansion
00x00.com/
10x01.org/
20x02.edu/
30x03.net/
40x04.info/
50x05.biz/
60x06.gov/
70x07.com
80x08.org
90x09.edu
100x0a.net
110x0b.info
120x0c.biz
130x0d.gov
14..320x0e..0x20Reserved for Future Use
127..2550x7F..0xFFReserved for Future Use

And the remaining strings in url are converted to hex ascii codes. The URL is 17 bytes including the expansion value.

Here is the raw data from nRF Connect App:



And here is the actual raw data:



To Interpret the data
Byte offsetFieldValue
0Frame Type0x10 
1TX Power0x04 
2URL Scheme0x00 = "http://www."
3+Encoded URL0x06D696E65777465636807
The last 2 digits "07" is the URL extension for ".com"
0x06D696E657774656368 when converted to string is "minewtech". I used the foloowing website for the conversion:

http://www.unit-conversion.info/texttools/hexadecimal/#data

Combining the decoded data that we will get "http://www.minewtech.com"


Using Eddystones as Land Mines in a Simulated Military Game

We often hear about simulated military games involving land mines as major obstacles. Land Mines in real sense is an explosive buried underground to disable vehicles of the enemy and eliminate the enemy. In simulated military games, we can use the eddystone beacons instead as land mines.

One common problem encountered is the complexity of how land mines are distributed in a given space. In a mathematical sense, this can be rephrased as what is the optimal or fairest possible distribution of land mines in order to give the highest unpredictability of locating such weapons in a given space. The space is large but quantifiable.

One possible solution is the Simulated Annealing Algorithm. Annealing in metallurgy is the process of heating and controlled cooling in order to strengthen further the metal. So in the algorithm, it is basically providing a series of solutions until an optimal solution was reached with the goal of evenly distributing the land mines in such a unpredictable pattern.

Let us just imagine the mine field as rectangle where we need to distribute land mines which in this case are the eddystone beacons in a random pattern where the transmission range never overlaps. And to arrange them in an optimized way where the enemy will not be able to easily avoid the landmines to avoid getting eliminated we will use Simulated Annealing Algorithm. 

The High Level Implementation

Based on the above picture, we have 4 eddystone beacons at hand and our main goal is to arrange them so that the enemy will have a very hard time in surviving by avoiding the landmines which when he stays within range, of the landmine, it explodes. Arranging the landmines can have a lot of possibilities. This problem is almost the same as the Travelling Salesman Problem nut our goal is generate a solution where the salesman has to travel is the longest possible time. Yes, there should be a way for the selesman to at least survive the test. To be continued...

Tuesday, December 26, 2017

Developing Eddystones App In Android

Eddystone beacons is google's answer to Apple's Ibeacon. Both devices use BLE technology but differ in advertising protocols.

Ibeacons transmit a single frame wherein the Ibeacon identifier bytes is at the 8th and 9th byte of the frame. See picture below as seen on Ibeacon Detector app on android:

And a more detailed data structure of the frame is below:


In comparison, Eddystones transmit 3 frames which is like below:
As an evidence, if you use Ibeacon Detector App to detect the Eddystones, you will notice that the data packets received always changes that is because eddystones transmit 3 frames.

I have previously developed an android app to scan ibeacons (A Simple Ibeacon Scanner App in Android) and this app can be slightly modified to scan for eddystones as well by removing the validation that look for "0215" pattern in the frame and look for valid values to identify the frame type as described in the last picture.

A lot of people ask, why do we need eddystones since we already have ibeacons? The answer is obvious, eddystones is able to transmit more data than ibeacons. Ibeacon frame contain only the RSSI, UUID, Majo and Minor. The latter 3 is used to identify the ibeacon, while the eddystones frames contain the TX power, Temperature, URL, Battery Charge Level, Instance Id, Namespace Id, and many more.

Changing URL in Eddystone using LighBlue App

1. Carefully remove the battery of the eddystone to hard reset.
2. Download the LightBlue app from apple app store.
3. After installing the app scan for the eddystone. Once detected, just tap on the eddystone to connect.
4. If connected successfully, search for 0xBEE2. Tap on it to change the value.
5. Then enter your URL in ASCII code. The url must start with 00 for http:// and ends with 07 for .com

6. Save the new value.

7. Search for 0xFFFF, this is located at the bottom of the service(UUID:FFF0).
8. Enter "minew123" and save.
9. You may check with your favorite sample eddystone detector app which can be download for free in the app store.

The url can also be changed in Android using Nrf Connect app. The Android version must be at least 6.0