I get the error "Bluetooth is not available when running my latest app from either eclipse or the device itself. The device(s) in question are two Galaxy Nexus Phones (Both CDMA/LTE) with bluetooth enabled. One is running AOKP with Android 4.2.1, the other is stock, with Android 4.1.1.
I had believed that my unique solution to others problems with bluetooth which required a hardcoded mac address to possibly be the issue as these devices have no external storage, but rather mount internal storage to /mnt/sdcard. However, I tried hardcoding the mac address instead of reading it from a file, to no avail. The first link is my MainActivity, the second link is my Manifest.xml (I have proper persmissions for bluetooth and for storage (r/w). I will enclose logcat in a third link.
Main Activity:
package com.hyperspacecg.thermolitics;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
public static final String PREFS_NAME = "ThermolyticsPrefsFile";
String Packet = null;
private BluetoothAdapter mBluetoothAdapter = null;
private BluetoothSocket btSocket = null;
private OutputStream outStream = null;
SeekBar seekbarTemp, seekbarBlower;
TextView valueTemp, valueBlower;
RadioGroup ventilation;
private static int Blower = 0;
private static int Vent = 255;
private static int Temp = 0;
private static int Defrost = 0;
private static int AC = 0;
private static final UUID MY_UUID = UUID
.fromString("00001101-0000-1000-8000-00805F9B34FB");
// hardcoded MAC address
private static String address = null;
private BufferedReader buf;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
valueTemp = (TextView) findViewById(R.id.textViewTemp);
seekbarTemp = (SeekBar) findViewById(R.id.seekBarTemp);
valueBlower = (TextView) findViewById(R.id.textViewBlower);
seekbarBlower = (SeekBar) findViewById(R.id.seekBarBlower);
ventilation = (RadioGroup) findViewById(R.id.radioGroup1);
BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
Toast.makeText(this, "Bluetooth is not available.",
Toast.LENGTH_LONG).show();
finish();
return;
}
if (!mBluetoothAdapter.isEnabled()) {
Toast.makeText(this,
"Please enable your BT and re-run this program.",
Toast.LENGTH_LONG).show();
finish();
return;
}
int sbar_blower_position = settings.getInt("seekbar_blower_pref", 0);
seekbarBlower.setProgress(sbar_blower_position);
valueBlower.setText("Blower Power: " + sbar_blower_position);
Blower = sbar_blower_position;
int sbar_temp_position = settings.getInt("seekbar_temp_pref", 0);
seekbarTemp.setProgress(sbar_temp_position);
valueTemp.setText("Temperature: " + sbar_temp_position);
Temp = sbar_temp_position;
Vent = ventilation.indexOfChild(findViewById(ventilation.getCheckedRadioButtonId()));
seekbarBlower.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
valueBlower.setText("Blower Power: " + progress);
Blower = progress;
packet(Blower, Temp, Vent, AC, Defrost);
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
seekbarTemp.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
valueTemp.setText("Temperature:" + progress);
Temp = progress;
packet(Blower, Temp, Vent, AC, Defrost);
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
}
#Override
public void onStart() {
super.onStart();
}
#Override
public void onResume() {
super.onResume();
try{
File f = new File(Environment.getExternalStorageDirectory()+"/therm.txt");
FileInputStream fileIS = new FileInputStream(f);
buf = new BufferedReader(new InputStreamReader(fileIS));
String readString = new String();
//just reading each line and pass it on the debugger
while((readString = buf.readLine())!= null){
address = readString;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
// When this returns, it will 'know' about the server,
// via it's MAC address.
address = "00:16:68:2B:40:90";
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
try {
btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
}
mBluetoothAdapter.cancelDiscovery();
try {
btSocket.connect();
} catch (IOException e) {
try {
btSocket.close();
} catch (IOException e2) {
}
}
// Create a data stream so we can talk to server.
try {
outStream = btSocket.getOutputStream();
} catch (IOException e) {
}
packet(Blower, Temp, Vent, AC, Defrost);
byte[] msgBuffer = Packet.getBytes();
try {
outStream.write(msgBuffer);
Toast.makeText(this, "BT successfully connected.",
Toast.LENGTH_LONG).show();
} catch (IOException e) {
}
}
public void onClickDefrost(View v) throws IOException
{
if(Defrost == 1)
{
Defrost = 0;
}
else
{
Defrost = 1;
}
packet(Blower, Temp, Vent, AC, Defrost);
byte[] msgBuffer = Packet.getBytes();
try
{
outStream.write(msgBuffer);
}
catch (IOException e)
{
}
}
public void onClickAC(View v) throws IOException
{
if(AC == 1)
{
AC = 0;
}
else
{
AC = 1;
}
packet(Blower, Temp, Vent, AC, Defrost);
byte[] msgBuffer = Packet.getBytes();
try
{
outStream.write(msgBuffer);
}
catch (IOException e)
{
}
}
#Override
public void onPause() {
super.onPause();
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("seekbar_blower_pref", Blower);
editor.putInt("seekbar_temp_pref", Temp);
editor.commit();
if (outStream != null) {
try {
outStream.flush();
} catch (IOException e) {
}
}
try {
btSocket.close();
} catch (IOException e2) {
}
}
#Override
public void onStop() {
super.onStop();
}
#Override
public void onDestroy() {
super.onDestroy();
// Stop the Bluetooth chat services
}
public String packet(int Blower, int Temp, int Vent, int AC,int Defrost) {
Blower = Blower * 4;
Temp = Temp * 4;
Packet = "<HMG:" + Blower + ":" + Temp + ":" + Vent + ":" + AC + ":" + Defrost + ">";
return Packet;
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hyperspacecg.thermolitics"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.hyperspacecg.thermolitics.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Logcat:
02-27 21:59:54.572: D/dalvikvm(8472): Late-enabling CheckJNI
02-27 21:59:54.689: E/Trace(8472): error opening trace file: No such file or directory (2)
02-27 21:59:55.041: D/dalvikvm(8472): GC_CONCURRENT freed 81K, 10% free 2696K/2964K, paused 2ms+6ms, total 115ms
02-27 21:59:55.236: D/dalvikvm(8472): GC_CONCURRENT freed 18K, 7% free 3078K/3308K, paused 12ms+12ms, total 69ms
Related
I used the following code in my app.
package com.kalusudu.dp.smarthike;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.PowerManager;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class RecentStories extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recent_stories);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
TextView text1 = (TextView) findViewById(R.id.TextView01);
text1.setText("Knuckles mountain range is a part of the Hill Country of Sri Lanka which is also about 3000 Ft or 915 Mts. from sea level and covers an area of about 90 Sq. Milles or 234 Sq. Km of land extent.");
TextView text2 = (TextView) findViewById(R.id.TextView02);
text2.setText("Knuckles mountain range is a part of the Hill Country of Sri Lanka which is also about 3000 Ft or 915 Mts. from sea level and covers an area of about 90 Sq. Milles or 234 Sq. Km of land extent.");
ImageView imageviewOne = (ImageView) findViewById(R.id.imageView01);
imageviewOne.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(RecentStories.this, Cloud.class);
//intent.setAction(Intent.ACTION_VIEW);
//intent.addCategory(Intent.CATEGORY_BROWSABLE);
//intent.setData(Uri.parse("http://m.facebook.com"));
startActivity(intent);
}
});
// execute this when the downloader must be fired
final DownloadTask downloadTask = new DownloadTask(RecentStories.this);
downloadTask.execute("https://maps.awesome.com/sd.f");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_recent_stories, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
// usually, subclasses of AsyncTask are declared inside the activity class.
// that way, you can easily modify the UI thread from here
private class DownloadTask extends AsyncTask<String, Integer, String> {
private Context context;
private PowerManager.WakeLock mWakeLock;
public DownloadTask(Context context) {
this.context = context;
}
#Override
protected String doInBackground(String... sUrl) {
InputStream input = null;
OutputStream output = null;
HttpURLConnection connection = null;
try {
URL url = new URL(sUrl[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
// expect HTTP 200 OK, so we don't mistakenly save error report
// instead of the file
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
return "Server returned HTTP " + connection.getResponseCode()
+ " " + connection.getResponseMessage();
}
// this will be useful to display download percentage
// might be -1: server did not report the length
int fileLength = connection.getContentLength();
// download the file
input = connection.getInputStream();
output = new FileOutputStream((Environment.getExternalStorageDirectory())+"/stories.data");
byte data[] = new byte[4096];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
// allow canceling with back button
if (isCancelled()) {
input.close();
return null;
}
total += count;
// publishing the progress....
if (fileLength > 0) // only if total length is known
publishProgress((int) (total * 100 / fileLength));
output.write(data, 0, count);
}
} catch (Exception e) {
return e.toString();
} finally {
try {
if (output != null)
output.close();
if (input != null)
input.close();
} catch (IOException ignored) {
}
if (connection != null)
connection.disconnect();
}
return null;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
// take CPU lock to prevent CPU from going off if the user
// presses the power button during download
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
getClass().getName());
mWakeLock.acquire();
}
#Override
protected void onProgressUpdate(Integer... progress) {
}
#Override
protected void onPostExecute(String result) {
mWakeLock.release();
if (result != null)
Toast.makeText(context,"Download error: "+result, Toast.LENGTH_LONG).show();
else
Toast.makeText(context,"File downloaded", Toast.LENGTH_SHORT).show();
}
}
The app stops as soon as it starts. What is the reason for this?
When I comment the following line the app runs.
downloadTask.execute("http://ietf.org/rfc/rfc4871.txt");
The url is not a valid one. but the result is same with a real one too.
Permissions :
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
try this:
DownloadTask downloadTask = new DownloadTask();
downloadTask.execute("http://ietf.org/rfc/rfc4871.txt", "", "");
private class DownloadTask extends AsyncTask<String, Integer, String> {
#Override
protected void onPreExecute() {
super.onPreExecute();
//visible progress bar
}
#Override
protected String doInBackground(String... sUrl) {
InputStream input = null;
OutputStream output = null;
HttpURLConnection connection = null;
try {
URL url = new URL(sUrl[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
// expect HTTP 200 OK, so we don't mistakenly save error report
// instead of the file
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
return "Server returned HTTP " + connection.getResponseCode()
+ " " + connection.getResponseMessage();
}
// this will be useful to display download percentage
// might be -1: server did not report the length
int fileLength = connection.getContentLength();
// download the file
input = connection.getInputStream();
File sourcePath = Environment.getExternalStorageDirectory();
File path = new File(sourcePath + "/" + "stories.data" + "/");
path.mkdir();
File file = new File(path, "/" + "rfc4871.txt");
if (file.exists()) {
file.delete();
}
output = new FileOutputStream(file);
byte data[] = new byte[4096];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
// allow canceling with back button
if (isCancelled()) {
input.close();
return null;
}
total += count;
// publishing the progress....
if (fileLength > 0) // only if total length is known
publishProgress((int) (total * 100 / fileLength));
output.write(data, 0, count);
}
} catch (Exception e) {
return e.toString();
} finally {
try {
if (output != null)
output.close();
if (input != null)
input.close();
} catch (IOException ignored) {
}
if (connection != null)
connection.disconnect();
}
return null;
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
#Override
protected void onProgressUpdate(final Integer... values) {
super.onProgressUpdate(values);
// Log.e("progressing", "" + values[0]);
// edt_search.setText("" + values[0]);
// new Thread(new Runnable() {
// public void run() {
progressBar.setProgress(values[0]);
// tv_downloading.setText("" + values[0] + "%");
// }
// }).start();
}
#Override
protected void onCancelled(String s) {
super.onCancelled(s);
}
#Override
protected void onCancelled() {
super.onCancelled();
}
}
and also add this pernissions
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I'm developing SIP client for Android and a have a problem. The problem is that I don't have audio while two clients are connected. The server that I'm using is www.minisipserver.com. The method onCallEstablished from the SipAudioCall.Listener is called every time.
This is the class which extends BroadcastReceiver, which is listening for the call.
`
package uk.co.tbw.sip.calling;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.sip.SipAudioCall;
import android.net.sip.SipException;
import android.net.sip.SipProfile;
import uk.co.tbw.utils.TBWLogger;
public class IncomingCallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
SipAudioCall incomingCall = null;
try {
Call callActivity = (Call) context;
SipAudioCall.Listener listener = new myListener(context);
incomingCall = callActivity.mManager.takeAudioCall(intent, listener);
incomingCall.startAudio();
callActivity.mAudioCall = incomingCall;
callActivity.updateStatus(incomingCall);
} catch (SipException e) {
if (incomingCall != null) {
incomingCall.close();
e.printStackTrace();
}
}
}
class myListener extends SipAudioCall.Listener {
private Context context;
public myListener(Context context) {
this.context = context;
}
#Override
public void onRinging(SipAudioCall call, SipProfile caller) {
try {
call.answerCall(30);
} catch (SipException e) {
e.printStackTrace();
}
}
#Override
public void onReadyToCall(SipAudioCall call) {
TBWLogger.d("IncomingCallReceiver.java onReadyToCall : " + call.toString());
}
#Override
public void onCalling(SipAudioCall call) {
TBWLogger.d("IncomingCallReceiver.java onCalling : " + call.toString());
}
#Override
public void onRingingBack(SipAudioCall call) {
TBWLogger.d("IncomingCallReceiver.java onRingingBack : " + call.toString());
}
#Override
public void onCallEstablished(SipAudioCall call) {
TBWLogger.d("IncomingCallReceiver.java onCallEstablished : " + call.toString());
if (call.isInCall()) {
TBWLogger.d("IncomingCallReceiver.java isInCall : " + call.toString());
}
if (call.isOnHold()) {
TBWLogger.d("IncomingCallReceiver.java isOnHold : " + call.toString());
}
if (call.isMuted()) {
TBWLogger.d("IncomingCallReceiver.java isMuted : " + call.toString());
}
call.startAudio();
}
#Override
public void onCallEnded(SipAudioCall call) {
TBWLogger.d("IncomingCallReceiver.java onCallEnded : " + call.toString());
}
#Override
public void onCallBusy(SipAudioCall call) {
TBWLogger.d("IncomingCallReceiver.java onCallBusy : " + call.toString());
}
#Override
public void onCallHeld(SipAudioCall call) {
TBWLogger.d("IncomingCallReceiver.java onCallHeld : " + call.toString());
}
#Override
public void onError(SipAudioCall call, int errorCode, String errorMessage) {
TBWLogger.d("IncomingCallReceiver.java IncomingCallReceiver.java onError : " + call.toString() + "; errorCode: " + errorCode + "; errorMessage: " + errorMessage);
}
#Override
public void onChanged(SipAudioCall call) {
TBWLogger.d("IncomingCallReceiver.java onReadyToCall : " + call.toString());
}
}
}
`
Call.java Activity
package uk.co.tbw.sip.calling;
import android.app.Dialog;
import android.app.PendingIntent;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.net.sip.SipAudioCall;
import android.net.sip.SipException;
import android.net.sip.SipManager;
import android.net.sip.SipProfile;
import android.net.sip.SipRegistrationListener;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.c77.androidstreamingclient.lib.rtp.RtpMediaDecoder;
import java.text.ParseException;
import uk.co.tbw.R;
import uk.co.tbw.utils.TBWLogger;
public class Call extends AppCompatActivity {
public static final int REQUEST_PERMISSION_CODE = 1567;
public String mSipAddress = null;
public SipManager mManager = null;
public SipProfile mProfile = null;
public SipAudioCall mAudioCall = null;
public IncomingCallReceiver mCallReceiver;
private static final int CALL_ADDRESS = 1;
private static final int SET_AUTH_INFO = 2;
private static final int UPDATE_SETTINGS_DIALOG = 3;
private static final int HANG_UP = 4;
private RtpMediaDecoder rtpMediaDecoder;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_call);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button callBtn = (Button) findViewById(R.id.answer_btn);
Button closeBtn = (Button) findViewById(R.id.close_btn);
SurfaceView view = new SurfaceView(this);
rtpMediaDecoder = new RtpMediaDecoder(view);
rtpMediaDecoder.start();
callBtn.setOnClickListener(v -> {
EditText textField = (EditText) findViewById(R.id.userToCallEditText);
mSipAddress = textField.getText().toString();
initiateCall();
});
closeBtn.setOnClickListener(v -> {
if (mAudioCall != null) {
try {
mAudioCall.endCall();
TBWLogger.d("Call.java endCall");
} catch (SipException e) {
Log.d("WalkieTalkieActivity/onOptionsItemSelected",
"Error ending call.", e);
}
mAudioCall.close();
}
});
IntentFilter filter = new IntentFilter();
filter.addAction("android.SipDemo.INCOMING_CALL");
mCallReceiver = new IncomingCallReceiver();
this.registerReceiver(mCallReceiver, filter);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
private void showDialogOK(String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", okListener)
.create()
.show();
}
#Override
protected void onStart() {
super.onStart();
initializeManager();
}
#Override
protected void onDestroy() {
super.onDestroy();
if (mAudioCall != null) {
mAudioCall.close();
}
closeLocalProfile();
if (mCallReceiver != null) {
this.unregisterReceiver(mCallReceiver);
}
rtpMediaDecoder.release();
}
#Override
protected void onStop() {
rtpMediaDecoder.release();
super.onStop();
}
private void closeLocalProfile() {
if (mManager == null) {
return;
}
try {
if (mProfile != null) {
mManager.close(mProfile.getUriString());
}
} catch (SipException e) {
Log.d("CallActivity/onDestroy", "Failed to close local profile.", e);
}
}
private void initializeManager() {
TBWLogger.d("initializeManager()");
if (mManager == null) {
mManager = SipManager.newInstance(this);
}
initializeLocalProfile();
}
private void initializeLocalProfile() {
TBWLogger.d("initializeLocalProfile");
if (mManager == null) {
return;
}
if (mProfile != null) {
closeLocalProfile();
}
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String username = preferences.getString("namePref", "");
String domain = preferences.getString("domainPref", "");
String password = preferences.getString("passPref", "");
if (username.length() == 0 || domain.length() == 0 || password.length() == 0) { //|| password.length() == 0
showDialog(UPDATE_SETTINGS_DIALOG);
return;
}
try {
TBWLogger.d("Building new sipProfile");
SipProfile.Builder builder = new SipProfile.Builder(username, domain);
builder.setPassword(password);
mProfile = builder.build();
Intent intent = new Intent();
intent.setAction("android.SipDemo.INCOMING_CALL");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, Intent.FILL_IN_DATA);
mManager.open(mProfile, pendingIntent, new SipRegistrationListener() {
#Override
public void onRegistering(String localProfileUri) {
TBWLogger.d("onRegistering");
updateStatus("Registering with SIP Server...");
}
#Override
public void onRegistrationDone(String localProfileUri, long expiryTime) {
TBWLogger.d("onRegistrationDone");
updateStatus("Ready");
}
#Override
public void onRegistrationFailed(String localProfileUri, int errorCode, String errorMessage) {
TBWLogger.d("onRegistrationFailed:" + " errorMessage:" + errorMessage + " errorCode:" + errorCode
+ " localProfile:" + localProfileUri);
updateStatus("Registration failed. Please check settings.");
}
});
} catch (ParseException e) {
updateStatus("Connection Error.");
e.printStackTrace();
} catch (SipException e) {
updateStatus("Connection Error.");
e.printStackTrace();
}
}
public void initiateCall() {
updateStatus(mSipAddress);
try {
SipAudioCall.Listener listener = new SipAudioCall.Listener() {
#Override
public void onReadyToCall(SipAudioCall call) {
TBWLogger.d("Call.java onReadyToCall: " + call);
}
#Override
public void onRingingBack(SipAudioCall call) {
TBWLogger.d("Call.java onRingingBack: " + call);
}
#Override
public void onCallBusy(SipAudioCall call) {
TBWLogger.d("Call.java onCallBusy: " + call);
}
#Override
public void onCallHeld(SipAudioCall call) {
TBWLogger.d("Call.java onCallHeld: " + call);
}
#Override
public void onChanged(SipAudioCall call) {
TBWLogger.d("Call.java onChanged: " + call);
}
#Override
public void onCallEstablished(SipAudioCall call) {
// AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
// audio.setMode(AudioManager.MODE_IN_COMMUNICATION);
// AudioGroup audioGroup = new AudioGroup();
// audioGroup.setMode(AudioGroup.MODE_NORMAL);
// AudioStream audioStream = null;
// try {
// audioStream = new AudioStream(InetAddress.getByAddress(IncomingCallReceiver.getLocalIPAddress()));
// } catch (SocketException e) {
// e.printStackTrace();
// } catch (UnknownHostException e) {
// e.printStackTrace();
// }
// audioStream.setCodec(AudioCodec.PCMU);
// audioStream.setMode(RtpStream.MODE_NORMAL);
// //set receiver(vlc player) machine ip address(please update with your machine ip)
// try {
// audioStream.associate(InetAddress.getByAddress(new byte[] {(byte)192, (byte)168, (byte)1, (byte)19 }), 22222);
// } catch (UnknownHostException e) {
// e.printStackTrace();
// }
// audioStream.join(audioGroup);
//call.setSpeakerMode(true);
call.startAudio();
// if (call.isMuted()) {
// call.toggleMute();
// }
updateStatus(call);
mAudioCall.startAudio();
//mAudioCall.setSpeakerMode(true);
TBWLogger.d("Call.java onCallEstablished(): ");
}
#Override
public void onRinging(SipAudioCall call, SipProfile caller) {
Toast.makeText(Call.this, "Calling", Toast.LENGTH_LONG).show();
try {
call.answerCall(30);
} catch (SipException e) {
e.printStackTrace();
}
TBWLogger.d("Call.java onRinging(): ");
}
#Override
public void onCalling(SipAudioCall call) {
Toast.makeText(Call.this, "Calling", Toast.LENGTH_LONG).show();
TBWLogger.d("Call.java onRinging(): ");
}
#Override
public void onCallEnded(SipAudioCall call) {
updateStatus("Ready");
TBWLogger.d("Call.java onCallEnded(): ");
}
#Override
public void onError(SipAudioCall call, int errorCode, String errorMessage) {
TBWLogger.d("Call.java onError: errorCode: " + errorCode + "; errorMessage: " + errorMessage);
updateStatus("Call.java onError: errorCode: " + errorCode + "; errorMessage: " + errorMessage);
}
};
mAudioCall = mManager.makeAudioCall(mProfile.getUriString(), mSipAddress, listener, 30);
} catch (SipException e) {
Log.i("InitiateCall", "Error when trying to close manager", e);
if (mProfile != null) {
try {
mManager.close(mProfile.getUriString());
} catch (SipException e1) {
Log.i("InitiateCall", "Error when trying to close manager", e1);
e1.printStackTrace();
}
}
if (mAudioCall != null) {
mAudioCall.close();
}
}
}
public void updateStatus(String s) {
this.runOnUiThread(() -> {
TextView label = (TextView) findViewById(R.id.label);
label.setText(s);
});
}
public void updateStatus(SipAudioCall s) {
String userName = s.getPeerProfile().getDisplayName();
if (userName == null) {
userName = s.getPeerProfile().getUserName();
}
updateStatus(userName + "#" + s.getPeerProfile().getSipDomain());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, SET_AUTH_INFO, 0, "Edit your SIP Info");
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case SET_AUTH_INFO:
updatePreferences();
break;
}
return true;
}
//TODO: check the break statements
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case CALL_ADDRESS:
LayoutInflater factory = LayoutInflater.from(this);
final View textBoxView = factory.inflate(R.layout.call_address_dialog, null);
return new AlertDialog.Builder(this)
.setTitle("Call Someone")
.setView(textBoxView)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
EditText textField = (EditText) (textBoxView.findViewById(R.id.call_address_edit));
mSipAddress = textField.getText().toString();
initiateCall();
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
}).create();
case UPDATE_SETTINGS_DIALOG:
return new AlertDialog.Builder(this)
.setMessage("Please update your SIP Account Settings.")
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
updatePreferences();
}
})
.setNegativeButton(
android.R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Noop.
}
})
.create();
}
return null;
}
private void updatePreferences() {
Intent settingsActivity = new Intent(getBaseContext(), SipSetting.class);
startActivity(settingsActivity);
}
}
`
Manifest`
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.USE_SIP" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CONFIGURE_SIP" />
<uses-permission android:name="android.software.sip" android:required="true" />
<uses-permission android:name="android.software.sip.voip" android:required="true" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CONFIGURE_SIP" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.hardware.wifi" android:required="true"/>
<uses-permission android:name="android.software.sip.voip" android:required="true" />
<uses-permission android:name="android.hardware.microphone" android:required="true"/>
<uses-permission android:name="android.hardware.sip.voip" android:required="true" />
<uses-permission android:name="android.hardware.telephony" android:required="false" />
<application ...
<receiver android:name=".sip.calling.IncomingCallReceiver" android:label="Call Receiver"/>
<activity
android:name=".sip.calling.Call"
android:configChanges="orientation|keyboardHidden"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="uk.co.tbw.MainActivity"/>
</activity>
</application>
`
What library are you using for the SIP signaling?
In some libraries you have to assign the Microphone and the Speaker to the call when it is estabilished.
I have write some code for receiving data from Bluetooth module (HC-05), I want to read string '#900~' from Arduino, but I get nothing in response. I have made a connection with module, but nothing more. I need only reading on Android. It is probably easy task but I cannot get across :/. What am I doing wrong? it Below is my code:
package nichten.pneumobil;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;
public class MainApplication extends AppCompatActivity {
boolean doubleBackToExitPressedOnce = false;
private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
static BluetoothSocket mmSocket;
static BluetoothDevice mmDevice;
static OutputStream mmOutputStream;
static InputStream mmInputStream;
private static final UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
private String BTdevice = "POJAZD1";
private boolean polaczenie = false;
Handler bluetoothIn;
final int handlerState = 0;
private ConnectedThread mConnectedThread;
private StringBuilder recDataString = new StringBuilder();
private Button btnModuleConnect;
private TextView txtModuleConnect;
private TextView txtTest;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_application);
btnModuleConnect = (Button) findViewById(R.id.btnModuleConnect);
txtModuleConnect = (TextView) findViewById(R.id.txtModuleConnect);
txtTest = (TextView) findViewById(R.id.txtTest);
if (!mBluetoothAdapter.isEnabled()) {
mBluetoothAdapter.enable();
}
btnModuleConnect.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
if (connectWithModule())
txtModuleConnect.setText("Connection status: Connected");
else
txtModuleConnect.setText("Connection status: Module not found");
} catch(IOException e){
e.printStackTrace();
}
}
});
bluetoothIn = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.what == handlerState) {
String readMessage = (String) msg.obj;
recDataString.append(readMessage);
int endOfLineIndex = recDataString.indexOf("~");
if (endOfLineIndex > 0) {
String dataInPrint = recDataString.substring(0, endOfLineIndex);
int dataLength = dataInPrint.length();
if (recDataString.charAt(0) == '#')
{
txtTest.setText(recDataString);
}
recDataString.delete(0, recDataString.length());
dataInPrint = " ";
}
}
}
};
}
#Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
disconnectWithModule();
mBluetoothAdapter.disable();
finish();
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
doubleBackToExitPressedOnce=false;
}
}, 2000);
}
public boolean connectWithModule() throws IOException {
if(polaczenie){
try {
mmSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
mmDevice = null;
mmSocket = null;
mBluetoothAdapter = null;
}
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
if(device.getName().equals(BTdevice)){
mmDevice = device;
break;
}
}
}
if (mmDevice != null) {
try {
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
mmSocket.connect();
Toast.makeText(getApplicationContext(), "Połączono!", Toast.LENGTH_SHORT).show();
polaczenie = true;
mmOutputStream = mmSocket.getOutputStream();
mmInputStream = mmSocket.getInputStream();
return true;
} catch (Exception e2) {
Toast.makeText(getApplicationContext(),"Nie udało się połaczyć.", Toast.LENGTH_SHORT).show();
mmDevice = null;
try {
mmSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
mmSocket = null;
}
}
else if (mmDevice == null){
Toast.makeText(getApplicationContext(), "Nie ma sparowanych urządzeń.", Toast.LENGTH_SHORT).show();
polaczenie = false;
}
return false;
}
public void disconnectWithModule(){
if (polaczenie) {
try {
mmSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private class ConnectedThread extends Thread{
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
InputStream tmpIn = null;
OutputStream tmpOut = null;
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) { }
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
byte[] buffer = new byte[256];
int bytes;
while (true) {
try {
bytes = mmInStream.read(buffer);
String readMessage = new String(buffer, 0, bytes);
bluetoothIn.obtainMessage(handlerState, bytes, -1, readMessage).sendToTarget();
} catch (IOException e) {
break;
}
}
}
//write method
public void write(String input) {
byte[] msgBuffer = input.getBytes();
try {
mmOutStream.write(msgBuffer);
} catch (IOException e) {
Toast.makeText(getBaseContext(), "Connection Failure", Toast.LENGTH_LONG).show();
finish();
}
}
}
}
I am a beginner in Android (only 2 days with it :P), but I have no idea what to do. I also include my AndroidManifest, my other Activity, maybe it would help to find out what is wrong:
package nichten.pneumobil;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnNextScreen = (Button) findViewById(R.id.btnNextScreen);
btnNextScreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent nextScreen = new Intent(getApplicationContext(), MainApplication.class);
startActivity(nextScreen);
}
});
}
}
And manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="nichten.pneumobil">
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainApplication"
android:screenOrientation="landscape">
</activity>
</application>
</manifest>
I am using csipsimple code and customised it to show brandname by getting json value from webservice . I am using SharedPreferences to store value .
Once the application is force closed , or device restart SharedPreferences are lost . I am using commit and clear but still the values are null .
BasePrefsWizard is the class responsible for pulling web data and DialerFragment is the other class i am calling the BrandName ( it is SavedBrand/ brand in code)
package com.mydial.wizards;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.ContentUris;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.preference.EditTextPreference;
import android.util.Base64;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.mydial.api.SipManager;
import com.mydial.api.SipProfile;
import com.mydial.db.DBProvider;
import com.mydial.models.Filter;
import com.mydial.ui.SipHome;
import com.mydial.ui.dialpad.DialerFragment;
import com.mydial.ui.filters.AccountFilters;
import com.mydial.ui.prefs.GenericPrefs;
import com.mydial.utils.AccountListUtils;
import com.mydial.utils.Log;
import com.mydial.utils.PreferencesProviderWrapper;
import com.mydial.utils.PreferencesWrapper;
import com.mydial.utils.AccountListUtils.AccountStatusDisplay;
import com.mydial.utils.animation.ActivitySwitcher;
import com.mydial.wizards.WizardUtils.WizardInfo;
import com.mydial.wizards.impl.Advanced;
import com.worldfone.R;
#SuppressLint("NewApi") public class BasePrefsWizard extends GenericPrefs {
public static final int SAVE_MENU = Menu.FIRST + 1;
public static final int TRANSFORM_MENU = Menu.FIRST + 2;
public static final int FILTERS_MENU = Menu.FIRST + 3;
public static final int DELETE_MENU = Menu.FIRST + 4;
private PreferencesProviderWrapper prefProviderWrapper;
private static final String THIS_FILE = "Base Prefs wizard";
protected SipProfile account = null;
private Button saveButton,cancel;
private String wizardId = "";
private WizardIface wizard = null;
private BroadcastReceiver mReceiver;
IntentFilter intentFilter;
public static final String myData = "mySharedPreferences";
public static String bal = null;
public static String sip = null;
public static String header = null;
public static String date = null;
public static String savedBal="";
public static String savedSip="";
public static String savedBrand="";
public static String webArray[] =new String[6];
#Override
protected void onCreate(Bundle savedInstanceState)
{
// Get back the concerned account and if any set the current (if not a
// new account is created)
Intent intent = getIntent();
long accountId = 1;
//intent.getLongExtra(SipProfile.FIELD_ID, SipProfile.INVALID_ID);
// TODO : ensure this is not null...
// setWizardId(intent.getStringExtra(SipProfile.FIELD_WIZARD));
setWizardId();
account = SipProfile.getProfileFromDbId(this, accountId, DBProvider.ACCOUNT_FULL_PROJECTION);
super.onCreate(savedInstanceState);
prefProviderWrapper = new PreferencesProviderWrapper(this);
// Bind buttons to their actions
cancel = (Button) findViewById(R.id.cancel_bt);
//cancel.setEnabled(false);
cancel.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
isOnline();
//saveAndFinish();
}
});
saveButton = (Button) findViewById(R.id.save_bt);
//saveButton.setEnabled(false);
saveButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
//setResult(RESULT_CANCELED, getIntent());
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
finish();
System.exit(0);
}
});
wizard.fillLayout(account);
loadValue();
}
public void isOnline()
{
ConnectivityManager connMgr = (ConnectivityManager) this
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected())
{
try
{
String webAccessNumberLink="http://myweblink.info/test/code.php?user=1234";
new webAccessNumber().execute(webAccessNumberLink);
}
catch(Exception e)
{
// System.out.println("exception in basepreference "+e);
}
}
else
{
// display error
showNetworkAlert();
}
}
void showNetworkAlert()
{
new AlertDialog.Builder(this)
.setTitle("Alert")
.setMessage(
"Please make sure you have Network Enabled")
.setNeutralButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
/*Intent siphome = new Intent(getApplicationContext(),SipHome.class);
startActivity(siphome);*/
}
}).show();
}
public void saveArray(String[] arrayOfweb)
{
int mode = Context.MODE_PRIVATE;
SharedPreferences mySharedPreferences =this.getSharedPreferences(myData,
mode);
SharedPreferences.Editor editor = mySharedPreferences.edit();
String f1 = arrayOfweb[0];
String f2 = arrayOfweb[1];
String f3 = arrayOfweb[2];
String f4 = arrayOfweb[3];
for(int i=0;i<arrayOfweb.length;i++)
{
}
//byMe added below to make preferences persistant
editor.clear();
editor.putString("balance",f1);
editor.putString("sipp",f2);
editor.putString("brand",f3);
editor.putString("prfx",f4);
editor.commit();
loadValue();
}
private void loadValue()
{
int mode = Context.MODE_PRIVATE;
SharedPreferences mySharedPreferences = this.getSharedPreferences(myData,
mode);
savedBal= mySharedPreferences.getString("balance", "");
savedSip = mySharedPreferences.getString("sipp", "");
savedBrand = mySharedPreferences.getString("barnd", "");
}
private boolean isResumed = false;
#Override
protected void onResume()
{
super.onResume();
isResumed = true;
updateDescriptions();
updateValidation();
//byMe
}
#Override
protected void onPause()
{
super.onPause();
isResumed = false;
//this.unregisterReceiver(this.mReceiver);
}
private boolean setWizardId()
{
try {
wizard=Advanced.class.newInstance();
// wizard = (WizardIface) wizardInfo.classObject.newInstance();
} catch (IllegalAccessException e) {
Log.e(THIS_FILE, "Can't access wizard class", e);
/*if (!wizardId.equals(WizardUtils.EXPERT_WIZARD_TAG)) {
return setWizardId(WizardUtils.EXPERT_WIZARD_TAG);
}*/
return false;
} catch (InstantiationException e) {
Log.e(THIS_FILE, "Can't access wizard class", e);
/* if (!wizardId.equals(WizardUtils.EXPERT_WIZARD_TAG)) {
return setWizardId(WizardUtils.EXPERT_WIZARD_TAG);
}
*/ return false;
}
//wizardId = wId;
wizard.setParent(this);
if(getSupportActionBar() != null) {
getSupportActionBar().setIcon(WizardUtils.getWizardIconRes(wizardId));
}
return true;
}
private boolean setWizardId(String wId) {
if (wizardId == null) {
return setWizardId(WizardUtils.EXPERT_WIZARD_TAG);
}
WizardInfo wizardInfo = WizardUtils.getWizardClass(wId);
if (wizardInfo == null) {
if (!wizardId.equals(WizardUtils.EXPERT_WIZARD_TAG)) {
return setWizardId(WizardUtils.EXPERT_WIZARD_TAG);
}
return false;
}
try {
wizard = (WizardIface) wizardInfo.classObject.newInstance();
} catch (IllegalAccessException e) {
Log.e(THIS_FILE, "Can't access wizard class", e);
if (!wizardId.equals(WizardUtils.EXPERT_WIZARD_TAG)) {
return setWizardId(WizardUtils.EXPERT_WIZARD_TAG);
}
return false;
} catch (InstantiationException e) {
Log.e(THIS_FILE, "Can't access wizard class", e);
if (!wizardId.equals(WizardUtils.EXPERT_WIZARD_TAG)) {
return setWizardId(WizardUtils.EXPERT_WIZARD_TAG);
}
return false;
}
wizardId = wId;
wizard.setParent(this);
if(getSupportActionBar() != null) {
getSupportActionBar().setIcon(WizardUtils.getWizardIconRes(wizardId));
}
return true;
}
#Override
protected void beforeBuildPrefs() {
// Use our custom wizard view
setContentView(R.layout.wizard_prefs_base);
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if(isResumed) {
updateDescriptions();
updateValidation();
}
}
private void resolveStatus()
{
AccountStatusDisplay accountStatusDisplay = AccountListUtils
.getAccountDisplay(this, 1);
//status.setTextColor(accountStatusDisplay.statusColor);
//status.setText(accountStatusDisplay.statusLabel);
}
/**
* Update validation state of the current activity.
* It will check if wizard can be saved and if so
* will enable button
*/
public void updateValidation()
{
cancel.setEnabled(wizard.canSave());
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
// menu.findItem(SAVE_MENU).setVisible(wizard.canSave());
return super.onPrepareOptionsMenu(menu);
}
private static final int CHOOSE_WIZARD = 0;
private static final int MODIFY_FILTERS = CHOOSE_WIZARD + 1;
private static final int FINAL_ACTIVITY_CODE = MODIFY_FILTERS;
private int currentActivityCode = FINAL_ACTIVITY_CODE;
public int getFreeSubActivityCode()
{
currentActivityCode ++;
return currentActivityCode;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
return super.onOptionsItemSelected(item);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
}
/**
* Save account and end the activity
*/
public void saveAndFinish()
{
//this.registerReceiver(mReceiver, intentFilter);
saveAccount();
Intent intent = getIntent();
setResult(RESULT_OK, intent);
Intent it = new Intent(this,SipHome.class);
startActivity(it);
finish();
}
void updateStatus()
{
AccountStatusDisplay accountStatusDisplay = AccountListUtils
.getAccountDisplay(this, account.id);
}
/*
* Save the account with current wizard id
*/
private void saveAccount() {
saveAccount(wizardId);
}
#Override
protected void onDestroy() {
//byMe
super.onDestroy();
getSharedPreferences(WIZARD_PREF_NAME, MODE_PRIVATE).edit().clear().commit();
saveArray(webArray);
}
/**
* Save the account with given wizard id
* #param wizardId the wizard to use for account entry
*/
private void saveAccount(String wizardId) {
boolean needRestart = false;
PreferencesWrapper prefs = new PreferencesWrapper(getApplicationContext());
account = wizard.buildAccount(account);
account.wizard = wizardId;
if (account.id == SipProfile.INVALID_ID) {
// This account does not exists yet
prefs.startEditing();
wizard.setDefaultParams(prefs);
prefs.endEditing();
Uri uri = getContentResolver().insert(SipProfile.ACCOUNT_URI, account.getDbContentValues());
// After insert, add filters for this wizard
account.id = ContentUris.parseId(uri);
List<Filter> filters = wizard.getDefaultFilters(account);
if (filters != null) {
for (Filter filter : filters) {
// Ensure the correct id if not done by the wizard
filter.account = (int) account.id;
getContentResolver().insert(SipManager.FILTER_URI, filter.getDbContentValues());
}
}
// Check if we have to restart
needRestart = wizard.needRestart();
} else {
// TODO : should not be done there but if not we should add an
// option to re-apply default params
prefs.startEditing();
wizard.setDefaultParams(prefs);
prefs.endEditing();
getContentResolver().update(ContentUris.withAppendedId(SipProfile.ACCOUNT_ID_URI_BASE, account.id), account.getDbContentValues(), null, null);
}
// Mainly if global preferences were changed, we have to restart sip stack
if (needRestart) {
Intent intent = new Intent(SipManager.ACTION_SIP_REQUEST_RESTART);
sendBroadcast(intent);
}
}
#Override
protected int getXmlPreferences() {
return wizard.getBasePreferenceResource();
}
#Override
protected void updateDescriptions() {
wizard.updateDescriptions();
}
#Override
protected String getDefaultFieldSummary(String fieldName) {
return wizard.getDefaultFieldSummary(fieldName);
}
private static final String WIZARD_PREF_NAME = "Wizard";
#Override
public SharedPreferences getSharedPreferences(String name, int mode) {
return super.getSharedPreferences(WIZARD_PREF_NAME, mode);
}
private class webAccessNumber extends AsyncTask<String, Void, String>
{
//String balance;
ProgressDialog progressDialog;
#Override
protected String doInBackground(String... params) {
return getAccessNumber(params[0]);
}
#Override
protected void onPostExecute(String result)
{
if(result!=null)
{
try {
System.out.println("value of webAccessNumber "+result);
byte[] decoded = Base64.decode(result,Base64.DEFAULT);
String decodedStr =new String(decoded, "UTF-8");
//System.out.println(decodedStr);
JSONArray arr = new JSONArray(decodedStr);
//loop through each object
for (int i=0; i<arr.length(); i++)
{
JSONObject jsonObject = arr.getJSONObject(i);
bal = jsonObject.getString("balance");
sip = jsonObject.getString("server");
header = jsonObject.getString("brand");
webArray[1]=bal;
webArray[2]=sip;
webArray[3]=barnd;
saveArray(webArray);
saveAndFinish();
progressDialog.dismiss();
}
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
#Override
protected void onPreExecute()
{
super.onPreExecute();
progressDialog = new ProgressDialog(BasePrefsWizard.this);
progressDialog.setMessage("Loading..............");
progressDialog.setIndeterminate(false);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setCancelable(true);
progressDialog.show();
}
#Override
protected void onProgressUpdate(Void... values) {
}
public String getAccessNumber(String b) {
String balance = "";
String currency = "USD";
try {
balance = DownloadText(b).trim();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return balance;
}
String DownloadText(String URL) {
int BUFFER_SIZE = 2000;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return "";
}
InputStreamReader isr = new InputStreamReader(in);
int charRead;
String str = "";
char[] inputBuffer = new char[BUFFER_SIZE];
try {
while ((charRead = isr.read(inputBuffer)) > 0) {
String readString = String.copyValueOf(inputBuffer, 0,
charRead);
str += readString;
inputBuffer = new char[BUFFER_SIZE];
}
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "";
}
return str;
}
InputStream OpenHttpConnection(String urlString) throws IOException {
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try {
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
} catch (Exception ex) {
throw new IOException("Error connecting");
}
return in;
}
}//end of webAccessNumber asynchronus
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
{
isOnline();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
You should not clear the SharedPreferences in the onDestroy() method, because when you run your activity or your application again, all the saved values and variables in preferences are deleted, ( like you installed your application for the first time).
PS : here is a tutorial about Using SharedPreferences and Storing Data in Android
You are clearing your preferences in your code,
#Override
protected void onDestroy() {
//byMe
super.onDestroy();
//Preferences cleared
getSharedPreferences(WIZARD_PREF_NAME, MODE_PRIVATE).edit().clear().commit();
saveArray(webArray);
}
This is clearing your preferences.
You are clearing the shared prefereence in onDestroy:
#Override
protected void onDestroy() {
//byMe
super.onDestroy();
getSharedPreferences(WIZARD_PREF_NAME, MODE_PRIVATE).edit().clear().commit(); // <-- Remove this
saveArray(webArray);
}
You delete it in:
#Override
protected void onDestroy() {
super.onDestroy();
getSharedPreferences(WIZARD_PREF_NAME, MODE_PRIVATE).edit().clear().commit();
saveArray(webArray);
}
Also see this http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
I am trying to recieve a string via RFCOMM in android
I am a newbie to android and please help me
I can send data
but receiving fails
Here is my code
Please help me
package com.example.btspp;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Scanner;
import java.util.UUID;
import java.util.regex.Pattern;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class Buttons extends Activity {
private BluetoothAdapter btAdaptor;
private BluetoothSocket btSocket = null;
private OutputStream outStream = null;
private InputStream inStream = null;
private static final UUID MY_UUID = UUID
.fromString("00001101-0000-1000-8000-00805F9B34FB");
Thread workerThread;
byte[] readBuffer;
int readBufferPosition;
int counter;
volatile boolean stopWorker;
public String addressToConnect;
public static StringBuilder readStr;
TextView tv;
int aa;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buttons);
tv = (TextView) findViewById(R.id.textView1);
addressToConnect = getIntent().getStringExtra("addressToConnect");
connectToDevice(addressToConnect);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
sendData("H");
// Toast.makeText(getBaseContext(), addressToConnect,
// Toast.LENGTH_SHORT).show();
}
});
}
private void sendData(String message) {
byte[] msgBuffer = message.getBytes();
try {
// final BT bt = new BT();
outStream.write(msgBuffer);
//readData();
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(getApplicationContext(),
"Not Sent BT Data : " + e.getMessage(), Toast.LENGTH_SHORT)
.show();
}
}
/*private void readData(){
String instring = "";
try {
inStream = btSocket.getInputStream();
} catch (Exception e) {
// TODO: handle exception
}
Scanner scan = new Scanner(new InputStreamReader(inStream));
scan.useDelimiter(Pattern.compile("[\\r\\n]+"));
instring = scan.next();
scan = null;
Toast.makeText(getApplicationContext(),
"Got Data : " + instring, Toast.LENGTH_SHORT)
.show();
//return instring;
}*/
void beginListenForData()
{
final Handler handler = new Handler();
final byte delimiter = 10; //This is the ASCII code for a newline character
stopWorker = false;
readBufferPosition = 0;
readBuffer = new byte[1024];
workerThread = new Thread(new Runnable()
{
public void run()
{
while(!Thread.currentThread().isInterrupted() && !stopWorker)
{
try
{
int bytesAvailable = inStream.available();
if(bytesAvailable > 0)
{
byte[] packetBytes = new byte[bytesAvailable];
inStream.read(packetBytes);
for(int i=0;i<bytesAvailable;i++)
{
byte b = packetBytes[i];
if(b == delimiter)
{
byte[] encodedBytes = new byte[readBufferPosition];
System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
final String data = new String(encodedBytes, "US-ASCII");
readBufferPosition = 0;
handler.post(new Runnable()
{
public void run()
{
tv.setText(data);
}
});
}
else
{
readBuffer[readBufferPosition++] = b;
}
}
}
}
catch (IOException ex)
{
stopWorker = true;
}
}
}
});
workerThread.start();
}
private void connectToDevice(String address) {
btAdaptor = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice device = btAdaptor.getRemoteDevice(address);
try {
btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
btSocket.connect();
outStream = btSocket.getOutputStream();
inStream = btSocket.getInputStream();
beginListenForData();
tv.setText("Bluetooth Opened");
//listenForMessages(btSocket, readStr);
// beginListenForData();
} catch (IOException e) {
// errorExit("Fatal Error",
// "In onResume() and socket create failed: " + e.getMessage() +
// ".");
Toast.makeText(getApplicationContext(),
"Not Connected : " + e.getMessage(), Toast.LENGTH_SHORT)
.show();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_buttons, menu);
return true;
}
}
I am a newbie to android and please help me
I can send data
but receiving fails
Here is my code
Please help me
Isn't 13 "\r" the usual delimiter and not 10?