Android Wear のムダ知識

Preview:

Citation preview

※※ 注意 ※※

Android Wear についてのムダな話をします。

30分間ムダな話にお付き合いください。

いやなら聞かなくていいんだよ?

Android Wear のムダ知識

日本Androidの会 神戸支部野田悟志

ムダな話 その1

名前:

 野田悟志

 @scarviz ←実はもう使ってない

所属:

 日本Androidの会 神戸支部 ←実はまだあった

 GDG神戸←実はあった

OK! Google!

忘れてませんか?

…何が違うの?

なんとかせねば!

'Difficult meeting' by Simon Blackley.CC BY-ND 2.0 http://goo.gl/Oh1LWu

MediaRecorder

Mediarecorderstart failed -2147483648って超やる気のない例外…

↑こんなのとか↑こんなのとか

AV

VideoView

VideoView(とMediaPlayer)Error: 1,-2147483648

MP4(H.264AVC)と3GPP(H.263)サイズは280×280に収まるようにリサイズ済み

Media関連は呼べるけど

ダメっぽい…

http://developer.android.com/intl/ja/training/wearables/apps/index.html

Bluetoothで自由に

通信できる!

Bluetooth機器を検出してみる

これはBTヘッドホン→

AndroidWearを検出させる

ActivityNotFoundExceptionで落ちる

BluetoothAdapter.ACTION_REQUEST_DISCOVERABLEでIntent

投げてもダメっぽい。

'Thinker @ U of L' by Angela http://goo.gl/isFoTCCC BY-NC-ND 2.0

Bluetooth機器として検出されるかどうかは問題じゃない

これで何がしたいかが

問題なんだ

http://www.android.com/wear/

Android4.0

僕もみんなみたいに

「Notificationめっちゃ便利!」

って言ってみたいんだよ!

AccessibilityServiceで拾う

Notification発生

BluetoothServiceで通信

↑まずはここ

Notificationを発生させる

Bluetooth通信の流れ

BT接続する

Notification情報を送る

スマートフォンをWearが検出する

BluetoothAdapter.ACTION_REQUEST_DISCOVERABLEでIntentを投げる

BluetoothSocket.getOutputStreamでNotification情報を書き込む

端末のアドレスからBluetoothAdapter.getRemoteDeviceで、BluetoothDeviceを取得し、createRfcommSocketToServiceRecordで通信できるようにする

IntentFilterとBroadcastReceiverを登録し、BluetoothAdapter.startDiscoveryメソッドで端末を見つける

BluetoothSocket.getInputStreamでNotification情報を読み込む

これらのBluetooth通信はServiceで行う

BluetoothAdapter.listenUsingRfcommWithServiceRecordでBluetoothServerSocketを取得し、acceptで接続を待ち受ける

AccessibilityServiceで拾う

Notification発生

BluetoothServiceで通信

↑できた

Notificationを発生させる←つぎここ

NotificationはAccessibilityServiceで拾う

とはいったものの…

Notification情報がどういう形式でくるのか

全然わかんない

欲しい情報

1. タイトル2. アイコン3. テキスト4. パッケージ名(グループ名にする)

AccessibilityEvent.getParcelableDataから、RemoteViewでNotificationのレイアウト取得

ここからリソースIDにandroid.R.id.title指定タイトルを取得

1. タイトル

タイトルと同じくNotificationのレイアウトから取得する

リソースIDにandroid.R.id.icon指定アイコンを取得

PackageManager.getApplicationIconでアプリアイコンの方が良さそう

2. アイコン

AccessibilityEvent.getTextでテキストを取得

これはTickerに設定されている文字列

NotificationのレイアウトからリソースIDにandroid.R.id.textなどを指定しても空っぽ

3. テキスト

AccessibilityEvent.getPackageNameでパッケージ名を取得

4. パッケージ名

Notification以外にも色々飛んでくる

accessibility_service.xmlを用意してandroid:accessibilityEventTypesに

"typeNotificationStateChanged"を設定

GooglePlayストアの通知や、アップデート可能アプリからの通知は受け取る

それ以外のシステム(プリインストール)のものは無視する

if(!packageName.startsWith("com.android.vending")&& ((ai.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 0)&& ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0)) {

// これらは無視する

}

aiはApplicationInfo(PackageManagerからApplicationInfoを取得)

Notificationの選別

AccessibilityServiceで拾う

Notification発生

BluetoothServiceで通信

↑できた

Notificationを発生させる←できた

受け取った情報をNotification.Builder

に詰めるだけ ↓

Notification.Builder notification = new Notification.Builder(this).setSmallIcon(R.drawable.ic_launcher).setContentTitle(notifyInfo.Title).setContentText(notifyInfo.Text).setGroup(notifyInfo.GroupName).setStyle(new Notification.BigPictureStyle()

.bigPicture(notifyInfo.Icon));

((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).notify(GetNotifyId(), notification.build());

こんな感じ

←bigPictureに設定する

Notificationを送ってみる

できた!!

Android4.0以降とAndroidWearをBluetooth経由で通知するサンプル

https://github.com/scarviz/SampleBTNotification

ご清聴ありがとうございました!

Recommended