could anyone help my with my app code. I tryied to make an application that could send data (number or letter) to arduino through bluetooth. This is how my JAVA code look:
package com.example.btprojektas;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;
public class MainActivity extends Activity{
private static final String TAG = "btprojektas";
Button btnON, btnOFF;
BluetoothAdapter bluetoothAdapter = null;
BluetoothDevice device = null;
OutputStream outputStream = null;
BluetoothSocket socket = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
btnON = (Button) findViewById(R.id.btnON);
btnOFF = (Button) findViewById(R.id.btnOFF);
if(!bluetoothAdapter.isEnabled()){
Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetooth, 0);
}
loadPairedDevice();
connectBT();
btnON.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
sendData("0");
Toast.makeText(getBaseContext(), "Turn on LED", Toast.LENGTH_SHORT).show();
}
});
btnOFF.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
sendData("1");
Toast.makeText(getBaseContext(), "Turn off LED", Toast.LENGTH_SHORT).show();
}
});
}
private void connectBT() {
if (device != null) {
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); //Standard SerialPortService ID
try {
socket = device.createRfcommSocketToServiceRecord(uuid);
socket.connect();
outputStream = socket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void disconnect() {
try {
if (outputStream != null) outputStream.close();
if (socket != null) socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void loadPairedDevice() {
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
Log.d(TAG, "Device found");
for (BluetoothDevice device : pairedDevices)
if (device.getName().equals("HC-06")) {
this.device = device;
break;
}
}
}
#Override
protected void onPause() {
super.onPause();
disconnect();
}
#Override
protected void onResume() {
super.onResume();
loadPairedDevice();
connectBT();
}
private void sendData(String message) {
byte[] buffer = message.getBytes();
Log.d(TAG,"Send data:"+ message);
try{
outputStream.write (buffer);
} catch (IOException e) {}
}
}
in XML I have two buttons. When the program starts I push one of those buttons and the "Applications ... stopped unexpectedly" appears with fatal exeption fault code:
01-08 15:55:15.439 15354-15354/com.example.btprojektas E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.btprojektas.MainActivity.sendData(MainActivity.java:122)
at com.example.btprojektas.MainActivity.access$000(MainActivity.java:22)
at com.example.btprojektas.MainActivity$1.onClick(MainActivity.java:55)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
P.S. Sorry for this question I know that it is quite common but I am new at programming especially JAVA.
It's either the socket or the outputStream. In ConnectBT, you don't check if socket is not null. You directly call socket.connect() assuming socket is valid. The same applies to outputStream. You use it before making sure it's not null.
Also you call
startActivityForResult(enableBluetooth, 0);
but you don't check for the result which is whether bluetooth got enabled or not. This makes your device also suspicious.
Calling
loadPairedDevice();
connectBT();
makes sense only when bluetooth is enabled. Enabling bluetooth can take couple of seconds, but you call them right away.
A couple of tips:
you're calling loadPairedDevice() and connectBT() twice: in onCreate() and in onResume() - do it only once
before using outputStream, check if it's not null (as advised by others)
in sendData(), catch AND print your exception:
try {
if (outputStream != null) {
outputStream.write(buffer);
}
else {
Log.d("TAG", "sendData() - outputStream is null!");
}
}
catch (IOException e) {
e.printStackTrace();
}
in loadPairedDevice(), if you don't find the device "HC-06", your variable device will be null...
enabling bluetooth takes few seconds, so register and listen to ACTION_STATE_CHANGED broadcast Intent. It will contain extra field EXTRA_STATE; look for STATE_ON, and then call your loadPairedDevices() and connectBT() there:
create receiver (inside your MainActivity class):
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
//this is the action you are observing
if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
switch(state) {
//and the state we were looking for
//which means that bluetooth has switched on
//so now you can call your functions
//and set the flag to true, which then use in your
//onClick listeners
case BluetoothAdapter.STATE_ON:
loadPairedDevice();
connectBT();
isBluetoothOn = true;
break;
}
}
}
}
in onCreate(), create IntentFilter and register receiver with it
IntentFilter btFilter = new IntentFilter();
btFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mReceiver, btFilter);
remember to unregister receiver in onPause():
unregisterReceiver(mReceiver);
disable your buttons and enable them in the above listener, when you know BT is switched on; alternatively, keep a flag and use it in your click listeners, eg.:
boolean isBluetoothOn = false;
then later in listener when you get STATE_ON
isBluetooth = true;
And in your button click listener:
//for btnON
public void onClick(View v) {
if (isBluetoothOn) {
sendData("0");
Toast.makeText(getBaseContext(), "Turn on LED", Toast.LENGTH_SHORT).show();
}
}
Do the same for btnOFF.
Related
I am currently working on a simple Wi-fi scanner android application
with min API level 26 and target API level 28.
I want real time update in scan results so i have created a broadcast receiver but it is not working as intended.
Note: I have already tried
Wifi scan results broadcast receiver not working, Broadcast receiver with wifi scan not working
PLEASE NOTE THAT I WANT EXPLICIT BROADCAST RECEIVER NOT VIA MANIFEST FILE
I will be grateful to you.
Below is my java code:
package com.example.quickshare;
import androidx.appcompat.app.AppCompatActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
public class ActivitySend extends AppCompatActivity {
WifiManager wifiManager;
ListView ScanList;
List<ScanResult> results;
ListAdapter listAdapter;
WifiReceiver wifiReceiver;
IntentFilter intentFilter;
TextView msg;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send);
wifiManager = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
CheckWifiStatus();
msg = findViewById(R.id.wifiStatus);
intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
intentFilter.addAction(WifiManager.EXTRA_RESULTS_UPDATED);
try {
getApplicationContext().registerReceiver(wifiReceiver, intentFilter);
}
catch(Exception e){
System.out.println(e);
}
boolean success = wifiManager.startScan();
if(success)
Toast.makeText(ActivitySend.this, "Scanning", Toast.LENGTH_SHORT).show();
}
#Override
protected void onResume() {
super.onResume();
CheckWifiStatus();
registerReceiver(wifiReceiver, intentFilter);
wifiManager.startScan();
results = wifiManager.getScanResults();
if (results.size() > 0)
Toast.makeText(ActivitySend.this, "Scan Successful", Toast.LENGTH_LONG).show();
else
Toast.makeText(ActivitySend.this, "No Device Available", Toast.LENGTH_LONG).show();
ScanList = findViewById(R.id.ScanList);
listAdapter = new ListAdapter(getApplicationContext(), results);
ScanList.setAdapter(listAdapter);
ScanList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(ActivitySend.this, "Selected" + results.get(position).SSID, Toast.LENGTH_LONG).show();
//TODO: Establish Connection with selected SSID
}
});
}
class WifiReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(ActivitySend.this,"Available Device list changed",Toast.LENGTH_LONG).show();
//TODO: Append SSID of new Available APs in ListView and arrange a callback to onResume().
}
}
public void CheckWifiStatus(){
if (!wifiManager.isWifiEnabled()){
wifiManager.setWifiEnabled(true);
Toast.makeText(ActivitySend.this, "Wifi turned 'On' Successfully", Toast.LENGTH_SHORT).show();
msg.setText("Wifi Status : ON");
}
}
#Override
protected void onPause() {
unregisterReceiver(wifiReceiver);
super.onPause();
}
}
Using Above java code i can scan available APs if they are available before launching the activity.
After Launching this activity nothing changes in scan result and it keep showing previously fetched results even if i turn off that AP.
In order to detect your AP being disconnected, your intentFilter is lacking the ConnectivityManager.CONNECTIVITY_ACTION.
You can listen
to these action with the following line:
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
Also, you need to add brackets to your else code blocks, i.e.
if {
// ...
} else {
Toast.makeText(ActivitySend.this, "No Device Available", Toast.LENGTH_LONG).show();
ScanList = findViewById(R.id.ScanList);
// ...
}
I am trying to develop an app where the user takes a photo and uploads it to Google Drive automatically (The image is not saved on the phone). I figured out the code for that thanks to a Google Drive tutorial, but I have no idea how I would do that when it comes to upload videoes?
Here is the code for photo uploading:
package com.google.android.gms.drive.sample.quickstart;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentSender;
import android.content.IntentSender.SendIntentException;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.drive.Drive;
import com.google.android.gms.drive.DriveApi.DriveContentsResult;
import com.google.android.gms.drive.MetadataChangeSet;
/**
* Android Drive Quickstart activity. This activity takes a photo and saves it
* in Google Drive. The user is prompted with a pre-made dialog which allows
* them to choose the file location.
*/
public class VideoCapture extends Activity implements ConnectionCallbacks,
OnConnectionFailedListener {
private static final String TAG = "drive-quickstart";
private static final int REQUEST_CODE_CAPTURE_VIDEO = 1;
private static final int REQUEST_CODE_CREATOR = 2;
private static final int REQUEST_CODE_RESOLUTION = 3;
private GoogleApiClient mGoogleApiClient;
private Byte mVideoToSave;
/**
* Create a new file and save it to Drive.
*/
private void saveFileToDrive() {
// Start by creating a new contents, and setting a callback.
Log.i(TAG, "Creating new contents.");
final Byte video = mVideoToSave;
Drive.DriveApi.newDriveContents(mGoogleApiClient)
.setResultCallback(new ResultCallback<DriveContentsResult>() {
#Override
public void onResult(DriveContentsResult result) {
// If the operation was not successful, we cannot do anything
// and must
// fail.
if (!result.getStatus().isSuccess()) {
Log.i(TAG, "Failed to create new contents.");
return;
}
// Otherwise, we can write our data to the new contents.
Log.i(TAG, "New contents created.");
// Get an output stream for the contents.
OutputStream outputStream = result.getDriveContents().getOutputStream();
// Write the bitmap data from it.
ByteArrayOutputStream mBAOS = new ByteArrayOutputStream();
byte[] mByte = new byte[1024];
try {
outputStream.write(mBAOS.toByteArray());
} catch (IOException e1) {
Log.i(TAG, "Unable to write file contents.");
}
// Create the initial metadata - MIME type and title.
// Note that the user will be able to change the title later.
MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
.setMimeType("video/mp4").setTitle("Android Video.mp4").build();
// Create an intent for the file chooser, and start it.
IntentSender intentSender = Drive.DriveApi
.newCreateFileActivityBuilder()
.setInitialMetadata(metadataChangeSet)
.setInitialDriveContents(result.getDriveContents())
.build(mGoogleApiClient);
try {
startIntentSenderForResult(
intentSender, REQUEST_CODE_CREATOR, null, 0, 0, 0);
} catch (SendIntentException e) {
Log.i(TAG, "Failed to launch file chooser.");
}
}
});
}
#Override
protected void onResume() {
super.onResume();
if (mGoogleApiClient == null) {
// Create the API client and bind it to an instance variable.
// We use this instance as the callback for connection and connection
// failures.
// Since no account name is passed, the user is prompted to choose.
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
// Connect the client. Once connected, the camera is launched.
mGoogleApiClient.connect();
}
#Override
protected void onPause() {
if (mGoogleApiClient != null) {
mGoogleApiClient.disconnect();
}
super.onPause();
}
#Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
switch (requestCode) {
case REQUEST_CODE_CAPTURE_VIDEO:
// Called after a photo has been taken.
if (resultCode == Activity.RESULT_OK) {
// Store the image data as a bitmap for writing later.
mVideoToSave = (Byte) data.getExtras().get("data");
}
break;
case REQUEST_CODE_CREATOR:
// Called after a file is saved to Drive.
if (resultCode == RESULT_OK) {
Log.i(TAG, "Image successfully saved.");
mVideoToSave = null;
// Just start the camera again for another photo.
startActivityForResult(new Intent(MediaStore.ACTION_VIDEO_CAPTURE),
REQUEST_CODE_CAPTURE_VIDEO);
}
break;
}
}
#Override
public void onConnectionFailed(ConnectionResult result) {
// Called whenever the API client fails to connect.
Log.i(TAG, "GoogleApiClient connection failed: " + result.toString());
if (!result.hasResolution()) {
// show the localized error dialog.
GoogleApiAvailability.getInstance().getErrorDialog(this, result.getErrorCode(), 0).show();
return;
}
// The failure has a resolution. Resolve it.
// Called typically when the app is not yet authorized, and an
// authorization
// dialog is displayed to the user.
try {
result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
} catch (SendIntentException e) {
Log.e(TAG, "Exception while starting resolution activity", e);
}
}
#Override
public void onConnected(Bundle connectionHint) {
Log.i(TAG, "API client connected.");
if (mVideoToSave == null) {
// This activity has no UI of its own. Just start the camera.
startActivityForResult(new Intent(MediaStore.ACTION_VIDEO_CAPTURE),
REQUEST_CODE_CAPTURE_VIDEO);
return;
}
saveFileToDrive();
}
#Override
public void onConnectionSuspended(int cause) {
Log.i(TAG, "GoogleApiClient connection suspended");
}
}
I have this interface on my Android project:
package com.kkoci.shairlook;
/**
* Created by kristian on 07/07/2015.
*/
public interface OnTaskFinishListener{
void onFinish();
}
I'm using this, to call a onPostExecute on my AsyncTask class:
package com.kkoci.shairlook;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;
import com.appspot.shairlook1.userEndpoint.UserEndpoint;
import com.appspot.shairlook1.userEndpoint.model.User;
import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.extensions.android.json.AndroidJsonFactory;
import com.google.api.client.googleapis.services.AbstractGoogleClientRequest;
import com.google.api.client.googleapis.services.GoogleClientRequestInitializer;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
/**
* Created by kristian on 04/07/2015.
*/
public class EndpointsAsyncTaskInsert extends AsyncTask<String, Void, User> implements GoogleClientRequestInitializer {
private static UserEndpoint myApiService = null;
private Context context;
private OnTaskFinishListener listener;
EndpointsAsyncTaskInsert(Context context) {
this.context = context;
}
public EndpointsAsyncTaskInsert(OnTaskFinishListener listener){
this.listener = listener;
}
#Override
public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest) throws IOException {
// put it here no in MyClass
abstractGoogleClientRequest.setDisableGZipContent(true);
}
#Override
protected User doInBackground(String... params) {
User response = null;
if (myApiService == null) { // Only do this once
UserEndpoint.Builder builder = new UserEndpoint.Builder(AndroidHttp.newCompatibleTransport(),
new AndroidJsonFactory(), null)
// options for running against local devappserver
// - 10.0.2.2 is localhost's IP address in Android emulator
// - turn off compression when running against local devappserver
.setRootUrl("https://shairlook1.appspot.com/_ah/api/")
.setGoogleClientRequestInitializer(this);
// end options for devappserver
myApiService = builder.build();
}
try {
User users = new User();
users.setEmail(params[0]);
users.setPassword(params[1]);
users.setName(params[2]);
response = myApiService.insertUser(users).execute();
} catch (Exception e) {
Log.d("Could not Add User", e.getMessage(), e);
}
return response;
}
protected void onPostExecute(User user){
listener.onFinish();
}
}
But this is giving me java.lang.NoCalssDefFound error, I'm initializing it in my Activity like this:
public class LoginMember extends Activity implements OnTaskFinishListener {
public void onFinish(){
Intent intent = new Intent(LoginMember.this, WelcomeScreen.class);
startActivity(intent);
}
Then execute:
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(txtEmail.getWindowToken(), 0);
imm.hideSoftInputFromWindow(txtPassword.getWindowToken(), 0);
String password = txtPassword.getText().toString();
String email = txtEmail.getText().toString();
if ((txtEmail.length() == 0) || (txtPassword.length() == 0)) {
Toast.makeText(LoginMember.this, "You need to provide values for Email and Password", Toast.LENGTH_SHORT).show();
return;
}
//Go ahead and perform the transaction
String[] params = {email, password};
new EndpointsAsyncTaskInsert(currentActivity.getApplicationContext()).execute(params);
}
});
So, the problem arises when executing EndpointAsyncTaskInsert method listener.OnFinish();
This is the complete logcat:
7752-7752/com.kkoci.shairlook E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.kkoci.shairlook.EndpointsAsyncTaskInsert.onPostExecute(EndpointsAsyncTaskInsert.java:74)
at com.kkoci.shairlook.EndpointsAsyncTaskInsert.onPostExecute(EndpointsAsyncTaskInsert.java:24)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:175)
at android.app.ActivityThread.main(ActivityThread.java:5279)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
I've seen this question here, seems to be the same kind of problem, but I'm not really sure about that solution.
Any ideas, please?
Thanks in advance!
The logs say the error is java.lang.NullPointerException... and it's because you're creating a EndpointsAsyncTaskInsert using the Context constructor, thus listener inside of it null... and you dereference it without checking for null in onPostExecute
I seem to be having major problems with my eclipse emulator and don't know if it's the ram size or my code. In my opinion, my code should run, as it does so in my java netbeans project.
Everytime I run my application and push the connect button, I want to get the string that the server sends back and then do something with it. I have a "Process Connection" method that reads in the string, but when I return it and actually use what is being returned, my emulator crashes
My code is as follows:
package za.nmmu.wrap302.networks.example02;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ListView lstMessages;
private EditText txtMessage;
private ArrayList<String> messages;
private ArrayAdapter<String> adapter;
String message = "";
private ServerConnection connection;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get references to View objects
txtMessage = (EditText) findViewById(R.id.txtMessage);
lstMessages = (ListView) findViewById(R.id.lstMessages);
// set up adapter
messages = new ArrayList<String>();
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, messages);
lstMessages.setAdapter(adapter);
// attach event listener
txtMessage.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_ENTER)
&& (event.getAction() == KeyEvent.ACTION_DOWN)) {
try {
onTxtMessageEnterPressed();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
return false;
}
});
}
public void onBtnConnectClicked(View view) {
clearMessages();
connection = new ServerConnection();
connection.start();
}
public void onTxtMessageEnterPressed() throws IOException {
if (connection != null) {
String message = txtMessage.getText().toString();
txtMessage.getText().clear();
connection.sendData(message);
}
}
public void addMessage(String message) {
adapter.add(message);
}
public void clearMessages() {
adapter.clear();
}
// the thread that will be communicating with the server
public class ServerConnection extends Thread {
// the I/O streams that will be receiving/sending data from/to the
// server
private ObjectOutputStream output;
private ObjectInputStream input;
private Socket client;
#Override
public void run() {
try {
// Step 1: Create a Socket to make connection
connectToServer();
// Step 2: Get the input and output streams
getStreams();
// Step 3: Process connection
processConnection();
// Step 4: Close connection
//closeConnection();
} catch (IOException e) {
Log.e("CONNECTION", e.getMessage());
}
}
public void addMessage(final String message) {
runOnUiThread(new Runnable() {
#Override
public void run() {
MainActivity.this.addMessage(message);
}
});
}
private void connectToServer() throws IOException {
addMessage("Attempting connection\n");
client = new Socket("10.0.0.7", 5001);
addMessage("Connected to: " + client.getInetAddress().getHostName());
}
private void getStreams() throws IOException {
output = new ObjectOutputStream(client.getOutputStream());
output.flush();
input = new ObjectInputStream(client.getInputStream());
addMessage("Got I/O streams");
}
//I would like to call the message below and return it to anywhere else in the code
private String processConnection() throws IOException
{
do {
try {
message = (String) input.readObject();
addMessage(message);
return message;
}
catch (ClassNotFoundException classNotFoundException)
{
addMessage("ERROR: Unknown object type received");
}
return message;
} while (!message.equals("SERVER>>> TERMINATE"));
}
private void sendData(String message) {
try {
output.writeObject(message);
output.flush();
addMessage("CLIENT>>>" + message);
} catch (IOException ioException) {
addMessage("ERROR: Error writing object");
}
}
private void closeConnection() throws IOException {
addMessage("Closing connection");
output.close();
input.close();
client.close();
}
}
}
My application seems to just crash whenever I call the processConnection method from anywhere.
My server picks up that I've sent the message, but my client doesn't read.
http://i.stack.imgur.com/cNu7m.png
My logcat shows:
06-13 08:18:00.460: D/dalvikvm(1145): GC_FOR_ALLOC freed 45K, 4% free 3076K/3204K, paused 293ms, total 296ms
06-13 08:18:00.460: I/dalvikvm-heap(1145): Grow heap (frag case) to 3.687MB for 635812-byte allocation
06-13 08:18:00.530: D/dalvikvm(1145): GC_FOR_ALLOC freed 1K, 4% free 3695K/3828K, paused 55ms, total 55ms
06-13 08:18:02.220: I/Choreographer(1145): Skipped 172 frames! The application may be doing too much work on its main thread.
06-13 08:18:02.240: D/gralloc_goldfish(1145): Emulator without GPU emulation detected.
06-13 08:18:03.100: I/Choreographer(1145): Skipped 198 frames! The application may be doing too much work on its main thread.
06-13 08:18:27.660: E/InputEventSender(1145): Exception dispatching finished signal.
06-13 08:18:27.660: E/MessageQueue-JNI(1145): Exception in MessageQueue callback: handleReceiveCallback
Does the ram in the emulator affect this? What am I doing wrong?
You need to use Async task to spawn off uploading to a different thread. Android works on a single thread model and using the same thread to make HTTPRequest can result in FATAL exception. Create an Async task and spawn off the upload to it.
AsyncTaskRunner runner = new AsyncTaskRunner();
runner.execute(<pass the required parameters here for file upload>);
private class AsyncTaskRunner extends AsyncTask<String, String, String> {
#Override
protected String doInBackground(String... params) {
//Call the function to upload the file here
}
This is what the logcat is telling you by Main thread is doing too many tasks.
So for my research, I have to send accelometer data to an arduino mega as a constant stream. I have the module connected to the arduino via serial. However, when I ran the code, it only runs once. I tried to place the Bluetooth connect part of the code inside my on accuracy change part of my code, but it keeps freezing the device. Here's my code:
package com.example.arduino_bluetooth2;
//=================================================================================================
//Imports
//=================================================================================================
import java.io.IOException;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;
import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.widget.TextView;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
public class MainActivity extends Activity implements SensorEventListener {
// Setup necessary sensor objects
private Sensor acc;
private SensorManager sm;
private TextView t1;
private double value;
// Bluetooth Object
private BluetoothAdapter bAdapter;
private BluetoothDevice device;
private BluetoothSocket mmServerSocket;
private OutputStream btoutput;
private static final UUID SPP_UUID = UUID
.fromString("00001101-0000-1000-8000-00805F9B34FB");
private static final int DISCOVERY_REQUEST = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
accelerometer_initialization();
bluetooth_initialization();
}
// Setsup the accelerometer object
private void accelerometer_initialization() {
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
acc = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sm.registerListener(this, acc, SensorManager.SENSOR_DELAY_NORMAL);
}
// Setup bluetooth object
private void bluetooth_initialization() {
bAdapter = BluetoothAdapter.getDefaultAdapter();
startActivityForResult(new Intent(
BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE),
DISCOVERY_REQUEST);
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
bAdapter.startDiscovery();
}
#Override
public void onSensorChanged(SensorEvent event) {
value = event.values[0];
}
#Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
}
final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
if (BluetoothDevice.ACTION_FOUND.equals(intent.getAction())) {
device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (new String(device.getName()).equals("BT UART")) {
bAdapter.cancelDiscovery();
try {
BluetoothSocket test = null;
test = device
.createInsecureRfcommSocketToServiceRecord(SPP_UUID);
mmServerSocket = test;
mmServerSocket.connect();
String message = Double.toString(value);
byte[] send = message.getBytes();
btoutput = mmServerSocket.getOutputStream();
btoutput.write(send);
btoutput.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
};
}
I am not sure you should creating and connecting the bluetooth socket in the broadcast receiver. I do the bluetooth connection management in the onResume() of the activity.
Also I use a thread to manage getting data from the serial data connection between the arduino and the device, it is spawned off and runs continuously in the background. There is a write method to send data out that i call from the activity
/* Call this from the main activity to send data to the remote device */
public void write(String message) {
System.out.println("...Data to send: " + message + "...");
byte[] msgBuffer = message.getBytes();
try {
mmOutStream.write(msgBuffer);
} catch (IOException e) {
System.out.println("...Error data send: " + e.getMessage() + "...");
}
}
then the run() method of the tread takes care of getting data back
See my answer in this thread for an example
Error with receiving xml strings via bluetooth in Android
Good luck!
Check out this page from arduino: http://arduino.cc/en/Reference/Loop
The problem is that it only goes once because it is not in a loop that continues forever until the device is shut off or told otherwise.