Ací es mostren les diferències entre la revisió seleccionada i la versió actual de la pàgina.
|
android_ble [2026/01/10 16:26] enric_mieza_sanchez creat |
android_ble [2026/01/10 16:37] (actual) enric_mieza_sanchez |
||
|---|---|---|---|
| Línia 1: | Línia 1: | ||
| - | ====== Android i BLE (Bluetooth Low Emission) ====== | + | ~~GOTO>android_bluetooth~~ |
| - | + | ||
| - | {{ android-bluetooth.jpg? | + | |
| - | + | ||
| - | En el cas del BLE podriem optar per 2 estratègies diferents: | + | |
| - | + | ||
| - | * Escanejar els dispositius des de la nostra app. | + | |
| - | * Llista i triar entre els dispositius que han estat aparellats al sistema operatiu. | + | |
| - | + | ||
| - | + | ||
| - | + | ||
| - | ===== Llistar els dispositius emparellats ===== | + | |
| - | + | ||
| - | Obtenir aquesta llista és senzill accedint '' | + | |
| - | + | ||
| - | <code kotlin> | + | |
| - | fun updatePairedDevices() { | + | |
| - | // empty list | + | |
| - | dataset.clear() | + | |
| - | + | ||
| - | // update list | + | |
| - | val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter() | + | |
| - | for( elem in bluetoothAdapter.bondedDevices.filter { device -> | + | |
| - | // Filtrar per dispositius BLE | + | |
| - | device.type == BluetoothDevice.DEVICE_TYPE_LE || | + | |
| - | device.type == BluetoothDevice.DEVICE_TYPE_DUAL || | + | |
| - | device.type == BluetoothDevice.DEVICE_TYPE_UNKNOWN | + | |
| - | } ) { | + | |
| - | // afegim element al dataset | + | |
| - | dataset.add( elem ) | + | |
| - | } | + | |
| - | } | + | |
| - | </ | + | |
| - | + | ||
| - | \\ | + | |
| - | + | ||
| - | ===== Gestió de permisos ===== | + | |
| - | + | ||
| - | La gestió de permisos del Bluetooth és una part més engorrosa. | + | |
| - | + | ||
| - | Primer de tot, declarar els permisos estàtics a '' | + | |
| - | + | ||
| - | <code xml> | + | |
| - | < | + | |
| - | < | + | |
| - | < | + | |
| - | + | ||
| - | <!-- Per a Android 12 o superior --> | + | |
| - | < | + | |
| - | < | + | |
| - | </ | + | |
| - | + | ||
| - | + | ||
| - | Seguidament, | + | |
| - | + | ||
| - | + | ||
| - | <code kotlin> | + | |
| - | private fun requestBluetoothPermissionAndUpdate() { | + | |
| - | val permission = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { | + | |
| - | // Android 12+ requereix BLUETOOTH_CONNECT | + | |
| - | Manifest.permission.BLUETOOTH_CONNECT | + | |
| - | } else { | + | |
| - | // Versions anteriors | + | |
| - | Manifest.permission.BLUETOOTH | + | |
| - | } | + | |
| - | + | ||
| - | if (ContextCompat.checkSelfPermission(this, | + | |
| - | PackageManager.PERMISSION_GRANTED) { | + | |
| - | + | ||
| - | // Demanar el permís | + | |
| - | ActivityCompat.requestPermissions( | + | |
| - | this, | + | |
| - | arrayOf(permission), | + | |
| - | REQUEST_CODE_BLUETOOTH | + | |
| - | ) | + | |
| - | } else { | + | |
| - | // Permís ja concedit - llegir dispositius | + | |
| - | updatePairedDevices() | + | |
| - | } | + | |
| - | } | + | |
| - | + | ||
| - | override fun onRequestPermissionsResult( | + | |
| - | requestCode: | + | |
| - | permissions: | + | |
| - | grantResults: | + | |
| - | ) { | + | |
| - | super.onRequestPermissionsResult(requestCode, | + | |
| - | + | ||
| - | if (requestCode == REQUEST_CODE_BLUETOOTH) { | + | |
| - | if (grantResults.isNotEmpty() && grantResults[0] == | + | |
| - | PackageManager.PERMISSION_GRANTED) { | + | |
| - | // Permís concedit - llegir dispositius | + | |
| - | updatePairedDevices() | + | |
| - | } else { | + | |
| - | // Permís denegat | + | |
| - | Toast.makeText(this, | + | |
| - | Toast.LENGTH_SHORT).show() | + | |
| - | } | + | |
| - | } | + | |
| - | } | + | |
| - | </code> | + | |