Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
abook_check
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abook_android
abook_check
Commits
33487e93
You need to sign in or sign up before continuing.
Commit
33487e93
authored
Jan 24, 2020
by
Lee Jaebin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
端末の再起動時、ble通信ができない問題対応(検証)
parent
55311ee9
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
123 additions
and
28 deletions
+123
-28
ABVJE_UI_Android/src/jp/agentec/abook/abv/cl/util/BleManagerUtil.java
+112
-25
ABVJE_UI_Android/src/jp/agentec/abook/abv/ui/common/activity/ABVCheckContentViewActivity.java
+9
-1
ABVJE_UI_Android/src/jp/agentec/abook/abv/ui/home/activity/BlePairingSettingActivity.java
+2
-2
No files found.
ABVJE_UI_Android/src/jp/agentec/abook/abv/cl/util/BleManagerUtil.java
View file @
33487e93
...
@@ -9,8 +9,12 @@ import android.bluetooth.BluetoothGattDescriptor;
...
@@ -9,8 +9,12 @@ import android.bluetooth.BluetoothGattDescriptor;
import
android.bluetooth.BluetoothGattService
;
import
android.bluetooth.BluetoothGattService
;
import
android.bluetooth.BluetoothManager
;
import
android.bluetooth.BluetoothManager
;
import
android.bluetooth.BluetoothProfile
;
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.content.Context
;
import
android.os.Build
;
import
android.os.Build
;
import
android.os.Handler
;
import
android.widget.Toast
;
import
android.widget.Toast
;
import
java.nio.ByteBuffer
;
import
java.nio.ByteBuffer
;
...
@@ -20,7 +24,9 @@ import java.util.UUID;
...
@@ -20,7 +24,9 @@ import java.util.UUID;
import
jp.agentec.abook.abv.bl.common.Constant.DeviceType
;
import
jp.agentec.abook.abv.bl.common.Constant.DeviceType
;
import
jp.agentec.abook.abv.bl.common.log.Logger
;
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.launcher.android.R
;
import
jp.agentec.abook.abv.ui.common.activity.ABVActivity
;
import
jp.agentec.adf.util.StringUtil
;
import
jp.agentec.adf.util.StringUtil
;
import
static
android
.
bluetooth
.
BluetoothDevice
.
TRANSPORT_LE
;
import
static
android
.
bluetooth
.
BluetoothDevice
.
TRANSPORT_LE
;
...
@@ -30,8 +36,8 @@ public class BleManagerUtil {
...
@@ -30,8 +36,8 @@ public class BleManagerUtil {
private
final
String
TAG
=
"BleManagerUtil"
;
private
final
String
TAG
=
"BleManagerUtil"
;
private
BluetoothManager
bluetoothManager
;
private
BluetoothManager
bluetoothManager
;
private
Context
c
ontext
;
private
Context
mC
ontext
;
private
BleManagerUtilListener
l
istener
;
private
BleManagerUtilListener
mL
istener
;
// 定数(Bluetooth LE Gatt UUID)
// 定数(Bluetooth LE Gatt UUID)
// Private Service
// Private Service
...
@@ -45,10 +51,74 @@ public class BleManagerUtil {
...
@@ -45,10 +51,74 @@ public class BleManagerUtil {
public
BluetoothAdapter
mBluetoothAdapter
;
// BluetoothAdapter : Bluetooth処理で必要
public
BluetoothAdapter
mBluetoothAdapter
;
// BluetoothAdapter : Bluetooth処理で必要
public
BluetoothGatt
mBluetoothGatt
=
null
;
// Gattサービスの検索、キャラスタリスティックの読み書き
public
BluetoothGatt
mBluetoothGatt
=
null
;
// Gattサービスの検索、キャラスタリスティックの読み書き
private
int
mBleConnectDeviceType
;
private
int
mBleConnectDeviceType
;
private
Handler
mHandler
;
public
BleManagerUtil
(
Context
context
,
BleManagerUtilListener
listener
)
{
public
BleManagerUtil
(
Context
context
,
BleManagerUtilListener
listener
)
{
this
.
context
=
context
;
mContext
=
context
;
this
.
listener
=
listener
;
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コールバック
// BluetoothGattコールバック
...
@@ -60,17 +130,18 @@ public class BleManagerUtil {
...
@@ -60,17 +130,18 @@ public class BleManagerUtil {
final
int
fStatus
=
status
;
final
int
fStatus
=
status
;
// デバイスと接続されていない場合のメッセージコード:133, 62
// デバイスと接続されていない場合のメッセージコード:133, 62
// デバイスと接続が切れた場合のメッセージコード:19
// デバイスと接続が切れた場合のメッセージコード:19
Logger
.
e
(
"-^--onConnectionStateChange status = "
+
status
);
Logger
.
e
(
"-^--onConnectionStateChange newState = "
+
newState
);
if
(
status
==
133
||
status
==
62
)
{
// 接続失敗
if
(
status
==
133
||
status
==
62
)
{
// 接続失敗
runOnUiThread
(
new
Runnable
()
{
runOnUiThread
(
new
Runnable
()
{
@Override
@Override
public
void
run
()
{
public
void
run
()
{
Logger
.
e
(
"onConnectionStateChange status = "
+
fStatus
);
Logger
.
e
(
"onConnectionStateChange status = "
+
fStatus
);
l
istener
.
onConnectionError
(
fStatus
);
mL
istener
.
onConnectionError
(
fStatus
);
}
}
});
});
return
;
}
else
{
}
// 通信失敗ではない
if
(
status
==
BluetoothGatt
.
GATT_SUCCESS
&&
BluetoothProfile
.
STATE_CONNECTED
==
newState
)
{
if
(
status
==
BluetoothGatt
.
GATT_SUCCESS
&&
BluetoothProfile
.
STATE_CONNECTED
==
newState
)
{
// 接続完了
// 接続完了
if
(!
mBluetoothGatt
.
discoverServices
())
{
// サービス検索
if
(!
mBluetoothGatt
.
discoverServices
())
{
// サービス検索
...
@@ -78,7 +149,7 @@ public class BleManagerUtil {
...
@@ -78,7 +149,7 @@ public class BleManagerUtil {
runOnUiThread
(
new
Runnable
()
{
// 接続失敗
runOnUiThread
(
new
Runnable
()
{
// 接続失敗
@Override
@Override
public
void
run
()
{
public
void
run
()
{
l
istener
.
onGetDeviceInfoFailed
(
fStatus
);
mL
istener
.
onGetDeviceInfoFailed
(
fStatus
);
Logger
.
e
(
"onConnectionStateChange2 status = "
+
fStatus
);
Logger
.
e
(
"onConnectionStateChange2 status = "
+
fStatus
);
}
}
});
});
...
@@ -87,16 +158,13 @@ public class BleManagerUtil {
...
@@ -87,16 +158,13 @@ public class BleManagerUtil {
@Override
@Override
public
void
run
()
{
public
void
run
()
{
// 接続成功
// 接続成功
l
istener
.
onConnectionState
();
mL
istener
.
onConnectionState
();
}
}
}
);
}
);
return
;
}
else
if
(
BluetoothProfile
.
STATE_DISCONNECTED
==
newState
)
{
// 切断完了(接続可能範囲から外れて切断された)
}
if
(
BluetoothProfile
.
STATE_DISCONNECTED
==
newState
)
{
// 切断完了(接続可能範囲から外れて切断された)
// 切断が発生する場合、Bluetoothと接続を切断する。
// 切断が発生する場合、Bluetoothと接続を切断する。
disconnect
();
disconnect
();
return
;
}
}
}
}
}
...
@@ -145,7 +213,7 @@ public class BleManagerUtil {
...
@@ -145,7 +213,7 @@ public class BleManagerUtil {
public
void
run
()
{
public
void
run
()
{
// 芯温計の温度を渡す。
// 芯温計の温度を渡す。
// 端末と接続時に呼ばれるのでコメント処理
// 端末と接続時に呼ばれるのでコメント処理
//
l
istener.onGetDeviceInfo(strTemperature);
//
mL
istener.onGetDeviceInfo(strTemperature);
}
}
});
});
...
@@ -169,7 +237,7 @@ public class BleManagerUtil {
...
@@ -169,7 +237,7 @@ public class BleManagerUtil {
return
;
return
;
}
}
// 芯温計の温度を渡す。
// 芯温計の温度を渡す。
l
istener
.
onGetDeviceInfo
(
strTemperature
);
mL
istener
.
onGetDeviceInfo
(
strTemperature
);
}
}
});
});
return
;
return
;
...
@@ -204,8 +272,9 @@ public class BleManagerUtil {
...
@@ -204,8 +272,9 @@ public class BleManagerUtil {
* @param deviceAddress デバイスアドレス
* @param deviceAddress デバイスアドレス
*/
*/
public
void
connect
(
int
connectTargetDeviceType
,
String
deviceAddress
)
{
public
void
connect
(
int
connectTargetDeviceType
,
String
deviceAddress
)
{
// デバイス
タイプ
セット
// デバイス
情報
セット
mBleConnectDeviceType
=
connectTargetDeviceType
;
mBleConnectDeviceType
=
connectTargetDeviceType
;
setUUID
();
setUUID
();
if
(
StringUtil
.
isNullOrEmpty
(
deviceAddress
))
{
// deviceAddressが空の場合は処理しない
if
(
StringUtil
.
isNullOrEmpty
(
deviceAddress
))
{
// deviceAddressが空の場合は処理しない
return
;
return
;
...
@@ -216,13 +285,31 @@ public class BleManagerUtil {
...
@@ -216,13 +285,31 @@ public class BleManagerUtil {
}
}
// mBluetoothGattのサービスと接続
// 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と接続する。
// GATT BLEを利用する時Androidのバージョン「23」をチェックしてGATTと接続する。
if
(
Build
.
VERSION
.
SDK_INT
>=
Build
.
VERSION_CODES
.
M
)
{
if
(
Build
.
VERSION
.
SDK_INT
>=
Build
.
VERSION_CODES
.
M
)
{
mBluetoothGatt
=
device
.
connectGatt
(
c
ontext
,
false
,
mGattcallback
,
TRANSPORT_LE
);
mBluetoothGatt
=
device
.
connectGatt
(
mC
ontext
,
false
,
mGattcallback
,
TRANSPORT_LE
);
}
else
{
}
else
{
mBluetoothGatt
=
device
.
connectGatt
(
c
ontext
,
false
,
mGattcallback
);
mBluetoothGatt
=
device
.
connectGatt
(
mC
ontext
,
false
,
mGattcallback
);
}
}
}
}
...
@@ -238,15 +325,15 @@ public class BleManagerUtil {
...
@@ -238,15 +325,15 @@ public class BleManagerUtil {
// ①「ユーザーの意思による切断」は、mBluetoothGattオブジェクトを解放する。再接続は、オブジェクト構築から。
// ①「ユーザーの意思による切断」は、mBluetoothGattオブジェクトを解放する。再接続は、オブジェクト構築から。
// ②「接続可能範囲から外れた切断」は、内部処理でmBluetoothGatt.disconnect()処理が実施される。
// ②「接続可能範囲から外れた切断」は、内部処理でmBluetoothGatt.disconnect()処理が実施される。
// 切断時のコールバックでmBluetoothGatt.connect()を呼んでおくと、接続可能範囲に入ったら自動接続する。
// 切断時のコールバックでmBluetoothGatt.connect()を呼んでおくと、接続可能範囲に入ったら自動接続する。
// mBluetoothGatt.close();
mBluetoothGatt
.
disconnect
();
mBluetoothGatt
.
disconnect
();
mBluetoothGatt
.
close
();
mBluetoothGatt
=
null
;
mBluetoothGatt
=
null
;
runOnUiThread
(
new
Runnable
()
{
runOnUiThread
(
new
Runnable
()
{
@Override
@Override
public
void
run
()
{
public
void
run
()
{
// 切断トーストメッセージを表示する。
// 切断トーストメッセージを表示する。
l
istener
.
onDisConnectionState
();
mL
istener
.
onDisConnectionState
();
}
}
}
);
}
);
}
}
...
@@ -290,11 +377,11 @@ public class BleManagerUtil {
...
@@ -290,11 +377,11 @@ public class BleManagerUtil {
// Bluetoothアダプタの取得処理
// Bluetoothアダプタの取得処理
public
void
startDeviceInfo
()
{
public
void
startDeviceInfo
()
{
// Bluetoothアダプタの取得
// Bluetoothアダプタの取得
BluetoothManager
bluetoothManager
=
(
BluetoothManager
)
c
ontext
.
getSystemService
(
Context
.
BLUETOOTH_SERVICE
);
BluetoothManager
bluetoothManager
=
(
BluetoothManager
)
mC
ontext
.
getSystemService
(
Context
.
BLUETOOTH_SERVICE
);
if
(
bluetoothManager
!=
null
)
{
if
(
bluetoothManager
!=
null
)
{
mBluetoothAdapter
=
bluetoothManager
.
getAdapter
();
mBluetoothAdapter
=
bluetoothManager
.
getAdapter
();
if
(
mBluetoothAdapter
==
null
)
{
// Android端末がBluetoothをサポートしていない
if
(
mBluetoothAdapter
==
null
)
{
// Android端末がBluetoothをサポートしていない
Toast
.
makeText
(
c
ontext
,
R
.
string
.
bluetooth_is_not_supported
,
Toast
.
LENGTH_SHORT
).
show
();
Toast
.
makeText
(
mC
ontext
,
R
.
string
.
bluetooth_is_not_supported
,
Toast
.
LENGTH_SHORT
).
show
();
return
;
return
;
}
}
}
else
{
}
else
{
...
...
ABVJE_UI_Android/src/jp/agentec/abook/abv/ui/common/activity/ABVCheckContentViewActivity.java
View file @
33487e93
...
@@ -206,7 +206,7 @@ public class ABVCheckContentViewActivity extends ABVContentViewActivity {
...
@@ -206,7 +206,7 @@ public class ABVCheckContentViewActivity extends ABVContentViewActivity {
bleManagerUtil
=
new
BleManagerUtil
(
this
,
new
BleManagerUtil
.
BleManagerUtilListener
()
{
bleManagerUtil
=
new
BleManagerUtil
(
this
,
new
BleManagerUtil
.
BleManagerUtilListener
()
{
@Override
@Override
public
void
onConnectionError
(
int
status
)
{
//bluetooth接続エラー
public
void
onConnectionError
(
int
status
)
{
//bluetooth接続エラー
Logger
.
e
(
TAG
,
"onConnectionError
"
);
Logger
.
e
(
TAG
,
"onConnectionError
status : "
+
status
);
// bluetoothのデバイスタイプ(中心温度計・放射温度計)
// bluetoothのデバイスタイプ(中心温度計・放射温度計)
int
bluetoothDeviceType
=
bleManagerUtil
.
getBluetoothDeviceType
();
int
bluetoothDeviceType
=
bleManagerUtil
.
getBluetoothDeviceType
();
// タイプによってメッセージ内容をセット
// タイプによってメッセージ内容をセット
...
@@ -215,7 +215,11 @@ public class ABVCheckContentViewActivity extends ABVContentViewActivity {
...
@@ -215,7 +215,11 @@ public class ABVCheckContentViewActivity extends ABVContentViewActivity {
}
else
if
(
bluetoothDeviceType
==
DeviceType
.
radiationThermomete
)
{
}
else
if
(
bluetoothDeviceType
==
DeviceType
.
radiationThermomete
)
{
errorAfterAbookCheckAip
(
String
.
format
(
getString
(
R
.
string
.
msg_bluetooth_connect_error
),
getString
(
R
.
string
.
radiation_thermometer
)));
errorAfterAbookCheckAip
(
String
.
format
(
getString
(
R
.
string
.
msg_bluetooth_connect_error
),
getString
(
R
.
string
.
radiation_thermometer
)));
}
}
// 通信切断
bleManagerDisconnect
();
bleManagerDisconnect
();
// ダイアログを閉じる
dismissWaitngDialog
();
}
}
@Override
@Override
...
@@ -998,6 +1002,10 @@ public class ABVCheckContentViewActivity extends ABVContentViewActivity {
...
@@ -998,6 +1002,10 @@ public class ABVCheckContentViewActivity extends ABVContentViewActivity {
protected
void
onStop
()
{
protected
void
onStop
()
{
super
.
onStop
();
super
.
onStop
();
Logger
.
d
(
TAG
,
"--onStop"
);
Logger
.
d
(
TAG
,
"--onStop"
);
// bluetooth通信されていたら切断
bleManagerDisconnect
();
// SPP通信の接続を切る
// SPP通信の接続を切る
disConnectSppBluetooth
();
disConnectSppBluetooth
();
...
...
ABVJE_UI_Android/src/jp/agentec/abook/abv/ui/home/activity/BlePairingSettingActivity.java
View file @
33487e93
...
@@ -69,7 +69,9 @@ public class BlePairingSettingActivity extends ABVUIActivity {
...
@@ -69,7 +69,9 @@ public class BlePairingSettingActivity extends ABVUIActivity {
@Override
@Override
public
void
run
()
{
public
void
run
()
{
BluetoothDevice
device
=
result
.
getDevice
();
BluetoothDevice
device
=
result
.
getDevice
();
if
(
device
.
getName
()
!=
null
)
{
Logger
.
d
(
"mScanCallback device.getName() = "
+
device
.
getName
());
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
(
device
.
getName
()
!=
null
&&
(
device
.
getName
().
startsWith
(
CENTER_THERMOMETE_DEVICE_NAME
)
||
device
.
getName
().
startsWith
(
RADIATION_THERMOMETE_DEVICE_NAME
)))
{
if
(!
mSavedDeviceAddressList
.
contains
(
device
.
getAddress
()))
{
//登録されたデバイスの場合、スキャン情報から除外する。
if
(!
mSavedDeviceAddressList
.
contains
(
device
.
getAddress
()))
{
//登録されたデバイスの場合、スキャン情報から除外する。
...
@@ -251,8 +253,6 @@ public class BlePairingSettingActivity extends ABVUIActivity {
...
@@ -251,8 +253,6 @@ public class BlePairingSettingActivity extends ABVUIActivity {
scanner
.
startScan
(
mLeScanCallback
);
scanner
.
startScan
(
mLeScanCallback
);
}
}
// スキャン開始(一定時間後にスキャン停止する)
// スキャン開始(一定時間後にスキャン停止する)
mHandler
.
postDelayed
(
new
Runnable
()
{
mHandler
.
postDelayed
(
new
Runnable
()
{
@Override
@Override
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment