CypressのGitソースをコピーしてBuildしたBLEアプリを動かしてみたが、
“Search for Device”から先に進まない。
起動後、”Search for Device”を2回押下したときのログは以下。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
2020-06-22 22:59:21.570 31186-31211/com.example.ble101 I/Adreno: QUALCOMM build : f7ad136, I02d3f1c2c3 Build Date : 11/03/17 OpenGL ES Shader Compiler Version: EV031.20.00.04 Local Branch : Remote Branch : refs/tags/AU_LINUX_ANDROID_LA.BR.1.3.7_RB1.08.00.00.336.020 Remote Branch : NONE Reconstruct Branch : NOTHING 2020-06-22 22:59:21.583 31186-31211/com.example.ble101 I/Adreno: PFP: 0x005ff087, ME: 0x005ff063 2020-06-22 22:59:21.602 31186-31211/com.example.ble101 I/OpenGLRenderer: Initialized EGL, version 1.4 2020-06-22 22:59:21.602 31186-31211/com.example.ble101 D/OpenGLRenderer: Swap behavior 2 2020-06-22 22:59:28.697 31186-31186/com.example.ble101 D/MainActivity: Starting BLE Service 2020-06-22 22:59:28.708 31186-31186/com.example.ble101 D/MainActivity: Bluetooth is Enabled 2020-06-22 22:59:28.751 31186-31186/com.example.ble101 I/MainActivity: onServiceConnected 2020-06-22 22:59:32.207 31186-31222/com.example.ble101 D/BluetoothAdapter: onBluetoothServiceUp: android.bluetooth.IBluetooth$Stub$Proxy@7faa39a 2020-06-22 22:59:41.818 31186-31186/com.example.ble101 D/BluetoothAdapter: isLeEnabled(): ON 2020-06-22 22:59:41.828 31186-31198/com.example.ble101 D/BluetoothLeScanner: onScannerRegistered() - status=0 scannerId=7 mScannerId=0 2020-06-22 23:00:01.532 31186-31191/com.example.ble101 I/zygote64: Do partial code cache collection, code=28KB, data=29KB 2020-06-22 23:00:01.532 31186-31191/com.example.ble101 I/zygote64: After code cache collection, code=28KB, data=29KB 2020-06-22 23:00:01.532 31186-31191/com.example.ble101 I/zygote64: Increasing code cache capacity to 128KB 2020-06-22 23:00:01.634 31186-31186/com.example.ble101 D/BluetoothAdapter: isLeEnabled(): ON 2020-06-22 23:00:21.184 31186-31193/com.example.ble101 I/zygote64: System.exit called, status: 1 2020-06-22 23:00:21.185 31186-31193/com.example.ble101 I/AndroidRuntime: VM exiting with result code 1, cleanup skipped. |
最初に”Start Bluetooth”をクリックすると、”Start Bluetooth”がグレーアウトして、”Search for Device”が有効化される。
それぞれのonClickで呼ばれる関数は、activity_main.xmlから以下。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<Button android:id="@+id/start_button" android:text="@string/Start" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAllCaps="false" android:enabled="true" android:onClick="startBluetooth" /> <Button android:id="@+id/search_button" android:text="@string/Search" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAllCaps="false" android:enabled="false" android:onClick="searchBluetooth" /> |
startBluetooth()
MainActivity.javaで以下のように定義。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
public void startBluetooth(View view) { // Find BLE service and adapter final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); BluetoothAdapter mBluetoothAdapter = bluetoothManager.getAdapter(); // Ensures Bluetooth is enabled on the device. If Bluetooth is not currently enabled, // fire an intent to display a dialog asking the user to grant permission to enable it. if (!mBluetoothAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BLE); } // Start the BLE Service Log.d(TAG, "Starting BLE Service"); Intent gattServiceIntent = new Intent(this, PSoCCapSenseLedService.class); bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE); // Disable the start button and turn on the search button start_button.setEnabled(false); search_button.setEnabled(true); Log.d(TAG, "Bluetooth is Enabled"); } |
最後の方でmServiceConnectionをbindServiceして、ボタンの無効/有効化している。
searchBluetooth()
MainActivity.javaで以下のように定義。
|
1 2 3 4 5 6 7 8 |
public void searchBluetooth(View view) { if(mServiceConnected) { mPSoCCapSenseLedService.scan(); } /* After this we wait for the scan callback to detect that a device has been found */ /* The callback broadcasts a message which is picked up by the mGattUpdateReceiver */ } |
mServiceConnectedは変数で、これをtrueにするのは、MainActivity.javaのonServiceConnected関数。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
private final ServiceConnection mServiceConnection = new ServiceConnection() { /** * This is called when the PSoCCapSenseLedService is connected * * @param componentName the component name of the service that has been connected * @param service service being bound */ @Override public void onServiceConnected(ComponentName componentName, IBinder service) { Log.i(TAG, "onServiceConnected"); mPSoCCapSenseLedService = ((PSoCCapSenseLedService.LocalBinder) service).getService(); mServiceConnected = true; mPSoCCapSenseLedService.initialize(); } |