岩間と申します。
AndroidのBluetooth接続について開発初心者のため助言を頂きたいです。
よろしくお願いいたします。
現在、Bluetoothの標準サンプルであるBluetoothChatを参考にし、
SerialPortのプロファイルを使用してのファイルの送受信に成功しました。
次に、opp obexプロファイルを使用してファイルの送受信を行いたいのですが、
サーバソケットを取得するlistenUsingRfcommWithServiceRecordで
01-22 10:31:38.915: E/BluetoothChatService(8270): Socket Type: Securelisten() failed
01-22 10:31:38.915: E/BluetoothChatService(8270): java.io.IOException: Not able to register SDP record for BluetoothSecure
以上のエラーが発生しサーバソケットを取得できないため、その後の
socket = mmServerSocket.accept();
でNullPointerExceptionのエラーでアプリが落ちてしまいます。
UUIDをSerialPortからopp obexに変更する他に何か特別な処理はいるのでしょうか?
どなたか回避方法を知っている方がいればご教授願います。
該当ソースは以下です。
private static final String NAME_SECURE = "BluetoothSecure";
private static final String NAME_INSECURE = "BluetoothInsecure";
private static final UUID MY_UUID_SECURE = UUID.fromString("00001105-0000-1000-8000-00805F9B34FB");
private static final UUID MY_UUID_INSECURE= UUID.fromString("00001105-0000-1000-8000-00805F9B34FB");
public synchronized void start() {
// connenct threadの初期化
if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}
// connected threadの初期化
if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}
// Start the thread to listen on a BluetoothServerSocket
if (mSecureAcceptThread == null) {
mSecureAcceptThread = new AcceptThread(true);
mSecureAcceptThread.start();
}
if (mInsecureAcceptThread == null) {
mInsecureAcceptThread = new AcceptThread(false);
mInsecureAcceptThread.start();
}
}
private class AcceptThread extends Thread {
// ローカルサーバソケット
private final BluetoothServerSocket mmServerSocket;
private String mSocketType;
public AcceptThread(boolean secure) {
BluetoothServerSocket tmp = null;
mSocketType = secure ? "Secure" : "Insecure";
// 新しいリスニングサーバソケットを作成します。
try {
if (secure) {
tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME_SECURE, MY_UUID_SECURE);
} else {
tmp = mAdapter.listenUsingInsecureRfcommWithServiceRecord(NAME_INSECURE, MY_UUID_INSECURE);
}
} catch (IOException e) {
Log.e(TAG, "Socket Type: " + mSocketType + "listen() failed", e);
}
mmServerSocket = tmp;
}
public void run() {
if (D)
Log.d(TAG, "Socket Type: " + mSocketType
+ "BEGIN mAcceptThread" + this);
setName("AcceptThread" + mSocketType);
BluetoothSocket socket = null;
// 接続していない場合は、サーバソケットを聞く
while (mState != STATE_CONNECTED) {
try {
// これはブロッキング呼び出しであり、唯一の成功した接続または例外に戻ります
socket = mmServerSocket.accept();
} catch (IOException e) {
Log.e(TAG, "Socket Type: " + mSocketType
+ "accept() failed", e);
break;
}
// 接続が受け入れられた場合
if (socket != null) {
synchronized (BluetoothService.this) {
switch (mState) {
case STATE_LISTEN:
case STATE_CONNECTING:
// 正常の場合。接続スレッドを起動します。
connected(socket, socket.getRemoteDevice(),
mSocketType);
break;
case STATE_NONE:
case STATE_CONNECTED:
// 準備または既に接続されない場合。新しいソケットを終了します。
try {
socket.close();
} catch (IOException e) {
Log.e(TAG, "Could not close unwanted socket", e);
}
break;
}
}
}
}
if (D)
Log.i(TAG, "END mAcceptThread, socket Type: " + mSocketType);
}