Upgrade to Pro — share decks privately, control downloads, hide ads and more …

AndroidでBLEあれこれ

 AndroidでBLEあれこれ

2016.03.30 関西モバイルアプリ研究会 #12 用資料です。
Android6.0以降のBLEの変更について。

masanori_msl

March 29, 2016
Tweet

More Decks by masanori_msl

Other Decks in Programming

Transcript

  1. 最近の変更 Android 6.0 (Central) 「ACCESS_FINE_LOCATION」 または「ACCESS_COARSE_LOCATION」 の権限が必要。 SDK 23 (Central)

    Android 6.0以上ではGPSが必要。 ※GPSがOFFの場合、Peripheral端末が見つけられない。
  2. 最近の変更 CentralActivity.kt @TargetApi(Build.VERSION_CODES.M) private fun requestBlePermission() { if (! checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION)

    == PackageManager.PERMISSION_GRANTED) { // 権限がなければリクエスト. requestPermissions(arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION), 0) } } override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String> , grantResults: IntArray) { when(requestCode){ 0 -> { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { // 権限取得後の処理. } } } }
  3. 最近の変更 BluetoothをONにする CentralActivity.kt private fun requestBleOn(){ if(! bleAdapter!!.isEnabled) { //

    BluetoothがOFFならインテントを表示して設定を促す. startActivityForResult(Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE), 1) } } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?){ super.onActivityResult(requestCode, resultCode, data) when(requestCode) { 1 -> { if(bleAdapter!!.isEnabled()){ // Bluetoothが使用可能になった後の処理. } } } }
  4. 最近の変更 GPSをONにする LocationAccesser.kt class LocationAccesser : GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { private

    var apiClient: GoogleApiClient? = null override fun onConnectionFailed(result: ConnectionResult) { } override fun onConnectionSuspended(cause: Int) { } override fun onConnected(bundle: Bundle?) { } fun checkIsGpsOn(activity: CentralActivity) { val locationRequest = LocationRequest.create() locationRequest.priority = LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY locationRequest.interval = 3000L locationRequest.fastestInterval = 500L
  5. 最近の変更 LocationAccesser.kt val builder = LocationSettingsRequest.Builder() .addLocationRequest(locationRequest) builder.setAlwaysShow(true) if (apiClient

    == null) { apiClient = GoogleApiClient .Builder(activity.applicationContext) .enableAutoManage(activity, this) .addApi(LocationServices.API) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build() } // GPSがONかどうかを確認する. val result = LocationServices.SettingsApi.checkLocationSettings(apiClient, builder.build()) result.setResultCallback { settingsResult ->
  6. 最近の変更 LocationAccesser.kt val status = settingsResult.status when (status.statusCode) { LocationSettingsStatusCodes.RESOLUTION_REQUIRED

    ->{ // GPSがOffならIntent表示. onActivityResultで結果取得. status.startResolutionForResult(activity, 2) } } } } }
  7. 参考 iOS×BLE Core Bluetoothプログラミング Getting Started with Bluetooth Low Energy

    Bluetooth Low Energy - Android Developers First Preview of Android N: Developer APIs & Tools - Android Developers Blog https://developer.android.com/guide/topics/connectivity/blueto oth-le.html http://www.socym.co.jp/book/973 http://shop.oreilly.com/product/0636920033011.do http://android-developers.blogspot.jp/2016/03/first-preview-of- android-n-developer.html
  8. Credit Kotlin  Main Page:  License: Material icons 

    Main Page:  License:  Main Page:  License: Google Noto Fonts https://github.com/JetBrains/kotlin Apache License 2.0 https://design.google.com/icons/ https://www.google.com/get/noto/ CC-BY License SIL Open Font License (OFL)