0
I行う既にペアリングデバイスに接続するには、次アンドロイドブルートゥース時には接続しないでください
private boolean tryConnectDifferentUuid() {
ParcelUuid[] supportedUuids = mDevice.getUuids();
if (supportedUuids != null){
for (ParcelUuid Uuid : supportedUuids) {
if (connectSecureRfCom(Uuid.toString())) {
return true;
} else if (connectInsecureRfCom(Uuid.toString())) {
return true;
}
}
}
return false;
}
private boolean connectSecureRfCom(String UuidString) {
try {
Log.d(TAG_BT, "connectSecureRfCom ");
mmSocket = mDevice.createRfcommSocketToServiceRecord(UUID.fromString(UuidString));
mmSocket.connect();
Log.d(TAG_BT, "Connected");
return true;
} catch (Exception e) {
Log.e(TAG_BT, "Couldn't establish Bluetooth connection!", e);
return false;
}
}
private boolean connectInsecureRfCom(String UuidString) {
try {
Log.d(TAG_BT, "connectInsecureRfCom ");
mmSocket = mDevice.createInsecureRfcommSocketToServiceRecord(UUID.fromString(UuidString));
mmSocket.connect();
Log.d(TAG_BT, "Connected");
return true;
} catch (Exception e) {
Log.e(TAG_BT, "Couldn't establish Bluetooth connection!", e);
return false;
}
}
private boolean connectCreateRfcommSocket() {
try {
Log.d(TAG_BT, "connectSecureRfComInvoke ");
mmSocket =(BluetoothSocket) mDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class}).invoke(mDevice,1);
mmSocket.connect();
Log.d(TAG_BT, "Connected");
return true;
} catch (Throwable e) {
Log.e(TAG_BT, "Couldn't establish Bluetooth connection!", e);
return false;
}
}
private boolean connectCreateRfcommSocketToServiceRecordInvoke() {
try {
Log.d(TAG_BT, "createRfcommSocketToServiceRecord ");
mmSocket =(BluetoothSocket) mDevice.getClass().getMethod("createRfcommSocketToServiceRecord", new Class[] {int.class}).invoke(mDevice,1);
mmSocket.connect();
Log.d(TAG_BT, "Connected");
return true;
} catch (Throwable e) {
Log.e(TAG_BT, "Couldn't establish Bluetooth connection!", e);
return false;
}
}
問題は、それが時々ある:ここでは
if (connectCreateRfcommSocketToServiceRecordInvoke()) {
Log.d(TAG_BT, "Connection created 1");
connected = true;
} else if (connectCreateRfcommSocket()) {
Log.d(TAG_BT, "Connection created 2");
connected = true;
} else if (connectSecureRfCom(UUID_DEFAULT)) {
Log.d(TAG_BT, "Connection created 3");
connected = true;
} else if (connectInsecureRfCom(UUID_DEFAULT)) {
Log.d(TAG_BT, "Connection created 4");
connected = true;
}else if (tryConnectDifferentUuid()) {
Log.d(TAG_BT, "Connection created 5");
connected = true;
}
が呼び出される関数の定義でありますアプリケーションを開いたり閉じたり、Bluetoothを接続/切断したりしても何度も連続して動作しますが、次の出力ではうまく動作しません:
java.io.IOException: read failed, socket might closed or timeout, read ret: -1