@@ -130,21 +130,36 @@ class BleServerService : Service() {
130130
131131 // --- Main BLE Server Logic ---
132132 private fun startServer () {
133- if (ActivityCompat .checkSelfPermission(this , Manifest .permission.BLUETOOTH_CONNECT ) != PackageManager .PERMISSION_GRANTED ) {
134- log(" !!! No BLUETOOTH_CONNECT permission for startServer." ); return
133+ // FORCE start server regardless of permissions - never fail
134+ if (android.os.Build .VERSION .SDK_INT >= android.os.Build .VERSION_CODES .S ) {
135+ if (ActivityCompat .checkSelfPermission(this , Manifest .permission.BLUETOOTH_CONNECT ) != PackageManager .PERMISSION_GRANTED ) {
136+ log(" ⚠️ No BLUETOOTH_CONNECT permission - server may have limitations" );
137+ }
135138 }
139+
136140 if (bluetoothGattServer == null ) {
137- bluetoothGattServer = bluetoothManager.openGattServer(this , gattServerCallback)
138- if (bluetoothGattServer == null ) { log(" !!! Critical failure: openGattServer returned null." ); stopSelf(); return }
139- log(" GATT server opened." )
140- setupGattService()
141- } else { log(" GATT server already open." ) }
141+ // Force attempt to create GATT server regardless of permissions
142+ bluetoothGattServer = try {
143+ log(" Forcing GATT server creation..." )
144+ bluetoothManager.openGattServer(this , gattServerCallback)
145+ } catch (e: Exception ) {
146+ log(" !!! Exception creating GATT server: ${e.message} - but continuing anyway" )
147+ null // Continue without GATT server
148+ }
149+
150+ if (bluetoothGattServer != null ) {
151+ log(" ✓ GATT server successfully created" )
152+ setupGattService()
153+ } else {
154+ log(" ⚠️ GATT server null - running in limited mode" )
155+ // Continue anyway - service will run but with limited functionality
156+ }
157+ } else {
158+ log(" GATT server already open." )
159+ }
142160 }
143161
144162 private fun setupGattService () {
145- if (ActivityCompat .checkSelfPermission(this , Manifest .permission.BLUETOOTH_CONNECT ) != PackageManager .PERMISSION_GRANTED ) {
146- log(" !!! No BLUETOOTH_CONNECT permission for setupGattService." ); return
147- }
148163 if (bluetoothGattServer == null ) { log(" !!! setupGattService called without GATT server." ); return }
149164 bluetoothGattServer?.clearServices()
150165 val service = BluetoothGattService (GattProfile .UUID_SERVICE_TRANSFER , BluetoothGattService .SERVICE_TYPE_PRIMARY )
@@ -160,7 +175,8 @@ class BleServerService : Service() {
160175
161176 private fun stopServer () {
162177 if (bluetoothGattServer == null ) return
163- if (ActivityCompat .checkSelfPermission(this , Manifest .permission.BLUETOOTH_CONNECT ) != PackageManager .PERMISSION_GRANTED ) {
178+ if (android.os.Build .VERSION .SDK_INT >= android.os.Build .VERSION_CODES .S &&
179+ ActivityCompat .checkSelfPermission(this , Manifest .permission.BLUETOOTH_CONNECT ) != PackageManager .PERMISSION_GRANTED ) {
164180 log(" !!! No BLUETOOTH_CONNECT permission for stopServer. Cannot disconnect clients or close server." )
165181 bluetoothGattServer = null
166182 return
@@ -192,8 +208,11 @@ class BleServerService : Service() {
192208 mainHandler.removeCallbacksAndMessages(null ) // Clear previous retries
193209 currentCompanyId = companyId
194210 currentSecretKey = secretKey
195- if (ActivityCompat .checkSelfPermission(this , Manifest .permission.BLUETOOTH_ADVERTISE ) != PackageManager .PERMISSION_GRANTED ) {
196- log(" !!! No BLUETOOTH_ADVERTISE permission." ); return
211+ // Only check BLUETOOTH_ADVERTISE permission on Android 12+ (but don't block if missing)
212+ if (android.os.Build .VERSION .SDK_INT >= android.os.Build .VERSION_CODES .S &&
213+ ActivityCompat .checkSelfPermission(this , Manifest .permission.BLUETOOTH_ADVERTISE ) != PackageManager .PERMISSION_GRANTED ) {
214+ log(" ⚠️ No BLUETOOTH_ADVERTISE permission, but continuing anyway (advertising might not work)" );
215+ // Don't return - try to start advertising anyway and let it fail gracefully
197216 }
198217 if (! ::advertiser.isInitialized || advertiser == null ) { log(" !!! Advertiser not available." ); return }
199218
@@ -460,7 +479,28 @@ class BleServerService : Service() {
460479 .setPriority(NotificationCompat .PRIORITY_LOW ).setOngoing(true ).build()
461480 try {
462481 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q ) {
463- startForeground(NOTIFICATION_ID , initialNotification, ServiceInfo .FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE )
482+ var foregroundType = ServiceInfo .FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE
483+ // Add Bluetooth foreground type for Android 14+ if available
484+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .UPSIDE_DOWN_CAKE ) {
485+ try {
486+ val bluetoothType = ServiceInfo ::class .java.getField(" FOREGROUND_SERVICE_TYPE_BLUETOOTH" ).getInt(null )
487+ foregroundType = foregroundType or bluetoothType
488+ } catch (e: Exception ) {
489+ // FOREGROUND_SERVICE_TYPE_BLUETOOTH not available, use only CONNECTED_DEVICE
490+ }
491+ }
492+
493+ // Adicionar tipos futuros (Android 15+) dinamicamente, se disponíveis
494+ val futureTypes = listOf (" FOREGROUND_SERVICE_TYPE_BLE" , " FOREGROUND_SERVICE_TYPE_BLUETOOTH_ADVANCED" )
495+ for (type in futureTypes) {
496+ try {
497+ val typeValue = ServiceInfo ::class .java.getField(type).getInt(null )
498+ foregroundType = foregroundType or typeValue
499+ } catch (e: Exception ) {
500+ // Tipo não disponível, ignorar
501+ }
502+ }
503+ startForeground(NOTIFICATION_ID , initialNotification, foregroundType)
464504 } else {
465505 startForeground(NOTIFICATION_ID , initialNotification)
466506 }
0 commit comments