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
0e7bcc1e
Commit
0e7bcc1e
authored
Jul 28, 2020
by
onuma
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
TR41BluetoothUtilクラスにコールバックインタフェースを作成
parent
e56f16bd
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
429 additions
and
663 deletions
+429
-663
ABVJE_UI_Android/src/jp/agentec/abook/abv/cl/util/TR41BluetoothReceiveTask.java
+0
-663
ABVJE_UI_Android/src/jp/agentec/abook/abv/cl/util/TR41BluetoothUtil.java
+421
-0
ABVJE_UI_Android/src/jp/agentec/abook/abv/ui/common/activity/ABVCheckContentViewActivity.java
+8
-0
No files found.
ABVJE_UI_Android/src/jp/agentec/abook/abv/cl/util/TR41BluetoothReceiveTask.java
deleted
100644 → 0
View file @
e56f16bd
package
jp
.
agentec
.
abook
.
abv
.
cl
.
util
;
import
android.Manifest
;
import
android.annotation.TargetApi
;
import
android.bluetooth.BluetoothAdapter
;
import
android.bluetooth.BluetoothDevice
;
import
android.bluetooth.BluetoothManager
;
import
android.bluetooth.le.BluetoothLeScanner
;
import
android.bluetooth.le.ScanCallback
;
import
android.bluetooth.le.ScanFilter
;
import
android.bluetooth.le.ScanResult
;
import
android.bluetooth.le.ScanSettings
;
import
android.content.Context
;
import
android.content.Intent
;
import
android.content.pm.PackageManager
;
import
android.graphics.Color
;
import
android.os.Build
;
import
android.os.Bundle
;
import
android.util.Log
;
import
android.view.ContextMenu
;
import
android.view.LayoutInflater
;
import
android.view.MenuItem
;
import
android.view.View
;
import
android.view.ViewGroup
;
import
android.widget.AdapterView
;
import
android.widget.BaseAdapter
;
import
android.widget.Button
;
import
android.widget.LinearLayout
;
import
android.widget.ListView
;
import
android.widget.TextView
;
import
java.io.UnsupportedEncodingException
;
import
java.text.SimpleDateFormat
;
import
java.util.ArrayList
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.Timer
;
import
java.util.TimerTask
;
import
static
android
.
bluetooth
.
le
.
ScanSettings
.
SCAN_MODE_LOW_LATENCY
;
public
class
TR41BluetoothReceiveTask
extends
Thread
{
public
static
final
String
TAG
=
"TR41BluetootReceivehTask"
;
//private DeviceList mDeviceList ;
//private ListView mListView = null ;
BluetoothManager
mBluetoothManager
;
BluetoothAdapter
mBluetoothAdapter
;
BluetoothLeScanner
mBluetoothLeScanner
=
null
;
private
long
mScanTime
;
private
Timer
mScanStop
;
// 検索を止めるタイマ
private
final
static
int
REQUEST_PERMISSIONS
=
1
;
private
final
static
int
SDKVER_MARSHMALLOW
=
23
;
protected
void
onCreate
(
Bundle
savedInstanceState
)
{
//super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
//mListView = (ListView)findViewById(R.id.devlist) ;
//mDeviceList = new DeviceList();
Sb_Init_Ble_Scan
()
;
// [TR4の検索]ボタン
// findViewById(R.id.btn_scan).setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
//mDeviceList.RefreshList() ;
//mDeviceList.DeleteAllItem() ;
//if (mBluetoothLeScanner != null) {
// mBluetoothLeScanner.stopScan(mScanCallback);
//}
Sb_Scan
()
;
// BLE検索開始
//TextView textView = findViewById(R.id.textView);
//textView.setText("Scan.");
//Button btn = (Button)findViewById(R.id.btn_scan);
//btn.setEnabled(false);
// }
// }) ;
/**
Button btn = (Button)findViewById(R.id.btn_stop);
btn.setEnabled(false);
findViewById(R.id.btn_stop).setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
//mScanStop.cancel();
//mBluetoothLeScanner.stopScan(mScanCallback) ;
}
});
// 長押しでコンテキストメニュー表示
registerForContextMenu(mListView) ;
*/
// Android6.0以降なら権限確認
if
(
Build
.
VERSION
.
SDK_INT
>=
SDKVER_MARSHMALLOW
)
this
.
requestBlePermission
();
}
public
void
onCreateContextMenu
(
ContextMenu
menu
,
View
v
,
ContextMenu
.
ContextMenuInfo
menuInfo
)
{
//super.onCreateContextMenu(menu, v, menuInfo) ;
//コンテキストメニューの設定
//menu.add(0, 100, 0, "現在値取得") ;
//menu.add(0, 101, 0, "データ吸い上げ") ;
}
@TargetApi
(
SDKVER_MARSHMALLOW
)
private
void
requestBlePermission
(){
// 権限が許可されていない場合はリクエスト.
//if(checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
// requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION
// },REQUEST_PERMISSIONS) ;
//}
}
public
boolean
onContextItemSelected
(
MenuItem
item
)
{
/**
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo() ;
BluetoothDevice btdv = mDeviceList.infoList.get(info.position).getBluetoothDevice() ;
IntentInterface ii ;
Intent intent ;
switch (item.getItemId()) {
case 100:
// 現在値取得
ii = new IntentInterface() ;
ii.SetBLEDevice(btdv) ;
ii.SetParam(MainActivity.this) ;
intent = new Intent(MainActivity.this, GetCurrentActivity.class) ;
intent.putExtra("IntentInterface", ii) ;
startActivity(intent) ;
return true ;
case 101:
// データ吸い上げ
ii = new IntentInterface() ;
ii.SetBLEDevice(btdv) ;
ii.SetParam(MainActivity.this) ;
intent = new Intent(MainActivity.this, DownloadActivity.class) ;
intent.putExtra("IntentInterface", ii) ;
startActivity(intent) ;
return true ;
default:
return super.onContextItemSelected(item) ;
}
*/
return
false
;
}
// ************************************************************************
// void Sb_Init_Ble_Scan()
// 概要 Bluetoothの初期化
// 引数 :IN :
// :OUT:
// :RET:
// ************************************************************************
private
void
Sb_Init_Ble_Scan
()
{
if
(
mBluetoothAdapter
!=
null
)
return
;
//BluetoothAdapter取得
BluetoothAdapter
btAdpt
=
BluetoothAdapter
.
getDefaultAdapter
()
;
if
(!
btAdpt
.
equals
(
null
))
{
// Bluetooth対応端末の場合
}
else
{
// Bluetooth非対応端末の場合
//finish() ;
}
boolean
btEnable
=
btAdpt
.
isEnabled
()
;
if
(
btEnable
==
true
)
{
// Bluetoothアダプタ作成
//mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE) ;
mBluetoothAdapter
=
mBluetoothManager
.
getAdapter
()
;
mBluetoothLeScanner
=
mBluetoothAdapter
.
getBluetoothLeScanner
()
;
}
else
{
// BluetoothがOFFだった場合、ONにすることを促すダイアログを表示
//Intent btOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE) ;
//startActivityForResult(btOn, 1) ;
}
}
// ************************************************************************
// OnScanStopTimer
// 概要 BLE検索を止めるタイマ
// ************************************************************************
public
class
OnScanStopTimer
extends
TimerTask
{
@Override
public
void
run
(){
// Log.d("","OnScanStopTimer");
//runOnUiThread( new Runnable() {
//@Override
//public void run() {
//Button btn = (Button)findViewById(R.id.btn_scan);
//btn.setEnabled(true);
//}
//});
//TextView textView = findViewById(R.id.textView);
//textView.setText("expired.");
mBluetoothLeScanner
.
stopScan
(
mScanCallback
)
;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////
// 検索関連
///////////////////////////////////////////////////////////////////////////////////////////////
/** BLE機器を検索する */
private
void
Sb_Scan
()
{
Sb_Init_Ble_Scan
()
;
mScanTime
=
new
Date
().
getTime
()
;
// スキャンフィルタの作成
List
<
ScanFilter
>
mScanFilters
=
new
ArrayList
<>()
;
// スキャンモードの作成
ScanSettings
.
Builder
mScanSettingBuiler
=
new
ScanSettings
.
Builder
()
;
mScanSettingBuiler
.
setScanMode
(
SCAN_MODE_LOW_LATENCY
)
;
ScanSettings
mScanSettings
=
mScanSettingBuiler
.
build
()
;
// 作成したスキャンフィルタとモードでスキャン開始
mBluetoothLeScanner
.
startScan
(
mScanFilters
,
mScanSettings
,
mScanCallback
)
;
// 5秒後に止める
mScanStop
=
new
Timer
()
;
TimerTask
timerTask
=
new
OnScanStopTimer
()
;
mScanStop
.
schedule
(
timerTask
,
5000
)
;
}
private
ScanCallback
mScanCallback
=
new
ScanCallback
()
{
@Override
protected
Object
clone
()
throws
CloneNotSupportedException
{
return
super
.
clone
();
}
@Override
public
void
onBatchScanResults
(
List
<
ScanResult
>
results
)
{
Log
.
d
(
""
,
"onBatchScanResults"
);
};
@Override
public
void
onScanFailed
(
int
errorCode
)
{
Log
.
d
(
""
,
"onScanFailed"
);
};
@Override
public
void
onScanResult
(
int
callbackType
,
ScanResult
result
)
{
//Log.d("", "onScanResult");
BluetoothDevice
device
=
result
.
getDevice
()
;
byte
[]
scanRecord
=
result
.
getScanRecord
().
getBytes
()
;
Log
.
d
(
""
,
"getBoundState = "
+
device
.
getBondState
());
if
(
device
.
getBondState
()
!=
BluetoothDevice
.
BOND_BONDED
)
{
//Log.d("", "onScanResult2");
long
serial
[]
=
new
long
[
1
]
;
int
security
[]
=
new
int
[
1
]
;
int
ch1Data
[]
=
new
int
[
1
]
;
int
ch2Data
[]
=
new
int
[
1
]
;
int
batLevel
[]
=
new
int
[
1
]
;
StringBuffer
localName
=
new
StringBuffer
()
;
int
found
;
int
rtn
;
String
devName
=
device
.
getName
()
;
if
(
devName
==
null
)
{
Log
.
d
(
""
,
"deviceName = null"
);
return
;
}
// 近くにあるTR4だけを検索したい場合
//if(result.getRssi() < -50)
// return ;
rtn
=
Sb_Parse_ScanRecord
(
scanRecord
,
serial
,
localName
,
security
,
ch1Data
,
ch2Data
,
batLevel
)
;
if
(
rtn
==
0
)
{
devName
=
localName
.
toString
().
trim
()
;
Log
.
d
(
""
,
"onScanResult3"
);
// すでに登録されたシリアル番号を調べる
found
=
0
;
/**
for(int i=0; i< mDeviceList.getCount(); i++) {
Log.d("","" + mDeviceList.infoList.get(i).getSerial());
if(mDeviceList.infoList.get(i).getSerial() == serial[0]) {
found = 1 ;
break ;
}
}
if(found == 0) {
Log.d("", "onScanResult4");
mDeviceList.addDeviceInfo(serial[0], devName, device.getAddress(),
device, ch1Data[0], mScanTime/1000);
mListView.setAdapter(mDeviceList) ;
mDeviceList.notifyDataSetChanged() ;
//mScanStop.cancel();
//mScanStop = null;
//mBluetoothLeScanner.stopScan(mScanCallback) ;
}
*/
}
}
};
};
// ************************************************************************
// int Sb_Parse_ScanRecord(final byte[] scanRecord, long serialNo[],
// StringBuffer devName, int security[],
// int ch1Data[], int ch2Data[], int batLevel[])
// 概要 スキャンデータを解析し、TR-4シリーズかどうかを調べる。
// 引数 :IN : scanRecord : スキャンデータ
// :OUT: serialNo : 機器シリアル番号
// devName : 機器名称
// security : セキュリティ
// ch1Data : Ch1データ(素データ形式)
// ch2Data : Ch2データ(素データ形式)=未使用
// batLevel : 電池残量(1-5)
// :RET: : 0=TR-4シリーズ -1=違う機器
// ************************************************************************
private
int
Sb_Parse_ScanRecord
(
final
byte
[]
scanRecord
,
long
serialNo
[],
StringBuffer
devName
,
int
security
[],
int
ch1Data
[],
int
ch2Data
[],
int
batLevel
[])
{
// [長さ][AD Type][データ][長さ][AD Type][データ]
// 02 01 06 1b ff .....
int
pt
=
0
;
int
companyCode
;
// 会社コード
int
formatCode
;
// データフォーマット番号
try
{
pt
=
(
int
)
scanRecord
[
0
]
+
1
;
// 会社コード
pt
+=
2
;
companyCode
=
Sb_Conv2ByteToInt
(
scanRecord
[
pt
],
scanRecord
[
pt
+
1
])
;
if
(
companyCode
!=
0x0392
)
{
Log
.
d
(
""
,
"CompanyCode Error"
);
return
-
1
;
}
// シリアル番号
pt
+=
2
;
serialNo
[
0
]
=
Sb_Conv4ByteToLong
(
scanRecord
[
pt
],
scanRecord
[
pt
+
1
],
scanRecord
[
pt
+
2
],
scanRecord
[
pt
+
3
])
;
int
devType
=
Sb_Get_DeviceType
(
serialNo
[
0
])
;
if
(
devType
!=
0x16
&&
devType
!=
0x17
&&
devType
!=
0x18
)
{
Log
.
d
(
""
,
"devType Error"
);
return
-
1
;
}
// セキュリティ
pt
+=
4
;
security
[
0
]
=
(
int
)
scanRecord
[
pt
]
;
// データフォーマット番号
pt
+=
1
;
formatCode
=
(
int
)
scanRecord
[
pt
]
;
if
(
formatCode
!=
1
)
{
Log
.
d
(
""
,
"formatCode Error"
);
return
-
1
;
}
// Ch1データ
pt
++
;
ch1Data
[
0
]
=
Sb_Conv2ByteToInt
(
scanRecord
[
pt
],
scanRecord
[
pt
+
1
])
;
// Ch2データ(未使用)
pt
+=
2
;
ch2Data
[
0
]
=
Sb_Conv2ByteToInt
(
scanRecord
[
pt
],
scanRecord
[
pt
+
1
])
;
// 電池残量
pt
+=
2
;
batLevel
[
0
]
=
(
int
)
scanRecord
[
pt
]
;
// 機器名称
devName
.
append
(
Sb_Get_Str_from_Byte
(
scanRecord
,
33
,
33
+
16
,
"UTF-8"
))
;
return
0
;
}
catch
(
Exception
e
)
{
Log
.
d
(
""
,
""
+
e
.
getMessage
());
return
-
1
;
}
}
// ************************************************************************
// int Sb_Conv2ByteToInt(byte b1, byte b2)
// 概要 2ByteのunsignedリトルデータをINTへ変換
// 引数 :IN : b1 : 1Byte目
// b2 : 2Byte目
// :OUT:
// :RET: : 変換した値
// ************************************************************************
private
int
Sb_Conv2ByteToInt
(
byte
b1
,
byte
b2
)
{
int
c1
,
c2
;
c1
=
b1
;
c2
=
b2
;
c1
=
c1
&
0xFF
;
c2
=
c2
&
0xFF
;
int
val
=
c1
+
(
c2
<<
8
)
;
return
val
;
}
// ************************************************************************
// int Sa_Conv4ByteToLong(byte b1, byte b2, byte b3, byte b4)
// 概要 4Byteのunsignedリトルデータをintへ変換
// 引数 :IN : b1 : 1Byte目
// b2 : 2Byte目
// b3 : 3Byte目
// b4 : 4Byte目
// :OUT:
// :RET: : 変換後の値
// ************************************************************************
private
long
Sb_Conv4ByteToLong
(
byte
b1
,
byte
b2
,
byte
b3
,
byte
b4
)
{
long
c1
,
c2
,
c3
,
c4
;
c1
=
b1
;
c2
=
b2
;
c3
=
b3
;
c4
=
b4
;
c1
=
c1
&
0xFF
;
c2
=
c2
&
0xFF
;
c3
=
c3
&
0xFF
;
c4
=
c4
&
0xFF
;
long
val
=
c1
+
(
c2
<<
8
)
+
(
c3
<<
16
)
+
(
c4
<<
24
)
;
return
val
;
}
// ************************************************************************
// int Sa_Get_DeviceType(long serial)
// 概要 引数のシリアル番号から、TR4の機種タイプを返す。
// 引数 :IN : serial : シリアル番号
// :OUT:
// :RET: : TR41=0x16 TR42=0x17 TR45=0x18
// ************************************************************************
private
int
Sb_Get_DeviceType
(
long
serial
)
{
return
(
int
)
((
serial
>>
17
)
&
0x7F
)
;
}
// ************************************************************************
// String Sb_Get_Str_from_Byte(final byte[] data, int start, int size,
// String charsetName)
// 概要 引数のバイト列からsize分の文字列を返す。
// 引数 :IN : data : byte[]
// start : 開始位置
// size : 切り取るサイズ(byte)
// charsetName : 文字コード("SJIS","UTF-8"など)
// :OUT:
// :RET: : 切り出した文字列
// ************************************************************************
private
String
Sb_Get_Str_from_Byte
(
final
byte
[]
data
,
int
start
,
int
size
,
String
charsetName
)
{
byte
[]
bTmp
=
new
byte
[
size
]
;
String
stTmp
;
int
i
;
int
c
=
0
;
for
(
i
=
start
;
i
<
start
+
size
;
i
++)
{
if
(
data
[
i
]
==
0x00
)
break
;
else
bTmp
[
c
]
=
data
[
i
]
;
c
++
;
}
try
{
byte
[]
bStr
=
new
byte
[
c
]
;
for
(
i
=
0
;
i
<
bStr
.
length
;
i
++)
bStr
[
i
]
=
bTmp
[
i
]
;
stTmp
=
new
String
(
bStr
,
charsetName
)
;
}
catch
(
UnsupportedEncodingException
e
)
{
return
""
;
}
return
stTmp
;
}
// ************************************************************************
// String Sb_GetTimeStr(long timeData, String format)
// 概要 UNIX時間から文字列時刻を返す。
// 引数 :IN : timeData : UNIX時間(秒)
// format : 日時フォーマット("MMM dd,yyyy HH:mm:ss" など)
// :OUT:
// :RET: : 時刻文字列
// ************************************************************************
private
String
Sb_GetTimeStr
(
long
timeData
,
String
format
)
{
String
retStr
=
""
;
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
format
)
;
Date
orgTime
=
new
Date
(
timeData
*
1000
)
;
retStr
=
sdf
.
format
(
orgTime
)
;
return
retStr
;
}
///////////////////////////////////////////////////////////////////////////////////////////////
// デバイスのリストを格納するアダプター
///////////////////////////////////////////////////////////////////////////////////////////////
/**
private class DeviceList extends BaseAdapter {
private List<DeviceInfo> infoList;
private DeviceList() {
infoList = new ArrayList<DeviceInfo>();
}
public void addDeviceInfo(long serial, String name, String address, BluetoothDevice bltDevice,
int ch1Data, long dataTime)
{
if(name == null)
return ;
infoList.add(new DeviceInfo(serial, name, address, bltDevice, ch1Data, dataTime)) ;
}
public void updataDeviceInfo(long serial, int ch1Data, long dataTime, int rssi) {
for(int i=0; i<infoList.size(); i++) {
if(infoList.get(i).getSerial() == serial) {
infoList.get(i).ch1Data = ch1Data ;
infoList.get(i).dataTime = dataTime ;
break ;
}
}
}
@Override
public int getCount() {
return infoList.size() ;
}
@Override
public Object getItem(int position) {
return infoList.get(position) ;
}
@Override
public long getItemId(int position) {
return position ;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView ;
if(v == null) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE) ;
v = inflater.inflate(R.layout.row, null) ;
}
DeviceInfo info = infoList.get(position) ;
TextView deviceText = (TextView)v.findViewById(devName) ;
deviceText.setText(info.getName()) ;
TextView ch1DataText = (TextView)v.findViewById(R.id.dataValue) ;
String ch1Str ;
if(info.getCh1Data() == 0xEEEE)
ch1Str = "----" ;
else if(info.getCh1Data() == 0xEEE0)
ch1Str = "" ;
else
ch1Str = String.format("%.1f ℃", (double)(info.getCh1Data()-1000)/10.0) ;
ch1DataText.setText(ch1Str) ;
if((info.getDataTime() + 3*60 < mScanTime/1000) && info.getCh1Data() != 0xEEE0)
ch1DataText.setTextColor(Color.rgb(210,210,210)) ;
else
ch1DataText.setTextColor(Color.rgb(0,0,0)) ;
TextView timeText = (TextView)v.findViewById(dataTime) ;
String dataStr ;
if(info.getDataTime() != 0)
dataStr = Sb_GetTimeStr(info.getDataTime(), "MM/dd HH:mm:ss") ;
else
dataStr = "" ;
timeText.setText(dataStr) ;
// 背景
LinearLayout ll = (LinearLayout)v.findViewById(R.id.list_framelayout) ;
ll.setBackgroundColor(Color.rgb(255,255,238)) ;
return v ;
}
public BluetoothDevice GetBlueToothDevice(int position) {
return infoList.get(position).getBluetoothDevice() ;
}
public void DeleteAllItem() {
infoList.clear() ;
}
//デバイスの名前とアドレスを持つクラス
private class DeviceInfo {
private long serial ;
private String name ;
private String address;
private BluetoothDevice bltDevice ;
private int ch1Data ;
private long dataTime ;
private DeviceInfo(long serial, String name, String address, BluetoothDevice bltDevice,
int ch1Data, long dataTime)
{
this.serial = serial ;
this.name = name ;
this.address = address ;
this.bltDevice = bltDevice ;
this.ch1Data = ch1Data ;
this.dataTime = dataTime ;
}
private long getSerial() {
return serial ;
}
private String getName() {
return name ;
}
private String getAddress() {
return address ;
}
private BluetoothDevice getBluetoothDevice() {
return bltDevice ;
}
private int getCh1Data() {
return ch1Data ;
}
private long getDataTime() {
return dataTime ;
}
}
public void RefreshList() {
notifyDataSetChanged() ;
}
}
*/
}
ABVJE_UI_Android/src/jp/agentec/abook/abv/cl/util/TR41BluetoothUtil.java
0 → 100644
View file @
0e7bcc1e
package
jp
.
agentec
.
abook
.
abv
.
cl
.
util
;
import
android.Manifest
;
import
android.annotation.TargetApi
;
import
android.bluetooth.BluetoothAdapter
;
import
android.bluetooth.BluetoothDevice
;
import
android.bluetooth.BluetoothManager
;
import
android.bluetooth.le.BluetoothLeScanner
;
import
android.bluetooth.le.ScanCallback
;
import
android.bluetooth.le.ScanFilter
;
import
android.bluetooth.le.ScanResult
;
import
android.bluetooth.le.ScanSettings
;
import
android.content.Context
;
import
android.content.Intent
;
import
android.content.pm.PackageManager
;
import
android.graphics.Color
;
import
android.os.Build
;
import
android.os.Bundle
;
import
android.os.Handler
;
import
android.util.Log
;
import
android.view.ContextMenu
;
import
android.view.LayoutInflater
;
import
android.view.MenuItem
;
import
android.view.View
;
import
android.view.ViewGroup
;
import
android.widget.AdapterView
;
import
android.widget.BaseAdapter
;
import
android.widget.Button
;
import
android.widget.LinearLayout
;
import
android.widget.ListView
;
import
android.widget.TextView
;
import
java.io.UnsupportedEncodingException
;
import
java.text.SimpleDateFormat
;
import
java.util.ArrayList
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.Timer
;
import
java.util.TimerTask
;
import
jp.agentec.abook.abv.bl.common.log.Logger
;
import
static
android
.
bluetooth
.
le
.
ScanSettings
.
SCAN_MODE_LOW_LATENCY
;
public
class
TR41BluetoothUtil
{
public
static
final
String
TAG
=
"TR41BluetootUtil"
;
private
BluetoothManager
mBluetoothManager
;
private
BluetoothAdapter
mBluetoothAdapter
;
private
BluetoothLeScanner
mBluetoothLeScanner
;
private
TR41BluetoothUtilLeScanner
mListener
;
private
Context
mContext
;
private
Handler
mHandler
;
private
static
final
long
SCAN_PERIOD
=
10000
;
// スキャン時間。単位はミリ秒
/**
*
*
*/
public
interface
TR41BluetoothUtilLeScanner
{
void
onGetDeviceInfo
(
String
strTemperature
);
// ディバイスから渡す情報
void
onGetDeviceInfoFailed
(
int
status
);
// ディバイスと接続失敗時の情報渡す。
void
onConnectionState
();
// ディバイスと接続された時情報渡す。
void
onDisConnectionState
();
// ディバイスと切断時
void
onConnectionError
(
int
status
);
// ディバイス接続エラー
void
onBatchScanResults
(
List
<
ScanResult
>
results
);
void
onScanFailed
(
int
errorCode
);
void
onScanResult
(
int
callbackType
,
ScanResult
result
);
}
public
TR41BluetoothUtil
(
Context
context
,
TR41BluetoothUtilLeScanner
listener
)
{
mContext
=
context
;
mListener
=
listener
;
mHandler
=
new
Handler
();
}
// ************************************************************************
// void Sb_Init_Ble_Scan()
// 概要 Bluetoothの初期化
// 引数 :IN :
// :OUT:
// :RET:
// ************************************************************************
private
void
Sb_Init_Ble_Scan
()
{
if
(
mBluetoothAdapter
!=
null
)
return
;
//BluetoothAdapter取得
BluetoothAdapter
btAdpt
=
BluetoothAdapter
.
getDefaultAdapter
()
;
if
(!
btAdpt
.
equals
(
null
))
{
// Bluetooth対応端末の場合
}
else
{
// Bluetooth非対応端末の場合
//finish() ;
}
boolean
btEnable
=
btAdpt
.
isEnabled
()
;
if
(
btEnable
==
true
)
{
// Bluetoothアダプタ作成
mBluetoothManager
=
(
BluetoothManager
)
mContext
.
getSystemService
(
Context
.
BLUETOOTH_SERVICE
);
mBluetoothAdapter
=
mBluetoothManager
.
getAdapter
()
;
mBluetoothLeScanner
=
mBluetoothAdapter
.
getBluetoothLeScanner
()
;
}
else
{
// BluetoothがOFFだった場合、ONにすることを促すダイアログを表示
//Intent btOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE) ;
//startActivityForResult(btOn, 1) ;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////
// 検索関連
///////////////////////////////////////////////////////////////////////////////////////////////
/** BLE機器を検索する */
private
void
Sb_Scan
()
{
Sb_Init_Ble_Scan
()
;
// スキャンフィルタの作成
List
<
ScanFilter
>
mScanFilters
=
new
ArrayList
<>()
;
// スキャンモードの作成
ScanSettings
.
Builder
mScanSettingBuiler
=
new
ScanSettings
.
Builder
()
;
mScanSettingBuiler
.
setScanMode
(
SCAN_MODE_LOW_LATENCY
)
;
ScanSettings
mScanSettings
=
mScanSettingBuiler
.
build
()
;
// 作成したスキャンフィルタとモードでスキャン開始
mBluetoothLeScanner
.
startScan
(
mScanFilters
,
mScanSettings
,
mScanCallback
)
;
mHandler
.
postDelayed
(
new
Runnable
()
{
@Override
public
void
run
()
{
// SCAN_PERIOD 秒後に呼ばれた時はスキャンで端末を取得できなかったと見做す。
mBluetoothLeScanner
.
stopScan
(
mScanCallback
)
;
Logger
.
d
(
TAG
,
"scan in 20 sec"
);
}
},
SCAN_PERIOD
);
}
private
ScanCallback
mScanCallback
=
new
ScanCallback
()
{
@Override
protected
Object
clone
()
throws
CloneNotSupportedException
{
return
super
.
clone
();
}
@Override
public
void
onBatchScanResults
(
List
<
ScanResult
>
results
)
{
Logger
.
d
(
TAG
,
"onBatchScanResults"
);
};
@Override
public
void
onScanFailed
(
int
errorCode
)
{
Logger
.
d
(
TAG
,
"onScanFailed"
);
};
@Override
public
void
onScanResult
(
int
callbackType
,
ScanResult
result
)
{
Logger
.
d
(
TAG
,
"onScanResult"
);
BluetoothDevice
device
=
result
.
getDevice
()
;
byte
[]
scanRecord
=
result
.
getScanRecord
().
getBytes
()
;
Logger
.
d
(
""
,
"getBoundState = "
+
device
.
getBondState
());
if
(
device
.
getBondState
()
!=
BluetoothDevice
.
BOND_BONDED
)
{
long
serial
[]
=
new
long
[
1
]
;
int
security
[]
=
new
int
[
1
]
;
int
ch1Data
[]
=
new
int
[
1
]
;
int
ch2Data
[]
=
new
int
[
1
]
;
int
batLevel
[]
=
new
int
[
1
]
;
StringBuffer
localName
=
new
StringBuffer
()
;
//int found ;
int
rtn
;
String
devName
=
device
.
getName
()
;
if
(
devName
==
null
)
{
Logger
.
d
(
""
,
"deviceName is null"
);
return
;
}
// 近くにあるTR4だけを検索したい場合
//if(result.getRssi() < -50)
// return ;
rtn
=
Sb_Parse_ScanRecord
(
scanRecord
,
serial
,
localName
,
security
,
ch1Data
,
ch2Data
,
batLevel
)
;
if
(
rtn
==
0
)
{
devName
=
localName
.
toString
().
trim
()
;
// すでに登録されたシリアル番号を調べる
//found = 0 ;
/**
for(int i=0; i< mDeviceList.getCount(); i++) {
Logger.d("","" + mDeviceList.infoList.get(i).getSerial());
if(mDeviceList.infoList.get(i).getSerial() == serial[0]) {
found = 1 ;
break ;
}
}
if(found == 0) {
mDeviceList.addDeviceInfo(serial[0], devName, device.getAddress(),
device, ch1Data[0], mScanTime/1000);
mListView.setAdapter(mDeviceList) ;
mDeviceList.notifyDataSetChanged() ;
//mScanStop.cancel();
//mScanStop = null;
//mBluetoothLeScanner.stopScan(mScanCallback) ;
}
*/
mBluetoothLeScanner
.
stopScan
(
mScanCallback
)
;
}
}
};
};
// ************************************************************************
// int Sb_Parse_ScanRecord(final byte[] scanRecord, long serialNo[],
// StringBuffer devName, int security[],
// int ch1Data[], int ch2Data[], int batLevel[])
// 概要 スキャンデータを解析し、TR-4シリーズかどうかを調べる。
// 引数 :IN : scanRecord : スキャンデータ
// :OUT: serialNo : 機器シリアル番号
// devName : 機器名称
// security : セキュリティ
// ch1Data : Ch1データ(素データ形式)
// ch2Data : Ch2データ(素データ形式)=未使用
// batLevel : 電池残量(1-5)
// :RET: : 0=TR-4シリーズ -1=違う機器
// ************************************************************************
private
int
Sb_Parse_ScanRecord
(
final
byte
[]
scanRecord
,
long
serialNo
[],
StringBuffer
devName
,
int
security
[],
int
ch1Data
[],
int
ch2Data
[],
int
batLevel
[])
{
// [長さ][AD Type][データ][長さ][AD Type][データ]
// 02 01 06 1b ff .....
int
pt
=
0
;
int
companyCode
;
// 会社コード
int
formatCode
;
// データフォーマット番号
try
{
pt
=
(
int
)
scanRecord
[
0
]
+
1
;
// 会社コード
pt
+=
2
;
companyCode
=
Sb_Conv2ByteToInt
(
scanRecord
[
pt
],
scanRecord
[
pt
+
1
])
;
if
(
companyCode
!=
0x0392
)
{
Logger
.
d
(
""
,
"CompanyCode Error"
);
return
-
1
;
}
// シリアル番号
pt
+=
2
;
serialNo
[
0
]
=
Sb_Conv4ByteToLong
(
scanRecord
[
pt
],
scanRecord
[
pt
+
1
],
scanRecord
[
pt
+
2
],
scanRecord
[
pt
+
3
])
;
int
devType
=
Sb_Get_DeviceType
(
serialNo
[
0
])
;
if
(
devType
!=
0x16
&&
devType
!=
0x17
&&
devType
!=
0x18
)
{
Logger
.
d
(
""
,
"serialNo Error"
);
return
-
1
;
}
// セキュリティ
pt
+=
4
;
security
[
0
]
=
(
int
)
scanRecord
[
pt
]
;
// データフォーマット番号
pt
+=
1
;
formatCode
=
(
int
)
scanRecord
[
pt
]
;
if
(
formatCode
!=
1
)
{
Logger
.
d
(
""
,
"formatCode Error"
);
return
-
1
;
}
// Ch1データ
pt
++
;
ch1Data
[
0
]
=
Sb_Conv2ByteToInt
(
scanRecord
[
pt
],
scanRecord
[
pt
+
1
])
;
// Ch2データ(未使用)
pt
+=
2
;
ch2Data
[
0
]
=
Sb_Conv2ByteToInt
(
scanRecord
[
pt
],
scanRecord
[
pt
+
1
])
;
// 電池残量
pt
+=
2
;
batLevel
[
0
]
=
(
int
)
scanRecord
[
pt
]
;
// 機器名称
devName
.
append
(
Sb_Get_Str_from_Byte
(
scanRecord
,
33
,
33
+
16
,
"UTF-8"
))
;
return
0
;
}
catch
(
Exception
e
)
{
Logger
.
d
(
""
,
""
+
e
.
getMessage
());
return
-
1
;
}
}
// ************************************************************************
// int Sb_Conv2ByteToInt(byte b1, byte b2)
// 概要 2ByteのunsignedリトルデータをINTへ変換
// 引数 :IN : b1 : 1Byte目
// b2 : 2Byte目
// :OUT:
// :RET: : 変換した値
// ************************************************************************
private
int
Sb_Conv2ByteToInt
(
byte
b1
,
byte
b2
)
{
int
c1
,
c2
;
c1
=
b1
;
c2
=
b2
;
c1
=
c1
&
0xFF
;
c2
=
c2
&
0xFF
;
int
val
=
c1
+
(
c2
<<
8
)
;
return
val
;
}
// ************************************************************************
// int Sa_Conv4ByteToLong(byte b1, byte b2, byte b3, byte b4)
// 概要 4Byteのunsignedリトルデータをintへ変換
// 引数 :IN : b1 : 1Byte目
// b2 : 2Byte目
// b3 : 3Byte目
// b4 : 4Byte目
// :OUT:
// :RET: : 変換後の値
// ************************************************************************
private
long
Sb_Conv4ByteToLong
(
byte
b1
,
byte
b2
,
byte
b3
,
byte
b4
)
{
long
c1
,
c2
,
c3
,
c4
;
c1
=
b1
;
c2
=
b2
;
c3
=
b3
;
c4
=
b4
;
c1
=
c1
&
0xFF
;
c2
=
c2
&
0xFF
;
c3
=
c3
&
0xFF
;
c4
=
c4
&
0xFF
;
long
val
=
c1
+
(
c2
<<
8
)
+
(
c3
<<
16
)
+
(
c4
<<
24
)
;
return
val
;
}
// ************************************************************************
// int Sa_Get_DeviceType(long serial)
// 概要 引数のシリアル番号から、TR4の機種タイプを返す。
// 引数 :IN : serial : シリアル番号
// :OUT:
// :RET: : TR41=0x16 TR42=0x17 TR45=0x18
// ************************************************************************
private
int
Sb_Get_DeviceType
(
long
serial
)
{
return
(
int
)
((
serial
>>
17
)
&
0x7F
)
;
}
// ************************************************************************
// String Sb_Get_Str_from_Byte(final byte[] data, int start, int size,
// String charsetName)
// 概要 引数のバイト列からsize分の文字列を返す。
// 引数 :IN : data : byte[]
// start : 開始位置
// size : 切り取るサイズ(byte)
// charsetName : 文字コード("SJIS","UTF-8"など)
// :OUT:
// :RET: : 切り出した文字列
// ************************************************************************
private
String
Sb_Get_Str_from_Byte
(
final
byte
[]
data
,
int
start
,
int
size
,
String
charsetName
)
{
byte
[]
bTmp
=
new
byte
[
size
]
;
String
stTmp
;
int
i
;
int
c
=
0
;
for
(
i
=
start
;
i
<
start
+
size
;
i
++)
{
if
(
data
[
i
]
==
0x00
)
break
;
else
bTmp
[
c
]
=
data
[
i
]
;
c
++
;
}
try
{
byte
[]
bStr
=
new
byte
[
c
]
;
for
(
i
=
0
;
i
<
bStr
.
length
;
i
++)
bStr
[
i
]
=
bTmp
[
i
]
;
stTmp
=
new
String
(
bStr
,
charsetName
)
;
}
catch
(
UnsupportedEncodingException
e
)
{
return
""
;
}
return
stTmp
;
}
// ************************************************************************
// String Sb_GetTimeStr(long timeData, String format)
// 概要 UNIX時間から文字列時刻を返す。
// 引数 :IN : timeData : UNIX時間(秒)
// format : 日時フォーマット("MMM dd,yyyy HH:mm:ss" など)
// :OUT:
// :RET: : 時刻文字列
// ************************************************************************
private
String
Sb_GetTimeStr
(
long
timeData
,
String
format
)
{
String
retStr
=
""
;
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
format
)
;
Date
orgTime
=
new
Date
(
timeData
*
1000
)
;
retStr
=
sdf
.
format
(
orgTime
)
;
return
retStr
;
}
}
\ No newline at end of file
ABVJE_UI_Android/src/jp/agentec/abook/abv/ui/common/activity/ABVCheckContentViewActivity.java
View file @
0e7bcc1e
...
...
@@ -331,6 +331,12 @@ public class ABVCheckContentViewActivity extends ABVContentViewActivity {
mBluetoothAdapter
=
BluetoothAdapter
.
getDefaultAdapter
();
if
(
mBluetoothAdapter
==
null
)
{
// エラー: Bluetooth なし.
Logger
.
e
(
TAG
,
"BluetoothAdapter is null."
);
}
// TR41温度計
if
(
mBluetoothAdapter
!=
null
){
}
}
...
...
@@ -661,6 +667,8 @@ public class ABVCheckContentViewActivity extends ABVContentViewActivity {
// Activityがバックグラウンドになったときは、受け取らない
mNfcAdapter
.
disableForegroundDispatch
(
ABVCheckContentViewActivity
.
this
);
break
;
case
DeviceType
.
tr41
:
// TR41温度計
break
;
}
successAfterAbookCheckAip
(
""
);
mWaitingDialog
=
null
;
...
...
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