蓝牙:对于ACTION_FOUND或ACTION_DISCOVERY_FINISHED没有广播 [英] Bluetooth: No broadcast for ACTION_FOUND or ACTION_DISCOVERY_FINISHED

查看:5414
本文介绍了蓝牙:对于ACTION_FOUND或ACTION_DISCOVERY_FINISHED没有广播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个简单的广播接收器没有收到任何东西。从BluetoothDevice类没有发现设备并没有启动/发现从站 BluetoothAdapter

在code早些时候,我检查已启用蓝牙和 BluetoothAdapter 正确地列出三个配对设备。我试过在电话手动解除配对其中的变化和我打开和关闭蓝牙可见三个远程设备。但没有什么是从我的广播接收器记录下来。我开始/停止与发现布局的按钮。 startDiscovery()总是返回真正 cancelDiscovery()总是返回false。在code基本上是从Android蓝牙开发指南。

我的code:

 包intrax.three;进口java.util.Set中;
进口android.app.Activity;
进口android.bluetooth.BluetoothAdapter;
进口android.bluetooth.BluetoothDevice;
进口android.content.BroadcastReceiver;
进口android.content.ComponentName;
进口android.content.Context;
进口android.content.Intent;
进口android.content.IntentFilter;
进口android.content.ServiceConnection;
进口android.os.Bundle;
进口android.os.IBinder;
进口android.os.SystemClock;
进口android.util.Log;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.TextView;
进口android.widget.Toast;
进口intrax.three.R;公共类BtAct延伸活动{
    最后的字符串标记=BtAct;
    最终诠释ENABLE_BLUETOOTH_REQ = 1;
    最终的String [] STATENAMES = {断开连接,连接,连接,断开,国家未定义,国家未定义,国家未定义,国家未定义,国家未定义,国家未定义,关,打开,开,关闭};
长discoveryStartTime = 0;BluetoothAdapter btAdapter;
IntentFilter的IntentFilter的;保护无效的onCreate(捆绑savedInstanceState){
    Log.d(TAG的onCreate);
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.btlayout);    btAdapter = BluetoothAdapter.getDefaultAdapter();    / *设备自身蓝牙* /
    如果(!btAdapter.isEnabled()){
        意图enableBluetoothIntent =新意图(btAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBluetoothIntent,ENABLE_BLUETOOTH_REQ);
        Log.v(TAG的设备自身蓝牙未启用。);
    }
    其他{
        Log.v(TAG,该设备自身的蓝牙激活。);
        串btOwnAddress = btAdapter.getAddress();
        串btOwnName = btAdapter.getName();
        INT btOwnState = btAdapter.getState();
        Log.v(TAG,btOwnAddress ++ btOwnName);
        Log.v(TAG,STATENAMES [btOwnState]);
    }    / **列表配对设备** /
    Log.v(TAG,检查配对设备:);
    discoveryStartTime = SystemClock.uptimeMillis();
    SET<&BluetoothDevice类GT; allPairedDevices = btAdapter.getBondedDevices();
    Log.v(TAG,花+(SystemClock.uptimeMillis() - discoveryStartTime +毫秒获得配对设备STARTTIME =+ discoveryStartTime));
    如果(allPairedDevices.size()大于0){
        对于(BluetoothDevice类pairedDevice:allPairedDevices){
            Log.v(TAG,pairedDevice.getName()++ pairedDevice.getAddress()+被发现);
        }
    }    / *远程蓝牙* /
    意图discoverableIntent =新意图(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,0);
    startActivity(discoverableIntent);    IntentFilter的=新的IntentFilter();
    intentFilter.addAction(ACTION_FOUND);
    intentFilter.addAction(ACTION_DISCOVERY_STARTED);
    intentFilter.addAction(ACTION_DISCOVERY_FINISHED);    /* 用户界面 */
    按钮bStartScan =(按钮)findViewById(R.id.buttonStartScan);
    bStartScan.setOnClickListener(新OnClickListener(){
        公共无效的onClick(视图v){
            Log.v(TAGintentReceiver =+ intentReceiver);
            Log.v(TAG,发现开始);
            布尔BOL = btAdapter.startDiscovery();
            Log.v(TAG,从发现返回,开始=+ BOL);
            Log.v(TAG,使光盘=+ btAdapter.isDiscovering());
        }
    }); // END bStart
    按钮bStopScan =(按钮)findViewById(R.id.buttonStopScan);
    bStopScan.setOnClickListener(新OnClickListener(){
        公共无效的onClick(视图v){
            Log.v(TAGbStopScan点击);
            如果(btAdapter.isDiscovering()){
                 布尔BOL = btAdapter.cancelDiscovery();
                    Log.v(TAG,发现取消=+ BOL);
            };
            Log.v(TAG,仍然发现=+ btAdapter.isDiscovering());
        }
    }); // END bStop层
} // END的onCreate
私人广播接收器intentReceiver =新的广播接收器(){//抽象类
    公共无效的onReceive(上下文的背景下,意图receivedIntent){
        Log.v(TAG,进入intentReceiver);
        如果(BluetoothDevice.ACTION_FOUND.equals(receivedIntent.getAction()))
        {
            ; - Log.v(discoveryStartTime)TAG,+(SystemClock.uptimeMillis()之后=找到)
            BluetoothDevice类foundDevice = receivedIntent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            Log.v(TAG,foundDevice.getName()++ foundDevice.getAddress()+被发现);
            BluetoothDevice类foundDeviceClass = receivedIntent.getParcelableExtra(BluetoothDevice.EXTRA_CLASS);
            Log.v(TAG,BT类:+​​ foundDeviceClass.toString());
        }
        否则如果(btAdapter.ACTION_DISCOVERY_STARTED.equals(receivedIntent.getAction())){
            discoveryStartTime = SystemClock.uptimeMillis();
        }
        否则如果(btAdapter.ACTION_DISCOVERY_FINISHED.equals(receivedIntent.getAction())){
            Log.v(TAG,发现历时:+(SystemClock.uptimeMillis() - discoveryStartTime +毫秒开始时间=+ discoveryStartTime));
        }
        Log.v(TAGintentReceiver完成);
    } // END的onReceive
}; // END广播接收器保护无效的onActivityResult(INT申请code,INT结果code,意图resultIntent){
    super.onActivityResult(要求code,结果code,resultIntent);
    开关(要求code){
    案例ENABLE_BLUETOOTH_REQ:
        如果(结果code == RESULT_OK){
            Log.v(TAG,通过蓝牙申请成功启用);
        }
        其他{
            Log.v(TAG,未启用蓝牙整理。);
            完();
        }
    } // END开关
} // END的onActivityResult保护无效onResume(){
    Log.v(TAGonResume);
    super.onResume();
    registerReceiver(intentReceiver,IntentFilter的);
    Log.v(TAGintentReceiver =+ intentReceiver);
}保护无效的onPause(){
    Log.v(TAG的onPause);
    super.onPause();
    如果(intentReceiver!= NULL){
        Log.v(TAG,intentReceiver而注销);
        unregisterReceiver(intentReceiver);
    }
}
} // END级

