Single function to retrive data from Broadcast receiver - java

I have a function ReadConfig in non activity class which is invoked by a Service. This function uses IntentService to read data from a file.
Function in non activity class
//Start of globalVariables
public static boolean received;
public static String actionID = "ACTION_ID";
public static ArrayList<String> configData;
public static BroadcastReceiver configDataReceiver;
//End of globalVariables
public static List<String> ReadConfig(Context context, String configFileName)
{
configDataReceiver = new BroadcastReceiver()
{
#Override
public void onReceive(Context arg0, Intent intent)
{
if(intent.getAction().equals(actionID))
{
configData = intent.getStringArrayListExtra("CONFIGDATA");
received = true;
}
}
};
LocalBroadcastManager.getInstance(context)
.registerReceiver(configDataReceiver,new IntentFilter(actionID));
Intent workRequest = new Intent(context,IOConfigurations.class);
workRequest.putExtra("OPERATION","READ");
workRequest.putExtra("FILENAME",configFileName);
//Calling intentservice
context.startService(workRequest);
//Wait for broadcast receiver to get data and assign to global variables
while (!received)
{
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
LocalBroadcastManager.getInstance(context).unregisterReceiver(configDataReceiver);
return configData;
}
Intent Service
Intent configData = new Intent(actionID);
configData.putStringArrayListExtra("CONFIGDATA",FileOperations.readFile(fileName));
LocalBroadcastManager.getInstance(this).sendBroadcast(configData);
The intent service is getting invoked and broadcasts the data but the receiver however doesn't receive any data from Broadcast manager and the while loop continues for ever.

check my comments
public static List<String> ReadConfig(Context context, String configFileName)
{
configDataReceiver = new BroadcastReceiver()
{
#Override
public void onReceive(Context arg0, Intent intent)
{
if(intent.getAction().equals(actionID))
{
configData = intent.getStringArrayListExtra("CONFIGDATA");
received = true;
}
}
};
LocalBroadcastManager.getInstance(context)
.registerReceiver(configDataReceiver,new IntentFilter(actionID));
Intent workRequest = new Intent(context,IOConfigurations.class);
workRequest.putExtra("OPERATION","READ");
workRequest.putExtra("FILENAME",configFileName);
//Calling intentservice
context.startService(workRequest);
//Wait for broadcast receiver to get data and assign to global variables
while (!received)
{
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
// Why here your unregistering the broacast listener,
// Your registering and unregistering in same call, unregiter when your work is done
LocalBroadcastManager.getInstance(context).unregisterReceiver(configDataReceiver);
return configData;
}

Related

Why can't attributes of "custom defined" classes be set (in java (7))?

I'm working on an android project that overrides the code of BroadcastReceiver:
private void registerBroadcastReceiver() {
final IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(REQ_ACTION);
intentFilter.addAction(RESPONSE_ACTION);
this.broadcastReceiver = new BroadcastReceiver() {
private Object syncObject;
private WebServer webServer;
public void setSyncObject(Object obj) {
this.syncObject = obj;
}
public void setWebServer(WebServer webServer) {
this.webServer = webServer;
}
#Override
public void onReceive(Context context, Intent intent) {
printIntentInfo(intent);
}
};
// Registers the receiver so that the service will listen for broadcasts
this.registerReceiver(this.broadcastReceiver, intentFilter);
}
where registerBroadcastReceiver is called in onCreate() (in a service).
Whenever I try to call setSyncObject() in broadcast receiver:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "Starting web server");
syncObject = new Object();
try {
webServer = new WebServer(getApplicationContext(), PORT_NO);
broadcastReceiver.setSyncObject(syncObject); // <= ERROR HERE
} catch (IOException e) {
Log.d(TAG, "\nCouldn't start web server on port " + PORT_NO + "\n");
e.printStackTrace();
}
...
}
I get an error:
Cannot resolve method setSyncObject in BroadcastReceiver
The answer seems obvious, just create a new receiver that extends bc and add the methods there. But I'm curious as to why the methods can't be seen(, unless they're resolved at compile time).
Even when creating a separate object:
public class CustomBroadcastReceiver extends BroadcastReceiver {
private Object syncObject;
private WebServer webServer;
public void setSyncObject(Object obj) {
this.syncObject = obj;
}
public void setWebServer(WebServer webServer) {
this.webServer = webServer;
}
#Override
public void onReceive(Context context, Intent intent) {
printIntentInfo(intent);
}
}
the attribute has to be cast to its type
((CustomBroadcastReceiver)broadcastReceiver).setSyncObject(syncObject);

Immortal Service

I need to implement a project, such as chat. We decided to use the Socket.IO library. FCM is not considered. To receive messages in the background using Service. Here:
public class SocketServiceProvider extends Service {
private Socket mSocket;
private final String EVENT_NEW_MESSAGE = "new_message";
private final String LOG_TAG = "SocketServiceProvider";
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
Log.e(LOG_TAG, "created()");
realm = Realm.getDefaultInstance();
startForeground(1, new Notification());
if (mSocket == null)
mSocket = BaseApplication.getSocket();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(LOG_TAG, "onStartedCommand()");
startSocket();
return START_STICKY;
}
private void startSocket() {
if (mSocket.connected()){
stopSocket();
}
mSocket.on(Socket.EVENT_CONNECT, onConnect);
mSocket.on(EVENT_NEW_MESSAGE, onNewMessage);
mSocket.connect();
}
private void stopSocket() {
mSocket.off();
mSocket.disconnect();
}
private Emitter.Listener onConnect = new Emitter.Listener() {
#Override
public void call(Object... args) {
new Handler(Looper.getMainLooper()).post(() -> {
if (mSocket.connected()) {
isOnline = true;
Log.e(LOG_TAG, "Connected!");
}
});
}
};
private Emitter.Listener onNewMessage = args -> {
final JSONObject data = (JSONObject) args[0];
final String username;
final String message;
try {
username = data.getString("from");
message = data.getString("message");
} catch (JSONException e) {
Log.e("MainActivity", e.getMessage());
return;
}
Log.e(LOG_TAG, username + " wrote: " + message);
};
#Override
public void onDestroy() {
super.onDestroy();
Log.e(LOG_TAG, "onDestroy()");
stopSocket();
ContextCompat.startForegroundService(this, new Intent(this, SocketServiceProvider.class));
}
}
The only problem is that when the phone goes into Doze mode, messages do not come. Tried to wake up with AlarmManager in onTaskRemoved(), onDestroy(), unsuccessfully.
Even with onDestroy() tried to call BroadcastReceiver, so that it started back my Service, just did not understand why, but its onReceive() method does not work.
Here is my last option, the code that posted. There is I usе startForegroundService. And this option worked, at least not dying. Only in this case, the battery discharges quickly
Googled, Write that using JobIntentService can be implemented, but nowhere described in detail.
Question: How can this be done and how did you implement such tasks? And how can this be achieved with JobIntentService?

