我碰巧遇到了这个问题,我的broadcast
接收器不再接收bluetooth
发现开始/停止或设备发现事件的广播。它工作得很好,突然间他们不再工作了。然而,相同的代码在我的moto e
上正常工作(正在捕获发现和新设备查找)。
但为什么不在我的red mi note 3
上,即使它在某个时候有效...
代码就像......
package com.bluetoothtwo;
import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Set;
import java.util.UUID;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_ENABLE_BT = 20;
Set<BluetoothDevice> devicesArray;
BluetoothAdapter mBluetoothAdapter;
ArrayList<BluetoothDevice> pairedDevices;
ArrayList<BluetoothDevice> connectedDevices;
ArrayList<String> displayValues;
//TextView myBondedDevices;
IntentFilter filter;
BroadcastReceiver myreceiver;
ListAdapter listAdapter;
ListView devicesList;
public static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
Handler mHandler;
public static final int SUCCESS_CONNECT = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pairedDevices = new ArrayList<>();
devicesList = (ListView)findViewById(R.id.listView);
displayValues = new ArrayList<>();
listAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,displayValues);
connectedDevices = new ArrayList<>();
pairedDevices = new ArrayList<>();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { // Only ask for these permissions on runtime when running Android 6.0 or higher
switch (ContextCompat.checkSelfPermission(getBaseContext(), Manifest.permission.BLUETOOTH)) {
case PackageManager.PERMISSION_DENIED:
Log.i("permission","bt denied");
break;
case PackageManager.PERMISSION_GRANTED:
Log.i("permission","bt granted");
break;
}
switch (ContextCompat.checkSelfPermission(getBaseContext(), Manifest.permission.BLUETOOTH_ADMIN)) {
case PackageManager.PERMISSION_DENIED:
Log.i("permission","bt admin denied");
break;
case PackageManager.PERMISSION_GRANTED:
Log.i("permission","bt admin granted");
break;
}
}
int MY_PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION = 1;
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
MY_PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
Toast.makeText(this,"No Adapter",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this,"Bluetooth adapter exists",Toast.LENGTH_SHORT).show();
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
Log.i("Printttttt","------>");
getPairedDevices();
}
myreceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i("found","test ------ ");
if(BluetoothDevice.ACTION_FOUND.equals(action)){
Log.i("status","Device found");
String p = "";
BluetoothDevice dev = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
connectedDevices.add(dev);
if(pairedDevices.indexOf(dev)>=0){
p = "(Paired)";
}
displayValues.add(dev.getName()+" "+p+"\n"+dev.getAddress());
((BaseAdapter)listAdapter).notifyDataSetChanged();
}
else if(BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)){
Log.i("status","state chnaged");
if(mBluetoothAdapter.getState() != mBluetoothAdapter.STATE_OFF){
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
else if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){
Log.i("status","Discovery started");
}
else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
Log.i("status","Discovery completed");
}
}
};
Log.i("status",">>>>>>>>>>>>>>>.");
filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
filter.addAction("");
registerReceiver(myreceiver,filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(myreceiver,filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
registerReceiver(myreceiver,filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(myreceiver,filter);
mBluetoothAdapter.startDiscovery();
Log.i("status","////////////");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_CANCELED){
Toast.makeText(this,"cancelled",Toast.LENGTH_SHORT).show();
//finish();
}
else if (resultCode == RESULT_OK){
Toast.makeText(this,"activated",Toast.LENGTH_SHORT).show();
//finish();
getPairedDevices();
}
}
public void getPairedDevices(){
devicesArray = mBluetoothAdapter.getBondedDevices();
if(devicesArray.size()>0){
for(BluetoothDevice a : devicesArray){
pairedDevices.add(a);
}
}
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(myreceiver);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bluetoothtwo">
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<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>