Has anyone tried this API?
I'm developing a privileged system app to scan the network.
For some reason I get error 2 'Wrong parameters' when I try using it.
Granted the app MODIFY_PHONE_STATE permission, and still get the error
Here's an extract:
public class ScannerActivity extends Activity implements View.OnClickListener {
private final int PHONE_STATE_REQUEST = 1;
private Button scanButton;
private TextView resultsTextView;
private class RadioCallback extends TelephonyScanManager.NetworkScanCallback {
private List<CellInfo> mCellInfoResults;
private int mScanError;
#Override
public void onResults(List<CellInfo> cellInfoResults) {
mCellInfoResults = cellInfoResults;
ScannerActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
for (CellInfo cellInfo:mCellInfoResults) {
resultsTextView.append(" " + cellInfo.toString() + " ");
}
}
});
}
#Override
public void onError(int error) {
mScanError = error;
ScannerActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
resultsTextView.append(" Error: " + mScanError);
}
});
}
#Override
public void onComplete() {
ScannerActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
resultsTextView.append(" Scan Completed! ");
}
});
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scanner);
scanButton = (Button) findViewById(R.id.scan_button);
scanButton.setOnClickListener(this);
resultsTextView = (TextView) findViewById(R.id.results_text_view);
}
public void onClick(View view) {
NetworkScanRequest networkScanRequest;
RadioAccessSpecifier radioAccessSpecifiers[];
int bands[];
ArrayList<String> PLMNIds = new ArrayList<String>(Arrays.asList("42501"));
TelephonyManager telephonyManager = (TelephonyManager) getApplicationContext()
.getSystemService(Context.TELEPHONY_SERVICE);
bands[0] = AccessNetworkConstants.UtranBand.BAND_1;
radioAccessSpecifiers = new RadioAccessSpecifier[1];
radioAccessSpecifiers[0] = new RadioAccessSpecifier(
AccessNetworkConstants.AccessNetworkType.UTRAN,
bands,
null);
networkScanRequest = new NetworkScanRequest(
NetworkScanRequest.SCAN_TYPE_ONE_SHOT,
radioAccessSpecifiers,
0,
60,
false,
0,
PLMNIds);
telephonyManager.requestNetworkScan(networkScanRequest, AsyncTask.SERIAL_EXECUTOR,new RadioCallback());
}
}
Does anyone has an idea what parameters did i pass wrong?
I first thought that it might be LTE / 3G issue, but I get the same results both for UTRAN & EUTRAN.
I also ruled out SIM issue - the same error happens with 2 different SIMS, both successfully scanning the network form settings app for example.
OK, figured it out - for anyone who may come across this issue -
searchPeriodicity & incrementalResultsPeriodicity cannot be 0, despite the documentation,
and irrelevance of these parameters for one shot scan.
Related
I am new to creating AR app using ARCore and Sceneform for Android (Java). I am building a multiplayer app using cloud anchors where two or more people can interact on one or more asset. When the asset changes it's position, how to get the event to broadcast to other connected users?
Here's my code:
private AppAnchorState appAnchorState = AppAnchorState.NONE;
private SnackbarHelper snackbarHelper = new SnackbarHelper();
private StorageManager storageManager;
private Socket mSocket;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/******************* Here **************************/
// connect to socket
mSocket = new ArSync().onCreate();
mSocket.connect();
// listen for change event
mSocket.on("modelChanged", new Emitter.Listener() {
#Override
public void call(final Object... args) {
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
//TODO: call the application to load reslove by number
//textView.setText("Current ID is :" +
args[0].toString());
Log.d("Triggered", args[0].toString());
onResolveOkPressed(args[0].toString());
}
});
}
});
fragment = (CustomArFragment) getSupportFragmentManager().findFragmentById(R.id.sceneform_fragment);
//To add
fragment.getPlaneDiscoveryController().hide();
fragment.getArSceneView().getScene().setOnUpdateListener(this::onUpdateFrame);
Button clearButton = findViewById(R.id.clear_button);
clearButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setCloudAnchor(null);
}
});
Button resolveButton = findViewById(R.id.resolve_button);
resolveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (cloudAnchor != null){
snackbarHelper.showMessageWithDismiss(getParent(), "Please clear Anchor");
return;
}
ResolveDialogFragment dialog = new ResolveDialogFragment();
dialog.setOkListener(MainActivity.this::onResolveOkPressed);
dialog.show(getSupportFragmentManager(), "Resolve");
}
});
fragment.setOnTapArPlaneListener(
(HitResult hitResult, Plane plane, MotionEvent motionEvent) -> {
if (plane.getType() != Plane.Type.HORIZONTAL_UPWARD_FACING ||
appAnchorState != AppAnchorState.NONE){
return;
}
Anchor newAnchor = fragment.getArSceneView().getSession().hostCloudAnchor(hitResult.createAnchor());
setCloudAnchor(newAnchor);
appAnchorState = AppAnchorState.HOSTING;
snackbarHelper.showMessage(this, "Now hosting anchor...");
placeObject(fragment, cloudAnchor, Uri.parse("Fox.sfb"));
}
);
storageManager = new StorageManager(this);
}
/*.....
.......
.......
*/
I have written a separate Socket.io server in NodeJS to broadcast the Short hand GUID to other connected nodes to download and resolve the asset at launch of the app. It works fine when I am rendering the asset for the first time. But I am not sure how to handle when one person moves that asset. I want it to render it other connected devices. I could not find any event to asset changed so that I could attach my code to it.
I need to show list of all possible displays , and after that select one of them to connect with miracast. Here is my code:
private Context context;
private DisplayManager mDisplayManager;
private WifiP2pManager wifiP2pManager;
private ArrayList<WifiP2pDevice> devices;
private WifiP2pManager.Channel channel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
context = this;
devices = new ArrayList<WifiP2pDevice>();
buttonScan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startScan();
}
});
mDisplayManager = (DisplayManager)this.getSystemService(Context.DISPLAY_SERVICE);
wifiP2pManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
channel = wifiP2pManager.initialize(this, getMainLooper(), new WifiP2pManager.ChannelListener() {
public void onChannelDisconnected() {
channel = null;
Logger.makeLog("onChannelDisconnected");
}
});
}
WifiP2pManager.PeerListListener myPeerListListener = new WifiP2pManager.PeerListListener() {
#Override
public void onPeersAvailable(WifiP2pDeviceList peerList) {
// Out with the old, in with the new.
devices.clear();
devices.addAll(peerList.getDeviceList());
Logger.makeLog("devices size " + devices.size());
Toast.makeText(context, "devices size " + devices.size(), Toast.LENGTH_LONG).show();
if (devices.size() == 0) {
Logger.makeLog("No devices found");
return;
}
}
};
private void startScan() {
wifiP2pManager.requestPeers(channel, myPeerListListener);
}
After compiling this code, i press buttonScan, and it show, that 0 displays in area. But one display was near me. But then i connected to the display (TV), entered my app, pressed buttonScan, and it shows me, that 2 displays around me (TV and display of mobile). But its bad, i need to scan all possible displays to connect BEFORE connect... So, what am I doing wrong?
I am using sony smartwatch 3 and it has a power button which I want to control or say manipulate.
I have already tried using :
onKeyDown()
onKeyUp()
onKeyLongPress()
but with no success it doesn't even detect the press of key.
I have also tried using dispatchkeyevent(keyevent event) and shouldOverrideKeyEvent(keyevent event)but again no success.
Please guide.
You need to initialize and connect with GoogleApiClient. Once you clicks the button you have to get list of nodes and send a message to them. The last step is to read this message fron phone part of app, this can be done by registering proper WearableListenerService. Please see the sample code below.
Wearable App:
public class WearableButtonActivity extends Activity {
private GoogleApiClient mGoogleApiClient;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wearable_button_activity);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.build();
mGoogleApiClient.connect();
}
public void onButtonClicked(View target) {
if (mGoogleApiClient == null)
return;
final PendingResult<NodeApi.GetConnectedNodesResult> nodes = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient);
nodes.setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
#Override
public void onResult(NodeApi.GetConnectedNodesResult result) {
final List<Node> nodes = result.getNodes();
if (nodes != null) {
for (int i=0; i<nodes.size(); i++) {
final Node node = nodes.get(i);
// You can just send a message
Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), "/MESSAGE", null);
// or you may want to also check check for a result:
// final PendingResult<SendMessageResult> pendingSendMessageResult = Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), "/MESSAGE", null);
// pendingSendMessageResult.setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
// public void onResult(SendMessageResult sendMessageResult) {
// if (sendMessageResult.getStatus().getStatusCode()==WearableStatusCodes.SUCCESS) {
// // do something is successed
// }
// }
// });
}
}
}
});
}
}
Listener:
findViewById(R.id.button).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
onButtonClicked(v);
}
});
Phone App:
public class DataLayerListenerService extends WearableListenerService {
#Override
public void onMessageReceived(MessageEvent messageEvent) {
super.onMessageReceived(messageEvent);
if("/MESSAGE".equals(messageEvent.getPath())) {
// launch some Activity or do anything you like
}
}
}
I'm working with neura sdk in order to detect current data of a user(where he/she is, where were they 10 min ago etc).
I want to login to their api, and authenticate my user, however - when i call NeuraApiClient.authenticate(...) nothing happens.
I followed neura documentations, but still - nothing happens.
Here's my code :
public class MainActivity extends AppCompatActivity {
private ArrayList<Permission> mPermissions;
private AuthenticationRequest mAuthenticateRequest;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Builder builder = new Builder(this);
NeuraApiClient neuraApiClient = builder.build();
neuraApiClient.setAppUid(getResources().getString(R.string.app_uid));
neuraApiClient.setAppSecret(getResources().getString(R.string.app_secret));
neuraApiClient.connect();
fetchPermissions(neuraApiClient);
neuraApiClient.authenticate(100, mAuthenticateRequest);
}
private void fetchPermissions(final NeuraApiClient client) {
client.getAppPermissions(new GetPermissionsRequestCallbacks() {
#Override
public void onSuccess(final List<Permission> permissions) throws RemoteException {
runOnUiThread(new Runnable() {
#Override
public void run() {
mPermissions = new ArrayList<>(permissions);
mAuthenticateRequest = new AuthenticationRequest();
mAuthenticateRequest.setAppId(client.getAppUid());
mAuthenticateRequest.setAppSecret(client.getAppSecret());
mAuthenticateRequest.setPermissions(mPermissions);
}
});
}
#Override
public void onFailure(Bundle resultData, int errorCode) throws RemoteException {
}
#Override
public IBinder asBinder() {
return null;
}
});
}
}
getAppPermissions is an asynchronous call, and the data is fetched on GetPermissionsRequestCallbacks.
in GetPermissionsRequestCallbacks you're initiating mAuthenticateRequest which is in use of authenticate method.
Which means you have to wait untill onSuccess of GetPermissionsRequestCallbacks is called, and only then you can call
neuraApiClient.authenticate(100, mAuthenticateRequest);
Basically, if you don't wait for mAuthenticateRequest to be fetched, you authenticate with mAuthenticateRequest = null, and neuraApiClient.authenticate(..) fails.
You can do something like this : call authenticate when the results are received -
private void fetchPermissions(final NeuraApiClient client) {
client.getAppPermissions(new GetPermissionsRequestCallbacks() {
#Override
public void onSuccess(final List<Permission> permissions) throws RemoteException {
runOnUiThread(new Runnable() {
#Override
public void run() {
mPermissions = new ArrayList<>(permissions);
mAuthenticateRequest = new AuthenticationRequest();
mAuthenticateRequest.setAppId(client.getAppUid());
mAuthenticateRequest.setAppSecret(client.getAppSecret());
mAuthenticateRequest.setPermissions(mPermissions);
client.authenticate(100, mAuthenticateRequest);
}
});
}
...
});
}
I'm developing an application which is using the android SpeechRecognizer. I'm using it for something simple. I click in a button, my SpeechRecognizer start listening and I got some results from what I said.
Easy right? Well, My problem is that I need to make SpeechRecognizer fast. I mean, I click in my button, I say "Hello" and SpeechRecognizer takes like 3-4 seconds in return an array with the possible results. My question is:
It's possible to make SpeechRecognizer return results more faster?
Or take less time to close the Listening intent and start to process what it listen?
Maybe another way to do it? which will have a better performance than this?
I was checking the library and I saw this 3 parameters:
EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS:
The amount of time that it should take after we stop hearing speech to consider the input complete.
EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS
The minimum length of an utterance.
EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS
The amount of time that it should take after we stop hearing speech to
consider the input possibly complete.
http://developer.android.com/intl/es/reference/android/speech/RecognizerIntent.html
I have tried all of them but it is not working, or maybe I'm not using them right. Here is my code:
public class MainActivity extends Activity {
private static final String TIME_FORMAT = "%02d:%02d:%02d";
private final String TAG = "MainActivity";
private StartTimerButton mSpeakButton;
private CircleProgressBar mCountdownProgressBar;
private CountDownTimer mCountDownTimer;
private TextView mTimer;
private int mRunSeconds = 0;
private SpeechRecognizer mSpeechRecognizer;
private Intent mSpeechRecognizerIntent;
private boolean mIsListening = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRunSeconds = 0;
mTimer = (TextView) findViewById(R.id.timerText);
mCountdownProgressBar = (CircleProgressBar) findViewById(R.id.progressBar);
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
this.getPackageName());
// mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS,
// 1000);
// mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS,
// 1000);
// mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS,
// 1000);
SpeechRecognitionListener listener = new SpeechRecognitionListener();
mSpeechRecognizer.setRecognitionListener(listener);
mSpeakButton = (StartTimerButton) findViewById(R.id.btnSpeak);
mSpeakButton.setReadyState(false);
mSpeakButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mSpeakButton.isReady()) {
if (!mIsListening)
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
} else
mSpeakButton.setReadyState(true);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
return true;
}
public void onSpeechResults(ArrayList<String> matches) {
for (String match : matches) {
match = match.toLowerCase();
Log.d(TAG, "Got speech: " + match);
if (match.contains("go")) {
//Do Something
mSpeechRecognizer.stopListening();
}
if (match.contains("stop")) {
//Do Something
mSpeechRecognizer.stopListening();
}
}
}
protected class SpeechRecognitionListener implements RecognitionListener
{
#Override
public void onBeginningOfSpeech()
{
//Log.d(TAG, "onBeginingOfSpeech");
}
#Override
public void onBufferReceived(byte[] buffer)
{
}
#Override
public void onEndOfSpeech()
{
//Log.d(TAG, "onEndOfSpeech");
}
#Override
public void onError(int error)
{
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
//Log.d(TAG, "error = " + error);
}
#Override
public void onEvent(int eventType, Bundle params)
{
}
#Override
public void onPartialResults(Bundle partialResults)
{
ArrayList<String> matches = partialResults.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
for (String match : matches) {
match = match.toLowerCase();
Log.d(TAG, "onPartialResults : " + match);
}
}
#Override
public void onReadyForSpeech(Bundle params)
{
Log.d(TAG, "onReadyForSpeech"); //$NON-NLS-1$
}
#Override
public void onResults(Bundle results)
{
//Log.d(TAG, "onResults"); //$NON-NLS-1$
ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
onSpeechResults(matches);
// matches are the return values of speech recognition engine
// Use these values for whatever you wish to do
}
#Override
public void onRmsChanged(float rmsdB)
{
}
}}
Yes, it is possible to reduce the delay before shutdown....
You cannot alter the amount of time that Google considers to be silence at the end of a user speaking. The EXTRA_SPEECH_* parameters used to work, now they appear to sporadically work at best, or not work at all.
What you can do, is use the partial results to detect the words or phrase you want and then manually shut down the recognition service.
Here's an example of how to do this:
public boolean isHelloDetected(#NonNull final Context ctx, #NonNull final Locale loc, #NonNull final Bundle results) {
boolean helloDetected = false;
if (!results.isEmpty()) {
final String hello = ctx.getString(R.string.hello);
final ArrayList<String> partialData = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
/* handles empty string bug */
if (partialData != null && !partialData.isEmpty()) {
partialData.removeAll(Collections.singleton(""));
if (!partialData.isEmpty()) {
final ListIterator<String> itr = partialData.listIterator();
String vd;
while (itr.hasNext()) {
vd = itr.next().toLowerCase(loc).trim();
if (vd.startsWith(hello)) {
helloDetected = true;
break;
}
}
}
}
if (!helloDetected) {
final ArrayList<String> unstableData = results.getStringArrayList("android.speech.extra.UNSTABLE_TEXT");
/* handles empty string bug */
if (unstableData != null && !unstableData.isEmpty()) {
unstableData.removeAll(Collections.singleton(""));
if (!unstableData.isEmpty()) {
final ListIterator<String> itr = unstableData.listIterator();
String vd;
while (itr.hasNext()) {
vd = itr.next().toLowerCase(loc).trim();
if (vd.startsWith(hello)) {
helloDetected = true;
break;
}
}
}
}
}
}
return helloDetected;
}
You would run this method each time you receive from onPartialResults()
If true is returned, you'll need to call stopListening() on the main thread (probably by new Handler(Looper.getMainLooper()).post(...
Be aware though, once you've shut down the recognizer, the subsequent and final results you receive in onResults() may not contain "hello". As that word may have only be classified as unstable.
You'll need to write additional logic to prevent using detectHello() once hello has been detected (otherwise you'll repeatedly call stopListening()) - some simple boolean markers would resolve this.
Finally, the use of Collections.singleton("") to remove empty strings is part of an internal bug report, details to replicate here and the use of a ListIterator may be overkill for just your sample; a simple for loop would suffice.
Good luck.