How to pass a String from a UDP Listening Service to the MainActivity?

I have a service listening for UDP packets that is bound to my MainActivity (which is the only activity in the app). The service runs on its own thread and I can see the UDP messages as well as the parsed messages in logcat. I created a setParsedMessage() and a public getParsedMessage() in order to get the parsed string and send it to my main activity in order to change a TextView and an ImageView depending on what the parsed message is, however it does not appear to be retrieving the String for some reason. I read about this method on the Developer.Android website, however I've also seen something about using Handler to do this instead. Here is my code:
MainActivity:
public class MainActivity extends Activity {
AlertAssignments mAlertAssignments;
Button startListeningButton;
boolean started;
int counter;
boolean mBound = false;
Context context;
ListenerService mListenerService;
TextView mTextView;
TextView mBlinkView;
ImageView mImageView;
private StartListening _StartListeningTask;
String messageFromService = "";
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//start listener service
Intent listenerServiceIntent = new Intent(MainActivity.this, ListenerService.class);
this.bindService(listenerServiceIntent, mConnection, Context.BIND_AUTO_CREATE);
mImageView = (ImageView) findViewById(R.id.image_view);
mTextView = (TextView) findViewById(R.id.alert_text);
mBlinkView = (TextView) findViewById(R.id.blinking_text);
Animation mAnimation = new AlphaAnimation(0.0f, 1.0f);
mAnimation.setDuration(50);
mAnimation.setStartOffset(20);
mAnimation.setRepeatCount(Animation.INFINITE);
mAnimation.setRepeatMode(Animation.REVERSE);
mBlinkView.startAnimation(mAnimation); //animation value
mAlertAssignments = new AlertAssignments();
}
private ServiceConnection mConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName className, IBinder service) {
ListenerService.LocalBinder binder = (ListenerService.LocalBinder) service;
mListenerService = binder.getService();
mBound = true;
if(mBound) {
Log.e("UDP", "Service has been bound successfully");
}
else {
Log.e("UDP", "Service has not been bound");
}
readFromService();
}
#Override
public void onServiceDisconnected(ComponentName name) {
mBound = false;
}
};
#Override
protected void onStart() {
super.onStart();
}
#Override
protected void onStop() {
super.onStop();
//unbind from service
if(mBound) {
this.unbindService(mConnection);
mBound = false;
}
}
private void readFromService() {
try {
Integer parsedMessage = Integer.valueOf(mListenerService.getParsedMessage());
mImageView.setImageResource(mAlertAssignments.alarmImages[parsedMessage]);
if(parsedMessage >= 10 && parsedMessage <= 19 && parsedMessage != 0) {
mTextView.setText(mAlertAssignments.alertTextMessages[parsedMessage]);
} else {
mBlinkView.setText(mAlertAssignments.alertTextMessages[parsedMessage]);
}
} catch(NumberFormatException e) {
e.printStackTrace();
}
}
}
I had read that using the public getter like this:
Integer parsedMessage = Integer.valueOf(mListenerService.getParsedMessage());
would allow me to access the string value of mListenerService.getParsedMessage, however I'm guessing that may only work for started services, not bound services.
AlertAssignments is a simple enumeration that uses ordinal arrays to bind images and Strings to values, so mImageView.setImageResource(mAlertAssignments.alarmImages[parsedMessage]) would set the ImageView to an image. Finally, here is the Service:
public class ListenerService extends Service{
public String the_alarm_S;
public String parsedMessage = "";
private final IBinder mBinder = new LocalBinder();
public class LocalBinder extends Binder {
ListenerService getService() {
return ListenerService.this;
}
}
#Override
public IBinder onBind(Intent intent) {
return mBinder;
}
DatagramSocket socket;
Thread UDPBroadcastThread;
void startListenForUDPBroadcast() {
UDPBroadcastThread = new Thread(new Runnable() {
public void run() {
try {
while (shouldRestartSocketListen) {
try {
socket = new DatagramSocket(12001);
socket.setReuseAddress(true);
String message = "";
byte[] recvBuf = new byte[1024];
DatagramPacket packet = new DatagramPacket(recvBuf, 1024);
Log.e("UDP", "Waiting for UDP broadcast");
try {
socket.receive(packet);
Log.e("UDP", "Received Packet");
} catch (IOException e) {
e.printStackTrace();
}
message = new String(packet.getData());
Log.e("UDP", "Got UDB broadcast message: " + message);
setParsedMessage(message);
if(socket != null) {
socket.close();
}
} catch (SocketException e) {
e.printStackTrace();
}
}
//if (!shouldListenForUDPBroadcast) throw new ThreadDeath();
} catch (Exception e) {
Log.i("UDP", "no longer listening for UDP broadcasts cause of error " + e.getMessage());
}
}
});
UDPBroadcastThread.start();
}
private Boolean shouldRestartSocketListen = true;
private void setParsedMessage(String messageContents) {
the_alarm_S = messageContents;
String parseMessage[] = the_alarm_S.split("!!!");
Log.e("UDP", "Parsed message with value " + parseMessage[1]);
parsedMessage = parseMessage[1];
}
public String getParsedMessage() {
return parsedMessage;
}
private void stopListen() {
shouldRestartSocketListen = false;
if(socket != null) {
socket.close();
}
}
#Override
public void onCreate() {
startListenForUDPBroadcast();
}
#Override
public void onDestroy() {
stopListen();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
shouldRestartSocketListen = true;
startListenForUDPBroadcast();
Log.i("UDP", "Service started");
return START_STICKY;
}
}
Can someone give me the simplest method of getting the String from the service to the main activity, or if I already have it, where I am going wrong in using it? I would like to avoid having to rewrite my Service as an IntentService unless it's absolutely necessary to do so since this is a relatively simple object to pass to MainActivity
Thanks
You could try subscribing to the service. What I mean is pass some interface that the service calls to notify the activity about changes, here's an example I just tested:
A Subscriber interface
public interface ServiceSubscriber {
void messageCallback(String message);
}
Subscribe to the service using the Subscriber
public class TestService extends Service {
ArrayList<ServiceSubscriber> subscribers = new ArrayList<>();
private TestBinder testBinder = new TestBinder();
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
new Thread(){
#Override
public void run() {
while(true){
//this is where you are receiving UDP packets
doStuff();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
return super.onStartCommand(intent, flags, startId);
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return testBinder;
}
private void doStuff() {
System.out.println("Service is doing stuff!");
//loop through your subscribers and notify them of your changes
//a loop here isn't very costly, if there aren't many subscribers
for (ServiceSubscriber subscriber : subscribers) {
subscriber.messageCallback("I'm doing stuff");
}
}
public class TestBinder extends Binder {
public TestService getService() {
return TestService.this;
}
}
public void subscribeToMessages(ServiceSubscriber subscriber) {
subscribers.add(subscriber);
}
public void unSubscribeToMessages(ServiceSubscriber subscriber) {
subscribers.remove(subscriber);
}
}
Now for the usual Binding Activity, where you define what you need to do with the Message Callback:
public class MainActivity extends AppCompatActivity {
private TestService testService;
private Subscriber subscriber;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onStart() {
super.onStart();
bindService(new Intent(this, TestService.class),serviceConnection, Context.BIND_AUTO_CREATE);
}
private ServiceConnection serviceConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName name, IBinder service) {
testService = ((TestService.TestBinder)service).getService();
subscriber = new ServiceSubscriber() {
#Override
public void messageCallback(String message) {
//I'm just printing out the message received
//Be careful if you need to do UI stuff to use a
//Handler
System.out.println(message);
}
}
testService.subscribeToMessages(subscriber );
}
#Override
public void onServiceDisconnected(ComponentName name) {
}
};
}
Of course don't forget to unsubscribe on destroy.
Updating UI often doesn't break your app if you do it by using a handler
//activity fields
Handler handler
//in activity constructor
handler = new Handler();
//update UI by calling
handler.post(new Runnable(){
#Override
public void run(){
//update the UI here
}
EDIT: I forgot to keep a reference of the subscriber, to unsubscribe later. Changed from anonymous instance to a field.
Make below method to your sevice class:
private void sendMessage() {
Intent intent = new Intent("message");
intent.putExtra("message", your_message);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
And put the below code in your activity class:
#Override
public void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(this)
.registerReceiver(mMessageReceiver,
new IntentFilter("message"));
}
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String yourMessage = intent.getIntExtra("message",-1);
}
};
#Override
protected void onPause() {
LocalBroadcastManager.getInstance(this)
.unregisterReceiver(mMessageReceiver);
super.onPause();
}
Note: -1 is for default value

