1 package net.osdn.gokigen.blecontrol.lib.ble.connection;
3 import android.app.Activity;
4 import android.bluetooth.BluetoothAdapter;
5 import android.bluetooth.BluetoothDevice;
6 import android.bluetooth.BluetoothGatt;
7 import android.bluetooth.BluetoothGattCallback;
8 import android.bluetooth.BluetoothManager;
9 import android.content.Context;
10 import android.content.SharedPreferences;
11 import android.os.Build;
12 import android.preference.PreferenceManager;
13 import android.util.Log;
14 import android.widget.Toast;
16 import androidx.annotation.NonNull;
17 import androidx.annotation.Nullable;
19 import net.osdn.gokigen.blecontrol.lib.ble.R;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.Locale;
29 public class PowerOnCamera implements ICameraPowerOn
31 private final String TAG = toString();
32 private final int BLE_SCAN_TIMEOUT_MILLIS = 5 * 1000; // 5秒間
33 private final int BLE_WAIT_DURATION = 100; // 100ms間隔
34 private final Activity context;
35 private List<CameraBleSetArrayItem> myCameraList;
36 private BluetoothDevice myBluetoothDevice = null;
37 private String myBtDevicePassCode = "";
42 public PowerOnCamera(@NonNull Activity context)
44 Log.v(TAG, "PowerOnCamera()");
45 this.context = context;
49 public void wakeup(final PowerOnCameraCallback callback)
51 Log.v(TAG, "PowerOnCamera::wakeup()");
55 BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
56 if (!btAdapter.isEnabled()) {
57 // Bluetoothの設定がOFFだった
58 Log.v(TAG, "Bluetooth is currently off.");
59 context.runOnUiThread(new Runnable()
64 // Toastで カメラ起動エラーがあったことを通知する
65 Toast.makeText(context, context.getString(R.string.ble_setting_is_off), Toast.LENGTH_LONG).show();
68 callback.wakeupExecuted(false);
75 callback.wakeupExecuted(false);
79 final BluetoothManager btMgr;
80 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
83 btMgr = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
86 // Bluetooth LEのサポートがない場合は、何もしない
87 Log.v(TAG, "PowerOnCamera::wakeup() NOT SUPPORT BLE...");
90 callback.wakeupExecuted(false);
93 final List<CameraBleSetArrayItem> deviceList = myCameraList;
95 // BLE_SCAN_TIMEOUT_MILLIS の間だけBLEのスキャンを実施する
96 Thread thread = new Thread(new Runnable()
101 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
103 class bleScanCallback implements BluetoothAdapter.LeScanCallback
106 public void onLeScan(final BluetoothDevice bluetoothDevice, int i, byte[] bytes)
110 final String btDeviceName = bluetoothDevice.getName();
111 // Log.v(TAG, "onLeScan() " + btDeviceName); // BluetoothDevice::getName() でログ出力してくれるので
112 for (CameraBleSetArrayItem device : deviceList)
114 final String btName = device.getBtName();
115 // Log.v(TAG, "onLeScan() [" + btName + "]");
116 if (btName.equals(btDeviceName))
120 myBluetoothDevice = bluetoothDevice;
121 myBtDevicePassCode = device.getBtPassCode();
135 myBluetoothDevice = null;
136 myBtDevicePassCode = "";
145 bleScanCallback scanCallback = new bleScanCallback();
149 scanCallback.reset();
150 BluetoothAdapter adapter = btMgr.getAdapter();
151 if (!adapter.startLeScan(scanCallback))
153 // Bluetooth LEのスキャンが開始できなかった場合...
154 Log.v(TAG, "Bluetooth LE SCAN START fail...");
155 callback.wakeupExecuted(false);
158 Log.v(TAG, "BT SCAN STARTED");
160 while (passed < BLE_SCAN_TIMEOUT_MILLIS)
162 // BLEデバイスが見つかったときは抜ける...
163 if (myBluetoothDevice != null)
169 Thread.sleep(BLE_WAIT_DURATION);
170 passed = passed + BLE_WAIT_DURATION;
173 adapter.stopLeScan(scanCallback);
174 Log.v(TAG, "BT SCAN STOPPED");
177 callback.wakeupExecuted(wakeupViaBle(adapter, myBluetoothDevice, myBtDevicePassCode));
182 Log.v(TAG, "Bluetooth LE SCAN EXCEPTION...");
183 callback.wakeupExecuted(false);
187 final String btName = (myBluetoothDevice != null) ? myBluetoothDevice.getName() : "";
188 context.runOnUiThread(new Runnable()
193 // Toastで カメラ起動エラーがあったことを通知する
194 Toast.makeText(context, context.getString(R.string.launch_fail_via_ble) + btName, Toast.LENGTH_LONG).show();
200 ee.printStackTrace();
203 Log.v(TAG, "Bluetooth LE SCAN STOPPED");
204 } // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
211 private boolean wakeupViaBle(@Nullable BluetoothAdapter adapter, @Nullable BluetoothDevice myBluetoothDevice, String passCode)
215 Log.v(TAG, " BluetoothAdapter is UNKNOWN(null).");
219 if (myBluetoothDevice == null)
221 Log.v(TAG, " Bt Device is UNKNOWN(null).");
225 Log.v(TAG, "WAKE UP CAMERA : " + myBluetoothDevice.getName() + " [" + myBluetoothDevice.getAddress() + "]");
228 Log.v(TAG, "PASSCODE : " + passCode);
229 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
232 myBluetoothDevice.connectGatt(context, false, new BleConnectionApi18());
248 private void setupCameraList()
250 myCameraList = new ArrayList<>();
252 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
253 for (int index = 1; index <= ICameraBleProperty.MAX_STORE_PROPERTIES; index++)
255 String idHeader = String.format(Locale.ENGLISH, "%03d", index);
256 String prefDate = preferences.getString(idHeader + ICameraBleProperty.DATE_KEY, "");
257 if (prefDate.length() <= 0)
262 String btName = preferences.getString(idHeader + ICameraBleProperty.NAME_KEY, "");
263 String btCode = preferences.getString(idHeader + ICameraBleProperty.CODE_KEY, "");
264 myCameraList.add(new CameraBleSetArrayItem(idHeader, btName, btCode, prefDate));
266 Log.v(TAG, "setupCameraList() : " + myCameraList.size());