I usually use this
private void ensureDiscoverable() {
if(D) Log.d(TAG, "ensure discoverable");
if (mBluetoothAdapter.getScanMode() !=
BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);
}
}
But that prompts a user confirmation. Is there a way to bypass this programmatically?
Also, I suppose there are no "news" on the "always discoverable mode" ?
After some research I concluded that setting discoverable timeout without user interaction it's only possible with root access (as already suggested in the previous answer). However for someone who need that here is the necessary solution:
private void ensureBluetoothDiscoverability() {
try {
IBluetooth mBtService = getIBluetooth();
Log.d("TESTE", "Ensuring bluetoot is discoverable");
if(mBtService.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
Log.e("TESTE", "Device was not in discoverable mode");
try {
mBtService.setDiscoverableTimeout(100);
// mBtService.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, 1000);
} catch(Exception e) {
Log.e("TESTE", "Error setting bt discoverable",e);
}
Log.i("TESTE", "Device must be discoverable");
} else {
Log.e("TESTE", "Device already discoverable");
}
} catch(Exception e) {
Log.e("TESTE", "Error ensuring BT discoverability", e);
}
}
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
And then create a new package android.bluetooth, place two files inside IBluetooth.aidl and IBluetoothCallback.aidl and put inside the code as shown here.
This will allow to access functions that are not available on the standard API, but for some of them you will need permission to "write secure settings" (the comment line above is where you will get that exception for lack of permissions of the process/user).
That's what I did and it works fine for me :
Method :
public void makeDiscoverable (int timeOut){
Class <?> baClass = BluetoothAdapter.class;
Method [] methods = baClass.getDeclaredMethods();
Method mSetScanMode = methods[44];
try {
mSetScanMode.invoke(Util.mBluetoothAdapter, BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, timeOut);
} catch (Exception e) {
Log.e("discoverable", e.getMessage());
}
}
Add permission :
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
Nothing - It still needs user confirmation.
BEing always discoverable is a drain on battery - so there is no easy solution
Also it is a privacy issue.
Its better to ask the user.
After testing and searching for various options, I have created these lines that don't require an user interaction. I hope it helps:
In method:
try{
Method metodo = BluetoothAdapter.class.getMethod("setScanMode", int.class);
try {
metodo.invoke(bluetoothAdaptador, SCAN_MODE_CONNECTABLE_DISCOVERABLE);
}catch (InvocationTargetException e) {
e.printStackTrace();
}
}catch (NoSuchMethodException | IllegalArgumentException | IllegalAccessException e) {
Log.e(TAG, "Fallo al hacer visibile el dispositivo.", e);}
Related
So i got my ESP32 and wanted to do some Porjects and I want to control them with an Android App, for example LED Stripes. I already did this with my Raspberry Pi where it runs perfectly.
I already tried some codes and it could connect to the Wifi. And my PC and even Raspberry Pi could connect to it but when i tried with my Smartphone i just didn't worked.
Here' my Android Code:
mainActivity.jre
connectButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!IPAddress.getText().equals("") || IPAddress.getText() != null || !portNumber.getText().equals("") || portNumber.getText() != null){
return;
}
IPaddresse= IPAddress.getText().toString();
port=Integer.parseInt(portNumber.getText().toString());
try {
client = new Socket(IPaddresse,port);
pw = new PrintWriter(client.getOutputStream());
dataOutputStream= new DataOutputStream(client.getOutputStream());
msg.setText("Verbunden!");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
Permissions:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
ESP32 Code:
#include <WiFi.h>
const char* ssid="Name";
const char* password="password";
WiFiServer server(80 );
void setup() {
Serial.begin(115200);
Serial.println("start");
delay(1000);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
Serial.println(WiFi.localIP());
server.begin();
}
void loop() {
WiFiClient client = server.available();
if (client) {
while (client.connected()) {
while (client.available()>0) {
char c = client.read();
client.write(c);
}
Serial.println(client.localIP());
delay(10);
}
}
}
Sorry for my English. And my Code isn't the best im just beginning to Code
Thanks
First on the ESP32 i suggest you add mDNS to your setup, this allows you to give your esp a name on the network like when you give your esp the name pietje then you can find it as pietje.local on your network.
This seems not to work properly with the socket class, but maybe i did something work. It did not finding the ip of it. It was the first time working with sockets. So i learned something new.
I tried your example and it did fault me to. With some short search i found this website:
https://www.tutorialspoint.com/sending-and-receiving-data-with-sockets-in-android
By adding you socket creation inside a thread it did work for me.
I hope this helps you ahead.
I need to include a walki talkie in my app. I am always getting a registration failure -9 error code. I created 2 free Sip account (sip2sip.info, sip.linphone.org) and 1 commercial one (onsip.com). I added all those permission:
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.INTERNET" />
<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.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
and the following features:
<uses-feature android:name="android.hardware.sip.voip" android:required="true" />
<uses-feature android:name="android.hardware.wifi" android:required="true" />
<uses-feature android:name="android.hardware.microphone" android:required="true" />
I am calling the above fonction in the onCreate:
void log_and_register()
{
try
{
c.DEBUG().ftrace("VOIP Supported: " + SipManager.isVoipSupported(c.ma()));
c.DEBUG().ftrace("SIP API Supported: " + SipManager.isApiSupported(c.ma()));
mSipManager = SipManager.newInstance(c.ma());
SipProfile.Builder builder = new SipProfile.Builder(c.config().getSIP_UserName(), c.config().getSIP_Domain());
builder.setPassword(c.config().getSIP_Password());
//builder.setProtocol("UDP"); //"TCP");
//builder.setPort(5060); //5080 5070
builder.setAutoRegistration(true);
mSipProfile = builder.build();
}
catch (ParseException pe)
{
c.DEBUG().ftrace("incapable of parsing domain name, username or password!");
c.DEBUG().ASSERT(0 == 1);
}
try
{
Intent intent = new Intent();
intent.setAction("android.SipDemo.INCOMING_CALL");
PendingIntent pendingIntent = PendingIntent.getBroadcast(c.ma(), 0, intent, Intent.FILL_IN_DATA);
mSipManager.open(mSipProfile, pendingIntent, null);
}
catch (SipException se)
{
c.DEBUG().ftrace("WALKIE TALKIE NOT WORKING - Sip Exception!!");
c.DEBUG().ASSERT(0 == 1);
}
if (ContextCompat.checkSelfPermission(c.ma(), Manifest.permission.USE_SIP) == PackageManager.PERMISSION_GRANTED)
c.DEBUG().ftrace("GRANTED!!!");
else
ActivityCompat.requestPermissions(c.ma(), new String[]{Manifest.permission.USE_SIP}, 1);
try
{
if (mSipManager.isRegistered(mSipProfile.getUriString()))
{
c.DEBUG().ftrace("already registered !!" + mSipManager.isRegistered(mSipProfile.getUriString()));
return;
}
}
catch (Exception e)
{
c.DEBUG().ftrace("NO!!");
}
try
{
//mSipManager.register(mSipProfile, 30, new SipRegistrationListener(){
//mSipManager.register(mSipProfile, 30000, new SipRegistrationListener(){
c.DEBUG().ftrace("THIS IS THE TRACE BEFORE REGISTATION : " + mSipProfile.getUriString());
mSipManager.setRegistrationListener(mSipProfile.getUriString(), new SipRegistrationListener()
{
public void onRegistering(String localProfileUri)
{
c.DEBUG().ftrace("Registering with SIP Server...");
}
// next version has it!!
public void onRegistrationTimeout() {}
public void onRegistrationDone(String localProfileUri, long expiryTime)
{
c.DEBUG().ftrace("SIP Ready");
}
public void onRegistrationFailed(String localProfileUri, int errorCode, String errorMessage)
{
// -9 signifie qu'il y a un appel en cours
c.DEBUG().ftrace("CANNOT REGISTER domain=" + c.config().getSIP_Domain() + " / username=" + c.config().getSIP_UserName());
c.DEBUG().ftrace("SIP ERROR MSG : localProfileUri=" + localProfileUri + " errorCode=" + errCode(errorCode) + " errorMessage=" + errorMessage);
}
});
}
catch (Exception e)
{
c.DEBUG().ftrace("Cannot initialise wakie talkie!");
c.DEBUG().ASSERT(0 == 1);
}
// https:github.com/aosp-mirror/platform_development/commit/a025796211f15c2796f8ea3208c066801aa250b6
initiateCall();
}
public SipAudioCall call = null;
public void initiateCall() {
try {
SipAudioCall.Listener listener = new SipAudioCall.Listener() {
// Much of the client's interaction with the SIP Stack will
// happen via listeners. Even making an outgoing call, don't
// forget to set up a listener to set things up once the call is established.
#Override
public void onCallEstablished(SipAudioCall call) {
call.startAudio();
call.setSpeakerMode(true);
call.toggleMute();
}
#Override
public void onCallEnded(SipAudioCall call) {
}
};
c.DEBUG().ftrace("rafael - Format="+mSipProfile.getUriString());
//call = mSipManager.makeAudioCall(mSipProfile.getUriString(), sipAddress, listener, 30);
call = mSipManager.makeAudioCall(mSipProfile.getUriString(), "sip:rafael.hogue#sip2sip.info", listener, 30);
} catch (Exception e) {
Log.i("WalkieTalkieActivity/InitiateCall", "Error when trying to close manager.", e);
if (mSipProfile != null) {
try {
mSipManager.close(mSipProfile.getUriString());
} catch (Exception ee) {
Log.i("WalkieTalkieActivity/InitiateCall",
"Error when trying to close manager.", ee);
ee.printStackTrace();
}
}
if (call != null) {
call.close();
}
}
}
I compile for Nougat because I am using deprecated function. Then I modified my code to compile with Oreo.
I used Firewall Voip Checker to check my connection with my SIP server and the test 1 is successful but the second one seam to be in an infinite loop. One of the result of the test one is captured my attention but I don't know what it means:
Check NAT type
100% / Blocked or could not reach STUN server (but it's green so I guess it pass the test??). I had the flag :
android.useDeprecatedNdk=true
I change it to false...
and I always get the return code "registration faild"
I hadded this fonction to see what the -9 error code was:
// FOR MORE DETAILS SEE
// Sip Error while registration
// How to send instant message via SIP
//https://developer.android.com/reference/android/net/sip/SipErrorCode
private String errCode(int iErrorCode)
{
String sErr = "";
switch (iErrorCode)
{
case CLIENT_ERROR:
sErr = "client error!!";
break;
case CROSS_DOMAIN_AUTHENTICATION:
sErr = "cross domain authentification!!";
break;
case DATA_CONNECTION_LOST:
sErr = "data connection lost!!";
break;
case INVALID_CREDENTIALS:
sErr = "invalid credentials!!";
break;
case INVALID_REMOTE_URI:
sErr = "invalid remote uri!!";
break;
case IN_PROGRESS:
sErr = "In progress!!";
break;
case NO_ERROR:
sErr = "No error!!";
break;
case PEER_NOT_REACHABLE:
sErr = "peer not reachable!!";
break;
case SERVER_ERROR:
sErr = "server error!!";
break;
case SERVER_UNREACHABLE:
sErr = "server unreachable!!";
break;
case SOCKET_ERROR:
sErr = "socket error!!";
break;
case TIME_OUT:
sErr = "time out!!";
break;
case TRANSACTION_TERMINTED:
sErr = "No transaction terminated!!";
break;
default:
sErr = "No error detected!!";
break;
}
return (sErr);
}
The error message is "In progress..." witch means that he is busy already but I don't know what it means.
I when thru the error code on Wikipedia to have clues of the potential problem:
https://en.wikipedia.org/wiki/List_of_SIP_response_codes#4xx.E2.80.94Client_Failure_Responses
I tried to find a SDK of a higher lever to implement my walki talki and found.
I search for the app wireshark but I only found it for a laptop and not for my android smartphone.
It's important for me to implement the walki talkie because I am creating a app to increase the security of primary school daycare and we need to communicate with each other.
I tried to change the port and the communication protocol and I tried the AutoRegistration flag to true.
I tried to make a phone call after the registration failed in case the open statement did the registration assuming it could be trying to register a second time.
I have no more idea!!
I will implementing a WIFI Walki Talki without a sip account.
Reference: stackoverflow.com/questions/11176988/…
Then I will save, on startup, the port information, ip adresse, username of every smart phones (use by my collegue) into my firebase (online database) in order to establish communication without asking anything to the user to make it automatique.
I am realizing that if I have trouble connecting with sample code that is suppose to work this probably means that my clients will have the same kinds of problemes wish I want to avoid.
I don't need to communicate with people that are not on the same network but I think this method would also work over the internet even for cliente that are on an other router if they are all connected to the internet.
hypothesis.getHypstr() is always one value, even after I change the keyword!
I am using pocketsphinx to do speech recognition, and I let the user change what to listen for. This value is stored in my shared preferences. My problem is that hypothesis.getHypstr() is only called when the previous keyword is spoken.
For example:
If it is set to default keyword (oranges and rainbows), then the recognition works fine. But, if the user changes it to "hello computer" then the onPartialResult method still only gets called when the user says hello, and hypothesis.getHypstr() is still oranges and rainbows.
onCreate:
try {
Assets assets = new Assets(MyService.this);
File assetDir = assets.syncAssets();
setupRecognizer(assetDir);
Log.v(TAG, "SET UP DIRECTORIES STARTING LISTENING!");
mSpeechRecognizer.startListening("usersKeyword");
} catch (IOException e) {
e.printStackTrace();
Log.v(TAG, e.toString());
}
setupRecognizer()
public void setupRecognizer(File sphinxDir) {
try {
mSpeechRecognizer = defaultSetup()
.setAcousticModel(new File(sphinxDir, "en-us-ptm"))
.setDictionary(new File(sphinxDir, "cmudict-en-us.dict"))
.setBoolean("-allphone_ci", true)
.setKeywordThreshold(1e-40f)
.getRecognizer();
} catch (IOException e) {
e.printStackTrace();
}
mSpeechRecognizer.addListener(this);
mSpeechRecognizer.addKeyphraseSearch("usersKeyword", keyword.getString("keyword", "oranges and rainbows"));
}
onPartialResult:
#Override
public void onPartialResult(Hypothesis hypothesis) {
if (hypothesis == null) { //no one spoke
return;
}
String text = hypothesis.getHypstr();
Log.v(TAG, "TEXT: " + text + "hypothesis.getHypstr: " + hypothesis.getHypstr());
if (text.equals(keyword.getString("keyword", "oranges and rainbows"))) { //Only happens when text is oranges and rainbows, even after changing preference value!!!
Log.v(TAG, "Heard user keyword!");
mSpeechRecognizer.cancel();
mSpeechRecognizer.startListening("usersKeyword");
}
}
Why is hypothesis.getHypstr() always only one value, even after I change the value of the addKeyphraseSearch?
Thanks,
Ruchir
EDIT:
I actually stop and start the service every time the user changes their input, and so onCreate() is called every time the user changes their data.
FULL CODE:
https://gist.github.com/anonymous/47efc9c1ca08d808e0be
You do not need to destroy the service, you create it once with onCreate.
You can set the command in onStartCommand:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
recognizer.cancel();
recognizer.addKeyphraseSearch("usersKeywords", intent.getStringExtra("keyword"););
recognizer.startListening("usersKeywords");
}
From the other class which is a user of the service you start service with intent:
Intent i = new Intent(this, MyService);
i.putExtra("keyword", "hello");
startService(i);
For more details read documentation
You need to call mSpeechRecognizer.addKeyphraseSearch() every time you want to change the key phrase.
Is it possible to change default sms application on Android 4.4 without user knowledge? I'm trying to do it on a ROOTED Galaxy S5 using reflection and invoking system methods, so here is what I done so far:
Created small app that implements all things needed for app to be a SMS manager (as per http://android-developers.blogspot.in/2013/10/getting-your-sms-apps-ready-for-kitkat.html) and I can make it default sms app but Android asks me to confirm that in dialog. Now I'm exploring android source code and I found something in this class:
https://github.com/android/platform_packages_apps_settings/blob/2abbacb7d46657e5863eb2ef0035521ffc41a0a8/src/com/android/settings/SmsDefaultDialog.java
So I'm trying to invoke internal method:
SmsApplication.setDefaultApplication(mNewSmsApplicationData.mPackageName, this);
via reflection and here is how I done it so far:
Class[] params = new Class[2];
params[0]=String.class;
params[1]=Context.class;
Class cls = null;
try {
cls = Class.forName("com.android.internal.telephony.SmsApplication");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Method method = null;
try {
method = cls.getDeclaredMethod("getSmsApplicationData", params);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
try {
Object[] params2 = new Object[2];
params2[0]=getPackageName();
params2[1]=getApplicationContext();
Log.d("Current package", getPackageName());
method.invoke(null, params2);
} catch (InvocationTargetException e) {
e.printStackTrace();
}
And I made this app system app and put it into folder /system/priv-app, rebooted phone and started it successfully with adb, but when I click on the button that executes code above, nothing happens. No errors, no log output, but default app is still Messaging (com.android.mms).
Is there any way to do this?
This information contained in file /data/system/users/0/settings_secure.xml. For example, default application for SMS stored by key sms_default_application.
<setting id="85" name="sms_default_application" value="com.google.android.talk" package="com.android.settings" />
More info you can find in this class: https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/provider/Settings.java#6067
Accordingly, if you have ROOT permissions, you can edit this file.
Here is my registration code:
protected void initializeManagerOpen(){
consoleWrite("initializeOpen");
if(mSipManager==null) {
return;
}
SipProfile.Builder builder;
try {
builder = new SipProfile.Builder("13", "10.0.0.4");
builder.setPassword("13");
builder.setPort(5062);
builder.setProtocol("UDP");
mSipProfile = builder.build();
try {
Intent intent = new Intent();
intent.setAction("android.SipDemo.INCOMING_CALL");
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, Intent.FILL_IN_DATA);
mSipManager.open(mSipProfile, pendingIntent, null);
mSipManager.setRegistrationListener(mSipProfile.getUriString(), new SipRegistrationListener() {
public void onRegistering(String localProfileUri) {
mNotificationTask.endNotification();
mNotificationTask.createNotification(R.drawable.ic_stat_connecting,"Test","Connecting");
consoleWrite("Registering with SIP Server...");
}
public void onRegistrationDone(String localProfileUri, long expiryTime){
mNotificationTask.endNotification();
mNotificationTask.createNotification(R.drawable.ic_stat_connected,"Test","Connected");
consoleWrite("Ready");
}
public void onRegistrationFailed(String localProfileUri, int errorCode, String errorMessage){
mNotificationTask.endNotification();
mNotificationTask.createNotification(R.drawable.ic_stat_disconnected,"Test","Failed to connect:"+errorCode);
consoleWrite("Registration failed. Please check settings.");
consoleWrite(""+errorCode);
consoleWrite(errorMessage);
}
});
} catch (SipException e) {
e.printStackTrace();
}
} catch (ParseException e) {
e.printStackTrace();
}
}
Though sometimes it registered successfully, most time I got a error code -9:
Registration failed. Please check settings.
-9
0
I found this description on reference site:
public static final int IN_PROGRESS
The client is in a transaction and cannot initiate a new one.
Constant Value: -9 (0xfffffff7)
What does it means exactly? I don't have any other SIP application running on my phone.
PS. First time when i am trying to connect, it is working. But second time it returns -9. Maybe i not close connection correctly? I think i have problem because i am trying to close connection but it is not closing...
public void closeLocalProfile() {
if(mSipManager==null){
return;
}
try{
if(mSipProfile!=null){
mSipManager.close(mSipProfile.getUriString());
consoleWrite("mSipManager Closed - "+mSipProfile.getUriString());
}
}catch(Exception e){
consoleWrite("Failed to close local profile. - "+e);
}
}
Delete all SIP account from Call Parameter and retry :
Call App->Parameter->Call account internet
Delete all account
PS: Sorry for the name menu, my phone isn't in english
I've faced the same issue and zicos22's comment helped me to solve it. The problem starts because of unclosed SipProfiles, so you need to run closeLocalProfile() inside onPause() method (it does not work when called inside onDestroy()). Actually, i think i have to run this sip stuff in separate thread, but for now i'm just closing profile in onPause. On my android phone with ZenUI i'am able to close currently opened sip profiles manually in Settings -> Call Settings -> Phone Account Settings -> SIP accounts.