|
Why have you reposted this question? You have already been given a number of suggestions below.
|
|
|
|
|
Hello there. I am trying to make a simple http POST request using HttpUrlConnection. Problem is that response code is 200 but the response message NOT success.
You can see that I set all the necessary header parameters. I am supposed to get success message. But I get {"status":false,"message":"Invalid access"} with responseCode = 200.
Below is the code (I am developing against Lollipop)
protected SubmitRequestTask.ResponseObject doInBackground(Object... object)
{
String encodedStr = URLEncoder.encode(object.getDataToSendRAW(), "UTF-8");
BufferedReader reader = null;
int responseCode = -1;
try
{
URL url = new URL();
Map<String, String> headerParams = new HashMap<String, String>();
headerParams.put("Accept", "application/json");
headerParams.put("Content-Type", "application/json");
HttpUrlConnection networkConnection = MakeRequest(
url,
"POST",
headerParams,
null);
if(networkConnection == null)
return new ResponseObject(-1, "Uknown Error");
StringBuilder sb = new StringBuilder();
reader = new BufferedReader(new InputStreamReader(networkConnection.getInputStream()));
while ((response = reader.readLine()) != null)
sb.append(response + "\n");
response = sb.toString();
}
catch(Exception ex) {}
finally {
responseCode = networkConnection.getResponseCode();
networkConnection.disconnect();
}
return new ResponseObject(responseCode, response);
}
private HttpUrlConnection MakeRequest(URL url, String method, Map<String, String> headerParams, NetworkConfig config)
{
HttpURLConnection htp_url_connection = (HttpURLConnection) url.openConnection();
htp_url_connection.setRequestMethod(method);
if (headerParams != null && method.trim().toLowerCase().equals("post"))
{
htp_url_connection.setDoOutput(true);
for (Map.Entry<String, String> entry : headerParams.entrySet()) {
(htp_url_connection.setRequestProperty(entry.getKey(), entry.getValue());
}
if (encodedStr.trim().length() > 0) {
OutputStreamWriter writer = new OutputStreamWriter(htp_url_connection.getOutputStream());
writer.write(encodedStr);
writer.flush();
}
}
htp_url_connection.setConnectTimeout(5000);
int responseCode = htp_url_connection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK )
return htp_url_connection;
}
Sample JSON data (which I encode in doInBackground).
{
"plot_unique_id":"355618",
"plot_physical_address":"owner",
"plot_id":"355617",
"location":"31.475885,74.3422574",
"action_type":"1",
"plot_sub_id":"1",
"apk_version":"1.0",
"imei_no":"862037029044152",
"owners":[......]
}
What could be the problem?? Thanks for any input.
|
|
|
|
|
That is correct; the HTTP response is 200 telling you that the server responded with some data. The response data is a JSON reply which contains information about the JSON request. You need to contact the owners of the website, or the documentation, to find out why it returned the "Invalid access" status.
|
|
|
|
|
How to detect OTG Cable when plugged in inside micro usb port and enable switch inside app?
|
|
|
|
|
What was the answer given to you for your previous post? Just read the answer 1 thread below.
Never mind.
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
modified 21-Jan-17 18:59pm.
|
|
|
|
|
This has nothing to do with windows.I want to detect when OTG Cable is plugged inside inside android usb port and go to app and enable switch!!!
|
|
|
|
|
That was just an example to give you an example of how other devices can prevent your device from making changes, unless provided with enough permissions.
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|
|
How to also give permission?
|
|
|
|
|
|
I cannot have client-based application on other device because it's cable for otg !!!
|
|
|
|
|
Pavlex4 wrote: I want to detect when OTG Cable is plugged inside inside android usb port... I may be wrong, but I don't think that is possible, since a circuit has not yet been completed. You've basically just extended the wires at the USB port.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
As already said by David, there will be no event when only plugging in a cable.
To monitor events see for example (first result when googling for "android usb events"): How to Monitor USB Events on Android[^].
Read also USB Host and Accessory | Android Developers[^]:
Quote: Note: Support for USB host and accessory modes are ultimately dependant on the device's hardware, regardless of platform level. So it might be not possible with your device.
|
|
|
|
|
When otg cable is plugged in kernel says "USB connector connected" !
How to enable switch inside app when kernel says above message?
or to detect only OTG cable when plugged in or out using android app ? When otg cable is detected inside app should be enabled switch or disabled when otg cable is plugged out?
or to detect when Android enters Host Mode using app?
modified 24-Jan-17 9:50am.
|
|
|
|
|
Then it is not a simple cable but has built-in resistors or something connected at the other end.
If you got a kernel message, you should be able to get an event using udev rules. But this requires root privileges to save the rules file.
|
|
|
|
|
My phone is rooted!!!How to get event using udev?
|
|
|
|
|
Sorry that I mentioned udev. It is not used by Android.
|
|
|
|
|
Than how to do any of this:
When otg cable is plugged in kernel says "USB connector connected" !
How to enable switch inside app when kernel says above message?
or to detect only OTG cable when plugged in or out using android app ? When otg cable is detected inside app should be enabled switch or disabled when otg cable is plugged out?
or to detect when Android enters Host Mode using app?
|
|
|
|
|
By inspecting the Android sources to find which module is generating the message (if the message is not prefixed with the module name). Once the module is known check if it provides some kind of notification that can be used.
Or trying to use the provided Android APIs like android.hardware.usb | Android Developers[^].
Or polling the /dev directory for changes.
The host mode is entered when an USB device is attached. That can be detected as described in my initial answer (if supported).
If you did not get a final answer here or searching the web, you can try to do it yourself. This requires some research and implementing one or more of the above.
|
|
|
|
|
How to find modules that send messages inside kernel source?
|
|
|
|
|
I have searched all files and I didn't find kernel message for USB !!!!
I ran android terminal on my phone, typed cat /proc/kmsg and when plugged in otg cable I got msm_otg f9a55000.usb : host on
I found that message inside msm_otg kernel source:
static void msm_otg_start_host(struct usb_otg *otg, int on)
{
struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
struct msm_otg_platform_data *pdata = motg->pdata;
struct usb_hcd *hcd;
if (!otg->host)
return;
#ifdef CONFIG_USB_HOST_NOTIFY
msm_otg_host_notify(motg, on);
#endif
hcd = bus_to_hcd(otg->host);
if (on) {
dev_dbg(otg->phy->dev, "host on\n");
if (pdata->otg_control == OTG_PHY_CONTROL)
ulpi_write(otg->phy, OTG_COMP_DISABLE,
ULPI_SET(ULPI_PWR_CLK_MNG_REG));
if (pdata->setup_gpio)
pdata->setup_gpio(OTG_STATE_A_HOST);
usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
} else {
dev_dbg(otg->phy->dev, "host off\n");
usb_remove_hcd(hcd);
writel_relaxed(0x80000000, USB_PORTSC);
if (pdata->setup_gpio)
pdata->setup_gpio(OTG_STATE_UNDEFINED);
if (pdata->otg_control == OTG_PHY_CONTROL)
ulpi_write(otg->phy, OTG_COMP_DISABLE,
ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
}
}
and do I need to create something like this: As code goes by: UEvents in Android - from kernel events to notifications[^]
|
|
|
|
|
The above code seems to be executed when activating and deactivating the host mode and calls the msm_otg_host_notify function when enabled by build configuration. So it might be useful to inspect this function.
But please understand that I don't have the time and interest to dig into this further (especially because it would also require to get the sources first).
I pointed you to directions that might help to solve the problem. But I'm not going to do the research for you.
|
|
|
|
|
Can this be helpful.It's from sec-switch.c file!
#ifdef CONFIG_USB_HOST_NOTIFY
} else if (usb_mode == USB_OTGHOST_DETACHED
|| usb_mode == USB_OTGHOST_ATTACHED) {
if (usb_mode == USB_OTGHOST_DETACHED) {
pr_info("USB Host detached");
sec_otg_notify(HNOTIFY_ID_PULL);
} else {
pr_info("USB Host attached");
sec_otg_notify(HNOTIFY_ID);
}
} else if (usb_mode == USB_POWERED_HOST_DETACHED
|| usb_mode == USB_POWERED_HOST_ATTACHED) {
if (usb_mode == USB_POWERED_HOST_DETACHED){
pr_info("USB Host HNOTIFY_SMARTDOCK_OFF");
sec_otg_notify(HNOTIFY_SMARTDOCK_OFF);
}else{
pr_info("USB Host HNOTIFY_SMARTDOCK_ON");
sec_otg_notify(HNOTIFY_SMARTDOCK_ON);
}
#endif
}
}
or this file sm5502.c :
#if defined(CONFIG_USB_HOST_NOTIFY)
if (adc == 0x11 || adc == ADC_AUDIO_DOCK) {
val2 = DEV_AUDIO_DOCK;
val1 = 0;
}
#endif
dev_err(&client->dev,
"dev1: 0x%x,dev2: 0x%x,dev3: 0x%x,Carkit: 0x%x,ADC: 0x%x,Jig: %s\n",
val1, val2, val3, val4, adc,
(check_sm5502_jig_state() ? "ON" : "OFF"));
if (val1 & DEV_USB || val2 & DEV_T2_USB_MASK ||
val4 & DEV_CARKIT_CHARGER1_MASK) {
pr_info("[MUIC] USB Connected\n");
pdata->callback(CABLE_TYPE_USB, SM5502_ATTACHED);
} else if (val1 & DEV_USB_CHG) {
pr_info("[MUIC] CDP Connected\n");
pdata->callback(CABLE_TYPE_CDP, SM5502_ATTACHED);
} else if (val1 & DEV_T1_UART_MASK || val2 & DEV_T2_UART_MASK) {
uart_sm5502_connecting = 1;
pr_info("[MUIC] UART Connected\n");
i2c_smbus_write_byte_data(client, REG_MANUAL_SW1, SW_UART);
if(vbus & DEV_VBUSIN_VALID)
pdata->callback(CABLE_TYPE_JIG_UART_OFF_VB, SM5502_ATTACHED);
else
pdata->callback(CABLE_TYPE_UARTOFF, SM5502_ATTACHED);
#if (!defined(CONFIG_MACH_CT01) && !defined(CONFIG_MACH_CT01_CHN_CU))
flash_control(true);
#endif
} else if ((val1 & DEV_T1_CHARGER_MASK) ||
(val3 & DEV_T3_CHARGER_MASK)) {
pr_info("[MUIC] Charger Connected\n");
pdata->callback(CABLE_TYPE_AC, SM5502_ATTACHED);
#if defined(CONFIG_USB_HOST_NOTIFY)
} else if (val1 & DEV_USB_OTG && adc == ADC_OTG) {
pr_info("[MUIC] OTG Connected\n");
#if defined(CONFIG_MUIC_SM5502_SUPPORT_LANHUB_TA)
sm5502_enable_rawdataInterrupts(usbsw);
usbsw->dock_attached = SM5502_ATTACHED;
usbsw->previous_dock = ADC_OTG;
#endif
sm5502_set_otg(usbsw, SM5502_ATTACHED);
pdata->callback(CABLE_TYPE_OTG, SM5502_ATTACHED);
#endif
} else if (val2 & DEV_T2_JIG_MASK) {
pr_info("[MUIC] JIG Connected\n");
pdata->callback(CABLE_TYPE_JIG, SM5502_ATTACHED);
#if (!defined(CONFIG_MACH_CT01) && !defined(CONFIG_MACH_CT01_CHN_CU))
flash_control(true);
#endif
} else if ((val2 & DEV_AV) || (val3 & DEV_AV_VBUS)) {
pr_info("[MUIC] Deskdock Connected\n");
local_usbsw->dock_attached = SM5502_ATTACHED;
if(vbus & DEV_VBUSIN_VALID)
sm5502_dock_control(usbsw, CABLE_TYPE_DESK_DOCK,
SM5502_ATTACHED, SW_AUDIO);
else
sm5502_dock_control(usbsw, CABLE_TYPE_DESK_DOCK_NO_VB,
SM5502_ATTACHED, SW_AUDIO);
#if defined(CONFIG_VIDEO_MHL_V2)
} else if (val3 & DEV_MHL) {
pr_info("[MUIC] MHL Connected\n");
sm5502_disable_interrupt();
if (!poweroff_charging)
else
pr_info("LPM mode, skip MHL sequence\n");
sm5502_enable_interrupt();
#endif
} else if (val2 & DEV_JIG_UART_ON) {
pr_info("[MUIC] Cardock Connected\n");
#if defined(CONFIG_SEC_FACTORY)
local_usbsw->dock_attached = SM5502_ATTACHED;
sm5502_dock_control(usbsw, CABLE_TYPE_CARDOCK,
SM5502_ATTACHED, SW_AUDIO);
#endif
} else if (val2 & DEV_SMARTDOCK) {
pr_info("[MUIC] Smartdock Connected\n");
sm5502_dock_control(usbsw, CABLE_TYPE_SMART_DOCK,
SM5502_ATTACHED, SW_DHOST);
#if defined(CONFIG_VIDEO_MHL_V2)
#endif
#if defined(CONFIG_USB_HOST_NOTIFY)
} else if (val2 & DEV_AUDIO_DOCK) {
pr_info("[MUIC] Audiodock Connected\n");
sm5502_dock_control(usbsw, CABLE_TYPE_AUDIO_DOCK,
SM5502_ATTACHED, SW_DHOST);
#endif
#if defined(CONFIG_MUIC_SM5502_SUPPORT_LANHUB_TA)
} else if (val2 & DEV_LANHUB) {
if(usbsw->previous_dock == ADC_LANHUB &&
usbsw->lanhub_ta_status == 1 )
pr_info("[MUIC] Lanhub + TA is connected\n");
else {
sm5502_enable_rawdataInterrupts(usbsw);
sm5502_detect_lanhub(usbsw);
}
#endif
} else if (vbus & DEV_VBUSIN_VALID) {
pr_info("[MUIC] Incompatible Charger Connected\n");
pdata->callback(CABLE_TYPE_INCOMPATIBLE,
SM5502_ATTACHED);
}
#if defined(CONFIG_TOUCHSCREEN_MMS144)
else{
tsp_noti_ignore = 1;
printk("[TSP] attached, but don't noti \n");
}
if(!tsp_noti_ignore)
tsp_charger_infom(1);
#endif
usbsw->dev1 = val1;
usbsw->dev2 = val2;
usbsw->dev3 = val3;
usbsw->adc = adc;
usbsw->vbus = vbus;
usbsw->carkit_dev = val4;
return adc;
}
static int sm5502_detach_dev(struct sm5502_usbsw *usbsw)
{
struct sm5502_platform_data *pdata = usbsw->pdata;
#if defined(CONFIG_TOUCHSCREEN_MMS144)
int tsp_noti_ignore = 0;
#endif
if (usbsw->dev1 & DEV_USB ||
usbsw->dev2 & DEV_T2_USB_MASK ||
usbsw->carkit_dev & DEV_CARKIT_CHARGER1_MASK) {
pr_info("[MUIC] USB Disonnected\n");
pdata->callback(CABLE_TYPE_USB, SM5502_DETACHED);
} else if (usbsw->dev1 & DEV_USB_CHG) {
pdata->callback(CABLE_TYPE_CDP, SM5502_DETACHED);
} else if (usbsw->dev1 & DEV_T1_UART_MASK ||
usbsw->dev2 & DEV_T2_UART_MASK) {
pr_info("[MUIC] UART Disonnected\n");
if(usbsw->vbus & DEV_VBUSIN_VALID)
pdata->callback(CABLE_TYPE_JIG_UART_OFF_VB, SM5502_DETACHED);
else
pdata->callback(CABLE_TYPE_UARTOFF, SM5502_DETACHED);
uart_sm5502_connecting = 0;
#if (defined(CONFIG_MACH_MS01_EUR_3G) || defined(CONFIG_MACH_MS01_CHN_CMCC_3G) || defined(CONFIG_MACH_MS01_CHN_CU_3G))
flash_control(false);
#endif
} else if ((usbsw->dev1 & DEV_T1_CHARGER_MASK) ||
(usbsw->dev3 & DEV_T3_CHARGER_MASK)) {
pr_info("[MUIC] Charger Disonnected\n");
pdata->callback(CABLE_TYPE_AC, SM5502_DETACHED);
#if defined(CONFIG_USB_HOST_NOTIFY)
} else if (usbsw->dev1 & DEV_USB_OTG) {
pr_info("[MUIC] OTG Disonnected\n");
#if defined(CONFIG_MUIC_SM5502_SUPPORT_LANHUB_TA)
sm5502_disable_rawdataInterrupts(usbsw);
pr_info("%s:lanhub_ta_status(%d)\n",
__func__, usbsw->lanhub_ta_status);
lanhub_ta_case = false;
if (usbsw->lanhub_ta_status == 0) {
pdata->callback(CABLE_TYPE_OTG,SM5502_DETACHED);
sm5502_set_otg(usbsw, SM5502_DETACHED);
}
else if (pdata->lanhub_cb && usbsw->lanhub_ta_status == 1)
pdata->lanhub_cb(CABLE_TYPE_LANHUB,SM5502_DETACHED, LANHUB);
usbsw->dock_attached = SM5502_DETACHED;
usbsw->lanhub_ta_status=0;
#else
sm5502_set_otg(usbsw, SM5502_DETACHED);
pdata->callback(CABLE_TYPE_OTG, SM5502_DETACHED);
#endif
#endif
} else if (usbsw->dev2 & DEV_T2_JIG_MASK) {
pr_info("[MUIC] JIG Disonnected\n");
pdata->callback(CABLE_TYPE_JIG, SM5502_DETACHED);
#if (defined(CONFIG_MACH_MS01_EUR_3G) || defined(CONFIG_MACH_MS01_CHN_CMCC_3G) || defined(CONFIG_MACH_MS01_CHN_CU_3G))
flash_control(false);
#endif
} else if ((usbsw->dev2 & DEV_AV) ||
(usbsw->dev3 & DEV_AV_VBUS)) {
pr_info("[MUIC] Deskdock Disonnected\n");
local_usbsw->dock_attached = SM5502_DETACHED;
if(usbsw->vbus & DEV_VBUSIN_VALID)
sm5502_dock_control(usbsw, CABLE_TYPE_DESK_DOCK,
SM5502_DETACHED, SW_ALL_OPEN);
else
sm5502_dock_control(usbsw, CABLE_TYPE_DESK_DOCK_NO_VB,
SM5502_DETACHED, SW_ALL_OPEN);
#if defined(CONFIG_MHL_D3_SUPPORT)
} else if (usbsw->dev3 & DEV_MHL) {
pr_info("[MUIC] MHL Disonnected\n");
detached_sm5502_status = 1;
#endif
} else if (usbsw->dev2 & DEV_JIG_UART_ON) {
pr_info("[MUIC] Cardock Disonnected\n");
#if defined(CONFIG_SEC_FACTORY)
local_usbsw->dock_attached = SM5502_DETACHED;
sm5502_dock_control(usbsw, CABLE_TYPE_CARDOCK,
SM5502_DETACHED, SW_ALL_OPEN);
#endif
} else if (usbsw->dev2 == DEV_SMARTDOCK) {
pr_info("[MUIC] Smartdock Disonnected\n");
sm5502_dock_control(usbsw, CABLE_TYPE_SMART_DOCK,
SM5502_DETACHED, SW_ALL_OPEN);
#if defined(CONFIG_VIDEO_MHL_V2)
#endif
#if defined(CONFIG_USB_HOST_NOTIFY)
} else if (usbsw->dev2 == DEV_AUDIO_DOCK) {
pr_info("[MUIC] Audiodock Disonnected\n");
sm5502_dock_control(usbsw, CABLE_TYPE_AUDIO_DOCK,
SM5502_DETACHED, SW_ALL_OPEN);
#endif
#if defined(CONFIG_MUIC_SM5502_SUPPORT_LANHUB_TA)
} else if (usbsw->adc == ADC_LANHUB) {
pr_info("[MUIC] Lanhub disconnected\n");
sm5502_disable_rawdataInterrupts(usbsw);
lanhub_ta_case = false;
usbsw->dock_attached = SM5502_DETACHED;
sm5502_set_lanhub(usbsw,SM5502_DETACHED);
sm5502_mask_vbus_detect(usbsw, SM5502_DETACHED);
pr_info("%s:lanhub_ta_status(%d)\n",__func__, usbsw->lanhub_ta_status);
if (pdata->lanhub_cb && usbsw->lanhub_ta_status==1)
pdata->lanhub_cb(CABLE_TYPE_LANHUB, SM5502_DETACHED, LANHUB);
else if (usbsw->lanhub_ta_status == 0) {
pdata->callback(CABLE_TYPE_OTG, SM5502_DETACHED);
sm5502_set_otg(usbsw, SM5502_DETACHED);
}
usbsw->lanhub_ta_status=0;
usbsw->dock_attached = SM5502_DETACHED;
#endif
} else if (usbsw->vbus & DEV_VBUSIN_VALID) {
pr_info("[MUIC] Incompatible Charger Disonnected\n");
pdata->callback(CABLE_TYPE_INCOMPATIBLE,
SM5502_DETACHED);
}
|
|
|
|
|
How to use app to detect when otg cable is plugged in and when it's plugged out? Is there intent for otg cable like this for usb devices: "android.hardware.usb.action.USB_DEVICE_ATTACHED"
I have created app like this but I only detects flash drive not otg cable:
public class MainActivity extends AppCompatActivity
{
private TextView mInfo;
private Logger mLogger;
private HashMap<UsbDevice, UsbDataBinder> mHashMap = new HashMap<UsbDevice, UsbDataBinder>();
private UsbManager mUsbManager;
private PendingIntent mPermissionIntent;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mInfo = (TextView)findViewById(R.id.log);
mLogger = new Logger(this);
mLogger.setMode(Logger.MODE_TOAST);
mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
usbConnection();
}
private void usbConnection() {
IntentFilter filter = new IntentFilter(UsbManager.ACTION_USB_DEVICE_ATTACHED);
registerReceiver(mUsbAttachReceiver , filter);
filter = new IntentFilter(UsbManager.ACTION_USB_DEVICE_DETACHED);
registerReceiver(mUsbDetachReceiver , filter);
mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
filter = new IntentFilter(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filter);
showDevices();
}
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mUsbDetachReceiver);
unregisterReceiver(mUsbAttachReceiver);
unregisterReceiver(mUsbReceiver);
};
BroadcastReceiver mUsbDetachReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (device != null) {
UsbDataBinder binder = mHashMap.get(device);
if (binder != null) {
binder.onDestroy();
mHashMap.remove(device);
Toast.makeText(MainActivity.this, "Attached!", Toast.LENGTH_SHORT).show();
}
}
}
}
};
BroadcastReceiver mUsbAttachReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
showDevices();
Toast.makeText(MainActivity.this, "Detached!", Toast.LENGTH_SHORT).show();
}
}
};
private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
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) {
UsbDataBinder binder = new UsbDataBinder(mUsbManager, device);
mHashMap.put(device, binder);
Toast.makeText(MainActivity.this, "Permission Granted!", Toast.LENGTH_SHORT).show();
}
} else {
}
}
}
}
};
private void showDevices() {
HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
while(deviceIterator.hasNext()){
UsbDevice device = deviceIterator.next();
mUsbManager.requestPermission(device, mPermissionIntent);
mLogger.log("usb", "name: " + device.getDeviceName() + ", " +
"ID: " + device.getDeviceId());
mInfo.append(device.getDeviceName() + "\n");
mInfo.append(device.getDeviceId() + "\n");
mInfo.append(device.getDeviceProtocol() + "\n");
mInfo.append(device.getProductId() + "\n");
mInfo.append(device.getVendorId() + "\n");
}
}
@Override
protected void onResume() {
super.onResume();
}
}
|
|
|
|
|
Have you tried creating a BroadcastReceiver and registering it with a ACTION_USB_DEVICE_ATTACHED filter?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Yes I tried but it only work when I plug in otg cable with usb device attached to it and enable manually otg inside app and enter app that detects it.Without usb device on otg cable it won't detect it!!!I used ACTION_USB_DEVICE_ATTACED with this app: How to Monitor USB Events on Android[^]
|
|
|
|
|