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 ...