再次编辑:
更改为:

  intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);

当我在一个物理按钮,点击配对的远程设备(耳机)中的一个,虽然作为接收仍然没有被记录以下记录。我想,这是一些其他的应用程序或操作系统,这不是我的应用程序:

  02-24 14:12:23.096:D / InputMethodManager(28164):dispatchKeyEvent
02-24 14:12:23.096:V / InputMethodManager(28164):调度KEY:com.android.internal.view.IInputMethodSession$Stub$Proxy@47c89938
02-24 14:12:23.156:D / InputMethodManager(28164):dispatchKeyEvent
02-24 14:12:23.156:V / InputMethodManager(28164):调度KEY:com.android.internal.view.IInputMethodSession$Stub$Proxy@47c89938


解决方案

好像你是不是在你的code注册广播接收器:

 的IntentFilter intFilter =新的IntentFilter();
intFilter.addAction(BluetoothDevice.ACTION_FOUND);
intFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
intFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);

在您的onResume:

  registerReceiver(intentReceiver,intFilter);

在的onPause:

  unregisterReceiver(intentReceiver);

This simple broadcastreceiver never receives anything. No discovered device from BluetoothDevice and no start/stop of discovery from BluetoothAdapter.

Earlier in the code, I check that Bluetooth is enabled and the BluetoothAdapter correctly lists the three paired devices. I've tried variations of unpairing them manually in the phone and I turn bluetooth visibility in the three remote devices on and off. But nothing is logged from my broadcast receiver. I start/stop discovery with layout buttons. startDiscovery() always returns true, cancelDiscovery() always returns false. The code is basically from the Android Bluetooth Dev Guide.

