搜索不到蓝牙设备
使用两台真机测试,从系统设置中可以扫描到另一台手机,但是用app扫描不出来。
package com.fanxin.android.bluetoothchatdemo;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.bluetooth.BluetoothAdapter;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_ENABLE_BT = 10;
private static final String TAG = "MainActivity-app";
private Button pairedButton,scanButton;
private BluetoothAdapter bluetoothAdapater;
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d(TAG,"ACTION: "+action);
if (action.equals(BluetoothDevice.ACTION_FOUND)){
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.d(TAG,"New Device name: "+device.getName());
Log.d(TAG,"New Device addr: "+device.getAddress());
}else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)){
Log.d(TAG,"Discovery Done! ");
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bluetoothAdapater = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapater == null){
Log.e(TAG,"Device not support bluetooth");
}else {
Log.e(TAG,"Device support bluetooth");
}
pairedButton = (Button)findViewById(R.id.id_button_paired_devices);
scanButton = (Button)findViewById(R.id.id_button_scan_devices);
pairedButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Set<BluetoothDevice> pairedDevices = bluetoothAdapater.getBondedDevices();
for (BluetoothDevice device: pairedDevices){
Log.d(TAG,"Device name: "+device.getName());
Log.d(TAG,"Device addr: "+device.getAddress());
}
}
});
scanButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//如果正在扫描,则停止扫描
if(bluetoothAdapater.isDiscovering()){
bluetoothAdapater.cancelDiscovery();
}
bluetoothAdapater.startDiscovery();
}
});
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(broadcastReceiver,filter);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(broadcastReceiver);
}
@Override
protected void onStart() {
super.onStart();
if (!bluetoothAdapater.isEnabled()){
//向系统请求开启蓝牙
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent,REQUEST_ENABLE_BT);
}else {
//已经开启了蓝牙
Toast.makeText(this, "蓝牙已经开启",Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_ENABLE_BT){
Toast.makeText(this,"蓝牙已经开启kkkkk",Toast.LENGTH_SHORT).show();
}
}
}
权限在manifest中已经注册过了
正在回答 回答被采纳积分+1
1、onCreate()方法中,通过BluetoothAdapter.getDefaultAdapter()获取蓝牙adapter对象;
2、onStart()方法中,通过Intent向系统申请蓝牙权限;
3、onActivityResult()中,如果蓝牙权限没有成功finish()当前界面;
4、点击蓝牙图标时跳转到显示附近蓝牙设备的Activity;
5、在显示附近蓝牙设备Activity的onCreate()中,注册发现附近蓝牙的广播
6、在显示附近蓝牙设备Activity的扫描按钮按下时,做如下处理:
7、在显示附近蓝牙设备Activity中定义接收广播更新实现列表的处理
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.fanxin.android.bluetoothchatdemo"> <uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> <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>
- 参与学习 人
- 提交作业 147 份
- 解答问题 687 个
本专题介绍了Android开发核心组件:广播、服务及全局应用。教会你如何使用AIDL、Thread、Socket、蓝牙解决进程线程间通信问题。利用Glide等实现异步加载及NDK原生代码开发。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星