在6.0.1中搜索附近的蓝牙

时间:2016-06-06 10:58:32

标签: android bluetooth

我正在使用搜索附近蓝牙的应用程序。我使用以下代码,它可以在5.0中找到它返回附近的蓝牙设备但是当我在6.0.1中测试时BluetoothDevice.ACTION_FOUND未被调用时,仅调用BluetoothAdapter.ACTION_DISCOVERY_STARTEDBluetoothAdapter.ACTION_DISCOVERY_FINISHED

@Override
protected void onCreate(Bundle savedInstanceState) {
    BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    bluetoothAdapter = bluetoothManager.getAdapter();
    Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,0);
    startActivity(discoverableIntent);
}
private final BroadcastReceiver bReciever = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        System.out.println(action);
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            System.out.println(device);
        }
    }
};
public void onSearch(View v){
    if (bluetoothAdapter.isDiscovering()) {
        bluetoothAdapter.cancelDiscovery();
    }
    System.out.println(bluetoothAdapter.startDiscovery());
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
    intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    registerReceiver(bReciever, intentFilter);

}

我的问题是为什么在6.0.1中没有调用BluetoothDevice.ACTION_FOUND?谢谢。

1 个答案:

答案 0 :(得分:-1)

您是否检查过应用程序管理器中的权限?

转到" 设置" - > "的应用" - > " 您的应用" - > "的权限"

并检查是否给出了蓝牙许可。