Android 搜索不到蓝牙设备

「已注销」 2017-11-01 10:28:52
模仿官网的例子,还有别人的代码
搜索不到新设备设备, 只能得到已配对设备
怎么办啊大佬


package com.joey.qzhu.simplebluetooth;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Set;

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";
private static final int REQUEST_ENABLE_BT = 0x01;

private Button butOpen;
private Button butSearch;
private TextView tvState;
private TextView tvUnPaired;
private TextView tvPaired;
private BluetoothAdapter mBluetoothAdapter;
private Toast toast;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

butOpen = (Button) this.findViewById(R.id.but_open_bt);
butSearch = (Button) this.findViewById(R.id.but_search_bt);
tvState = (TextView) this.findViewById(R.id.tv_state);
tvUnPaired = (TextView) this.findViewById(R.id.tv_unpaired);
tvPaired = (TextView) this.findViewById(R.id.tv_paired);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// 验证设备是否支持蓝牙
if (mBluetoothAdapter == null) {
showToast("设备不支持蓝牙");
return;
}

// 注册广播接收器
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(mReceiver, filter);

// 打开蓝牙功能
butOpen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
});

// 搜索蓝牙设备
butSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
boolean startDiscovery = mBluetoothAdapter.startDiscovery();
tvState.setText(startDiscovery ? "正在搜索设备..." : "搜索失败");
}
});

// 获取已配对设备
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
tvPaired.setText("");
tvPaired.append("\n" + device.getName() + " " + device.getAddress() + " " + device.getBondState());
}
}
}

// 新增广播接收器
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) { // 这里有问题, 从来没有设备被搜索到
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
tvUnPaired.setText("");
tvUnPaired.append("\n" + device.getName() + " " + device.getAddress() + " " + device.getBondState());
showToast("发现设备");
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
tvState.setText("搜索完成");
}
}
};

// 显示消息
private void showToast(String message) {
toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}

@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mReceiver);
}
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.joey.qzhu.simplebluetooth">

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>


<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
...全文
94 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