My code:

package intrax.three;

import java.util.Set;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import intrax.three.R;

public class BtAct extends Activity {
    final String TAG = "BtAct";
    final int ENABLE_BLUETOOTH_REQ = 1;
    final String[] STATENAMES = {"Disconnected","Connecting","Connected","Disconnecting","STATE UNDEFINED","STATE UNDEFINED","STATE UNDEFINED","STATE UNDEFINED","STATE UNDEFINED","STATE UNDEFINED","Off","Turning on","On","Turning off"};
long discoveryStartTime = 0;

BluetoothAdapter btAdapter;
IntentFilter intentFilter;

protected void onCreate(Bundle savedInstanceState) {
    Log.d(TAG, "onCreate");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.btlayout);

    btAdapter = BluetoothAdapter.getDefaultAdapter();

    /* Device's own Bluetooth */
    if (!btAdapter.isEnabled()) {
        Intent enableBluetoothIntent = new Intent(btAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBluetoothIntent, ENABLE_BLUETOOTH_REQ);
        Log.v(TAG, "The device's own bluetooth is NOT enabled.");
    }
    else {
        Log.v(TAG, "The device's own bluetooth IS enabled.");
        String  btOwnAddress =  btAdapter.getAddress();
        String  btOwnName =         btAdapter.getName();
        int         btOwnState =        btAdapter.getState();
        Log.v(TAG, btOwnAddress+" "+btOwnName);
        Log.v(TAG, STATENAMES[btOwnState]);
    }

    /** List paired devices **/
    Log.v(TAG, "Check for paired devices:");
    discoveryStartTime = SystemClock.uptimeMillis();
    Set<BluetoothDevice> allPairedDevices = btAdapter.getBondedDevices();
    Log.v(TAG, "It took "+(SystemClock.uptimeMillis()-discoveryStartTime+"ms to get paired devices. Starttime="+discoveryStartTime));
    if (allPairedDevices.size() > 0) {
        for (BluetoothDevice pairedDevice : allPairedDevices) {
            Log.v(TAG, pairedDevice.getName()+" "+pairedDevice.getAddress()+" was found");
        }
    }

    /* Remote bluetooth */
    Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,0);
    startActivity(discoverableIntent);

    intentFilter = new IntentFilter();
    intentFilter.addAction("ACTION_FOUND");
    intentFilter.addAction("ACTION_DISCOVERY_STARTED");
    intentFilter.addAction("ACTION_DISCOVERY_FINISHED");

    /* User interface */
    Button bStartScan =(Button)findViewById(R.id.buttonStartScan);
    bStartScan.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Log.v(TAG, "intentReceiver="+intentReceiver);
            Log.v(TAG, "Discovery started");
            boolean bol = btAdapter.startDiscovery();
            Log.v(TAG, "Returned from discovery, start="+bol);
            Log.v(TAG, "Disc enabled="+btAdapter.isDiscovering());
        }
    });//END bStart
    Button bStopScan =(Button)findViewById(R.id.buttonStopScan);
    bStopScan.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Log.v(TAG, "bStopScan clicked");
            if (btAdapter.isDiscovering()) {
                 boolean bol = btAdapter.cancelDiscovery();
                    Log.v(TAG, "Discovery canceled="+bol);
            };
            Log.v(TAG, "still discovering="+btAdapter.isDiscovering());
        }
    });//END bStop


}//END onCreate


