我必须从我的(Ubuntu-12.04,64位)笔记本电脑发送消息到蓝牙手机,其友好名称作为参数提供。此手机可能已经或可能没有早先与笔记本电脑连接/配对。我正在使用此处所示的Java代码将笔记本电脑中的消息发送到蓝牙设备。
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.");
}
}
}
仅在设备之前已配对时才有效。否则,它会失败,如下所示。实际上,如果PIN算数并且我说是,我会在手机上收到提示。然后,这个失败在下面。
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
我尝试了各种身份验证和加密选项;但是,没有运气。
即使设备已经配对,我也注意到了另一种我不理解的行为。如果设备已成功配对,我原以为我可以使用retrieveDevices(DiscoveryAgent.CACHED)
方法或retrieveDevices(DiscoveryAgent.PREKNOWN)
方法。两者都返回null。
那么,我如何向之前未与笔记本电脑配对的手机发送消息?