Android上的蓝牙:startDiscovery不起作用,我启用了蓝牙
问题描述:
我遇到了使用startDiscovery()的麻烦。这是问题所在,我已经注册了以下意图,以我的广播接收器是这样的:Android上的蓝牙:startDiscovery不起作用,我启用了蓝牙
IntentFilter filterFound = new IntentFilter(BluetoothDevice.ACTION_FOUND);
IntentFilter filterStart = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
IntentFilter filterStop = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(myReceiver, filterFound);
registerReceiver(myReceiver, filterStart);
registerReceiver(myReceiver, filterStop);
这里是我的广播接收器:
private BroadcastReceiver myReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive (myReceiver)");
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Add the name and address to an array adapter to show in a ListView
Log.d(TAG,device.getName() + " " + device.getAddress());
deviceListAdapter.add(device.getName() + "\n" + device.getAddress());
}
if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
Log.d(TAG, "Started discovery");
}
if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
Log.d(TAG, "Finished discovery");
}
}
};
当我打电话startDiscovery我的蓝牙适配器(蓝牙现有和已启用),没有任何反应,我也看不到有关BroadcastReceiver的logcat中的任何内容。
您是否已经遇到过这种问题?任何帮助表示赞赏:)
感谢
答
确保你有你的AndroidManifest.xml相应的权限。特别是,要执行发现,您需要<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
。
答
正如Brad Hein所说,确保AndroidManifest具有蓝牙许可。
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
对于注册蓝牙扫描结果广播接收机请按照以下方法。
IntentFilter bluetoothFilter = new IntentFilter();
bluetoothFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
bluetoothFilter.addAction(BluetoothDevice.ACTION_FOUND);
bluetoothFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(myReceiver, bluetoothFilter);
开始蓝牙发现如下。
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
// No bluetooth Hardware found.
}
if (!bluetoothAdapter.isEnabled()) {
// Bluetooth is OFF
}
if (bluetoothAdapter.isDiscovering()) {
// cancel the discovery if it has already started
bluetoothAdapter.cancelDiscovery();
}
if (bluetoothAdapter.startDiscovery()) {
// bluetooth has started discovery
}
答
试图声明接收器如下:
private class SingBroadcastReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_STARTED)) {
log(getTimeStamp() + ":discovery started");
}
这里是在功能所需要的顺序调用:
1- cancelDiscovery()+ getAdapter()
2-启动接收器如下:
mReceiver = new SingBroadcastReceiver();
IntentFilter ifilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
ifilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
ifilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
this.registerReceiver(mReceiver, ifilter);
3- startDiscovery()