1 package net.osdn.gokigen.blecontrol.lib.ble.connection.fv100;
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;
10 import androidx.annotation.NonNull;
11 import androidx.annotation.RequiresApi;
12 import androidx.fragment.app.FragmentActivity;
14 import com.google.android.material.snackbar.Snackbar;
16 import net.osdn.gokigen.blecontrol.lib.ble.R;
17 import net.osdn.gokigen.blecontrol.lib.ble.connection.ITextDataUpdater;
19 public class FV100BleDeviceConnector implements FV100Finder.BleScanResult
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;
29 public FV100BleDeviceConnector(@NonNull FragmentActivity context, @NonNull ITextDataUpdater dataUpdater)
31 this.context = context;
32 this.dataUpdater = dataUpdater;
33 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
35 communicator = new FV100Communicator(context, dataUpdater);
39 public void query_to_device(String deviceName)
41 Log.v(TAG, " query_to_device : '" + deviceName + "'");
43 BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
44 if (!btAdapter.isEnabled()) {
45 // Bluetoothの設定がOFFだった
46 showMessage(R.string.ble_setting_is_off);
48 BluetoothManager btMgr;
49 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
52 btMgr = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
56 scanBleDevice(btMgr, new FV100Finder(deviceName, this));
60 // Bluetooth LEのサポートがない場合は、何もしない
61 showMessage(R.string.not_support_ble);
67 showMessage(R.string.not_support_android_version);
76 public void reload_device_information()
80 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
82 if (communicator != null)
84 communicator.data_reload();
94 private void showMessage(int stringId)
98 final String message = context.getString(stringId);
100 context.runOnUiThread(new Runnable() {
105 // Snackbarでメッセージを通知する
106 Snackbar.make(context.findViewById(R.id.drawer_layout), message, Snackbar.LENGTH_LONG).show();
108 // Toastで カメラ起動エラーがあったことを通知する
109 //Toast.makeText(context, message, Toast.LENGTH_LONG).show();
124 @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
125 private void scanBleDevice(BluetoothManager btMgr, FV100Finder finder)
130 foundBleDevice = false;
132 BluetoothAdapter adapter = btMgr.getAdapter();
133 if (!adapter.startLeScan(finder))
135 // Bluetooth LEのスキャンが開始できなかった場合...
136 Log.v(TAG, "Bluetooth LE SCAN START fail...");
137 showMessage(R.string.ble_scan_start_failure);
140 Log.v(TAG, " ----- BT SCAN STARTED ----- ");
142 while (passed < BLE_SCAN_TIMEOUT_MILLIS)
147 Log.v(TAG, "FOUND DEVICE");
152 Thread.sleep(BLE_WAIT_DURATION);
153 passed = passed + BLE_WAIT_DURATION;
156 adapter.stopLeScan(finder);
157 Log.v(TAG, " ----- BT SCAN STOPPED ----- ");
162 Log.v(TAG, "Bluetooth LE SCAN EXCEPTION...");
163 showMessage(R.string.scan_fail_via_ble);
165 Log.v(TAG, "Bluetooth LE SCAN STOPPED");
166 context.runOnUiThread(new Runnable() {
169 dataUpdater.setText(context.getString(R.string.ble_scan_finished));
175 public void foundBleDevice(BluetoothDevice device)
179 foundBleDevice = true;
180 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
182 if (communicator != null)
184 communicator.startCommunicate(device);