OSDN Git Service

コマンド送受信ができた(その3)。
[gokigen/BLEControl.git] / app / src / main / java / net / osdn / gokigen / blecontrol / lib / ble / connection / fv100 / FV100BleDeviceConnector.java
1 package net.osdn.gokigen.blecontrol.lib.ble.connection.fv100;
2
3 import android.bluetooth.BluetoothAdapter;
4 import android.bluetooth.BluetoothDevice;
5 import android.bluetooth.BluetoothManager;
6 import android.content.Context;
7 import android.os.Build;
8 import android.util.Log;
9
10 import androidx.annotation.NonNull;
11 import androidx.annotation.RequiresApi;
12 import androidx.fragment.app.FragmentActivity;
13
14 import com.google.android.material.snackbar.Snackbar;
15
16 import net.osdn.gokigen.blecontrol.lib.ble.R;
17 import net.osdn.gokigen.blecontrol.lib.ble.connection.ITextDataUpdater;
18
19 public class FV100BleDeviceConnector implements FV100Finder.BleScanResult
20 {
21     private String TAG = toString();
22     private static final int BLE_SCAN_TIMEOUT_MILLIS = 15 * 1000; // 15秒間
23     private static final int BLE_WAIT_DURATION  = 100;           // 100ms間隔
24     private final FragmentActivity context;
25     private final ITextDataUpdater dataUpdater;
26     private FV100Communicator communicator = null;
27     private boolean foundBleDevice = false;
28
29     public FV100BleDeviceConnector(@NonNull FragmentActivity context, @NonNull ITextDataUpdater dataUpdater)
30     {
31         this.context = context;
32         this.dataUpdater = dataUpdater;
33         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
34         {
35             communicator = new FV100Communicator(context, dataUpdater);
36         }
37     }
38
39     public void query_to_device(String deviceName)
40     {
41         Log.v(TAG, " query_to_device : '" + deviceName + "'");
42         try {
43             BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
44             if (!btAdapter.isEnabled()) {
45                 // Bluetoothの設定がOFFだった
46                 showMessage(R.string.ble_setting_is_off);
47             }
48             BluetoothManager btMgr;
49             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
50             {
51                 // BLE のサービスを取得
52                 btMgr = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
53                 if (btMgr != null)
54                 {
55                     // BLEデバイスをスキャンする
56                     scanBleDevice(btMgr, new FV100Finder(deviceName, this));
57                 }
58                 else
59                 {
60                     // Bluetooth LEのサポートがない場合は、何もしない
61                     showMessage(R.string.not_support_ble);
62                 }
63             }
64             else
65             {
66                 // Androidのバージョンが低かった
67                 showMessage(R.string.not_support_android_version);
68             }
69         }
70         catch (Exception e)
71         {
72             e.printStackTrace();
73         }
74     }
75
76     public void reload_device_information()
77     {
78         try
79         {
80             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
81             {
82                 if (communicator != null)
83                 {
84                     communicator.data_reload();
85                 }
86             }
87         }
88         catch (Exception e)
89         {
90             e.printStackTrace();
91         }
92     }
93
94     private void showMessage(int stringId)
95     {
96         try
97         {
98             final String message = context.getString(stringId);
99             Log.v(TAG, message);
100             context.runOnUiThread(new Runnable() {
101                 @Override
102                 public void run() {
103                     try
104                     {
105                         // Snackbarでメッセージを通知する
106                         Snackbar.make(context.findViewById(R.id.drawer_layout), message, Snackbar.LENGTH_LONG).show();
107
108                         // Toastで カメラ起動エラーがあったことを通知する
109                         //Toast.makeText(context, message, Toast.LENGTH_LONG).show();
110                     }
111                     catch (Exception e)
112                     {
113                         e.printStackTrace();
114                     }
115                 }
116             });
117         }
118         catch (Exception e)
119         {
120             e.printStackTrace();
121         }
122     }
123
124     @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
125     private void scanBleDevice(BluetoothManager btMgr, FV100Finder finder)
126     {
127         try
128         {
129             // スキャン開始
130             foundBleDevice = false;
131             finder.reset();
132             BluetoothAdapter adapter = btMgr.getAdapter();
133             if (!adapter.startLeScan(finder))
134             {
135                 // Bluetooth LEのスキャンが開始できなかった場合...
136                 Log.v(TAG, "Bluetooth LE SCAN START fail...");
137                 showMessage(R.string.ble_scan_start_failure);
138                 return;
139             }
140             Log.v(TAG, " ----- BT SCAN STARTED ----- ");
141             int passed = 0;
142             while (passed < BLE_SCAN_TIMEOUT_MILLIS)
143             {
144                 if (foundBleDevice)
145                 {
146                     // デバイス発見
147                     Log.v(TAG, "FOUND DEVICE");
148                     break;
149                 }
150
151                 // BLEのスキャンが終わるまで待つ
152                 Thread.sleep(BLE_WAIT_DURATION);
153                 passed = passed + BLE_WAIT_DURATION;
154             }
155             // スキャンを止める
156             adapter.stopLeScan(finder);
157             Log.v(TAG, " ----- BT SCAN STOPPED ----- ");
158         }
159         catch (Exception e)
160         {
161             e.printStackTrace();
162             Log.v(TAG, "Bluetooth LE SCAN EXCEPTION...");
163             showMessage(R.string.scan_fail_via_ble);
164         }
165         Log.v(TAG, "Bluetooth LE SCAN STOPPED");
166         context.runOnUiThread(new Runnable() {
167             @Override
168             public void run() {
169                 dataUpdater.setText(context.getString(R.string.ble_scan_finished));
170             }
171         });
172     }
173
174     @Override
175     public void foundBleDevice(BluetoothDevice device)
176     {
177         try
178         {
179             foundBleDevice = true;
180             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
181             {
182                 if (communicator != null)
183                 {
184                     communicator.startCommunicate(device);
185                 }
186             }
187         }
188         catch (Exception e)
189         {
190             e.printStackTrace();
191         }
192     }
193
194 }