I'm trying to assess my phones ability to connect to my wifi network. I want to figure out how long it takes to authenticate to my wifi access point, how long it is taking to obtain an IP address, etc..
I've made a lot of progress thanks to this website and lots of testing. However I can never seam to be able to get my broadcast receiver to trigger off certain connection states. I would really like to get somehow detect the time when authentication occurs.
Now I know what the Android documentation says and the Authentication state should be an easy thing to access. However in practice with multiple devices, that state is never reached. I've even tried polling with the following code.
public void start(long delayMillsec) {
handler.postDelayed(new Runnable() {
#Override
public void run() {
long timeout = System.currentTimeMillis() + 5000;
running = true;
while(true)
{
int oldState = state;
showWifiStatus();
int newState = state;
if(newState != oldState)
{
Log.e("STATE", stateString + ": " + System.nanoTime());
}
}
} }
, delayInMillsec);
}
public void showWifiStatus() {
boolean connected = false;
boolean associated = false;
ConnectivityManager connManager = (ConnectivityManager) thisContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
DetailedState val = mWifi.getDetailedState();
if (val == NetworkInfo.DetailedState.SCANNING) {
//Log.e("Detailed Wifi Info"," wifi state is SCANNING ");
state = 0;
stateString = "SCANNING";
}
if (val == NetworkInfo.DetailedState.CONNECTING) {
//Log.e("Detailed Wifi Info"," wifi state is CONNECTING ");
associated = true;
state = 1;
stateString = "CONNECTING";
}
if (val == NetworkInfo.DetailedState.AUTHENTICATING) {
//Log.e("Detailed Wifi Info"," 3wifi state is AUTHENTICATING");
associated = true;
state = 2;
stateString = "AUTHENTICATING";
}
if (val == NetworkInfo.DetailedState.OBTAINING_IPADDR) {
//Log.e("Detailed Wifi Info"," wifi state is OBTAINING_IPADDR");
associated = true;
state = 3;
stateString = "OBTAINING_IPADDR";
}
if (val == NetworkInfo.DetailedState.CONNECTED) {
//Log.e("Detailed Wifi Info"," wifi state is CONNECTED ");
connected = true;
associated = true;
state = 4;
stateString = "CONNECTED";
}
if (val == NetworkInfo.DetailedState.DISCONNECTED) {
//Log.e("Detailed Wifi Info"," wifi state is DISCONNECTED ");
state = 5;
stateString = "DISCONNECTED";
}
if (val == NetworkInfo.DetailedState.DISCONNECTING) {
// Log.e("Detailed Wifi Info"," wifi state is DISCONNECTING ");
state = 6;
stateString = "DISCONNECTING";
}
if (val == NetworkInfo.DetailedState.FAILED) {
//Log.e("Detailed Wifi Info"," wifi state is FAILED");
state = 7;
stateString = "FAILED";
}
if (val == NetworkInfo.DetailedState.IDLE) {
//Log.e("Detailed Wifi Info"," wifi state is IDLE");
state = 8;
stateString = "IDLE";
}
if (val == NetworkInfo.DetailedState.SUSPENDED) {
//Log.e("Detailed Wifi Info"," wifi state is SUSPENDED");
state = 9;
stateString = "SUSPENDED";
}
}
Where I call the start function immediately from a background service, then I walked around between known wifi AP's and disconnected the wifi and reconnected etc...
I've also tried the connectivity receiver to pick up whatever I could.
I used this intent filter:
IntentFilter ConnectedFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
ConnectedFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
ConnectedFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
ConnectedFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
getApplicationContext().registerReceiver(ConnectedToAPReceiver,
ConnectedFilter);
With this receiver, to output absolutely EVERYTHING that was being received. And there was never any indication that the authentication state is ever triggered.
private BroadcastReceiver ConnectedToAPReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Bundle extras = intent.getExtras();
if (extras != null) {
Log.e("NEW Action", intent.getAction());
for (String key: extras.keySet()) {
Log.e("CONN_ACTION", "key [" + key + "]: " +
extras.get(key));
}
Log.e("NEW THING", "------------");
}
else {
Log.e("CONNACTION", "no extras");
}
}};
I'm using the following permissions too:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
Are there any Android experts out there who can help me on this one? Ideally I would like to get some working code that I can use, but even some good information on why this state never does get reached would be very helpful. Is this a bug?
Thanks!
It works for me:
IntentFilter mIntentFilter = new IntentFilter();
mIntentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
registerReceiver(receiverWifi, mIntentFilter);
class WifiReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context c, Intent intent) {
String action = intent.getAction();
if(action.equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)){
Log.d("WifiReceiver", ">>>>SUPPLICANT_STATE_CHANGED_ACTION<<<<<<");
SupplicantState supl_state=((SupplicantState)intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE));
switch(supl_state){
case ASSOCIATED:Log.i("SupplicantState", "ASSOCIATED");
break;
case ASSOCIATING:Log.i("SupplicantState", "ASSOCIATING");
break;
case AUTHENTICATING:Log.i("SupplicantState", "Authenticating...");
break;
case COMPLETED:Log.i("SupplicantState", "Connected");
break;
case DISCONNECTED:Log.i("SupplicantState", "Disconnected");
break;
case DORMANT:Log.i("SupplicantState", "DORMANT");
break;
case FOUR_WAY_HANDSHAKE:Log.i("SupplicantState", "FOUR_WAY_HANDSHAKE");
break;
case GROUP_HANDSHAKE:Log.i("SupplicantState", "GROUP_HANDSHAKE");
break;
case INACTIVE:Log.i("SupplicantState", "INACTIVE");
break;
case INTERFACE_DISABLED:Log.i("SupplicantState", "INTERFACE_DISABLED");
break;
case INVALID:Log.i("SupplicantState", "INVALID");
break;
case SCANNING:Log.i("SupplicantState", "SCANNING");
break;
case UNINITIALIZED:Log.i("SupplicantState", "UNINITIALIZED");
break;
default:Log.i("SupplicantState", "Unknown");
break;
}
int supl_error=intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, -1);
if(supl_error==WifiManager.ERROR_AUTHENTICATING){
Log.i("ERROR_AUTHENTICATING", "ERROR_AUTHENTICATING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
}
}
}
<receiver
android:name=".MyActivity$WifiReceiver"
android:process=":remote" >
</receiver>
Related
I am developing a mobile app which keeps the screen ON when launched and all of you know that this kind of app drains the battery. So I wanted to keep the screen ON only when the device is plugged in. I found this code on stackoverflow (thanks to all of you).
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Context context = this;
Intent batteryStatus = context.registerReceiver(null, ifilter);
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
if (isCharging) {
if (usbCharge) {
tv1.setText("USB plugged in");
} else {
if (acCharge) {
tv1.setText("AC plugged in");
}
}
} else {
tv1.setText("Connect your charger");
}
}
I put this code in oncreate. But the problem is that it shows status only once(when app is launched). So I put a timer task to repeat checking like this
t = new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Context context = this;
Intent batteryStatus = context.registerReceiver(null, ifilter);
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
if (isCharging) {
if (usbCharge) {
tv1.setText("USB plugged in");
} else {
if (acCharge) {
tv1.setText("AC plugged in");
}
}
But this doesn't work and gives the error
Type missmatch : Cannot convert from new Runnable(){} to Context
I am a newbie so please see if you could help me with some modification or even a new code. Thanks in advance.
Important note: I work on Sketchware which lets you only to put codes in MainActivity.xml or MainActivity.java other features like manifest.xml is not editable. Please keep this in mind while answering.
Use Broadcast Receiver to get Battery Status.
Like That:-
// Initialize a new BroadcastReceiver instance
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver()
{
#Override
public void onReceive(Context context, Intent intent)
{
int status=intent.getIntExtra(BatteryManager.EXTRA_STATUS,-1);
if(status==BatteryManager.BATTERY_STATUS_CHARGING)
{
//Do anything if Charging
}
else
{
//Do anything if not Charging
}
}
}
Call it Like That:-
// Get the application context
Context mContext = getApplicationContext();
// Initialize a new IntentFilter instance
IntentFilter iFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
// Register the broadcast receiver
mContext.registerReceiver(mBroadcastReceiver,iFilter);
Hope this will help you!!!
I is there a way to Flag if a WIFI connection got disconnected/ dropped off OR if the user actually changed the WIFI network ?
I need my app to do :
Connect to a WIFI XYZ, if XYZ get disconnect (FLAG 1) or dropped off Then reconnect to XYZ.
But is the user change to another wifi BTOpen (FLAG 2) then allow the connect and Stop my service.
If user connect to XYZ again then start the loop again.
What I got so far is :
<!-- WIFI Receiver -->
<receiver android:name=".ReceiverWifi" >
<intent-filter>
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
<service android:name=".ServiceWifiMonitor" />
<receiver android:name=".ServiceController" >
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
BroadcastReceiver:
myApplication = (MyApplication) context.getApplicationContext();
conManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
networkInfo = conManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
boolean isConnected = networkInfo != null && networkInfo.isConnected();
int reconnectedCount = myApplication.getReconnectedCount();
if (wifiManager.isWifiEnabled()) {
if("android.net.conn.CONNECTIVITY_CHANGE".equals(intent.getAction())) {
//Start and Stop Service
if(myApplication.isReconnect()) startServiceWifiMonitor(); else stopServiceWifiMonitor();
if (isConnected) {
//There is a WIFI Connection
myApplication.setConnectedWifi(NetworkUtil.getCurrentSSID(context));
myApplication.setWifiStatus("connected");
if (NetworkUtil.isConnectedToXYZ(context)) {
startServiceWifiMonitor();
if(pref.getisFirstTime())
{
myApplication.setWifiByChoise("XYZ");
pref.setisFirstTime(false);
}
else { myApplication.setisReconnect(true); }
}
else {
//Connected to different NetWork
if(myApplication.isReconnect() && NetworkUtil.isXYZAvailable(context))
{
//ReConnect to XYZ
NetworkUtil.connectToXYZ(context);
myApplication.setReconnectedCount(reconnectedCount++);
}
else { resetValues("AAAA"); }
}
}//end if
else
{
if(NetworkUtil.isXYZAvailable(context) && myApplication.getWifiByChoise().equals("XYZ"))
{
NetworkUtil.connectToXYZ(context);
myApplication.setReconnectedCount(reconnectedCount++);
}
else { resetValues(""); }
}
}//end CONNECTIVITY_CHANGE
Service Monitor:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand > Received start id " + startId + ": " + intent);
objHandler.postDelayed(mTasks, 1000);
return START_STICKY;
}//end onStartCommand
private Runnable mTasks = new Runnable() {
public void run() {
if(myApplication.getWifiByChoise().equals("XYZ") && NetworkUtil.isXYZAvailable(context)) {
try
{
//Get the numbers of Reconnection
int count = myApplication.getReconnectedCount();
if(!NetworkUtil.isWifiConnected(context))
{
NetworkUtil.connectToXYZ(context);
myApplication.setisReconnect(true);
myApplication.setReconnectedCount(count++);
}
if(!NetworkUtil.isConnectedToXYZ(context))
{
NetworkUtil.connectToXYZ(context);
myApplication.setisReconnect(true);
myApplication.setReconnectedCount(count++);
}
} catch (Exception e) {e.printStackTrace();}
}
else { stopSelf(); }
int ms_interval = 3000;
objHandler.postDelayed(mTasks, ms_interval);
}
};//end Runnable mTasks
The problem with my app is that :
It crashed the device, Seems like its eating up all the memory ram.
sometimes with the wifi XYZ get disconnect it wont connect again and if user change to another wifi, it won't allow the connection.
I really appreciate your help. Thank you.
Check the network connected Name by using:
public String getWifiName(Context context) {
WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if (manager.isWifiEnabled()) {
WifiInfo wifiInfo = manager.getConnectionInfo();
if (wifiInfo != null) {
DetailedState state = WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState());
if (state == DetailedState.CONNECTED || state == DetailedState.OBTAINING_IPADDR) {
return wifiInfo.getSSID();
}
}
}
return null;
}
if this name matches your networkSSID, i.e. XYZ, then resume the service, else if it doesn't match, then stop the service:
if getWifiName(this).compareTo("XYZ") == 0 { //XYZ is your network name on which you want to resume the service
//code to resume
} else {
//code to stop the service
}
This is how I handle it in my app:
public class WifiStateWatcher extends BroadcastReceiver {
private MainActivity activity;
public WifiStateWatcher(MainActivity activity) {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
}
#Override
public void onReceive(Context context, Intent intent) {
SupplicantState supState;
WifiManager wifiManager = (WifiManager) activity.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
supState = wifiInfo.getSupplicantState();
if (supState.equals(SupplicantState.COMPLETED)) {
//we are connected to Wi-Fi network
} else {
//we lost Wi-Fi connectivity
}
}
}
You will need android.permission.ACCESS_WIFI_STATE permission
What you have done is almost correct. you need to check the network ssd name with the user connected wifi name.If it matched then do your part.
WifiManager wifiManager= (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if (wifiManager.isWifiEnabled()) {
WifiInfo networkInfo = wifiManager.getConnectionInfo();
if (networkInfo != null) {
DetailedState state = WifiInfo.getDetailedStateOf(networkInfo .getSupplicantState());
if (state == DetailedState.CONNECTED ) {
return networkInfo.getSSID();
}
}
}
return null;
Now you have the network SSID so try to check with the your wifi name and SSID then you will get to know the connection status.....
Happy Programming
Also just checked this and found out that the main difference is:
/** IP traffic should be available. */
DetailedState.CONNECTED
and:
/**
* …
* This state indicates that the supplicant has completed its
* processing for the association phase and that data connection is
* fully configured. Note, however, that there may not be any IP
* address associated with the connection yet. Typically, a DHCP
* request needs to be sent at this point to obtain an address.
*/
SupplicantState.COMPLETED
So to trust that wifi is completely up, i now added the checks that:
boolean isConnected = activeNetworkInfo.isConnected();
and DetailedState.CONNECTED :)
happy coding
I am using following code to print through android device with USB cable attached to my Samsung printer.
When use startPrinting method it give me following verification in debugging log:
Command to printer sent successfully
Permission from printer granted.
and the printer even starts beeping, but the data I provide does not get printed. I am stuck at this stage and have found no help from google or on stackoverflow either.
Note: There is no crash no error either
I am testing this code on Android Jelly bean 4.3 OS
Any help would be appreciated.
private UsbManager mUsbManager;
private UsbDevice mDevice;
private UsbDeviceConnection mConnection;
private UsbInterface mInterface;
private UsbEndpoint mEndPoint;
private PendingIntent mPermissionIntent;
private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
private static Boolean forceCLaim = true;
HashMap<String, UsbDevice> mDeviceList;
Iterator<UsbDevice> mDeviceIterator;
int protocol;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
mDeviceList = mUsbManager.getDeviceList();
mDeviceIterator = mDeviceList.values().iterator();
Button print = (Button) findViewById(R.id.buttonPrint);
Toast.makeText(this, "Device List Size: " + String.valueOf(mDeviceList.size()), Toast.LENGTH_SHORT).show();
TextView textView = (TextView) findViewById(R.id.usbDevice);
String usbDevice = "";
// This is just testing what devices are connected
while (mDeviceIterator.hasNext())
{
UsbDevice usbDevice1 = mDeviceIterator.next();
usbDevice += "\n" + "DeviceID: " + usbDevice1.getDeviceId() + "\n" + "DeviceName: " + usbDevice1.getDeviceName() + "\n" + "DeviceClass: " + usbDevice1.getDeviceClass() + " - "
+ translateDeviceClass(usbDevice1.getDeviceClass()) + "\n" + "DeviceSubClass: " + usbDevice1.getDeviceSubclass() + "\n" + "VendorID: " + usbDevice1.getVendorId() + "\n" + "ProductID: " + usbDevice1.getProductId()
+ "\n";
protocol = usbDevice1.getDeviceProtocol();
int interfaceCount = usbDevice1.getInterfaceCount();
Toast.makeText(this, "INTERFACE COUNT: " + String.valueOf(interfaceCount), Toast.LENGTH_SHORT).show();
mDevice = usbDevice1;
if (mDevice == null)
{
Toast.makeText(this, "mDevice is null", Toast.LENGTH_SHORT).show();
}
else
{
// Toast.makeText(this, "mDevice is not null", Toast.LENGTH_SHORT).show();
}
textView.setText(usbDevice);
}
if (mDevice == null)
{
Toast.makeText(this, "mDevice is null", Toast.LENGTH_SHORT).show();
}
else
{
// Toast.makeText(this, "mDevice is not null", Toast.LENGTH_SHORT).show();
}
print.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
mPermissionIntent = PendingIntent.getBroadcast(MainActivity.this, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filter);
if (mDevice != null)
mUsbManager.requestPermission(mDevice, mPermissionIntent);
// else
// Toast.makeText(this, "USB ", Toast.LENGTH_SHORT).show();
// print(mConnection, mInterface);
}
});
}
private String translateDeviceClass(int deviceClass)
{
switch (deviceClass)
{
case UsbConstants.USB_CLASS_APP_SPEC:
return "Application specific USB class";
case UsbConstants.USB_CLASS_AUDIO:
return "USB class for audio devices";
case UsbConstants.USB_CLASS_CDC_DATA:
return "USB class for CDC devices (communications device class)";
case UsbConstants.USB_CLASS_COMM:
return "USB class for communication devices";
case UsbConstants.USB_CLASS_CONTENT_SEC:
return "USB class for content security devices";
case UsbConstants.USB_CLASS_CSCID:
return "USB class for content smart card devices";
case UsbConstants.USB_CLASS_HID:
return "USB class for human interface devices (for example, mice and keyboards)";
case UsbConstants.USB_CLASS_HUB:
return "USB class for USB hubs";
case UsbConstants.USB_CLASS_MASS_STORAGE:
return "USB class for mass storage devices";
case UsbConstants.USB_CLASS_MISC:
return "USB class for wireless miscellaneous devices";
case UsbConstants.USB_CLASS_PER_INTERFACE:
return "USB class indicating that the class is determined on a per-interface basis";
case UsbConstants.USB_CLASS_PHYSICA:
return "USB class for physical devices";
case UsbConstants.USB_CLASS_PRINTER:
return "USB class for printers";
case UsbConstants.USB_CLASS_STILL_IMAGE:
return "USB class for still image devices (digital cameras)";
case UsbConstants.USB_CLASS_VENDOR_SPEC:
return "Vendor specific USB class";
case UsbConstants.USB_CLASS_VIDEO:
return "USB class for video devices";
case UsbConstants.USB_CLASS_WIRELESS_CONTROLLER:
return "USB class for wireless controller devices";
default:
return "Unknown USB class!";
}
}
// Broadcast receiver to obtain permission from user for connection
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver()
{
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action))
{
synchronized (this)
{
UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false))
{
if (device != null)
{
// call method to set up device communication
mInterface = device.getInterface(0);
mEndPoint = mInterface.getEndpoint(0);
mConnection = mUsbManager.openDevice(device);
Log.i("Info", "Device permission granted");
startPrinting(device);
// setup();
}
}
else
{
// Log.d("SUB", "permission denied for device " + device);
Toast.makeText(context, "PERMISSION DENIED FOR THIS DEVICE", Toast.LENGTH_SHORT).show();
}
}
}
}
};
public void startPrinting(final UsbDevice printerDevice)
{
Handler handler = new Handler();
handler.post(new Runnable()
{
UsbDeviceConnection conn;
UsbInterface usbInterface;
#Override
public void run()
{
try
{
Log.i("Info", "Bulk transfer started");
// usbInterface = printerDevice.getInterface(0);
for (int i = 0; i < printerDevice.getInterfaceCount(); i++)
{
usbInterface = printerDevice.getInterface(i);
if (usbInterface.getInterfaceClass() == UsbConstants.USB_CLASS_PRINTER)
{
// usbInterface = mDevice;
}
}
UsbEndpoint endPoint = usbInterface.getEndpoint(0);
conn = mUsbManager.openDevice(mDevice);
conn.claimInterface(usbInterface, true);
String myStringData = "TEXT";
myStringData += "\n";
byte[] array = myStringData.getBytes();
ByteBuffer output_buffer = ByteBuffer.allocate(array.length);
UsbRequest request = new UsbRequest();
request.initialize(conn, endPoint);
request.queue(output_buffer, array.length);
if (conn.requestWait() == request)
{
Log.i("Info", output_buffer.getChar(0) + "");
Message m = new Message();
m.obj = output_buffer.array();
output_buffer.clear();
}
else
{
Log.i("Info", "No request recieved");
}
int transfered = conn.bulkTransfer(endPoint, myStringData.getBytes(), myStringData.getBytes().length, 5000);
Log.i("Info", "Amount of data transferred : " + transfered);
}
catch (Exception e)
{
Log.e("Exception", "Unable to transfer bulk data");
e.printStackTrace();
}
finally
{
try
{
conn.releaseInterface(usbInterface);
Log.i("Info", "Interface released");
conn.close();
Log.i("Info", "Usb connection closed");
unregisterReceiver(mUsbReceiver);
Log.i("Info", "Brodcast reciever unregistered");
}
catch (Exception e)
{
Log.e("Exception", "Unable to release resources because : " + e.getMessage());
e.printStackTrace();
}
}
}
});
}
private void print(UsbDeviceConnection connection, UsbInterface intrface)
{
String test = "THIS IS A PRINT TEST";
// String text = "#move " + protocol + ";" + "#print" + test;
// Log.e("text", text);
byte[] testBytes = test.getBytes();
if (intrface == null)
{
Toast.makeText(this, "INTERFACE IS NULL", Toast.LENGTH_SHORT).show();
}
if (connection == null)
{
Toast.makeText(this, "CONNECTION IS NULL", Toast.LENGTH_SHORT).show();
}
if (forceCLaim == null)
{
Toast.makeText(this, "FORCE CLAIM IS NULL", Toast.LENGTH_SHORT).show();
}
connection.claimInterface(intrface, forceCLaim);
connection.bulkTransfer(mEndPoint, testBytes, testBytes.length, 0);
connection.close();
}
I have had this exact behavior from other operating systems, when the printer manufacturer uses a proprietary (and non disclosed :-( ) protocol over the USB bus. Specifically, the HP Laserjet P1060 series comes to mind. Both with GNU/Linux and Mac OS-X, the OS discoveres the printer quite well, and tries to print using a generic driver (e.g. HP Laserjet II). The printer's LED starts flashing - but nothing comes out. This felt a little as if some command was missing to make the printer actually print the page.
In these cases, a proprietary firmware blob needed to be downloaded to make things work. Unfortunately, it may be difficult to find such a driver for Android for home/small business printer models. I have had some luck with the Samsung Mobile Print Application (http://www.samsung.com/us/mobile-print-app/) with departamental networked laser printers (ML 3471-ND and suchlike). This was over Wifi + Ethernet.
HTH.
From your description everything seems to work - data transfer is happening, there are no errors but printing is not producing anything. Possibly because the Samsung printer is a page printer and the code you have is good for line-printing (Pos printers and Dot Matrix). In such a case the data will reside in the print buffer waiting for the page to be completed. Try to force a page to complete by issuing a formfeed and check.
I'm trying to do this: connect to USB device and get the opened (or failed) connection. I did the logic according to examples and explanations that I have found, but I have problem with waiting for permission grant. First I tried a "good" way of using wait()+notifyAll(), than I tried straightforward loop with checks, but both times the waiting method (waitConnection()) was blocking for the timeout I gave it, and only after that the message was received. So I tried these 2 versions.
wait/notifyAll:
public UsbConnector startConnection(Context context) {
BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action)) {
synchronized (syncObj) {
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (device != null) {
if (device.getVendorId() == vendorId && device.getProductId() == productId) {
connection = usbManager.openDevice(device);
connectedDevice = device;
}
}
}
syncObj.notyfyAll();
}
}
}
};
try {
usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
for (final UsbDevice device : usbManager.getDeviceList().values()) {
if (device.getVendorId() == this.vendorId && device.getProductId() == this.productId) {
context.registerReceiver(receiver, new IntentFilter(ACTION_USB_PERMISSION));
usbManager.requestPermission(device,
PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0));
break;
}
}
} catch (Exception e) {
}
return this;
}
public UsbDeviceConnection waitConnection(int timeout) {
try {
Thread.sleep(10, 0);
syncObj.wait(timeout);
} catch (InterruptedException e) {
}
return getConnection();
}
straightforward loop
public UsbConnector startConnection(Context context) {
BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action)) {
synchronized (this) {
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (device != null) {
if (device.getVendorId() == vendorId && device.getProductId() == productId) {
connection = usbManager.openDevice(device);
connectedDevice = device;
}
}
}
permissionRequested = false;
}
}
}
};
try {
permissionRequested = false;
usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
for (final UsbDevice device : usbManager.getDeviceList().values()) {
if (device.getVendorId() == this.vendorId && device.getProductId() == this.productId) {
permissionRequested = true;
context.registerReceiver(receiver, new IntentFilter(ACTION_USB_PERMISSION));
usbManager.requestPermission(device,
PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0));
break;
}
}
} catch (Exception e) {
}
return this;
}
public UsbDeviceConnection waitConnection(int timeout) {
int waited = timeout;
while (permissionRequested && waited > 0) {
try {
Thread.sleep(10, 0);
} catch (InterruptedException e) {
}
waited -= 10;
}
return getConnection();
}
So in both cases, according to logs, the waitConnection() method (that is called by the consumer immediately after startConnection()) seems to block the execution (I gave it timeout 10 seconds, and it was blocked for 10 seconds), and only right after it's completed, the BroadcastReceiver gets the message. It appears that requestPermission() is not async (as I thought it is), but in this case, how is it possible that startConnection() exits immediately and before the message is received? And how can I wait for BroadcastReceiver to get the message? Say if I don't use the waitConnection() method, how my consumer should know the moment when it can start checking for connection availability?
"and only right after it's completed, the BroadcastReceiver gets the message"
The onReceived callback, by default, is called on the main thread. It sounds like you are calling waitConnection() on the main thread as well. Since waitConnection() blocks, the main thread cannot process any additional messages until waitConnection() returns. This means that onReceived will not be called until waitConnection() times out.
It is generally a bad idea to block the main thread. Read here
Instead, you could have onReceive launch a new activity which then does whatever it is you need to do once you get USB permission. That may or may not be the best solution for you, but regardless, the key here is to never block the main thread.
I am trying to write a small android app (4.4) which searches for several Bluetooth LE devices. Once it has found each device it needs to connect to it and then continually read the RSSI of each device as quickly as it can. I have been trying to get this to work with 6 devices. My current code is as follows:
public class BluetoothGatt extends Activity {
private BluetoothAdapter mBluetoothAdapter;
private static final int REQUEST_ENABLE_BT = 1;
int count = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
// Initializes Bluetooth adapter.
final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
System.out.println("Adapter: " + mBluetoothAdapter);
BTScanStart();
}
// Start the Bluetooth Scan
private void BTScanStart() {
if (mBluetoothAdapter == null) {
System.out.println("Bluetooth NOT supported. Aborting.");
return;
} else {
if (mBluetoothAdapter.isEnabled()) {
System.out.println("Bluetooth is enabled...");
// Starting the device discovery
mBluetoothAdapter.startLeScan(mLeScanCallback);
}
}
}
// Device scan callback.
private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
public void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) {
count++;
System.out.println("Found " + count + ":" + device + " " + rssi + "db");
device.connectGatt(null, false, mGattCallback);
if (count > 5) mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
};
// Gatt Callback
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
System.out.println(gatt.getDevice() + ": Connected.. ");
gatt.readRemoteRssi();
}
if (newState == BluetoothProfile.STATE_DISCONNECTED) {
System.out.println(gatt.getDevice() + ": Disconnected.. ");
}
}
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
System.out.println(gatt.getDevice() + " RSSI:" + rssi + "db ");
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
gatt.readRemoteRssi();
}
};
}
I am having the following problems:
1) It connects to the devices successfully, but they all disconnect after around 5 seconds with a 'btm_sec_disconnected - Clearing Pending flag' error. Is there a was to keep them connected?
2) The code works fine for a single device, however when using more than one device only one device prints RSSI updates regularly, others update randomly and some don't update at all.
3) I am not sure what context I should supply when calling device.connectGatt .
Thank you in advance for your thoughts!
For the RSSI issue I will second Sam's answer (https://stackoverflow.com/a/20676785/4248895). It seems that for your use case continuously scanning should be sufficient. You don't want to add the overhead of connection if you can avoid it.
I will answer your other question in that if you do need to connect to these devices for some reason, your stability issues may be related to the fact that you're connecting and scanning simultaneously. Generally speaking, you should not perform any two gatt operations (scanning, connecting, reading, writing, etc.) at the same time.
How about just use startLeScan and get rssi?
If your android device filter the ble devices (like nexus 7), you could stopLeScan/ startLeScan repeatedly.
If not(like samsung s4 with android 4.3), just let it scan, onLeScan(...) will give every devices' rssi continuously.