I am using Robolectric to test my Android app. The app uses the AndroidBeaconLibrary.
When i use
Activity activity = Robolectric.setupActivity(MainActivity.class);
as a simple test, I get an error in the onBeaconServiceConnect() method:
#Override
public void onBeaconServiceConnect() {
beaconManager.setRangeNotifier(new RangeNotifier() {
#Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
if (beacons.size() > 0) {
final double distance = beacons.iterator().next().getDistance();
Log.i("X", "The beacon is about " + distance + " meters away.");
if (distance > 1.8 && !mAlertShown) {
mAlertShown = true;
alertOpen();
} else if (distance < 1) {
mAlertShown = false;
Toast.makeText(MainActivity.this, "alert is now reset", Toast.LENGTH_SHORT).show();
}
}
}
});
try {
Identifier i = Identifier.parse("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
Identifier i2 = Identifier.parse("xxxx");
beaconManager.startRangingBeaconsInRegion(new Region("rangingUniqueId", i, i2, null));
} catch (RemoteException e) {
}
}
The method fails after calling beaconManager.startRangingBeaconsInRegion(new Region("rangingUniqueId", i, i2, null)); at this.serviceMessenger.send(msg); in the BeaconManager.class. with a NullPointerException.
When I deploy the app on my phone, everything works just fine.
I can't comment so im going to write it here as answer but this sound like in this.serviceMessenger.send(msg); the value of msg is NULL and that is why you get the exception.
public void startRangingBeaconsInRegion(Region region) throws RemoteException {
Message msg = Message.obtain(null, IBeaconService.MSG_START_RANGING, 0, 0);
StartRMData obj = new StartRMData(new RegionData(region), rangingCallbackAction());
msg.obj = obj;
msg.replyTo = rangingCallback;
if(msg != null){
serviceMessenger.send(msg);
}
}
Related
I am planning to create an Android App that will automate sending a users response to a USSD Menu. At a click of a button, the app will send the initial code, followed by the rest of the menu inputs.
For example, the initial number is *143#, followed by 1, 1, 1, and a user PIN. I'd like to be able to automate that sequence of inputs so that the user won't have to input it on their own.
I know that in Android Oreo, they implemented a USSD Callback using the TelephonyManager, where the Android App can send a USSD Request, and then read the response given.
I am currently exploring that option and this is what I've tried so far. Heavily lifted from this StackOverflow Question.
interface UssdResultNotifiable {
void notifyUssdResult(String request, String returnMessage, int resultCode);
}
public class MainActivity extends Activity implements UssdResultNotifiable {
USSDSessionHandler hdl;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onUssdSend(View view) {
hdl = new USSDSessionHandler(MainActivity.this, MainActivity.this);
hdl.doSession(((EditText) this.findViewById(R.id.ussdText)).getText().toString());
}
#Override
public void notifyUssdResult(final String request, final String returnMessage, final int resultCode) {
this.runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(MainActivity.this, "Request was " + request + "\n response is "
+ returnMessage + "\n result code is " + resultCode, Toast.LENGTH_LONG).show();
Log.e(TAG, "ussd result hit! Response is = " + returnMessage);
hdl.doSession("1");
}
});
}
}
class USSDSessionHandler {
TelephonyManager tm;
private UssdResultNotifiable client;
private Method handleUssdRequest;
private Object iTelephony;
USSDSessionHandler(Context parent, UssdResultNotifiable client) {
this.client = client;
this.tm = (TelephonyManager) parent.getSystemService(Context.TELEPHONY_SERVICE);
try {
this.getUssdRequestMethod();
} catch (Exception ex) {
//log
}
}
private void getUssdRequestMethod() throws ClassNotFoundException, NoSuchMethodException,
InvocationTargetException, IllegalAccessException {
if (tm != null) {
Class telephonyManagerClass = Class.forName(tm.getClass().getName());
if (telephonyManagerClass != null) {
Method getITelephony = telephonyManagerClass.getDeclaredMethod("getITelephony");
getITelephony.setAccessible(true);
this.iTelephony = getITelephony.invoke(tm); // Get the internal ITelephony object
Method[] methodList = iTelephony.getClass().getMethods();
this.handleUssdRequest = null;
for (Method _m : methodList)
if (_m.getName().equals("handleUssdRequest")) {
handleUssdRequest = _m;
break;
}
}
}
}
#android.support.annotation.RequiresApi(api = Build.VERSION_CODES.N)
public void doSession(String ussdRequest) {
try {
if (handleUssdRequest != null) {
handleUssdRequest.setAccessible(true);
handleUssdRequest.invoke(iTelephony, SubscriptionManager.getDefaultSubscriptionId(), ussdRequest, new ResultReceiver(new Handler()) {
#Override
protected void onReceiveResult(int resultCode, Bundle ussdResponse) {
Object p = ussdResponse.getParcelable("USSD_RESPONSE");
if (p != null) {
Method[] methodList = p.getClass().getMethods();
for(Method m : methodList){
Log.e(TAG, "onReceiveResult: " + m.getName());
}
try {
CharSequence returnMessage = (CharSequence) p.getClass().getMethod("getReturnMessage").invoke(p);
CharSequence request = (CharSequence) p.getClass().getMethod("getUssdRequest").invoke(p);
USSDSessionHandler.this.client.notifyUssdResult("" + request, "" + returnMessage, resultCode); //they could be null
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
}
});
}
} catch (IllegalAccessException | InvocationTargetException e1) {
e1.printStackTrace();
}
}
}
What you get is an app that would ask for a USSD Input for the user, and once the "Send" button is hit, the USSD Response is displayed via a Toast on the notifyUssdResult function. In that same function, I send the next input in my sequence which is "1". Am I able to once again send a reply to the USSD, and the USSD takes it as the input and goes to the next menu.
However, as soon as I send the reply, the USSD menu shows up in my device and I'm unable to proceed further. I am unable to navigate the USSD Menu purely through my app, as the USSD Menu interferes with the screen and doesn't go away.
Is there any samples I can follow?
FOR EXAMPLE YOU WANT TO RUN *123# 1 then 1 then 1 then 2
so you can directly write this in string here is the code hope it helps
String s= "*123*1*1*2#"
//screen from which can get the number
if((s.startsWith("*"))&&(s.endsWith("#"))){
//if true then it is a USSD call----
callstring=s.substring(0, s.length()-1);
callstring=callstring+ Uri.encode("#");
Log.d("CALL TYPE---------->", "USSD CALL");
}else{
callstring=s;
Log.d("CALL TYPE---------->", "Not a USSD CALL");
}
Intent i=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+callstring));
startActivity(i);
I want my app track with beacon all the time
it seem work fine been a while when it start tracking
after that, didRangeBeaconsInRegion() has an empty beacons collection
Here my code
public void onCreate() {
super.onCreate();
Log.d(TAG, "create");
beaconManager = BeaconManager.getInstanceForApplication(this);
try {
beaconManager.getBeaconParsers().clear();
beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
beaconManager.setBackgroundScanPeriod(1101l);
beaconManager.setBackgroundBetweenScanPeriod(1101l);
beaconManager.setForegroundBetweenScanPeriod(1101l);
beaconManager.updateScanPeriods();
} catch (RemoteException e) { }
backgroundPowerSaver = new BackgroundPowerSaver(this);
startBeaconRanging();
}
public void startBeaconRanging() {
courseId = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString(getString(R.string.course_id), "");
Log.d(TAG, "startBeaconRanging on courseId = " + courseId);
if (!courseId.equals("")) {
syncBeaconWithFirebase(courseId);
endTime = CourseSQLiteManager.getColumnById(getApplicationContext(), courseId, COURSE_ENDTIME);
regionBootstrap = new RegionBootstrap(this, new Region("ranged region", null, null, null));
}
}
public void stopBeaconRanging() {
if (regionBootstrap != null) {
regionBootstrap.disable();
try {
Log.d(TAG, "end");
courseId = "";
beaconManager.stopRangingBeaconsInRegion(getRegion());
beaconManager.removeAllRangeNotifiers();
} catch (RemoteException e) { }
}
}
#Override
public void didEnterRegion(Region region) {
Log.d(TAG, "enter");
try {
Region r = getRegion();
if(r == null) {
regionBootstrap = new RegionBootstrap(this, new Region("ranged region", null, null, null));
Log.d(TAG, "null");
}
if(r != null) {
beaconManager.startRangingBeaconsInRegion(r);
beaconManager.addRangeNotifier(this);
}
} catch (RemoteException e) { }
}
#Override
public void didExitRegion(Region region) {
try {
if(getRegion() != null) beaconManager.stopRangingBeaconsInRegion(getRegion());
} catch (RemoteException e) { }
}
#Override
public void didDetermineStateForRegion(int i, Region region) {
}
#Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
if(beacons.size() > 0) {
Beacon beacon = beacons.iterator().next();
Log.d(TAG, beacon.getId2().toInt() + " " + beacon.getDistance() + " " + beacon.getRssi());
if(isAfterEndTime()) {
stopBeaconRanging();
}
}
}
EDIT: It happen like this both foreground and background. But If I kill my app and open it again, I can finds beacon but just a moment too.
EDIT2: isAfterEndTime is return false while it cannot finds any beacons. But the didExitRegion is triggered swap with didEnterRegion and it finds zero beacons
PS. I want to track with only one beacon at a time
PS.2 Sorry for my bad English. I'm not sure, are you get what I want to ask clearly or not?
A few things to check:
Make sure this code is not being triggered. I cannot see the definition of the method, but if it returns true, it will cause ranging to stop:
if(isAfterEndTime()) {
stopBeaconRanging();
}
Add a debug line in the didExitRegion to make sure it is not being called. (Since it also stops ranging.) As an aside, I don't think you need code that does this -- there is no efficiency cost to just continuing ranging if you aren't detecting beacons anyway.
Make sure this line of code is only executed once, as the RegionBootstrap is not designed to be created repeatedly:
regionBootstrap = new RegionBootstrap(this, new Region("ranged region", null, null, null));
Rather than repeatedly making a new RegionBootstrap object, just create one once at the beginning of your program. If you then want to change the monitored regions, you can do so with beaconManager.stopMonitoringBeaconsInRegion(region1); and beaconManager.startMonitoringBeaconsInRegion(region2);
I wrote a custom plugin to read blocks of data from an NfcA(i.e.non-ndef) tag. It seems to work fine , but only after the second scan. I am using Activity intent to derive the "NfcAdapter.EXTRA_TAG" to later use it for reading the values. I am also updating the Intents in onNewIntent(). OnNewIntent gets called after the second scan and after that I get result all the time.But in the first scan onNewIntent does not gets called, hence I end up using the Activity tag that does not have "NfcAdapter.EXTRA_TAG", hence I get null. Please see the my code below.
SE_NfcA.java(my native code for plugin)
#Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
String Result = "";
String TypeOfTalking = "";
if (action.contains("TalkToNFC"))
{
JSONObject arg_object = args.getJSONObject(0);
TypeOfTalking = arg_object.getString("type");
if(TypeOfTalking != "")
{
if (TypeOfTalking.contains("readBlock"))
{
if(TypeOfTalking.contains("#"))
{
try
{
String[] parts = TypeOfTalking.split("#");
int index = Integer.parseInt(parts[1]);
Result = Readblock(cordova.getActivity().getIntent(),(byte)index);
callbackContext.success(Result);
}
catch(Exception e)
{
callbackContext.error("Exception Reading "+ TypeOfTalking + "due to "+ e.toString());
return false;
}
}
}
else
{
return false;
}
}
else
{
return false;
}
}
else
{
return false;
}
return true;
}
#Override
public void onNewIntent(Intent intent) {
ShowAlert("onNewIntent called");
Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
super.onNewIntent(intent);
getActivity().setIntent(intent);
savedTag = tagFromIntent;
savedIntent = intent;
}
#Override
public void onPause(boolean multitasking) {
Log.d(TAG, "onPause " + getActivity().getIntent());
super.onPause(multitasking);
if (multitasking) {
// nfc can't run in background
stopNfc();
}
}
#Override
public void onResume(boolean multitasking) {
Log.d(TAG, "onResume " + getActivity().getIntent());
super.onResume(multitasking);
startNfc();
}
public String Readblock(Intent Intent,byte block) throws IOException{
byte[] response = new byte[]{};
if(Intent != null)
{
Tag myTag = Intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
if(savedTag != null)
myTag = savedTag;
if(myTag != null)
{
try{
Reader nTagReader = new Reader(myTag);
nTagReader.close();
nTagReader.connect();
nTagReader.SectorSelect(Sector.Sector0);
response = nTagReader.fast_read(block, block);
nTagReader.close();
return ConvertH(response);
}catch(Exception e){
ShowAlert(e.toString());
}
}
else
ShowAlert("myTag is null.");
}
return null;
}
private void createPendingIntent() {
if (pendingIntent == null) {
Activity activity = getActivity();
Intent intent = new Intent(activity, activity.getClass());
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);
pendingIntent = PendingIntent.getActivity(activity, 0, intent, 0);
}
}
private void startNfc() {
createPendingIntent(); // onResume can call startNfc before execute
getActivity().runOnUiThread(new Runnable() {
public void run() {
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
if (nfcAdapter != null && !getActivity().isFinishing()) {
try {
nfcAdapter.enableForegroundDispatch(getActivity(), getPendingIntent(), getIntentFilters(), getTechLists());
if (p2pMessage != null) {
nfcAdapter.setNdefPushMessage(p2pMessage, getActivity());
}
} catch (IllegalStateException e) {
// issue 110 - user exits app with home button while nfc is initializing
Log.w(TAG, "Illegal State Exception starting NFC. Assuming application is terminating.");
}
}
}
});
}
private void stopNfc() {
Log.d(TAG, "stopNfc");
getActivity().runOnUiThread(new Runnable() {
public void run() {
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
if (nfcAdapter != null) {
try {
nfcAdapter.disableForegroundDispatch(getActivity());
} catch (IllegalStateException e) {
// issue 125 - user exits app with back button while nfc
Log.w(TAG, "Illegal State Exception stopping NFC. Assuming application is terminating.");
}
}
}
});
}
private Activity getActivity() {
return this.cordova.getActivity();
}
private PendingIntent getPendingIntent() {
return pendingIntent;
}
private IntentFilter[] getIntentFilters() {
return intentFilters.toArray(new IntentFilter[intentFilters.size()]);
}
private String[][] getTechLists() {
//noinspection ToArrayCallWithZeroLengthArrayArgument
return techLists.toArray(new String[0][0]);
}
}
My index.js file
nfc.addTagDiscoveredListener(
function(nfcEvent){
console.log(nfcEvent.tag.id);
alert(nfcEvent.tag.id);
window.echo("readBlock#88");//call to plugin
},
function() {
alert("Listening for NFC tags.");
},
function() {
alert("NFC activation failed.");
}
);
SE_NfcA.js(plugin interface for interaction b/w index.js and SE_NfcA.java)
window.echo = function(natureOfTalk)
{
alert("Inside JS Interface, arg =" + natureOfTalk);
cordova.exec(function(result){alert("Result is : "+result);},
function(error){alert("Some Error happened : "+ error);},
"SE_NfcA","TalkToNFC",[{"type": natureOfTalk}]);
};
I guess I have messed up with the Intents/Activity Life-Cycle, please help. TIA!
I found a tweak/hack and made it to work.
Before making any call to read or write, I made one dummy Initialize call.
window.echo("Initialize");
window.echo("readBlock#88");//call to plugin to read.
And in the native code of the plugin, on receiving the "Initialize" token I made a startNFC() call.
else if(TypeOfTalking.equalsIgnoreCase("Initialize"))
{
startNfc();
}
I released an app on the app store that seems to run into problems with the HTC One. As soon as the app is opened, the app execute an intent to make a phone call, ACTION_CALL. I have a call intent but it's accessed after a button press followed by accessing the location services and then a 5 second sleep() call. But with the HTC Sense, it goes straight to the call, without even showing the layouts. I have a Galaxy S3 running 4.3 and the app's target SDK is 19, and the app is working without a hitch.
Why is this happening? Does the HTC Sense have issues with calling intents? Is there anyway of stopping the intent from occuring, sort of like a signalHandler if it's an HTC Sense device?
HTC Sense 5.0, Android: 4.3
Code (Activity that calls phone intent):
protected void onCreate(Bundle paramBundle) {
super.onCreate(paramBundle);
setContentView(R.layout.activity_test);
x=9;
final TextView localTextView = (TextView) findViewById(R.id.addr);
LocationManager localLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
localLocationManager.getLastKnownLocation("gps");
localLocationManager.requestLocationUpdates("gps", 2000L, 10.0F, new LocationListener() {
public void onLocationChanged(Location paramAnonymousLocation) {
double d1 = paramAnonymousLocation.getLatitude();
double d2 = paramAnonymousLocation.getLongitude();
Geocoder localGeocoder = new Geocoder(Test.this.getApplicationContext(), Locale.getDefault());
try {
List localList = localGeocoder.getFromLocation(d1, d2, 1);
if (localList.size() == 1) {
Address localAddress = (Address) localList.get(0);
Object[] arrayOfObject = new Object[3];
if (localAddress.getMaxAddressLineIndex() > 0) ;
for (String str1 = localAddress.getAddressLine(0); ; str1 = "") {
arrayOfObject[0] = str1;
arrayOfObject[1] = localAddress.getAddressLine(1);
arrayOfObject[2] = localAddress.getCountryName();
String str2 = String.format("%s, %s, %s", arrayOfObject);
localTextView.setText(str2);
if(x==9){
Test.this.waititout();}
return;
}
}
} catch (IOException localIOException) {
localIOException.printStackTrace();
return;
} catch (InterruptedException localInterruptedException) {
localInterruptedException.printStackTrace();
}
}
public void onProviderDisabled(String paramAnonymousString) {
localTextView.setText("TURN ON GPS DUMMY");
}
public void onProviderEnabled(String paramAnonymousString) {
}
public void onStatusChanged(String paramAnonymousString, int paramAnonymousInt, Bundle paramAnonymousBundle) {
}
});
}
public void waititout()
throws InterruptedException {
new Thread() {
public void run() {
try {
Date localDate = new Date();
Calendar localCalendar = GregorianCalendar.getInstance();
localCalendar.setTime(localDate);
int i = localCalendar.get(Calendar.HOUR_OF_DAY);
Thread.currentThread();
Thread.sleep(4000L);
Intent localIntent = new Intent("android.intent.action.CALL");
localIntent.setData(Uri.parse("tel:17325450900"));
Test.this.startActivity(localIntent);
return;
} catch (InterruptedException localInterruptedException) {
System.out.println(localInterruptedException);
}
}
}
.start();
}
I have been messing around with the following issue for about 2 months now.
I am building an application on a custom android board which will act as a wifi display (WFD). When I boot the board, I can easily connect from several different phones (LG Optimus G, LG Optimus G pro, Samsung S3 and S4). After connecting and watching the screen being duplicated on the TV screen attached to the custom board, I can then disconnect and if I reconnect within 2 minutes, I can connect again.
If I boot the board (or disconnect) and then wait 2 minutes, the device no longer sees the phones, and the phones sometimes sees the board, and sometimes not. If the phone sees the board, I can initiate a connect, but I cannot connect succesfully.
I have tried all kind of options, but none seem viable.
The board is running on AOSP 4.2.2
I have right now some complicated code that does restart the discoverPeers function every 40 seconds, as normally it stops after 30 seconds. Besides that I disable WFD and re-enable it, with a 4 second delay between it.
I also tried disabling and reenabling p2p and the channel, all with no result.
Below is my code, the receiver is in a separate class.
#Override
protected void onCreate(Bundle savedInstanceState)
{
Settings.System.putInt(getContentResolver(), Settings.System.WIFI_SLEEP_POLICY, Settings.System.WIFI_SLEEP_POLICY_NEVER);
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
setContentView(R.layout.wifi_direct_discovery);
connectPicture = findViewById(R.id.LinearLayout1);
Log.d(TAG, "onCreate");
isConnecting = false;
localWifiP2pWfdInfo = new WifiP2pWfdInfo();
localWifiP2pWfdInfo.setWfdEnabled(true);
localWifiP2pWfdInfo.setDeviceType(1);
localWifiP2pWfdInfo.setSessionAvailable(true);
localWifiP2pWfdInfo.setControlPort(7236);
localWifiP2pWfdInfo.setMaxThroughput(50);
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
wl.acquire();
super.onCreate(savedInstanceState);
}
#Override
public void onPeersAvailable(WifiP2pDeviceList peers)
{
List<WifiP2pDevice> peersList = new ArrayList<WifiP2pDevice>();
final ArrayList<String> list = new ArrayList<String>();
final ListView listview = (ListView) findViewById(R.id.deviceListView);
peersList.clear();
peersList.addAll(peers.getDeviceList());
if (peersList.size() == 0)
{
Log.d(TAG, "No devices found");
list.clear();
if ((pd != null) && pd.isShowing() && (isConnecting == false))
{
pd.dismiss();
Log.d(TAG, "Closed connecting dialog, restarting P2P");
Toast.makeText(this, "Connection failed please try again", Toast.LENGTH_LONG).show();
restartP2p(false);
}
else if (isConnecting == false)
{
restartP2p(false);
}
listview.setAdapter(new ArrayAdapter<String>(this, R.layout.list_black_text, R.id.list_content, list));
return;
}
else
{
int i = 0;
boolean found = false;
for (i = 0; i < peersList.size(); i++)
{
Log.d(TAG, "Found device: " + peersList.get(i).toString());
list.add(peersList.get(i).deviceName);
if ((pd != null) && pd.isShowing())
{
if (peersList.get(i).deviceAddress.equalsIgnoreCase(client.deviceAddress))
{
found = true;
}
}
}
if (!found)
{
if ((pd != null) && pd.isShowing())
{
Log.d(TAG, "Failed to find current client: closed connecting dialog, restarting P2P");
Toast.makeText(this, "Disconnected please try again", Toast.LENGTH_LONG).show();
restartP2p(false);
}
else
{
Log.d(TAG, "Failed to find current client, but we weren't trying to connect");
}
}
}
listview.setAdapter(new ArrayAdapter<String>(this, R.layout.list_black_text, R.id.list_content, list));
}
#Override
public void onConnectionInfoAvailable(WifiP2pInfo p2pInfo)
{
if (p2pInfo.isGroupOwner)
{
stopP2PDispeerTimeoutThread();
Log.d(TAG, "Connected as group owner");
int i = p2pInfo.mWfdRtspPort;
Log.d(TAG, "------------------------- onConnectionInfoAvailable rtsp port = " + i);
// get the ip addres from the connected client. Mac address is in
// client
String sourceIP = null;
while (sourceIP == null)
{
sourceIP = getIPFromMac(client.deviceAddress);
millisleep(250);
if (sourceIP == null)
{
Log.e(TAG, "Failed to get client address!!!!! from " + client.deviceAddress);
}
}
if (i == 0)
{
// this is not a wfd source, so let's get into client mode for
// upgrade etc.
Log.e(TAG, "This device is not a WFD source, so switch to client mode, device = "
+ client.deviceAddress);
}
else
{
Log.d(TAG, "Start connect to source : " + sourceIP);
if ((pd != null) && pd.isShowing())
pd.dismiss();
// need to stop the connection timeout thread here..
stopConnectTimeoutThread();
Intent intent = new Intent(getBaseContext(), WFDPlayActivity.class);
intent.putExtra("SOURCE_IP", sourceIP);
intent.putExtra("RTSPPORT", i);
startActivityForResult(intent, 210);
}
}
else
{
Log.e(TAG, "Connected as peer, this is very very wrong!!!!, restartP2P");
restartP2p(false);
startP2PDispeerTimeoutThread(34);
}
}
#Override
public void onConnectionRequested(WifiP2pDevice arg0, WifiP2pConfig arg1)
{
Log.d(TAG, "------------------------------------- onConnectionRequested");
stopP2PDispeerTimeoutThread();
isConnecting = true;
client = arg0;
if ((pd != null) && (pd.isShowing()))
{
pd.dismiss();
}
if (client != null)
{
createConnectionProgressDialog(client.deviceName);
}
}
public void onResume()
{
if (first_time == false)
{
Log.d(TAG, "onResume");
intentFilter.addAction("android.net.wifi.p2p.STATE_CHANGED");
intentFilter.addAction("android.net.wifi.p2p.PEERS_CHANGED");
intentFilter.addAction("android.net.wifi.p2p.CONNECTION_STATE_CHANGE");
intentFilter.addAction("android.net.wifi.p2p.THIS_DEVICE_CHANGED");
registerReceiver(receiver, intentFilter);
if (!mP2PDispeerThread.isRunning)
startP2PDispeerTimeoutThread(34);
manager.setDialogListener(channel, this);
manager.discoverPeers(channel, new WifiP2pManager.ActionListener()
{
#Override
public void onSuccess()
{
Log.d(TAG, "discoverPeers success");
}
#Override
public void onFailure(int reasonCode)
{
Log.d(TAG, "discoverPeers failed");
}
});
}
else
{
Log.d(TAG, "onResume, first time");
restartP2p(first_time);
first_time = false;
}
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
super.onResume();
}
public void restartP2p(boolean first)
{
if (first)
{
mWifiManager = ((WifiManager) getSystemService("wifi"));
mWifiManager.setWifiEnabled(true);
sleep(4); // was 4
manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
channel = manager.initialize(this, getMainLooper(), null);
manager.enableP2p(channel);
sleep(3); // was 3
mDisplayManager = ((DisplayManager) getSystemService("display"));
intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
mlock = mWifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF , "LockTag");
mlock.acquire();
instance = this;
receiver = new WiFiDirectBroadcastReceiver(manager, channel, this);
registerReceiver(receiver, intentFilter);
manager.setWFDInfo(channel, localWifiP2pWfdInfo, new WifiP2pManager.ActionListener()
{
public void onFailure(int paramAnonymousInt)
{
Log.d(TAG, "Failed to set WFD info with reason " + paramAnonymousInt + ".");
}
public void onSuccess()
{
Log.d(TAG, "updateWfdEnableState");
}
});
sleep(1);
manager.setDialogListener(channel, this);
manager.discoverPeers(channel, new WifiP2pManager.ActionListener()
{
#Override
public void onSuccess()
{
Log.d(TAG, "discoverPeers success");
}
#Override
public void onFailure(int reasonCode)
{
Log.d(TAG, "discoverPeers failed");
}
});
}
else
{
manager.stopPeerDiscovery(channel, new WifiP2pManager.ActionListener()
{
#Override
public void onSuccess()
{
Log.d(TAG, "stopPeerDiscovery success");
}
#Override
public void onFailure(int reasonCode)
{
Log.d(TAG, "stopPeerDiscovery failed");
}
});
localWifiP2pWfdInfo.setWfdEnabled(false);
manager.setWFDInfo(channel, localWifiP2pWfdInfo, new WifiP2pManager.ActionListener()
{
public void onFailure(int paramAnonymousInt)
{
Log.d(TAG, "Failed to set WFD info with reason " + paramAnonymousInt + ".");
}
public void onSuccess()
{
Log.d(TAG, "updateWfdEnableState");
}
});
sleep(4);
localWifiP2pWfdInfo.setWfdEnabled(true);
manager.setWFDInfo(channel, localWifiP2pWfdInfo, new WifiP2pManager.ActionListener()
{
public void onFailure(int paramAnonymousInt)
{
Log.d(TAG, "Failed to set WFD info with reason " + paramAnonymousInt + ".");
}
public void onSuccess()
{
Log.d(TAG, "updateWfdEnableState");
}
});
manager.discoverPeers(channel, new WifiP2pManager.ActionListener()
{
#Override
public void onSuccess()
{
Log.d(TAG, "discoverPeers success");
}
#Override
public void onFailure(int reasonCode)
{
Log.d(TAG, "discoverPeers failed");
}
});
}
if (first)
startP2PDispeerTimeoutThread(40); // only start the thread 1 time...
}
In the logging initially I see that the device and the phone connect successfully.
After disconnect, I see the devices being lost in the log, and after that, when I try to connect, I see the message:
I/wpa_supplicant(1839): [CTRL_IFACE]p2p0: P2P_CONNECT da:57:ef:cb:8e:be pbc go_intent=0
After this everything stops, the application is still running, I could see that from a log line that runs every second in the thread that stops and restarts the discovery every 40 seconds.
If anyone has any idea left why this behavior is happening, let me know. The only way to recover right now is to reboot the board (but that is not an option).
Thanks for your replies,
Arvy