Can we distinguish Blackberry is in USB mode or charge mode? - java

I am new to blackberry-7. In my application when I plug my phone into the cable (by means of the USB cable) I want to find if my blackberry connected with USB is in charging mode or transfer mode. How is it possible?

The USB can be detected asynchronously using SystemListener2 and also synchronously using DeviceInfo.getBatteryStatus.
For what you need the battery status won't work because it probably charges in both normal and transfer modes, and then there's the problem of detecting the cable when the battery is full. So I'd try registering a SystemListener2 instance and hopefully the callback usbConnectionStateChange will be passed the flag SystemListener2.USB_STATE_CABLE_CONNECTED as argument. When it is transferring, I'd expect the flags USB_STATE_ENUMERATED and USB_STATE_MS_INTERFACE_ENUMERATED to be received in the callback as well, but I've never tested it on a real device.
And I don't know what would happen if the device is already connected when the listener is registered.
To register the listener, call
Application.getApplication().addSystemListener(mySysListener2);

Related

Communication between BLE device and different android apps on same device

I'm new in Android BLE so my question may be uncorrect or naive in some way. If this is the case please explain me where I'm wrong and kindly show me the correct way to manage this scenario.
Scenario is the following: my Android app communicates with a BLE device sending commands and getting answers from device using BLE characteristics.
Sequence is:
Device wakes up the app (the onConnectionStateChange method is called)
My app writes a command in a characteristic (I call writeCharacteristic putting the command in value parameter).
Device sends back the answer to command to my app (the onCharacteristicChanged method is triggered and value parameter contains the answer)
After waking up the app, the device doesn't do anything until a command is sent via writeCharacteristic. The device accepts different commands.
All good so far, but recently I developed a second different app to communicate with same device.
When I run both apps on same Android phone, one app sends a command to the device and the response is received by both apps! Of course the app that didn't sent the command receives an unexpected answer and goes to an unexpected status.
Ok, knowing the problem I can modify both my apps to handle this situation, but the question is: Is this behavior normal when two apps in same device communicate with same BLE device?
Is there a way for an app to establish a communication channel with a BLE device to avoid sending answer to specific commands to any other app except the one that sent the request?
My guess is that writeCharacteristic and onNotificationChanged aren't the right functions for such kind of communication, but in this case which are the alternatives?
The Bluetooth standard itself doesn't define anything how multiple apps would behave if both have a GATT connection to the same device. In the standard there is just one "GATT client".
Now both iOS and Android have taken one step further in a way that might seem unintuitive. Instead of only allowing one app at a time to communicate, multiple apps can be connected over the same GATT client to a device. The OS "multiplexes" the communication from/to the apps. The behaviour is that responses to read and write requests can only be seen by the app that made the request. So if you do readCharacteristic only that app will get the onCharacteristicRead callback. Notifications however will be delivered to both apps to the onCharacteristicChanged callback, since it wouldn't make any sense to send the notification to only one.
When you say that the "response" to a write request is the notification, that's not correct in terms of GATT terminology. The response to a write request is always empty per specification (or an error). If your peripheral emits a notification, then in your case that might be the "answer" according to your own logic, but it's not a response or any way related to your write request per the GATT specification. That's why Android can't (and shouldn't) send the notification to only one device.
I suggest that you simply ignore notifications you are not expecting. If you want to associate an "answer" to a write request, you can change your protocol to include a transaction id in both packets so they can be matched.
When I write "app" above, I really mean BluetoothGatt objects. You can call connectGatt twice with the same remote device in the same app, which will behave the same as if you connected from two different apps.

Is it possible to connect and communicate with multiple Bluetooth Low Energy sensors with an android box simultaneously?

I want to connect and stream the multiple Bluetooth low energy sensors by matching their gatt profiles. All the sensors devices are of the same type but different mac address. So I want to connect those sensors to my mobile application and stream those data to our other application through the serial port. I know the Bluetooth can connect to 7 devices at a time.
But for connectivity with Android devices, it is not stated properly and still in a dilemma. Or else is it possible to connect external Bluetooth module to the android box to connect it to multiple devices? If yes then to do this what are the changes I need to perform on my system? How many devices can be used to communicate with the Android device at the same time? Can the android box act as a master that collects all the data from the peripherals and stream via the serial port. We have completed our process for a single sensor. Or is there any other possibility to stream multiple ble sensor data to another device through serial port write. please guide me through this process.
Execute connectGatt one time for every device, which will then give you one BluetoothGatt object per device. The callback object can be the same, or a separate instance per device, it depends on the use-case but most probably the devices serve different purposes so you'll have separate specific instances per device.
As long as you have a BluetoothDevice object, you can make a GATT connection to it. You can get it either using Bluetooth device address (which can sometimes be problematic since the API misses the random / public address type bit) or by a scan. You can have one outstanding GATT operation at a time, per BluetoothGatt object, so yes you can read / write characteristics to both devices simultaneously.
Feel free to post some source code for more analysis.

How to reconnect to the network using Cordova/phoneGap/Ionic without rebooting the phone?

Is there a Cordova plugin which resets the network interface?
I'd like to create an app which runs in the background and periodically (from a remote call) does a few things:
Reboots mobile hotspot (there's a Cordova plugin for that, easy enough)
Autostarts the "monitor" software (there's a Cordova plugin for that too, easy enough)
Reboots the phone.
So think of it as a remote-controlled drone.
Rebooting turns out to be a problem as apps don't have this authority unless the phone is rooted. Rooting it is an option but I'd like to avoid it if I can as I don't know Java very well (and the app has to do a variety of other stuff as I mentioned)
When a reboot happens, the part I'm most interested in is the network reconnect at which point the phone receives a new IP. This is where, momentarily, there's no signal and no bars. So I'm thinking if I can trigger that somehow without actually rebooting the phone, I don't need to reboot the phone, as all I want is a fresh IP.
I can't find a Cordova plugin which RESETS the connection to a cell tower.
I don't care if it crashes the network interface or reboots or just sets it to a wrong value which then autocorrects - so long as the phone gets a new IP "on demand," I don't care.
Is there a Cordova plugin which resets the network interface?
I know it's possible as I've had issues with my Android where it had to reconnect; but perhaps there's no API to do so on demand.
This is going to be hard as the element assigning the IP addresses is not under your control.
Mobile networks are generally set up to have the PDP context, the connection that carries the IP connection, resilient so that they do not just reset when you temporarily lose coverage.
Hence even if you reboot the phone you may be inside the timeout window and still get the same PDP context.
In addition to this, network operators may have an IP address allocation scheme in place, sometimes in association with their NAT tables, that attempts to assign the same IP address to the same device even if it has been idle for some time.
You'll notice on WiFi also that many routers will remember the MAC address and the IP and simply assign the same IP address when they see the same device again.

Disable Bluetooth sound output

I am writing an app which monitors the volume setting of the notification stream (using a broadcast receiver for android.media.VOLUME_CHANGED_ACTION - kind of a hack I know) and when it is set to silent I would like to stop my Bluetooth headset from ringing but without disconnecting it, so that if I choose to answer the call (it vibrates or I see it ringing) I can do so from my Bluetooth headset.
So far I've tried muting the voice stream, the Bluetooth stream (by providing the int 6), setting setBluetoothScoOn(false), using stopBluetoothSco() and muting the previously mentioned streams but nothing seems to work. I've got the modify audio permission so I'm beginning to think it isn't possible.
Does anyone have an idea?
When connected on bluetooth headset, call notifications are send using HFP protocol , this protocol only internally sends certain AT commands(phone to headset) which lets headset know about the call , settings SCO ON/OFF will turn call audio to headset/phone, ringing is internal to bluetooth headset

Android Bluetooth Development - connecting to an UNPAIRED device

First off, I AM able to programmatically connect to a device via bluetooth if I have manually paired it beforehand. However, I haven't found a (user-friendly) way to connect with unpaired devices (that are discoverable, obviously), even using the unchanged sample Android Bluetooth code.
I would like to pop up the pairing dialog box that requests the user to input a passkey for the device. What happens with my code, and the sample Android Bluetooth code, is that making a socket with a discoverable bluetooth device
device.createRfcommSocketToServiceRecord(MY_UUID);
will add a notification to the notification bar at the top of the screen; however, it is pretty unlikely that the user will even notice a new notification (I know I didn't).
How would I pop up that pairing dialog box, prompting the user to enter the passkey - or better yet, supply a passkey for a particularly device automatically?
You can use BlueCove library to do that. However, it may be a bit heavier than you'd want as it requires a native component but in return gives you real access to Bluetooth on your phone.
Tim: I can see how a rogue app could abuse this mechanism but there are devices out there which don't support traditional PIN-based authentication method at all (for example the WiiMote).
I've seen a few threads on this but haven't really found a good
answer.
I am connecting my nexus 1 to a bluetooth spp server device to read
and write data.
After some experimentation I have found the following
If I offload the establishment of the connection from the UI thread
(which I would consider a good idea) the pairing dialog (enter pin) is
not displayed - rather a notification item appears which the user must
click to pair the devices.
If I establish the connection from within the UI thread then the
paring dialog will come to the foreground but obviously there is a
small ugly UI pause while waiting for this.
In either case the request to establish the connection is timing out
before the user (me) can enter the pin with an IOException Service
discovery failed.
My ideal would be that the pairing dialog comes to the front (without
have to block my UI thread) and that the connection attempt would not
time out while waiting on pairing entry.
Has anyone else come up with a way to make this work?

Categories

Resources