Commit 33487e93 by Lee Jaebin

端末の再起動時、ble通信ができない問題対応(検証)

parent 55311ee9
......@@ -9,8 +9,12 @@ import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanResult;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.widget.Toast;
import java.nio.ByteBuffer;
......@@ -20,7 +24,9 @@ import java.util.UUID;
import jp.agentec.abook.abv.bl.common.Constant.DeviceType;
import jp.agentec.abook.abv.bl.common.log.Logger;
import jp.agentec.abook.abv.bl.dto.BluetoothPairingDeviceInfoDto;
import jp.agentec.abook.abv.launcher.android.R;
import jp.agentec.abook.abv.ui.common.activity.ABVActivity;
import jp.agentec.adf.util.StringUtil;
import static android.bluetooth.BluetoothDevice.TRANSPORT_LE;
......@@ -30,8 +36,8 @@ public class BleManagerUtil {
private final String TAG = "BleManagerUtil";
private BluetoothManager bluetoothManager;
private Context context;
private BleManagerUtilListener listener;
private Context mContext;
private BleManagerUtilListener mListener;
// 定数(Bluetooth LE Gatt UUID)
// Private Service
......@@ -45,10 +51,74 @@ public class BleManagerUtil {
public BluetoothAdapter mBluetoothAdapter; // BluetoothAdapter : Bluetooth処理で必要
public BluetoothGatt mBluetoothGatt = null; // Gattサービスの検索、キャラスタリスティックの読み書き
private int mBleConnectDeviceType;
private Handler mHandler;
public BleManagerUtil(Context context, BleManagerUtilListener listener) {
this.context = context;
this.listener = listener;
mContext = context;
mListener = listener;
mHandler = new Handler();
}
// スキャンコールバック
private final ScanCallback mScanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, final ScanResult result) {
super.onScanResult(callbackType, result);
runOnUiThread(new Runnable() {
@Override
public void run() {
BluetoothDevice device = result.getDevice();
Logger.d(TAG, "--scaning : " + device.getName() );
if (device.getName() != null) {
BluetoothPairingDeviceInfoDto dto = ((ABVActivity)mContext).getABVApplication().getABVUIDataCache().getPairingBluetoothDeviceInfo(mBleConnectDeviceType);
if (dto == null || dto.deviceAddress == null) {
// 異常と見做し、stopScanでエラーを返す。
stopScan(null);
} else {
if (device.getName().equals(dto.deviceName)) {
stopScan(dto.deviceAddress);
}
}
}
}
});
}
// スキャンに失敗
@Override
public void onScanFailed(final int errorCode) {
super.onScanFailed(errorCode);
Logger.e(TAG, "scan failed errorCode : " + errorCode);
runOnUiThread( new Runnable() {
@Override
public void run() {
mListener.onConnectionError(errorCode);
}
});
}
};
/**
* スキャンを中止
*/
private void stopScan(String deviceAddress) {
Logger.d(TAG, "stop ble scan");
// 一定期間後にスキャン停止するためのHandlerのRunnableの削除
mHandler.removeCallbacksAndMessages( null );
if (StringUtil.isNullOrEmpty(deviceAddress)) {
// deviceAddressが見つからなかったと見做し、エラーで返す。
mListener.onConnectionError(-1);
return;
}
// スキャン停止
mBluetoothAdapter.getBluetoothLeScanner().stopScan(mScanCallback);
// ble接続
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(deviceAddress);
bleGattConnect(device);
}
// BluetoothGattコールバック
......@@ -60,17 +130,18 @@ public class BleManagerUtil {
final int fStatus = status;
// デバイスと接続されていない場合のメッセージコード:133, 62
// デバイスと接続が切れた場合のメッセージコード:19
Logger.e("-^--onConnectionStateChange status = " + status);
Logger.e("-^--onConnectionStateChange newState = " + newState);
if (status == 133 || status == 62) { // 接続失敗
runOnUiThread( new Runnable() {
@Override
public void run() {
Logger.e("onConnectionStateChange status = " + fStatus);
listener.onConnectionError(fStatus);
mListener.onConnectionError(fStatus);
}
});
return;
}
} else {
// 通信失敗ではない
if( status == BluetoothGatt.GATT_SUCCESS && BluetoothProfile.STATE_CONNECTED == newState ) {
// 接続完了
if (!mBluetoothGatt.discoverServices()) { // サービス検索
......@@ -78,7 +149,7 @@ public class BleManagerUtil {
runOnUiThread( new Runnable() { // 接続失敗
@Override
public void run() {
listener.onGetDeviceInfoFailed(fStatus);
mListener.onGetDeviceInfoFailed(fStatus);
Logger.e("onConnectionStateChange2 status = " + fStatus);
}
});
......@@ -87,16 +158,13 @@ public class BleManagerUtil {
@Override
public void run() {
// 接続成功
listener.onConnectionState();
mListener.onConnectionState();
}
} );
return;
}
if( BluetoothProfile.STATE_DISCONNECTED == newState ) { // 切断完了(接続可能範囲から外れて切断された)
} else if( BluetoothProfile.STATE_DISCONNECTED == newState ) { // 切断完了(接続可能範囲から外れて切断された)
// 切断が発生する場合、Bluetoothと接続を切断する。
disconnect();
return;
}
}
}
......@@ -145,7 +213,7 @@ public class BleManagerUtil {
public void run() {
// 芯温計の温度を渡す。
// 端末と接続時に呼ばれるのでコメント処理
// listener.onGetDeviceInfo(strTemperature);
// mListener.onGetDeviceInfo(strTemperature);
}
});
......@@ -169,7 +237,7 @@ public class BleManagerUtil {
return;
}
// 芯温計の温度を渡す。
listener.onGetDeviceInfo(strTemperature);
mListener.onGetDeviceInfo(strTemperature);
}
});
return;
......@@ -204,8 +272,9 @@ public class BleManagerUtil {
* @param deviceAddress デバイスアドレス
*/
public void connect(int connectTargetDeviceType, String deviceAddress) {
// デバイスタイプセット
// デバイス情報セット
mBleConnectDeviceType = connectTargetDeviceType;
setUUID();
if(StringUtil.isNullOrEmpty(deviceAddress)) { // deviceAddressが空の場合は処理しない
return;
......@@ -216,13 +285,31 @@ public class BleManagerUtil {
}
// mBluetoothGattのサービスと接続
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice( deviceAddress );
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(deviceAddress);
Logger.i(TAG, "----------device name : " + device.getName());
if (device.getName() == null) {
// deviceの名称がない場合、接続エラーになるため、再スキャンすることで検知したらgetRemoteDeviceメソッドに名称がセットされる
mBluetoothAdapter.getBluetoothLeScanner().startScan(mScanCallback);
// スキャン開始(一定時間後にスキャン停止する)
mHandler.postDelayed( new Runnable() {
@Override
public void run() {
// 20秒後に呼ばれた時はスキャンで端末を取得できなかったと見做す。
mListener.onConnectionError(-1);
Logger.d(TAG, "scan in 20 sec");
}
}, 20000 );
} else {
bleGattConnect(device);
}
}
private void bleGattConnect(BluetoothDevice device) {
// GATT BLEを利用する時Androidのバージョン「23」をチェックしてGATTと接続する。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mBluetoothGatt = device.connectGatt( context, false, mGattcallback, TRANSPORT_LE);
mBluetoothGatt = device.connectGatt(mContext, false, mGattcallback, TRANSPORT_LE);
} else {
mBluetoothGatt = device.connectGatt( context, false, mGattcallback );
mBluetoothGatt = device.connectGatt(mContext, false, mGattcallback );
}
}
......@@ -238,15 +325,15 @@ public class BleManagerUtil {
// ①「ユーザーの意思による切断」は、mBluetoothGattオブジェクトを解放する。再接続は、オブジェクト構築から。
// ②「接続可能範囲から外れた切断」は、内部処理でmBluetoothGatt.disconnect()処理が実施される。
// 切断時のコールバックでmBluetoothGatt.connect()を呼んでおくと、接続可能範囲に入ったら自動接続する。
// mBluetoothGatt.close();
mBluetoothGatt.disconnect();
mBluetoothGatt.close();
mBluetoothGatt = null;
runOnUiThread( new Runnable() {
@Override
public void run() {
// 切断トーストメッセージを表示する。
listener.onDisConnectionState();
mListener.onDisConnectionState();
}
} );
}
......@@ -290,11 +377,11 @@ public class BleManagerUtil {
// Bluetoothアダプタの取得処理
public void startDeviceInfo() {
// Bluetoothアダプタの取得
BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService( Context.BLUETOOTH_SERVICE );
BluetoothManager bluetoothManager = (BluetoothManager) mContext.getSystemService( Context.BLUETOOTH_SERVICE );
if (bluetoothManager != null) {
mBluetoothAdapter = bluetoothManager.getAdapter();
if( mBluetoothAdapter == null ) { // Android端末がBluetoothをサポートしていない
Toast.makeText( context, R.string.bluetooth_is_not_supported, Toast.LENGTH_SHORT ).show();
Toast.makeText(mContext, R.string.bluetooth_is_not_supported, Toast.LENGTH_SHORT ).show();
return;
}
} else {
......
......@@ -206,7 +206,7 @@ public class ABVCheckContentViewActivity extends ABVContentViewActivity {
bleManagerUtil = new BleManagerUtil(this, new BleManagerUtil.BleManagerUtilListener() {
@Override
public void onConnectionError(int status) { //bluetooth接続エラー
Logger.e(TAG, "onConnectionError");
Logger.e(TAG, "onConnectionError status : " + status);
// bluetoothのデバイスタイプ(中心温度計・放射温度計)
int bluetoothDeviceType = bleManagerUtil.getBluetoothDeviceType();
// タイプによってメッセージ内容をセット
......@@ -215,7 +215,11 @@ public class ABVCheckContentViewActivity extends ABVContentViewActivity {
} else if (bluetoothDeviceType == DeviceType.radiationThermomete) {
errorAfterAbookCheckAip(String.format(getString(R.string.msg_bluetooth_connect_error), getString(R.string.radiation_thermometer)));
}
// 通信切断
bleManagerDisconnect();
// ダイアログを閉じる
dismissWaitngDialog();
}
@Override
......@@ -998,6 +1002,10 @@ public class ABVCheckContentViewActivity extends ABVContentViewActivity {
protected void onStop() {
super.onStop();
Logger.d(TAG, "--onStop");
// bluetooth通信されていたら切断
bleManagerDisconnect();
// SPP通信の接続を切る
disConnectSppBluetooth();
......
......@@ -69,7 +69,9 @@ public class BlePairingSettingActivity extends ABVUIActivity {
@Override
public void run() {
BluetoothDevice device = result.getDevice();
if (device.getName() != null) {
Logger.d("mScanCallback device.getName() = " + device.getName());
}
// 識別商品名に絞る
if(device.getName() != null && (device.getName().startsWith(CENTER_THERMOMETE_DEVICE_NAME) || device.getName().startsWith(RADIATION_THERMOMETE_DEVICE_NAME))) {
if (!mSavedDeviceAddressList.contains(device.getAddress())) { //登録されたデバイスの場合、スキャン情報から除外する。
......@@ -251,8 +253,6 @@ public class BlePairingSettingActivity extends ABVUIActivity {
scanner.startScan(mLeScanCallback);
}
// スキャン開始(一定時間後にスキャン停止する)
mHandler.postDelayed( new Runnable() {
@Override
......
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