how to control the Activity method flow till non activity class perform network operation

i have one method in activity in which some operations are performed from non activity class. i want to know when the non activity class network operation are completed so that i can call that method again. but when i call non activity class then the method goes to at the end.
here is my code
public void somemethod(){
.
.
.
.
if (condition) {
new RegenerateToken().generate(DriverActivity.this); // calling non activity class which perform some network operation
//here i want to know that non activity class has performed the network operation so i can call this method again
}
.
.
.
log.d("method","ending");
}
non activity class
public class RegenerateToken {
public void generate(Context context) {
ExecuteServerReq executeServerReq = new ExecuteServerReq(context, client, Utilz.URL + "/authenticate", params, true, true);
executeServerReq.execute();
executeServerReq.getResponse = new ExecuteServerReq.GetResponse() {
#Override
public void onResponse(String objects) {
Utilz.printLog("RegenerateTokenresponse", objects);
}
};
}
}
You need to implement an interface to get the callback
public class RegenerateToken {
public interface Callback {
public void onResponce(String data);
}
public void generate(Context context,final Callback callBack) {
ExecuteServerReq executeServerReq = new ExecuteServerReq(context, client, Utilz.URL + "/authenticate", params, true, true);
executeServerReq.execute();
executeServerReq.getResponse = new ExecuteServerReq.GetResponse() {
#Override
public void onResponse(String objects) {
Utilz.printLog("RegenerateTokenresponse", objects);
callBack.onResponce(objects);
}};
}
}
Callback implementation
if(condition) {
new RegenerateToken().generate(DriverActivity.this, new Callback() {
#Override
public void onResponse(String objects) { //your data do ur processing
}
});
}
You can use LocalBroacastManager to send and receive information within your app
public class RegenerateToken {
public void generate(Context context) {
//your code
executeServerReq.getResponse = new ExecuteServerReq.GetResponse() {
#Override
public void onResponse(String objects) {
Utilz.printLog("RegenerateTokenresponse", objects);
Intent intent = new Intent("Respone");
if(expected responce){
intent.putExtra("result", "Successful");
}
else{
intent.putExtra("result", "UnSuccessful");
}
LocalBroadcastManager.getInstance(this).sendBroadcast(iIntent);
}
};
}
}
And at your Activity register and define a receiver for your Local Broadcast
#Override
public void onCreate(Bundle savedInstanceState) {
...
//Register the custom Broadcast "Response"
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
new IntentFilter("Response"));
}
// Our handler for received Intents. This will be called whenever an Intent
// with an action named "Response" is broadcasted.
private BroadcastReceiver myMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String response= intent.getStringExtra("result");
if(response.equals("Successful")){
//on successful execution
// call your method again or do whatever you wish
}else{
//on otherwise
}
}
};

