Reputation: 2481
I have to send message from my (Ubuntu-12.04, 64-bit) laptop to a Bluetooth phone whose friendly name is provided as an argument. This phone may or may not have connected/paired with the laptop earlier. I am using the Java code as shown here to send a message from my laptop to the Bluetooth device.
package com.test.bt;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.UUID;
public class BluetoothTest {
/**
* @param args
* @throws InterruptedException
* @throws IOException
* @throws UnsupportedEncodingException
*/
public static void main(String[] args) throws InterruptedException,
BluetoothStateException, UnsupportedEncodingException, IOException {
BluetoothDiscoveryListener bdl = new BluetoothDiscoveryListener();
LocalDevice ld = LocalDevice.getLocalDevice();
DiscoveryAgent da = ld.getDiscoveryAgent();
da.startInquiry(DiscoveryAgent.GIAC, bdl);
bdl.doLock();
UUID[] uuidSet = new UUID[1];
uuidSet[0] = new UUID(0x1105); // OBEX Object Push service
int[] attrSet = new int[] { 0x0100 }; // Service name
boolean deviceFound = false;
for (RemoteDevice rd : bdl.remoteDevices) {
if (rd.getFriendlyName(false).matches(args[0])) {
deviceFound = true;
da.searchServices(attrSet, uuidSet, rd, bdl);
bdl.doLock();
bdl.sendMessage("Bluetooth.txt", "text", args[1]);
}
}
if (!deviceFound) {
System.err.println("Requested device was not found.");
}
}
}
It works only when the device has formerly paired. Else, it fails as shown below. Actually, I get a prompt on my phone if the PIN mathces and I say Yes. And, then, this failure below.
BlueCove version 2.1.0 on bluez
Device Phone found at address 101DC0F72211
MAJOR device class 512
MINOR device class 4
MAJOR service classes 5898240
Inquiry completed successfully.
Following services are found ...
Service name : OPP
URL is : btgoep://101DC0F72211:3;authenticate=false;encrypt=false;master=false
Services search completed successfully.
Connecting to btgoep://101DC0F72211:3;authenticate=false;encrypt=false;master=false
Exception in thread "main" java.io.IOException: Failed to connect. [115] Operation now in progress
at com.intel.bluetooth.BluetoothStackBlueZ.connectionRfOpenClientConnectionImpl(Native Method)
at com.intel.bluetooth.BluetoothStackBlueZ.connectionRfOpenClientConnection(BluetoothStackBlueZ.java:560)
at com.intel.bluetooth.BluetoothRFCommClientConnection.<init>(BluetoothRFCommClientConnection.java:37)
at com.intel.bluetooth.MicroeditionConnector.openImpl(MicroeditionConnector.java:387)
at com.intel.bluetooth.MicroeditionConnector.open(MicroeditionConnector.java:162)
at javax.microedition.io.Connector.open(Connector.java:83)
at com.test.bt.BluetoothDiscoveryListener.sendMessage(BluetoothDiscoveryListener.java:106)
at com.test.bt.BluetoothTest.main(BluetoothTest.java:52)
BlueCove stack shutdown completed
I tried variation of authentication and encryption options; but, no luck.
Even when the device had already paired, I noticed another behavior that I am not understanding. If the device had paired successfully, I was thinking I could use the retrieveDevices(DiscoveryAgent.CACHED)
method or the retrieveDevices(DiscoveryAgent.PREKNOWN)
method. Both return null.
So, how do I send a message to such a phone that hadn't earlier paired with the laptop ?
Upvotes: 1
Views: 1357