- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个简单的广播接收器从不接收任何东西。没有从 BluetoothDevice 发现设备,也没有从 BluetoothAdapter
开始/停止发现。
在代码的前面,我检查蓝牙是否已启用,BluetoothAdapter
是否正确列出了三个配对设备。我尝试了在手机中手动取消配对的各种变体,并在三个远程设备中打开和关闭蓝牙可见性。但是我的广播接收器没有记录任何内容。我使用布局按钮开始/停止发现。 startDiscovery()
始终返回 true
,cancelDiscovery()
始终返回 false。代码基本上来自Android Bluetooth Dev Guide。
我的代码:
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
再次编辑:更改为:
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): 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
最佳答案
似乎您没有在代码中注册广播接收器:
IntentFilter intFilter = new IntentFilter();
intFilter.addAction(BluetoothDevice.ACTION_FOUND);
intFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
intFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
在你的 onResume 中:
registerReceiver(intentReceiver , intFilter);
在暂停时:
unregisterReceiver(intentReceiver);
关于android - 蓝牙:没有广播 ACTION_FOUND 或 ACTION_DISCOVERY_FINISHED,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9429041/
我已经编写了我的第一个 Android 应用程序并且一切都运行良好真的,除了......在下面的例程中,ACTION_DISCOVERY_FINISHED 似乎从未被调用(或广播 或接收或其他)。无论
我正在通过实现一项任务来实现蓝牙支持,该任务在后台搜索设备并在搜索完成时提供列表。但是,此列表有时包含 No devices found条目(仅当收到 ACTION_DISCOVERY_FINISHE
在我的 android 应用程序中,我正在寻找特定的蓝牙设备。当我找到一个蓝牙设备时,我检查它是否是我要找的那个,如果是,我调用 cancelDiscovery(); 我的问题是:如果我取消发现,我是
这个简单的广播接收器从不接收任何东西。没有从 BluetoothDevice 发现设备,也没有从 BluetoothAdapter 开始/停止发现。 在代码的前面,我检查蓝牙是否已启用,Bluetoo
作为刚开始接触 Android/Java 编程的人,我阅读了 https://developer.android.com/reference/android/bluetooth/BluetoothAd
我是一名优秀的程序员,十分优秀!