Slide 1

Slide 1 text

AndroidでBLEあれこれ 2016.03.30 @関モバ

Slide 2

Slide 2 text

Who? Twitter: @masanori_msl vaguely: http://mslgt.hatenablog.com/ SearchWakayamaToilet: https://play.google.com/store/apps/details? id=jp.searchwakayamatoilet

Slide 3

Slide 3 text

BLE? Bluetooth Low Energy  Bluetooth4.0規格の一部。  AndroidではOS ver.4.3から対応。 ※Peripheral側(後述)の機能はOS ver.5.0以降の一部端末でのみ 使用可能

Slide 4

Slide 4 text

BLE? 特徴  省電力  Central(Master)とPeripheral(Slave)に分かれて 通信する  etc.

Slide 5

Slide 5 text

Sample Kotlin版  AndroidKtBleController JAVA版  AndroidBleController (更新中...) https://github.com/masanori840816/AndroidKtBleController https://github.com/masanori840816/AndroidBleController

Slide 6

Slide 6 text

最近の変更 Android 6.0 (Central) 「ACCESS_FINE_LOCATION」 または「ACCESS_COARSE_LOCATION」 の権限が必要。 SDK 23 (Central) Android 6.0以上ではGPSが必要。 ※GPSがOFFの場合、Peripheral端末が見つけられない。

Slide 7

Slide 7 text

最近の変更 位置情報へのアクセス権限の要求 AndroidManifest.xml または を追加する。

Slide 8

Slide 8 text

最近の変更 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 , grantResults: IntArray) { when(requestCode){ 0 -> { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { // 権限取得後の処理. } } } }

Slide 9

Slide 9 text

最近の変更

Slide 10

Slide 10 text

最近の変更 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が使用可能になった後の処理. } } } }

Slide 11

Slide 11 text

最近の変更

Slide 12

Slide 12 text

最近の変更 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

Slide 13

Slide 13 text

最近の変更 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 ->

Slide 14

Slide 14 text

最近の変更 LocationAccesser.kt val status = settingsResult.status when (status.statusCode) { LocationSettingsStatusCodes.RESOLUTION_REQUIRED ->{ // GPSがOffならIntent表示. onActivityResultで結果取得. status.startResolutionForResult(activity, 2) } } } } }

Slide 15

Slide 15 text

最近の変更

Slide 16

Slide 16 text

N Releaseでの変更は?

Slide 17

Slide 17 text

N Releaseでの変更?  3/29時点ではBLEに関連した変更はなさそう。  ただしData Saverやマルチウインドウ機能の追 加による影響が考えられる。

Slide 18

Slide 18 text

最後に iOSと比較すると対応端末や安定性などの面で ツラミがある(´;ω;`) が、 ウェアラブルデバイスや他の端末との連携で、 よりよいAndroidライフが...?

Slide 19

Slide 19 text

参考 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

Slide 20

Slide 20 text

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)

Slide 21

Slide 21 text

Thank you.