private BroadcastReceiver intentReceiver = new BroadcastReceiver() {  // Abstract class
    public void onReceive(Context context, Intent receivedIntent) {
        Log.v(TAG, "Entered intentReceiver");
        if (BluetoothDevice.ACTION_FOUND.equals(receivedIntent.getAction())) 
        {
            Log.v(TAG, "Found after="+(SystemClock.uptimeMillis()-discoveryStartTime));
            BluetoothDevice foundDevice = receivedIntent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            Log.v(TAG, foundDevice.getName()+" "+foundDevice.getAddress()+" was found");
            BluetoothDevice foundDeviceClass = receivedIntent.getParcelableExtra(BluetoothDevice.EXTRA_CLASS);
            Log.v(TAG, "BT class: "+foundDeviceClass.toString());
        }
        else if (btAdapter.ACTION_DISCOVERY_STARTED.equals(receivedIntent.getAction())) {
            discoveryStartTime = SystemClock.uptimeMillis();
        }
        else if (btAdapter.ACTION_DISCOVERY_FINISHED.equals(receivedIntent.getAction())) {
            Log.v(TAG, "Discovery lasted: "+(SystemClock.uptimeMillis()-discoveryStartTime+"ms Starttime="+discoveryStartTime));
        }
        Log.v(TAG, "intentReceiver finished");
    }//END onReceive
};//END BroadcastReceiver

protected void onActivityResult(int requestCode, int resultCode, Intent resultIntent) {
    super.onActivityResult(requestCode, resultCode, resultIntent);
    switch(requestCode) {
    case ENABLE_BLUETOOTH_REQ:
        if(resultCode==RESULT_OK) {
            Log.v(TAG, "Bluetooth successfully enabled by request");
        }
        else {
            Log.v(TAG, "Bluetooth was not enabled. Finishing.");
            finish();
        }
    }//END switch
}//END onActivityResult

protected void onResume() {
    Log.v(TAG, "onResume");
    super.onResume();
    registerReceiver(intentReceiver, intentFilter);
    Log.v(TAG, "intentReceiver="+intentReceiver);
}

protected void onPause() {
    Log.v(TAG, "onPause");
    super.onPause();
    if (intentReceiver != null) {
        Log.v(TAG, "intentReceiver to be unregistered");
        unregisterReceiver(intentReceiver);
    }
}
}//END Class

EDIT AGAIN: Changed to:

intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);

The following is logged when I click on a physical button on one of the paired remote devices (a headphone), although still nothing is logged as received. I suppose that it is some other app or the OS, it's not my app:

02-24 14:12:23.096: D/InputMethodManager(28164): dispatchKeyEvent
02-24 14:12:23.096: V/InputMethodManager(28164): DISPATCH KEY: com.android.internal.view.IInputMethodSession$Stub$Proxy@47c89938
02-24 14:12:23.156: D/InputMethodManager(28164): dispatchKeyEvent
02-24 14:12:23.156: V/InputMethodManager(28164): DISPATCH KEY: com.android.internal.view.IInputMethodSession$Stub$Proxy@47c89938

解决方案

seems like you are not registering broadcast receivers in your code:

IntentFilter intFilter = new IntentFilter();
intFilter.addAction(BluetoothDevice.ACTION_FOUND);
intFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
intFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);

in your onResume:

registerReceiver(intentReceiver , intFilter);

in onPause:

unregisterReceiver(intentReceiver);

这篇关于蓝牙:对于ACTION_FOUND或ACTION_DISCOVERY_FINISHED没有广播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