0

I am new to Android and Java and I don't want to give up yet. I am using the Android Studio sample for BLE for this project. https://github.com/googlesamples/android-BluetoothLeGatt

If there is any sample code that is relevant for my purpose, and any advice at all, please do point me to the right direction. :)

I would like to make the phone ring, vibrate, and write to a custom characteristic when the RSSI value reaches a certain threshold. It should keep ringing, vibrating, unless we press a StopRing or StopVibrate button. It should write to characteristic (if threshold is breached) so that another BLE device can read the characteristic and respond by sounding its buzzer as well.

1) I read some sample codes, and saw how to make the phone ring, vibrate, and write (or stop doing so) when a button is clicked. However, now I have a timer that helps to read the RSSI value every, say, 10ms and the updateRssi function call below basically updates the RSSI value displayed on the phone app. I can add vibrate or ring function calls inside update RSSI (inside if RSSI < -threshold condition) but then it will just ring/vibrate and then stop before it finishes the ringtone/vibration pattern.

How can keep checking if the RSSI value meets the condition after we get it from GATT, and then break to another method that rings and vibrates indefinitely? Can I create an intent inside the if (RSSI < -threshold) condition? and then make the method that is supposed to do the actions above receive the intent? (Below is a snippet of the Message Handler inside DeviceControlActivity)

2) Below, I decide whether or not to write to custom characteristic each time we get RSSI values from the Gatt server. RSSI value is read every 10ms, so there might be a chance the writecharacteristic function will be called as long as the RSSI value remains above the threshold. While searching through the forum, I heard they say the Android BLE stack is not very stable, especially with consecutive characteristic writes. Is it alright in my case?

Thank you very much!

// Service message handler
//This message handler will be called when BluetoothLeService gets results back from the Gatt server.

private Handler mMessageHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        Bundle bundle;
        switch (msg.what) {
            case BluetoothLeService.GATT_SERVICES_DISCOVERED:
// start off the rssi reading timer
                DeviceControlActivity.this.startReadRssiTimer();
                break;
            case BluetoothLeService.GATT_DISCONNECT:
                break;
            case BluetoothLeService.GATT_REMOTE_RSSI:
                bundle = msg.getData();
                int rssi = bundle.getInt(BluetoothLeService.PARCEL_RSSI);
                DeviceControlActivity.this.updateRssi(rssi);
                if (rssi < - 80){
                    if(mBluetoothLeService!=null){
                        mBluetoothLeService.writeCustomCharacteristic(0xAA);
                    }
                }
                break;
        }
    }
};

EDIT: I decided I could just stop the timer (to stop reading new RSSI and calling the handler), and just proceed to let the phone vibrate and ring (until a button on the layout is pressed).

EDIT2: Actually I need a Service that runs in the background ...

  • The short answer no solution to trust RSSI signal, if you need I can make a longer answer. – Maytham Fahmi Mar 10 '16 at 18:08
  • Hey thanks for your reply. I actually did a moving average, which might not be the most accurate, but still better I guess? But beyond the reliability of RSSI, I am interested to find out how to use a value inside the message handler (instead of a button as I usually see in sample codes) to bring about a response from the phone. – CodeMonkey Mar 10 '16 at 19:28
  • I did not use GATT so I can not help with it I used another package called Altbeacon (http://altbeacon.org/examples/) and it has a beacon listner, where there I can fetch beacon id, beacon rssi etc. the problem with RSSI if you test it one place and you think it work with the average, you risk it won't work another environment for a lot of reasons. there is a lot of questions will be raised regarding this matter, how many beacon, the goal, what finally could do is a research project http://dev.itbackyard.dk/guidebelt/ – Maytham Fahmi Mar 10 '16 at 20:57
  • for guidance the direction using beacon, but that said we still not able to get a useful distance, but that need more than one beacon and some algorithms – Maytham Fahmi Mar 10 '16 at 20:57
  • Hey thank you so much for the links! :) – CodeMonkey Mar 10 '16 at 21:04

0 Answers0