Bluetoothle format for sending data - android

Currently I am working on my app which is going to run some code after receiving a specific type of data
So when I push on button, my arduino is going to send the following:
for button 1
ble_write(0x0A);
ble_write(0x01);
ble_write(0x00);
for button 2:
ble_write(0x0A);
ble_write(0x02);
ble_write(0x00);
The data is received by my phone but the problem is, it would only put out the code 1 toast and not code 2 when i press button 2. here is the code for my app.
for (int i = 0; i < data.length; i += 3) {
if (data[i] == 0x0A) {
if (data[i + 1] == 0x01)
Toast.makeText(getApplicationContext(), "CODE 1", Toast.LENGTH_SHORT).show();
} else if (data[i] == 0x02) {
Toast.makeText(getApplicationContext(), "CODE 2", Toast.LENGTH_SHORT).show();
}
}
I feel like because of the for loop it will just take one byte and always see it as code 1.
The code is based on: https://github.com/RedBearLab/Android/blob/master/Examples/Android%20Studio%20Examples/SimpleControls/src/com/redbear/simplecontrols/SimpleControls.java
I am kinda stuck dealing with the problem, any help would be great.

Just move the curly braces ?
for (int i = 0; i < data.length; i += 3) {
if (data[i] == 0x0A) {
if (data[i + 1] == 0x01) { // <- added {
Toast.makeText(getApplicationContext(), "CODE 1", Toast.LENGTH_SHORT).show();
} else if (data[i + 1] == 0x02) { // added '+ 1'
Toast.makeText(getApplicationContext(), "CODE 2", Toast.LENGTH_SHORT).show();
}
} //<- added }
}
Or did I miss something?

In the end I decided to change the received data (byte[]) to a string format via a standard character set. (UTF_8)

Related

Does c++/Arduino handle '?' differently compared with other chars

I have an Android app that is communicating with and arduino board via bluetooth
All the commands are going backward and forwards fine, until I wanted to send a comand of the type
"aT?bb"
from the android app, however when I print it in ardunio I am getting
"aT%3F"
I am logging the command in android and it is formed correctly My quesion is does c++/Arduino handle '?' differently then normal chars?
here is my arduino code->
while(bluetooth.available())
{
char toSend = (char)bluetooth.read();
if(toSend != '\0'){
if (toSend == 'a'){ i=0 ;}
inMsg[i] = toSend;
i++;
}
}
if(i == 5 )
{
// mock sending queries
if(inMsg[2] == '?'){
if(inMsg[1] == 'T'){
bluetooth.write("ty1");Serial.println("");
}else if(inMsg[1] == 'x'){ //normal cycle
bluetooth.write("xx1");
}else if(inMsg[1] == 'X'){ Serial.println(""); //booter
bluetooth.write("XX0");
}else if(inMsg[1] == 'N'){Serial.println(""); //On time
bluetooth.write("on1");
}else if(inMsg[1] == 'F'){ Serial.println(""); //Off time
bluetooth.write("of30");
}else if(inMsg[1] == 'S'){ Serial.println(""); //Speed percent
bluetooth.write("sp30");
}
}
// write to console
for(int j = 0; j < 5; j++){
Serial.write(inMsg[j]);
}
// new line
if(i == 5){Serial.println("");}
i = 0; // reset buffer
}
aT%3F <- this is mal formed
aS133 <- all the other are as I sent them from android
aN169
aF192
aS200
aXXXX
aYYYY
ayYYY
axXXX
my Android Code
...
command = "aT?bb";
writeCommand(command);
...
private void writeCommand(String command)
{
for (BluetoothGattCharacteristic characteristic : characteristics)
{
if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) >0)
{
try {
characteristic.setValue(URLEncoder.encode(command, "utf-8"));
gatt.writeCharacteristic(characteristic);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
}
As pointed out in the comments above it was the URLEncoder that was changing the String. I have now changed this method to
private void writeCommand(String command)
{
for (BluetoothGattCharacteristic characteristic : characteristics)
{
if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) >0)
{
characteristic.setValue(command);
gatt.writeCharacteristic(characteristic);
}else{
Log.i(TAG,"non write able");
}
}
}

Read content of another app through my app

