Commit 4d58bf12 by onuma

133エラー発生時、10回までリトライするように修正した

parent 6f2dea66
...@@ -40,10 +40,12 @@ public class BleManagerUtil { ...@@ -40,10 +40,12 @@ public class BleManagerUtil {
// メンバー変数 // メンバー変数
public BluetoothAdapter mBluetoothAdapter; // BluetoothAdapter : Bluetooth処理で必要 public BluetoothAdapter mBluetoothAdapter; // BluetoothAdapter : Bluetooth処理で必要
private String mDeviceAddress = ""; // デバイスアドレス
public BluetoothGatt mBluetoothGatt = null; // Gattサービスの検索、キャラスタリスティックの読み書き public BluetoothGatt mBluetoothGatt = null; // Gattサービスの検索、キャラスタリスティックの読み書き
private int mBleConnectDeviceType; private int mBleConnectDeviceType;
private String mDeviceAddress = ""; // デバイスアドレス
private BluetoothDevice mDevice;
private int retryCount = 0;
private static final int RETRY_MAX = 10;
public BleManagerUtil(Context context, BleManagerUtilListener listener) { public BleManagerUtil(Context context, BleManagerUtilListener listener) {
mContext = context; mContext = context;
...@@ -59,11 +61,23 @@ public class BleManagerUtil { ...@@ -59,11 +61,23 @@ public class BleManagerUtil {
Logger.i("onConnectionStateChange status = " + status + " newState = " + newState); Logger.i("onConnectionStateChange status = " + status + " newState = " + newState);
if (newState == BluetoothProfile.STATE_CONNECTED) { if (newState == BluetoothProfile.STATE_CONNECTED) {
gatt.discoverServices(); gatt.discoverServices();
return;
} }
// デバイスと接続されていない場合のメッセージコード:133, 62 // デバイスと接続されていない場合のメッセージコード:133, 62
// デバイスと離れて応答がなく、タイムアウトになる場合:8 // デバイスと離れて応答がなく、タイムアウトになる場合:8
// デバイスと接続が切れた場合のメッセージコード:19 // デバイスと接続が切れた場合のメッセージコード:19
if (status == 133 || status == 62 || status == 8 || status == 19) { // 接続失敗 if (status == 133 || status == 62 || status == 8 || status == 19) { // 接続失敗
if (status == 133) {
if (retryCount < RETRY_MAX) {
retryCount++;
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
mBluetoothGatt = mDevice.connectGatt(mContext, false, mGattcallback, TRANSPORT_LE);
}
}, 1500);
}
}
runOnUiThread( new Runnable() { runOnUiThread( new Runnable() {
@Override @Override
public void run() { public void run() {
...@@ -257,8 +271,8 @@ public class BleManagerUtil { ...@@ -257,8 +271,8 @@ public class BleManagerUtil {
} }
// mBluetoothGattのサービスと接続 // mBluetoothGattのサービスと接続
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(deviceAddress); mDevice = mBluetoothAdapter.getRemoteDevice(deviceAddress);
if (device.getName() == null) { if (mDevice.getName() == null) {
// deviceの名称がない場合、接続エラーになるため、再スキャンすることで検知したらgetRemoteDeviceメソッドに名称がセットされる // deviceの名称がない場合、接続エラーになるため、再スキャンすることで検知したらgetRemoteDeviceメソッドに名称がセットされる
mBluetoothAdapter.getBluetoothLeScanner().startScan(mScanCallback); mBluetoothAdapter.getBluetoothLeScanner().startScan(mScanCallback);
// スキャン開始(一定時間後にスキャン停止する) // スキャン開始(一定時間後にスキャン停止する)
...@@ -271,7 +285,7 @@ public class BleManagerUtil { ...@@ -271,7 +285,7 @@ public class BleManagerUtil {
} }
}, 20000 ); }, 20000 );
} else { } else {
bleGattConnect(device); bleGattConnect(mDevice);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment