简体   繁体   中英

Getting text from Edittext

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);

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM