0

在我的应用程序中,我可能需要建立蓝牙连接并使用自己的服务发送字节并将一些字节发送到接收器。

现在我尝试连接到我自己的服务器以发送文本并获得答案,但没有成功。出于任何原因,如果我尝试调用 sendMessage() 方法,我的服务会收到 NullPointerException。

服务:

import android.annotation.TargetApi;
import android.app.Service;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;

public class ClientService extends Service {
private String LOG_TAG = null;
private final IBinder mBinder = new LocalBinder();
private final String url_string = "";

private int receivedUcCommand = 0;
@Override
public void onCreate() {
    super.onCreate();
}

public void sendToServer(final String text){

    new Thread(new Runnable() {
        @Override
        public void run() {
            if(internetAvailable()){
                try {
                    String textParam = "text1" + URLEncoder.encode(text,"UTF-8");
                    URL scriptUrl = new URL(url_string);
                    HttpURLConnection connection = (HttpURLConnection) scriptUrl.openConnection();
                    connection.setDoOutput(true);
                    connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
                    connection.setFixedLengthStreamingMode(textParam.getBytes().length);
                    OutputStreamWriter contextWriter = new OutputStreamWriter(connection.getOutputStream());

                    contextWriter.write(textParam);
                    contextWriter.flush();
                    contextWriter.close();


                    InputStream answerInputStream = connection.getInputStream();
                    String answer = read(answerInputStream);

                    answerInputStream.close();
                    connection.disconnect();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }).start();

}

private String read(InputStream is){
    BufferedReader br = new BufferedReader(new InputStreamReader(is));

    StringBuilder sb = new StringBuilder();

    String currentLine;
    try {
        while((currentLine = br.readLine()) != null){
            sb.append(currentLine+"\n");
        }
    }catch (IOException e){
        e.printStackTrace();
    }
    return sb.toString().trim();
}
private boolean internetAvailable(){
    ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getActiveNetworkInfo();
    return ni != null && ni.isConnectedOrConnecting();
}

@Override
public IBinder onBind(Intent intent) {
    // Wont be called as service is not bound
    Log.i(LOG_TAG, "In onBind");
    return mBinder;
}


public class LocalBinder extends Binder {
    ClientService getService() {
        // Return this instance of LocalService so clients can call public methods
        return ClientService.this;
    }
}

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override
public void onTaskRemoved(Intent rootIntent) {
    super.onTaskRemoved(rootIntent);
    Log.i(LOG_TAG, "In onTaskRemoved");
}

@Override
public void onDestroy() {
    super.onDestroy();
    Log.i(LOG_TAG, "In onDestroy");
}
}

在活动中,我这样做:

public class loggedInActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener{
    public static final String mBroadcastStringAction = "com.truiton.broadcast.string";
    public static final String mBroadcastIntegerAction = "com.truiton.broadcast.integer";
    public static final String mBroadcastArrayListAction = "com.truiton.broadcast.arraylist";
    private TextView mTextView;
    private IntentFilter mIntentFilter;

    BluetoothService bService;
    ClientService cService;

    boolean btBound = false;
    boolean clientBound = false;


   // private View startButton;



    Button testButton;
    EditText et;
    static final int CAM_REQUEST = 1;
    //Button reconnectButton;

    TextView recText;







        recText = (TextView) findViewById(R.id.receivedText);
        //recText.setText("KEIN ALARM");

        et = (EditText) findViewById(R.id.editText);
        testButton = (Button) findViewById(R.id.testButton);


        // This way I start and bind the service for Bluetooth, to my activity
        Intent btIntent = new Intent(this, BluetoothService.class);
        startService(btIntent);
        bindService(btIntent, btServiceConnection, Context.BIND_AUTO_CREATE);

        //And this way I unsuccessfully trying to bind and start the ClientService for ServerConnection
        Intent clientIntent = new Intent(this, ClientService.class);
        startService(clientIntent);
        bindService(clientIntent, clientServiceConnection, Context.BIND_AUTO_CREATE);


       testButton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                //ATTENTION: TRYING TO SENDING TO SERVER HERE, IT THROWS ME A NULLPOINTER
                cService.sendToServer(et.getText().toString());


            }
        });
    }

 /** Defines callbacks for service binding, passed to bindService() */
    private ServiceConnection btServiceConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName className,
                                       IBinder service) {
            // We've bound to LocalService, cast the IBinder and get LocalService instance

            BluetoothService.LocalBinder binder = (BluetoothService.LocalBinder) service;
            bService = binder.getService();
            btBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            btBound = false;
        }
    };



    //I think it has to be to do something with that code here, but
this is the only place, where cService is defined...
    private ServiceConnection clientServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            ClientService.LocalBinder binder = (ClientService.LocalBinder) service;

cService = binder.getService();
            clientBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            clientBound = false;
        }
    };
4

1 回答 1

0

就像我说错了,只有我这样的新手才能产生。我忘记在 AndroidManifest.xml 中登录标签

于 2017-04-14T21:02:37.860 回答