質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Q&A

解決済

2回答

3724閲覧

Service内のクラスで定義したBroadcastReceiverが動作しません。

inca1987

総合スコア20

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

1グッド

0クリップ

投稿2016/10/05 02:30

編集2016/10/05 03:49

前提

1.AndroidのServiceでBluetooth端末の検出を行い、
2.検出したデバイスのリストをAndroidで動作するUnity上のUIに投げ、
3.Unity上で選択したデバイスにAndroidのServiceで接続
という流れを実装中です。

環境

Android: 4.2.2

問題・質問

Service内で定義したBluetooth端末検出・接続用のクラス(BTManager)において、BluetoothAdapter#startDiscovery(端末の検索)のアクションを受け取るレシーバーが動作しません。
アクションの受け取りは、IntentFilterとBroadcastReceiverを用いて下記ソース(関数StartConnect内)のように実装しています。
どこに誤りがあるのか、ご教授いただけると幸いです。

ソース(BTManager)

Java

1import android.bluetooth.BluetoothAdapter; 2import android.bluetooth.BluetoothDevice; 3import android.content.BroadcastReceiver; 4import android.content.Context; 5import android.content.Intent; 6import android.content.IntentFilter; 7 8import java.util.ArrayList; 9import java.util.List; 10import java.util.Set; 11 12/** 13 * Created by inca on 16/10/04. 14 */ 15public class BTManager { 16 private BluetoothAdapter ba; 17 18 private String device_name; 19 20 private BluetoothClientThread bct; 21 private BluetoothServerThread bst; 22 23 private Context service_context; 24 private List<BluetoothDevice> mDeviceList; 25 26 public BTManager(Context service_context){ 27 this.service_context = service_context; 28 mDeviceList = new ArrayList<BluetoothDevice>(); 29 } 30 31 public void SendNode(int index, String name, String addr) {} 32 33 public void SendConnectionResult(boolean is_connect, String msg){} 34 35 public void StartConnect() { 36 System.out.println("in BTManager start_connect"); 37 38 ba = BluetoothAdapter.getDefaultAdapter(); 39 40 Set<BluetoothDevice> pairedDevices = ba.getBondedDevices(); 41 42 if (pairedDevices.size() > 0) { 43 //接続履歴のあるデバイスが存在する 44 System.out.println("with pairred devices"); 45 int index = 0; 46 for (BluetoothDevice device : pairedDevices) { 47 System.out.println("detect device"); 48 mDeviceList.add(device); 49 SendNode(index, device.getName(), device.getAddress()); 50 System.out.println("send to unity: " + index); 51 index += 1; 52 } 53 }else{ 54 //接続履歴のあるデバイスが存在しない 55 System.out.println("without pairred devices"); 56 IntentFilter filter = new IntentFilter(); 57 filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); 58 filter.addAction(BluetoothDevice.ACTION_FOUND); 59 filter.addAction(BluetoothDevice.ACTION_NAME_CHANGED); 60 filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); 61 62 service_context.registerReceiver(DeviceFoundReceiver, filter); 63 64 if(ba.isDiscovering()){ 65 ba.cancelDiscovery(); 66 } 67 68 ba.startDiscovery(); 69 } 70 } 71 72 private final BroadcastReceiver DeviceFoundReceiver = new BroadcastReceiver(){ 73 private int index = 0; 74 75 //検出されたデバイスからのブロードキャストを受ける 76 @Override 77 public void onReceive(Context context, Intent intent){ 78 String action = intent.getAction(); 79 String dName = null; 80 BluetoothDevice foundDevice; 81 82 if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){ 83 System.out.println("device discover start"); 84 } 85 if(BluetoothDevice.ACTION_FOUND.equals(action)){ 86 System.out.println("device found"); 87 foundDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 88 if((dName = foundDevice.getName()) != null){ 89 SendNode(index, dName, foundDevice.getAddress()); 90 mDeviceList.add(foundDevice); 91 } 92 } 93 if(BluetoothDevice.ACTION_NAME_CHANGED.equals(action)){ 94 System.out.println("device found with name change"); 95 foundDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 96 if(foundDevice.getBondState() != BluetoothDevice.BOND_BONDED){ 97 SendNode(index, dName, foundDevice.getAddress()); 98 mDeviceList.add(foundDevice); 99 } 100 } 101 102 if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){ 103 System.out.println("device discover end"); 104 } 105 } 106 }; 107 108 public void Connecting(int index){ 109 System.out.println("index : " + index); 110 System.out.println("mDeviceList.size : " + mDeviceList.size()); 111 BluetoothDevice device = mDeviceList.get(index); 112 bct = new BluetoothClientThread(device, ba){ 113 @Override 114 public void CallbackConnectionResult(boolean is_connect, String msg){ 115 SendConnectionResult(is_connect, msg); 116 } 117 }; 118 119 bct.start(); 120 } 121 122} 123

ソース(Serviceクラス該当部分)

Java

1public class ConnectionService extends Service { 2 3 static final String TAG="LocalService"; 4 5 private final BTManager mBTManager = new BTManager(this){ 6 @Override 7 public void SendNode(int index, String name, String addr) { 8 MessageToUnity mtu = new MessageToUnity("node", index, name, addr, false); 9 try { 10 String mtu_json = LoganSquare.serialize(mtu); 11 mTCPClientForUnity.SendToUnity(mtu_json); 12 }catch (IOException e){ 13 e.getStackTrace(); 14 } 15 } 16 17 @Override 18 public void SendConnectionResult(boolean is_connect, String msg){ 19 MessageToUnity mtu = new MessageToUnity("connection", 0, "", "", is_connect); 20 try { 21 String mtu_json = LoganSquare.serialize(mtu); 22 mTCPClientForUnity.SendToUnity(mtu_json); 23 }catch (IOException e){ 24 e.getStackTrace(); 25 } 26 } 27 };

追記です。BTManager内でのservice_contextは、Serviceクラスのthisから得ています。

GeeChiki👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

大変申し訳ありません。BluetoothがOFFになっていたようです。
お目汚し失礼いたしました。

投稿2016/10/05 03:58

inca1987

総合スコア20

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

レシーバークラスのクラス宣言の修飾子をpublicとstaticにするとどうなりますか?

投稿2016/10/05 02:43

yona

総合スコア18155

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

inca1987

2016/10/05 03:45

public final BroadcastReceiver DeviceFoundReceiver...として試しましたが、変わらずBroadcastReceiver内のonReceiveは呼び出されていないようです。 BTManagerクラスを持つServiceクラスのコードを追記しました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問