I am developing an application which uses Bluetooth to connect to a device and send/receive data. I am doing all of my testing with a Nexus One phone.
I have never been able to establish a SPP (serial port) connection from my phone to any device. However, I have been able to connect from a device (my laptop) to my phone using a Mac equivalent of PuTTY (The only exception to this is the "Bluetooth File Transfer" app from the Marketplace seems to work, but I don't think that uses RFCOM/SPP...).
I keep seeing this message in my LogCat logs:
ERROR/BluetoothService.cpp(78): stopDiscoveryNative: D-Bus error in StopDiscovery: org.bluez.Error.Failed (Invalid discovery session)
as well as these:
java.io.IOException: Operation Canceled
java.io.IOException: Software caused connection abort
I have tried using the UUID of "00001101-0000-1000-8000-00805F9B34FB" and I have also tried using the:
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
sock = (BluetoothSocket) m.invoke(device, Integer.valueOf(1));
method instead of device.createRfcommSocketToServiceRecord(UUID); as well--with no luck.
I am using the BluetoothChat example and variations of that code to do all of my testing...
Solutions or suggestions would be great...or even a better/less complex example of some testing code I can run on the phone, or a python script or something I can run on my computer to help debug?
Thanks! I hope this isn't a bug with the Android OS, but if it is I hope to find a workaround.
EDIT: I should also note that most devices show up as "paired, but not connected" in the Bluetooth settings.
EDIT 2: The solution seems to be simply disabling any Bluetooth listening. See my answer post for more information.
The solution, as it turns out, was to disable the server functionality of the Bluetooth service. By only using createRfcommSocketToServiceRecord and never calling listenUsingRfcommWithServiceRecord (in the BluetoothChat example this means never starting the "AcceptThread") the problem was fixed.
Even though these two calls are supposed to be totally separated and have no affect on each other (according to the Android docs), simply commenting out listenUsingRfcommWithServiceRecord fixed my supposedly unrelated issue.
I can take the Bluetooth Chat program unedited and it will not be able to establish an outgoing connection to ANY bluetooth device I have tested (laptops, desktops, headsets, etc.), but if I remove that one thing it works flawlessly as a client.
Anyway, I hope this will help someone else if they come across the same issue. This must be a bug with the Android OS, or possibly the firmware on the Nexus One.
I would ignore the stopDiscovery error - its good that you're cancelling discovery before making your connection. Per the SDK docs:
Because discovery is a heavyweight
precedure for the Bluetooth adapter,
this method should always be called
before attempting to connect to a
remote device with connect().
Discovery is not managed by the
Activity, but is run as a system
service, so an application should
always call cancel discovery even if
it did not directly request a
discovery, just to be sure.
So with that said, were you able to get the Bluetooth Chat example to work before you made any modifications to the code?
The UUID you want for SPP/RFCOMM is:
static UUID UUID_RFCOMM_GENERIC = new UUID(0x0000110100001000L,0x800000805F9B34FBL);
or defined another way (both accomplish the same thing).
static final UUID UUID_RFCOMM_GENERIC = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
Related
I was searching a way to bypass the WPS dialog while two devices are connecting via wifi p2p. Right now, everytime when two devices are trying to connect with each other, I have to choose one of the WPS methods. Yet I don't want any of these.
I am aiming to connect multiple device via wifip2p without notify users, as long as they are running my program.
PS: only via wifip2p, not via Wifi.
Please, any help will be great.
I'm working on an app on Android that is supposed to connect to a custom device via Bluetooth, but in order to emulate that device (which I don't have yet), I made a simulator on PC, using Java's Bluecove.
However, I'm having a hard time figuring out the different connection's method.
First, on Android, we discover the device (here, a PC), then:
-We have the "UUID" solution, as explain in android sdk doc.
-Or the "Port" solution, as proposed here.
The second one seems more natural to me, as I don't quite understand what is a UUID in this scenario.
I do understand that we need to connect to a port of a device, but what does the UUID have to do with that? Why don't we need to specify it in the second solution?
If someone could give me a clue, that would be greatly appreciated :)
bluetooth communication is communication with service, so when you wanna connect to device you are attempting to connect to service,and uuid is number with which we connect to service, like ok, i wanna this uuid(this service).
my apologies if this is a trivial question.
I've recently begun doing some android programming and I'm writing a simple app that allows you to use your android device as a controller for your windows PC. Specifically it allows the user to do things like turn off the machine, make it sleep, reboot it etc etc. I'm currently using a python library called CherryPy as a server on the windows machine to execute the actual win32api calls to perform the desired function. What i'm not sure about is how to discover (dynamically) which machine on the network is actually hosting the server. Everything is working fine if I hardcode my machines public IP into the android app, but obviously that is far less than ideal. I've considered having the user manually enter their machines public IP in the app, but if there's a way to, say, broadcast a quick message to all machines on the WiFi and check for a pre-canned response that my Python server would send out, that'd be wonderful. Is that possible?
Thanks in advance guys.
Try sending a UDP packet to the special broadcast address 255.255.255.255. Every device in the network should receive a copy of that packet (barring firewalls), and you can arrange to have the server reply to the packet with its identity.
Write your app using the alljoyn framework. AllJoyn will handle these details for you.
I am developing an app where i am using browser field to load html files in it. My code is as follows.
Main.java
//pushing screen to browser field page..
public Main()
{
// Push a screen onto the UI stack for rendering.
pushScreen(new WebViewController());
}
WebViewController.java
BrowserFieldConfig bfConfig = new BrowserFieldConfig();
bfConfig.setProperty(BrowserFieldConfig.NAVIGATION_MODE,
BrowserFieldConfig.NAVIGATION_MODE_POINTER);
bfConfig.setProperty(BrowserFieldConfig.JAVASCRIPT_ENABLED,
Boolean.TRUE);
bfConfig.setProperty(BrowserFieldConfig.ALLOW_CS_XHR, Boolean.TRUE);
bfConfig.setProperty(BrowserFieldConfig.INITIAL_SCALE, new Float(0.0));
bwf = new BrowserField(bfConfig);
add(bwf);
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
// TODO Auto-generated method stub
bwf.requestContent("local:///html/index.html");
}
}, 500, false);
All my server code & UI design is in javascript & html pages respectively which invokes from index.html page...
As per the following code it is working(executing) in simulator & getting data from server during runtime. But when i am running in BlackBerry device only UI design is loading from index.html page but not getting the data from server. I am not getting any idea whats the error is.
I enabled javascript in Blackberry device also but still it is not working..
As i am new to this blackberry developing, unable to find whats there problem with my app while running in Blackberry device.
As per the following code should i have to add any external code to get data from server (like just to access internet in Blackberry device after signing)
In simulator it is working good & getting data from server. When executing in Blackberry device only main page UI (UI in index.html) is loading but not getting data from server.
Can anyone help me with this.....
This question is variation of questions "Why I have issues with connection on the BlackBerry". I'll try to give you idea how to solve them.
The network connection is much complex unlike j2me, android or iphone connection where you mostly ask system to open TCP/HTTP/UDP connection. RIM introduced Network Transports. I'm not sure about reasons - or they wanted to give developer power to select specific transport, or because BES admins/Carriers could restrict some connections, or other reasons. But side effect that BB developer has to specify which transport he wants to use for current connection on the device. The dramatic thing that default connection is Direct TCP (through the APN carrier settings). And direct TCP works perfect on simulators. But most used connection on the BB devices are BIS-B and WiFi. Also BIS-B was unavailable for usual developer before and they just opened it for everyone recently.
Before 5.0 OS developer had huge amount of code to determine what transports are available on the device and there was loop by transports trying to open in in order.
RIM introduced ConnectionFactory in 5.0 OS which provides standard mechanism to open determine available transports and open the connection (example).
You always have to use ConnectionFactory where you want to open connection. It doesn't matter if you reach remote host directly through Connector or through BrowserField. You have to use ConnectionFactory.
Dramatic thing is that even if you use it you can't reach BIS-B transport. As I mentioned before it was available only for alliance members. RIM doesn't restrict using of BIS-B for any developers right now. But I don't see that they also give any example. To allow ConnectionFactory to open BIS-B you need next line:
connFact.setTransportTypeOptions(TransportInfo.TRANSPORT_BIS_B,
new BisBOptions("mds-public"));
This is just small summary of connections on the BlackBerries. But it should give you right direction to solve your problem.
In my android application I am querying a database. I am only able to query the database if the phone is connected to the internal network, i.e: by being connected to company WiFi, or by using VPN connection.
I would like to check this connection first - maybe by trying to 'ping' the server first? Is this possible?
Thanks.
Just try to connect to the network/database/ip and handle any exception thrown b/c the network is unreachable or b/c of a time out.
You can also try InetAddress which has an isReachable() method.
You could consider using android.net.ConectivityManager if you really want to check the network status.
ConectivityManager.requestRouteToHost() can be used to ensure a server is reachable (although doubts have been raised about its reliability - mentioned in CommonsWare answer here : how to mange wifi internet connectivity in android? )
Alternatively, if you just want to check WIFI is connected, try
ConectivityManager.getNetworkInfo(ConectivityManager.TYPE_WIFI).isConnected()
Couple of caveats :
1) I'm typing this in a browser not an IDE, so it might have syntax errors,
2) You'll have to make that code much more defensive for production, and
3) All connectivity tests in ConectivityManager can be unreliable if networks are currently becoming available or unavailable.
Hope this covers what you were asking.