0

I have downloaded a BLE sample and i want to edit the code.

I have added an Edittext in the code to send the numbers in it instead of the string "Praveen" from the original code, but my Galaxy S4 can't even open the app right now. The app works well without the changes.

Here are the changes i made for the code. The commented lines are from the original code, and the lines just below them are the changes i made.

Thanks in advance

public static EditText Oe;

//public static String dummyName = "Praveen";
  public static String str = Oe.getText().toString();

public void sendAlert(BluetoothDevice device) {
    Log.d(TAG, "sendAlert");
    byte[] value = null;
    byte cat = (byte) callCategory;
    byte cnt = (byte) dummyCount;

    try {
       // String s = dummyName;
        String s = str;
        value = s.getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    byte[] attVal = new byte[value.length + 2];

    attVal[0] = cat;
    attVal[1] = cnt;
    for (int i = 0; i < value.length; i++)
        attVal[i + 2] = value[i];

    mNewAlert.setValue(attVal);
    mBluetoothGattServer.notifyCharacteristicChanged(device, mNewAlert, false);

}

The xml for Edittext

 <EditText
    android:id="@+id/Oe"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:text="@string/Oe"/>

I have defined the Edittext in the OnCreate

public void onCreate() {

    Log.d(TAG, "onCreate() called");
    if (mBtAdapter == null) {
        mBtAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBtAdapter == null) {
            Log.i(TAG, "adapter is null");
            return;
        }
    }

    if (mBluetoothGattServer == null) {
        Log.i(TAG, "mBluetoothGattServer::null");
        Log.i(TAG, "getting proxy::");
        BluetoothGattAdapter.getProfileProxy(this, mProfileServiceListener,      BluetoothGattAdapter.GATT_SERVER);
    }

    mSMSMMSObserver = new BluetoothSMSMMSContentObserver();
    mCallObserver = new CallContentObserver();
    getContentResolver().registerContentObserver(Uri.parse("content://mms-sms"), true, mSMSMMSObserver);
    getContentResolver().registerContentObserver(CallLog.Calls.CONTENT_URI, true, mCallObserver);

    Oe = (EditText)findViewById(R.id.Oe);
}

Mainactivity part

public void onClick(View v) {
    switch (v.getId()) {
    case R.id.btn_select:
        if (!mBluetoothAdapter.isEnabled()) {
            Log.i(TAG, "onClick - BT not enabled yet");
            Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableIntent, REQUEST_ENABLE_BT);

              case R.id.button_send_alert:
                Log.e("MainActivity", "Clicked");
                if (mService != null)
                    mService.sendAlert(mDevice);
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
Osman Esen
  • 1,704
  • 2
  • 21
  • 46

2 Answers2

0

The mistake seems to be that you haven't defined from which EditText should it get the text? Supposing your EditText is as follows:

<EditText 
    android:id="@+id/name" />

If you want to get the text from it, get it as follows :

public static EditText Oe;
Oe = (EditText)findViewById(R.id.name); //You are missing this one. It defines which EditText you wish to refer to.
public static String str = Oe.getText().toString();

Hope it helps else please comment. You can also checkout this answer for more.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • Thanks for the reply. I write this part just after the OnCreate and i got this error "The method findViewById(int) is undefined for the type ANPServerService" – Osman Esen May 04 '13 at 00:23
  • What is ANPServerService? Did you change the ID on the findViewById(ID) according to your xml file? It would be helpful if you post the EditText that you have and the complete LogCat. – Shobhit Puri May 04 '13 at 00:27
  • The ANPServerService is the public class. I have updated my comment with the code above. – Osman Esen May 04 '13 at 00:49
  • What does this class extend? An activity or sth else? If it extends fragment you may use getView().findViewById(R.id.Oe);. Am activity has this method but if you are extending something else it might now have a findViewById mehtod. So, you might need to call root view by calling getView() and then call findViewById() method. – Shobhit Puri May 04 '13 at 00:54
  • it is an: public class ANPServerService extends Service {} So it is a Service instead of Activity – Osman Esen May 04 '13 at 01:01
  • Its really a bad idea to do that from a service and most probably you cannot do that. Check the threads [here](http://stackoverflow.com/questions/8323777/using-findviewbyid-in-a-class-that-does-not-extend-activity-in-android) and [here](http://stackoverflow.com/questions/15861229/accessing-findviewbyid-from-service). You should do that from an activity only. – Shobhit Puri May 04 '13 at 01:14
  • Yes but this isn't the "Mainactivity" java code. It is a another .java code inside the src. So have to change it to activity ?. There occurs some errors, when i do that. – Osman Esen May 04 '13 at 01:30
  • So, you are calling findViewByID inside a class that extends Activity? – Shobhit Puri May 04 '13 at 01:35
  • There is multiple java codes in this project. This code i have posted, is from the file ANPServerService.java which extends Service. The another java file Mainactivity in this project extends activity and has the function i posted above in my code (onClick) – Osman Esen May 04 '13 at 17:16
  • Have the three lines that I've mentioned in the answer inside the class which extends activity and it will work. – Shobhit Puri May 04 '13 at 22:32
0

So, Here's your problem ..

you have not referenced your edit text , this is the problem but the reference in turn cannot be made at that specific point you say .

Actually , the whole thing is written ineffeciently . Basically what you want to do is get the string from the edit text in the onclick() method.

See here , in your onclick method's case where you call the sendAlert() method , modify it as you read this .

case R.id.button_send_alert:
       Log.e("MainActivity", "Clicked");
       if (mService != null)
          mService.sendAlert(mDevice);   

1. In your if method where you check if the mService variable is null , declare the edit text , reference it.

EditText Oe = (EditText)findViewById(R.id.name);

2. Now, change the last line to.

mService.sendAlert(mDevice , Oe.getText().toString());

3. You're almost there , here you run into an error as your sendalert method contains only one parameter.

Modify the sendAlert method as follows .

public void sendAlert(BluetoothDevice device , String s) {
Log.d(TAG, "sendAlert");
byte[] value = null;
byte cat = (byte) callCategory;
byte cnt = (byte) dummyCount;

try {
   // Using the string that's passed to it.
    value = s.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
}
byte[] attVal = new byte[value.length + 2];

attVal[0] = cat;
attVal[1] = cnt;
for (int i = 0; i < value.length; i++)
    attVal[i + 2] = value[i];

mNewAlert.setValue(attVal);
mBluetoothGattServer.notifyCharacteristicChanged(device, mNewAlert, false);

}

Everything should go as planned now .. be sure to note changes it will help later. KNOW YOUR CODE .

TheAnimatrix
  • 566
  • 1
  • 6
  • 19