Android蓝牙:startDiscovery()不起作用

时间:2016-05-23 06:43:58

标签: android bluetooth

import android.app.Service;
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.os.Handler;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;


public class BluetoothService extends Service {
BroadcastReceiver mReceiver;
final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
public static final String TAG = "bluetooth_service";

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
    this.mReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            Log.d(TAG, action);
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                Log.d(TAG, device.getName());
            } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                if (mBluetoothAdapter != null) {
                    if (mBluetoothAdapter.isEnabled()) {
                        mBluetoothAdapter.disable();
                    }
                }
                stopSelf();
            }
        }
    };
    IntentFilter filter = new IntentFilter();
    filter.addAction(BluetoothDevice.ACTION_FOUND);
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    this.registerReceiver(this.mReceiver, filter);

}

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

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (mBluetoothAdapter != null) {
        if (!mBluetoothAdapter.isEnabled()) {
            mBluetoothAdapter.enable();
            if (mBluetoothAdapter.isDiscovering()) {
                mBluetoothAdapter.cancelDiscovery();
            }
            mBluetoothAdapter.startDiscovery();
        }
    }
    return START_STICKY;
}
}

我正在运行检测蓝牙设备的服务。在接收器中我能够检测到ACTION_DISCOVERY_STARTEDACTION_DISCOVERY_FINISHED。它有时也会检测到ACTION_FOUND。但并非所有蓝牙设备都能看到。

如果我转到设置>蓝牙>更多设置>刷新。 现在所有设备都显示在日志中。 它仍会点击DISCOVERY_STARTEDDISCOVERY_FINISHED

2 个答案:

答案 0 :(得分:1)

尝试在android.permission.ACCESS_COARSE_LOCATION及以下

上使用权限SDK 22

https://code.google.com/p/android/issues/detail?id=190188

答案 1 :(得分:1)

Android 6.0需要额外的权限来发现范围内的设备。添加权限如下。

private void discoverDevices() {
    if (mBluetoothAdapter.isDiscovering()) {
        mBluetoothAdapter.cancelDiscovery();
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
        switch (ContextCompat.checkSelfPermission(mActivity, Manifest.permission.ACCESS_COARSE_LOCATION)) {
            case PackageManager.PERMISSION_DENIED:
                ActivityCompat.requestPermissions(mActivity,
                        new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                        REQUEST_ACCESS_COARSE_LOCATION);

                break;
            case PackageManager.PERMISSION_GRANTED:
                mBluetoothAdapter.startDiscovery();
                break;
        }
    }

}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case REQUEST_ACCESS_COARSE_LOCATION: {
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                mBluetoothAdapter.startDiscovery();
            }
            else {
               //exit application or do the needful
            }
            return;
        }
    }
}