I have been searching the same problem for days. But unable to get any hint for that.
I need to create an app like voodoo app, which shows its custom layout only on specific pages of different apps like flipkart,etc.
Now, till this time, i have found options of using AccessebilityService and MediaProjection classes for the same. But i am stuck, how can i know programmatically, that Flipkart's Product Detail Page is visible so that i can display my app's custom view over it like Voodoo app does.
Any suggestions?
What you want to do is the following.
Using accessibility services track incoming events. Then you want to track TYPE_WINDOW_CONTENT_CHANGED events, and detect when the window content matches what you'd expect.
#Override
public void onAccessibilityEvent(AccessibilityEvent e) {
switch (e.getEventType()) {
case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED: {
if (isFlipkartProdcutDetailPage(getRootInActiveWindow()) {
doStuff()
}
}
}
}
public boolean isFlipkartProductDetailPage(AccessibilityNodeInfo nodeInfo) {
//Use the node info tree to identify the proper content.
//For now we'll just log it to logcat.
Log.w("TAG", toStringHierarchy(nodeInfo, 0));
}
private String toStringHierarchy(AccessibilityNodeInfo info, int depth) {
if (info == null) return "";
String result = "|";
for (int i = 0; i < depth; i++) {
result += " ";
}
result += info.toString();
for (int i = 0; i < info.getChildCount(); i++) {
result += "\n" + toStringHierarchy(info.getChild(i), depth + 1);
}
return result;
}

Problems when sending a continuous stream of data over BLE

I'm wondering if anybody can help me figure out what is causing the data I am sending to become corrupt.
My setup is currently an Arduino pro mini with a HM-10 bluetooth module connected (I have also tried HM-11 Module too) and an Android application to receive the bluetooth data.
Module setup: http://letsmakerobots.com/node/38009
If I send data with big enough intervals then the data is fine, but if I send the data continuously I see messages getting mixed up and lost. To test this I send "$0.1,0.2,0.3,0.4,0.5" to the Android application from the Arduino, sometimes the stream of data appears to send fine but other times it is really quite scrambled. Please see the below graphs that demonstrate this:
Good case:
Bad case:
Arduino code:
String inputString = ""; //Hold the incoming data.
boolean stringComplete = false; //Determines if the string is complete.
boolean realtime = false;
void setup()
{
Serial.begin(9600);
delay(500);
Serial.print("AT+START");
delay(500);
}
void loop()
{
if(stringComplete)
{
if(inputString.equals("rStart"))
{
Serial.println("$startACK");
realtime = true;
}
else if(inputString.equals("stop"))
{
Serial.println("$stopACK");
realtime = false;
}
else{
Serial.print(inputString);
}
inputString = "";
stringComplete = false;
}
if(realtime)
{
Serial.println("$0.1,0.2,0.3,0.4,0.5,0.6");
delay(10);
}
}
void serialEvent() {
while (Serial.available())
{
// get the new byte:
char inChar = (char)Serial.read();
if (inChar == '\n')
{
stringComplete = true;
}
else
{
inputString += inChar;
}
}
}
The Android side just receives the data and then parses it in an IntentService:
#Override
protected void onHandleIntent(Intent intent) {
//Incoming command.
String rawData = intent.getStringExtra(DataProcessingIntentService.REQUEST);
//Append our new data to our data helper.
Log.i(this.getClass().getName(), "Previous Raw: (" + DataProcessingHelper.getInstance().getData() + ")");
DataProcessingHelper.getInstance().appendData(rawData);
Log.i(this.getClass().getName(), "New Raw: (" + DataProcessingHelper.getInstance().getData() + ")");
commandStartIndex = DataProcessingHelper.getInstance().getData().indexOf("$");
commandEndIndex = DataProcessingHelper.getInstance().getData().indexOf("\n");
//Set this as the data starting point.
if(commandStartIndex != -1){
DataProcessingHelper.getInstance().offsetData(commandStartIndex);
}
//Ensure that a command has been found and that the end index is after the starting index.
if(commandStartIndex != -1 && commandEndIndex > commandStartIndex){
//Remove the command structure from the command.
command = DataProcessingHelper.getInstance().getData().substring(commandStartIndex+1, commandEndIndex-1); //Remove the \r\n end command.
DataProcessingHelper.getInstance().offsetData(commandEndIndex+1);
if(command.length() > 1){
//Split the data out of the comand.
splitData = command.split(",");
Log.i(this.getClass().getName(), "Broadcasting the processed data. (" + command + ")");
//Broadcast data.
Intent broadcastIntent = new Intent();
broadcastIntent.setAction(DataProcessingIntentService.RESPONSE);
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
broadcastIntent.putExtra(DataProcessingIntentService.RESPONSE, splitData);
sendBroadcast(broadcastIntent);
}else{
Log.e(this.getClass().getName(), "Command is less than 1 character long!");
}
}
}
Thank you for any help!
I have now figured out what was causing this problem. It appears that BLE only supports a maximum of 20 bytes per a transaction. The time between these transactions is different depending on what you are using. I'm currently using notifications which means that I can send 20 bytes every 7.5 milliseconds maximum. I have opted for 10 milliseconds to be safe. I will now need to look into breaking up packets into 20 bytes maximum to ensure no data corruption.

How to send DTMF tones in active call in android?

Is it possible to send DTMF tones in active call in android ? I tried it with proxyphone.sendDtmf() but it was useless.
How can i achieve it ?
In VOIP only it is possible,Android applications have no access to the in-call audio stream. You can fake a it a bit in speakerphone mode.
Try this method() .It is getting the number and delay from user.
private void call(int profileid) {//call procedure logic
ProfileDo profile = adapter.getProfile(profileid);
if (profile.getStepCount() == 0) {
Toast.makeText(getApplicationContext(), "Please edit the profile and add atleast one value to make a call", 10000).show();
return;}
String call = "tel:";
for (StepDO step : profile.getSteps()) {
String value = URLEncoder.encode(step.getValue());
int delay = step.getDelay();
String pausesStr = "";
for (int i = 0; i < delay / 2; i++) {
pausesStr += PhoneNumberUtils.PAUSE;
}
call += value + pausesStr;
}
startActivity(new Intent("android.intent.action.CALL", Uri.parse(call)));
}

DTMF Tone Android [duplicate]

Is it possible to send DTMF tones in active call in android ? I tried it with proxyphone.sendDtmf() but it was useless.
How can i achieve it ?
In VOIP only it is possible,Android applications have no access to the in-call audio stream. You can fake a it a bit in speakerphone mode.
Try this method() .It is getting the number and delay from user.
private void call(int profileid) {//call procedure logic
ProfileDo profile = adapter.getProfile(profileid);
if (profile.getStepCount() == 0) {
Toast.makeText(getApplicationContext(), "Please edit the profile and add atleast one value to make a call", 10000).show();
return;}
String call = "tel:";
for (StepDO step : profile.getSteps()) {
String value = URLEncoder.encode(step.getValue());
int delay = step.getDelay();
String pausesStr = "";
for (int i = 0; i < delay / 2; i++) {
pausesStr += PhoneNumberUtils.PAUSE;
}
call += value + pausesStr;
}
startActivity(new Intent("android.intent.action.CALL", Uri.parse(call)));
}

Categories

Resources