Giter Site home page Giter Site logo

blog's People

Contributors

ininmm avatar

Watchers

 avatar  avatar

blog's Issues

Android 狀態欄的問題與包袱

最近新專案時發現有許多非純色的狀態列 UI ,於是決定好好研究一下狀態欄的控制管理方式。

問題

image
(版權為 CadabraStudio 所有)

考慮以上 UI 的實作方式,要如何實現這種非純色 / 圖片覆蓋的狀態列 UI 呢?

作法

總結

Android 10 以後的藍牙

最近公司又開了一個藍牙專案,想說把遇到的問題及解決方式紀錄一下。

權限請求

Android 10 以後想使用藍牙需要請求 ACCESS_FINE_LOCATION (看官網),另外如果需要背景使用藍牙也不要忘了請求 ACCESS_BACKGROUD_LOCATION

Android 10 以前,權限請求只會有允許、拒絕以及下次不要詢問的勾選框,如下圖:

image

但是到了 Android 10 之後,新增加了『僅在使用該應用程式時允許』的選項:

image

如果使用者點選的是這個選項的話,在 App 進入背景時是無法使用藍牙的,同理一些背景的 Location 功能也是如此。

因此如果需要背景使用藍牙或是 Location 的話,需要設計流程以確保用戶點選『一律允許』啟用 ACCESS_BACKGROUD_LOCATION

背景掃描

Android 8.1 之後,如果要在螢幕關閉時使用掃描,必須在 Scan 時加上 ScanFilter 參數,但這樣通常會掃得比較慢,因為藍牙需要一個一個進行過濾確認,所以如果有必要的話,建議在前景時掃描不要加太多限制,螢幕關閉後才使用 ScanFilter,相關的 StackOverFlow 討論

在 Android 8 之後,當 App 退到背景狀態時一般的 Scan 掃描是無法掃描到藍牙設備的,必須搭配使用 PendingIntent 並在 Broadcast 接收掃描結果才有辦法,具體作法如下 :

    val settings = ScanSettings.Builder()
            .setScanMode(android.bluetooth.le.ScanSettings.SCAN_MODE_LOW_POWER)
            .build()
    val filters: List<ScanFilter> = mutableListOf(
            ScanFilter.Builder()
                    .setDeviceName(deviceName) // 或是任意的 filter 
                    .build()
    )
    val bluetoothManager = context.applicationContext
            .getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager   
    val scanner = bluetoothManager.adapter.bluetoothLeScanner
    val intent = Intent(context, BLEBackgroundScanReceiver::class.java)
    intent.putExtra("o-scan", true)
    intent.putExtra("device", deviceName)
    
    val result: Int = scanner.startScan(
            filters,
            settings,
            PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
    )

這樣會建立一個 PendingIntent 開始進行掃描,在搜尋到對應的裝置後觸發廣播把資料發送到根據 PendingIntent 定義的 BroadcastReceiver 中。

class BLEBackgroundScanReceiver : BroadcastReceiver() {
    @RequiresApi(Build.VERSION_CODES.O)
    override fun onReceive(context: Context, intent: Intent) {
        val bleCallbackType = intent.getIntExtra(
            BluetoothLeScanner.EXTRA_CALLBACK_TYPE,
            -1
        ) // e.g. ScanSettings.CALLBACK_TYPE_FIRST_MATCH
     
        if (bleCallbackType != -1) {
            val scanResults =
                    intent.getParcelableArrayListExtra<ScanResult>(BluetoothLeScanner.EXTRA_LIST_SCAN_RESULT)
            ...
            ...
        }
    }
}

如此就可以在背景長時間進行 Scan 了,但是,在我們看到 Scan 訊息後,後台運行時間限制開始生效。OS 將在檢測到掃描結果後15分鐘內終止 App,因此,如果我們需要繼續持續進行掃描,系統限制會讓藍牙無法掃描超過 15 分鐘。

長時間的背景掃描

想要長時間的進行背景掃描的話,可以使用 WorkManager 或是 Service

WorkManager 是現在官方比較推薦的背景工作元件,如果掃描的資料需要考慮穩固工作的情境,推薦使用 WorkManager ,不過需要注意的是 WorkManager 最短周期是 15 分鐘,且因為不會立即執行的原因,實際工作的最短周期約 15-25 分鐘 。

如果需要在較短周期內進行背景掃描的話,則可使用 Foreground service + WakeLock 。缺點則是手機的耗電會因此而大幅上升,因此盡可能的選擇 PARTIAL_WAKE_LOCK ,或是只在必要情況下使用這種方式來避免電池消耗過快,也別忘了關閉 App 的電池最佳化。

總結

隨著 Google 對背景工作的限制越來越嚴格,如果要交換較複雜的藍牙資料的話,盡可能的在前景完成,或在背景使用 BLE 長連接的方案。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.