(Ubuntu-12.04、64ビット)ラップトップから、引数としてフレンドリ名が指定されているBluetooth電話にメッセージを送信する必要があります。この電話は、以前にラップトップに接続/ペアリングされている場合とされていない場合があります。ここに示すJavaコードを使用して、ラップトップからBluetoothデバイスにメッセージを送信しています。
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を返します。
では、以前にラップトップとペアリングされていなかったそのような電話にメッセージを送信するにはどうすればよいですか?