Android & Bluetooth(その1)

公開日: : Android, Java ,

さて、今回は自己満足の自作アプリをBluetoothで制御しようとしハマッたお話です。
大概記事書くときはハマッたお話ばかりです・・・。

まずはこちらの音楽再生アプリは、実はBluetoothガン無視で作ったからBluetoothで音楽聞くとすごい使いづらい・・・。
まぁなんとか音量変更だけは使えるんだけどね。
他のボタンを押すとなぜか他のアプリが自動起動して、邪魔をする・・・。

たとえば再生を押したら別アプリが再生始めて音楽がカオスに152

 

とりあえず環境

  • OS:Android 2.3.7 & 4.1.2
  • Bluetooth端末:SONY DRC-BTN40
  • アプリ:PL PLAYER v1.0.1

Bluetooth端末

 

PL PLAYER
https://play.google.com/store/apps/details?id=com.seiglab.PLPlayer

 

そろそろBluetoothやっちゃう?って事でさっそくハマッた163
まぁ感じとしては、Bluetoothとつながったら適当なクラスでボタン情報を取得してサクサクっと終了だと思っていたんだけど、悪魔が潜んでいた!
それらしいクラス(BluetoothAdapter)を探して、かくかくしかじか。
とりあえずリスナー設定して接続を取得しようかな。

public class AaaActivity extends Activity {
 BluetoothA2dp bluetoothA2dp;
 public void onCreate(Bundle bundle) {
  super.onCreate(bundle);
  BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  BluetoothProfile.ServiceListener profileListener = new BluetoothProfile.ServiceListener() {
   public void onServiceConnected(int profile, BluetoothProfile proxy) {
    if (profile == BluetoothProfile.A2DP) {
     // A2dpが検出された
     bluetoothA2dp = (BluetoothA2dp) proxy;
    }
   }
   public void onServiceDisconnected(int profile) {
    if (profile == BluetoothProfile.A2DP) {
     // A2dpが切断された
     bluetoothA2dp = null;
    }
   }
  };

  // デフォルトアダプタからA2dpデバイスへの接続権を取得する
  bluetoothAdapter.getProfileProxy(this, profileListener, BluetoothProfile.A2DP);
 }
}
さて、実行っと。

あれ?BluetoothProfile.A2DPが「Field requires API level 11 (current min is 10): android.bluetooth.BluetoothProfile#A2DP」になってるじゃねえかぁあああ!

つまりAndroid2.3.7では使えないのね・・・。
次の手としてペアリング情報回りで役に立つものないか手探り。 特にリスナー系はない模様。とりあえず、役に立たないかもだけどペアリング情報取得情報をカキコ。

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();

for (BluetoothDevice device : pairedDevices) {
 BluetoothClass bluetoothClass = device.getBluetoothClass();
 device.getName(); ・・・デバイス名
 bluetoothClass.getDeviceClass(); ・・・デバイスクラス
 bluetoothClass.hasService(Service.AUDIO)); ・・・サービスの確認
}
いろいろ検索しているとBluetoothChat関連のコピペが大量に結果に出て来る。googleさん、いい加減にコピペサイトは削除してくんねえかい?

そんなこんなでUUIDで送信端末と受信端末を接続するんだけど、SPPのUUIDばっかり検索で引っかかりやがる・・・。A2DPの情報があまりにも少なすぎると疑問に思いつつ検索で拾ったUUIDでひたすら接続にチャレンジ。

以下、受信側のソース。

public class Test extends Thread {
 private BluetoothServerSocket bluetoothServerSocket;
 private BluetoothSocket bluetoothSocket;
 private InputStream inputStream;
 private static final String NAME = “tekitou”;
 private static final UUID MYID = UUID.fromString(“UUIDを記入”);

 public Test(BluetoothAdapter bluetoothAdapter) {
  try {
   bluetoothServerSocket = bluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord(NAME, MYID);
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 public void run() {
  try {
   bluetoothSocket = bluetoothServerSocket.accept();
   inputStream = bluetoothSocket.getInputStream();
   Log.d(“debug”, String.valueOf(inputStream.read()));
  } catch (IOException e) {
   e.printStackTrace();
   return;
  }
 }
}

以下、activity。

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

Test test = new Test(bluetoothAdapter);
test.start();

 

結果は、Bluetoothのどのボタンを押しても反応ナッシング!

 

結構、お手上げ状態で次回はごにょごにょして逆コンパイルへ・・・。

 

メイン広告

関連記事

no image

AndroidManifest.xmlのandroid:launchModeについて

今回は、AndroidManifest.xmlのandroid:launchModeについてなんだか

記事を読む

no image

Android開発環境の導入(その4) – Eclipseのインストール

前回は「

記事を読む

no image

Android開発環境の導入(その6) – EclipseのAndroid Plug-inとSDKを連携させる

前回は「

記事を読む

no image

Android開発環境の導入(その5) – Android Plug-inのインストール

前回は「

記事を読む

no image

Android開発環境の導入(その7) – Android端末をPCに認識させる

前回は「

記事を読む

no image

androidをスリープ状態にしたくない!

今回はandroid開発ネタで。動画や音楽再生には必須といえる「スリープ状態にしない」とい対応につい

記事を読む

no image

Android開発環境の導入(その3) – Android SDKのインストール

前回は「

記事を読む

no image

Android開発環境の導入(その2) – JDKのインストール

前回は全体の流れを書いたけど、今回からは具体的な内容へ!   まずは「JDKのインスト

記事を読む

no image

Android開発環境の導入(その1)

自宅資料の中から使いそうなやつを抜粋。   何回かに分けて公開しよう。  

記事を読む

no image

端末の電池の残量をアクティビティに渡したい

アプリを作成している時に、端末の電池の残量を取得したい事があると思います。仕組みは電池の残量を管理す

記事を読む

横長広告

Message

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

メイン広告

no image
SSDの換装

久しぶりです。新しくブログが更新されたと言うことは・・・。 そうです。

CentOS7.2で最小限のインストール

今回は、最新のCentOS 7.2をインストールしてみよう。GUIベー

phpMyAdminの拡張機能を設定しよう

いまさらだけどちょうどサーバ環境を作る機会があったので phpMyAd

PHP7とPHP5を比較してみよう(3)

前回は配列について比較したけど今回はZendFramework、lar

PHP7とPHP5を比較してみよう(2)

前回予告したとおり、今回はPHP5の配列とPHP7の配列・配列に連想配

→もっと見る

PAGE TOP ↑