Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
New Developer
yugandhar_pathi
Posts: 11
Registered: ‎11-22-2012
My Device: Bold 9900
Accepted Solution

How to register for an event when device is connected to PC in USB mode or sync mode

I am developing application on blackberry using phonegap.

I want to listen for event when blackberry device is connected to PC in USB MODE or sync mode.
I saw mediagallery and Filemanager applications showing dailog that application cannot be used when connected to PC in USB mode.
I want to show similar dialog in my application.
Can someone please help on how to do this.

 

I saw blackberry webworks API

static Boolean blackberry.system.isMassStorageActive ()

which tells if massstorage is active or not.

 

But i can't keep on query this API every time. I am looking for an API which gives me callback.

Can some one please help.

BlackBerry Development Advisor
oros
Posts: 1,557
Registered: ‎04-12-2010
My Device: BlackBerry Z10

Re: How to register for an event when device is connected to PC in USB mode or sync mode

On the Java side, this can be done by implementing a FileSystemListener and the rootChanged method:

 

public void rootChanged(final int state, String rootName) {
	if (rootName.equalsIgnoreCase("sdcard/")) {
		if (state == FileSystemListener.ROOT_ADDED) {
			/* SD Card is available. */
		} else if (state == FileSystemListener.ROOT_REMOVED) {
			/* SD Card is not available. */
		}
	}
}

I have not been able to find a corresponding WebWorks API or community API though, so I believe you will need to create your own custom extension to implement this listener functionality.
https://developer.blackberry.com/html5/documentation/using_javascript_extensions_1866976_11.html


Erik Oros | @WaterlooErik | eoros@blackberry.com | Developer Issue Tracker

If a reply has answered your question, please click Accept as Solution to help other visitors in the future.
New Developer
yugandhar_pathi
Posts: 11
Registered: ‎11-22-2012
My Device: Bold 9900

Re: How to register for an event when device is connected to PC in USB mode or sync mode

Hi,

 

Thanks for your kind response.

When i connect my device to PC in USB mode will FileSystem give mount/unmount events ?

I just want to confirm becuase i tried solution given by you. it doesn't seem to work for me.

Please help.

 

Yugandhar.P

@Bangalore,India

 

BlackBerry Development Advisor
oros
Posts: 1,557
Registered: ‎04-12-2010
My Device: BlackBerry Z10

Re: How to register for an event when device is connected to PC in USB mode or sync mode

The above won't necessarily give USB connection status, but will fire notices on SD card availability. This means that if someone plugs in USB and goes into mass storage mode, then the SD Card root will become unavailable and the above event will trigger.

This will not trigger an event if the USB is connected in "charging" mode though as the SD Card remains available there.

Using SystemListener2, there is a usbConnectionStateChange function:
http://www.blackberry.com/developers/docs/7.1.0api/net/rim/device/api/system/SystemListener2.html

You could use this to listen to more generic changes. But you would need the first method to monitor SD Card availability (i.e. even if the USB is connected, there may be a delay before Mass Storage Mode is entered, if at all.)

In both cases though, a custom extension would be needed.

Erik Oros | @WaterlooErik | eoros@blackberry.com | Developer Issue Tracker

If a reply has answered your question, please click Accept as Solution to help other visitors in the future.
New Developer
yugandhar_pathi
Posts: 11
Registered: ‎11-22-2012
My Device: Bold 9900

Re: How to register for an event when device is connected to PC in USB mode or sync mode

Thanks a lot. Thanks a ton!!

Your solution for Listening to FileSystemListener worked for me.I was not knowing how to register the listener , i found out that some how (FileSystemRegistry.addFileSystemListener(rootListener) ).

I have one more issue now! :smileysad:  I want to know if memory card is present in the device slot or not.

I tried using FileSystemRegistry.listRoots() this will return only system/ even if memory card is present in the slot but phone is connected to PC in USB Drive mode.

Is there any way to detect this low level information? please help. I feel you are the best person.