can't start intent startActivityForResult from class

I will be very happy if someone can help me, because I'm new at object programming. My problem is: I'm writting some app with bluetooth communication. I wrote all methods and successfully connect and transfer data between devices in MainActivity.class. I have also one SearchActivity.class which shows all devices in range on List, so user can pick one. Device is then passed through Intent to MainActivity, where connection starts. But because of nature of my app I must created separate class, just for Bluetooth communication called BluetoothService.class. I moved all methods for Bluetooth and other stuff to BluetoothService.class.
Now I even can't compile my project, because I get error at creating Intent for SearchActivity, I also get error startActivityForResult and onActivityResult methods.
First error is: The constructor Intent(BluetoothService, Class) is undefined
Second error: The method startActivityForResult(Intent, int) is undefined for the type BluetoothService
public void startConnection() {
// Create an intent for SearchActivity
Intent intent = new Intent(this, SearchActivity.class);
//start SearchActivity through intent and expect for result.
//The result is based on result code, which is REQUEST_DISCOVERY
startActivityForResult(intent, REQUEST_DISCOVERY);
}
When I was calling method startConnection() from MainActivity everything worked, but now I it doesn't. I think the problem is, that I can't create new Activity from non-activity class.
Next error is in onActivityResult method: *RESULT_OK cannot be resolved to a variable*
//on ActivityResult method is called, when other activity returns result through intent!
//when user selected device in SearchActivity, result is passed through intent with //requestCode, resultCode (intent data + requestCode + resultCode)
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode != REQUEST_DISCOVERY) {
Log.d("Debug", ">>intent REQUEST_DISCOVERY failed!");
return;
}
if (resultCode != RESULT_OK) {
Log.d("Debug", ">>intent RESULT_OK failed!");
return;
}
Log.d("Debug", ">>onActivityResult!");
final BluetoothDevice device = data.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.d(device.getName(), "Name of Selected Bluetoothdevice");
new Thread () {
public void run() {
//call connect function with device argument
connect(device);
};
}.start();
}
Please, tell me how can I solve this. If you need more info or code tell me. Thanks.
public class SearchActivity extends ListActivity
{
//name of LxDevices, that will be shown on search
private String nameOfLxDevice = "DEBUG";
private Handler handler = new Handler();
/* Get Default Adapter */
private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
/* Storage the BT devices */
private List<BluetoothDevice> devices = new ArrayList<BluetoothDevice>();
/* Discovery is Finished */
private volatile boolean discoveryFinished;
/* Start search device */
private Runnable discoveryWorker = new Runnable() {
public void run()
{
//To start discovering devices, simply call startDiscovery(). The process is asynchronous and the method will
//immediately return with a boolean indicating whether discovery has successfully started.
mBluetoothAdapter.startDiscovery();
Log.d("debug", ">>Starting Discovery");
for (;;)
{
if (discoveryFinished)
{
Log.d("debug", ">>Finished");
break;
}
try
{
Thread.sleep(100);
}
catch (InterruptedException e){}
}
}
};
/* when discovery is finished, this will be called */
//Your application must register a BroadcastReceiver for the ACTION_FOUND Intent in order to receive information about each device discovered.
//For each device, the system will broadcast the ACTION_FOUND Intent. This Intent carries the extra fields EXTRA_DEVICE and EXTRA_CLASS,
//containing a BluetoothDevice and a BluetoothClass, respectively
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
/* get the search results */
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//add it on List<BluetoothDevice>
devices.add(device);
//show found LxDevice on list
showDevices();
}
}
};
private BroadcastReceiver discoveryReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent)
{
/* unRegister Receiver */
Log.d("debug", ">>unregisterReceiver");
unregisterReceiver(mBroadcastReceiver);
unregisterReceiver(this);
discoveryFinished = true;
}
};
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
/* BT isEnable */
if (!mBluetoothAdapter.isEnabled())
{
Log.w("debug", ">>BT is disable!");
finish();
return;
}
/* Register Receiver*/
IntentFilter discoveryFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(discoveryReceiver, discoveryFilter);
IntentFilter foundFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mBroadcastReceiver, foundFilter);
/* show a dialog "Scanning..." */
SamplesUtils.indeterminate(SearchActivity.this, handler, "Scanning for LX devices..", discoveryWorker, new OnDismissListener() {
public void onDismiss(DialogInterface dialog)
{
for (; mBluetoothAdapter.isDiscovering();) {
// Discovery is resource intensive. Make sure it isn't going on when you attempt to connect and pass your message.
mBluetoothAdapter.cancelDiscovery();
}
discoveryFinished = true;
}
}, true);
}
/* Show devices list */
private void showDevices()
{
//Create a list of strings
List<String> list = new ArrayList<String>();
for (int i = 0, size = devices.size(); i < size; ++i) {
StringBuilder b = new StringBuilder();
BluetoothDevice d = devices.get(i);
b.append(d.getName());
b.append('\n');
b.append(d.getAddress());
String s = b.toString();
list.add(s);
}
Log.d("debug", ">>showDevices");
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
handler.post(new Runnable() {
public void run()
{
setListAdapter(adapter);
}
});
}
/* Select device */
protected void onListItemClick(ListView l, View v, int position, long id) {
Log.d("debug", ">>Click device");
Intent result = new Intent();
result.putExtra(BluetoothDevice.EXTRA_DEVICE, devices.get(position));
setResult(RESULT_OK, result);
finish();
}
}
In MainActivity I am doing:
// Initialize the BluetoothChatService to perform bluetooth connections
mBluetoothService = new BluetoothService(this);
Constructor in BluetoothService is:
public BluetoothService(Context context) {
}
connect method:
protected void connect(BluetoothDevice device) {
try {
//Create a Socket connection: need the server's UUID number of registered
BluetoothSocket socket = null;
socket = device.createRfcommSocketToServiceRecord(MY_UUID);
socket.connect();
//Create temporary input and output stream
InputStreamtmpIn=socket.getInputStream();
OutputStream tmpOut = socket.getOutputStream();
//for use purposes
mmSocket = socket;
mmOutStream = tmpOut;
mmInStream = tmpIn;
tmpOut.write("Device connected..".getBytes());
//start Thread for receiving data over bluetooth
//dataReceiveThread.start();
} catch (IOException e) {
Log.e("Colibri2BB BT", "", e);
}
}
Your BluettoothService class is not a context and to initialise an Intent you need a context.So try creating your class like this:
public class BluettoothService{
Activity activity;
BluettoothService(Activity activity){
this.activity=activity;
}
public void startConnection() {
// Create an intent for SearchActivity
Intent intent = new Intent(activity, SearchActivity.class);
//start SearchActivity through intent and expect for result.
//The result is based on result code, which is REQUEST_DISCOVERY
activity.startActivityForResult(intent, REQUEST_DISCOVERY);
}
}
And you can create the BluettoothService class this way from any activity:
BluettoothService bluetooth=new BluettoothService(this);
Edit:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode != REQUEST_DISCOVERY) {
Log.d("Debug", ">>intent REQUEST_DISCOVERY failed!");
return;
}
if (resultCode != Activity.RESULT_OK) {
Log.d("Debug", ">>intent RESULT_OK failed!");
return;
}
Log.d("Debug", ">>onActivityResult!");
final BluetoothDevice device = data.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.d(device.getName(), "Name of Selected Bluetoothdevice");
new Thread () {
public void run() {
//call connect function with device argument
connect(device);
};
}.start();
}
You can't use this of a Service to start an ActivityForResult
You should to specify the #override for the onActivityResult().
Your code should to be put into a class who extends 'activity' (android.app.Activity).
It's for that you have also this :
Next error is in onActivityResult method: *RESULT_OK cannot be resolved to a variable*
This cannot be resolved because your class don't extends 'Activity'
//create this class that hold application context.
public class Application_Manager extends Application {
private static Context context;
public void onCreate() {
super.onCreate();
Application_Manager.context = getApplicationContext();
}
public static Context getAppContext() {
return Application_Manager.context;
}
}
//use this class getAppcontext() to get context in non-activity class.
public class BluettoothService{
static Context context=Application_Manager.getAppContext();
public void startConnection() {
Intent intent = new Intent(context, SearchActivity.class);
context.startActivityForResult(intent, REQUEST_DISCOVERY);//change edited
}
}

Categories

Resources