OSDN Git Service

データを取得するための準備。
[gokigen/ThetaThoughtShutter.git] / app / src / main / java / jp / osdn / gokigen / thetathoughtshutter / bluetooth / connection / BluetoothDeviceFinder.kt
1 package jp.osdn.gokigen.thetathoughtshutter.bluetooth.connection
2
3 import android.app.Activity
4 import android.bluetooth.BluetoothAdapter
5 import android.bluetooth.BluetoothDevice
6 import android.bluetooth.BluetoothManager
7 import android.bluetooth.le.BluetoothLeScanner
8 import android.bluetooth.le.ScanCallback
9 import android.bluetooth.le.ScanResult
10 import android.content.Context
11 import android.util.Log
12
13 class BluetoothDeviceFinder(private val context: Activity, private val scanResult: IBluetoothScanResult) : BluetoothAdapter.LeScanCallback, ScanCallback()
14 {
15
16     companion object
17     {
18         private val TAG = BluetoothDeviceFinder::class.java.simpleName
19     }
20     private lateinit var targetDeviceName: String
21     private var foundBleDevice = false
22     private var scanner : BluetoothLeScanner? = null
23
24     fun reset()
25     {
26         foundBleDevice = false
27     }
28
29     fun stopScan()
30     {
31         try
32         {
33             Log.v(TAG, " stopScan()")
34             val btMgr: BluetoothManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
35             btMgr.adapter.cancelDiscovery()
36             scanner?.flushPendingScanResults(this)
37             scanner?.stopScan(this)
38         }
39         catch (e : Exception)
40         {
41             e.printStackTrace()
42         }
43     }
44
45     fun startScan(targetDeviceName: String)
46     {
47         try
48         {
49             this.targetDeviceName = targetDeviceName
50             val btAdapter = BluetoothAdapter.getDefaultAdapter()
51             if (!btAdapter.isEnabled)
52             {
53                 // Bluetoothの設定がOFFだった
54                 Log.v(TAG, " BLUETOOTH SETTING IS OFF")
55                 scanResult.notFindBluetoothDevice()
56                 return
57             }
58             // Bluetooth のサービスを取得
59             val btMgr: BluetoothManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
60             scanBluetoothDevice(btMgr)
61         }
62         catch (e: Exception)
63         {
64             e.printStackTrace()
65         }
66     }
67
68     private fun scanBluetoothDevice(btMgr: BluetoothManager)
69     {
70         try
71         {
72             Log.v(TAG, " scanBluetoothDevice() ")
73
74             // スキャン開始
75             foundBleDevice = false
76             val adapter = btMgr.adapter
77             if (adapter.isDiscovering)
78             {
79                 adapter.cancelDiscovery()
80             }
81             //adapter.startDiscovery()
82             scanner = adapter.bluetoothLeScanner
83             scanner?.startScan(this)
84         }
85         catch (e: Exception)
86         {
87             e.printStackTrace()
88             Log.v(TAG, "Bluetooth SCAN EXCEPTION...")
89         }
90     }
91
92     override fun onLeScan(device: BluetoothDevice, rssi: Int, scanRecord: ByteArray?)
93     {
94         try
95         {
96             Log.v(TAG, " onLeScan() ")
97
98             val btDeviceName = device.name
99             if (btDeviceName != null && btDeviceName.matches(Regex(targetDeviceName)))
100             {
101                 // device発見!
102                 foundBleDevice = true
103                 scanResult.foundBluetoothDevice(device)
104             }
105         }
106         catch (e: Exception)
107         {
108             e.printStackTrace()
109         }
110     }
111
112     override fun onScanFailed(errorCode: Int)
113     {
114         Log.v(TAG, " onScanFailed : $errorCode")
115     }
116
117     override fun onScanResult(callbackType: Int, result: ScanResult?)
118     {
119         //super.onScanResult(callbackType, result)
120         val device = result?.device
121         Log.v(TAG, " onScanResult($callbackType, ${device?.name}) ")
122         val findDevice = (device?.name)?.contains(targetDeviceName)
123         if ((findDevice != null)&&(findDevice))
124         {
125             Log.v(TAG, " FIND DEVICE : $targetDeviceName")
126             scanResult.foundBluetoothDevice(device)
127             scanner?.stopScan(this)
128         }
129     }
130
131     override fun onBatchScanResults(results: MutableList<ScanResult>?)
132     {
133         //super.onBatchScanResults(results)
134         Log.v(TAG, " onBatchScanResults ")
135     }
136 }