开启蓝牙扫描,开启显示成功但是回调接口里面的方法onLeScan不被调用,在这个方法中Log打印不出东西 权限开过了 这是什么原因 折磨一天了
package com.example.mrwuchao.bluetoothtest;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
public static final int REQUEST = 100;
private BluetoothAdapter adapter;
private List<String> list = new ArrayList<>();
private ListView listView;
private ListAdapter listAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.blue_list_view);
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, "该设备不支持蓝牙BLE", Toast.LENGTH_SHORT).show();
} else {
BluetoothManager manager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
//获得蓝牙适配器
adapter = manager.getAdapter();
if (adapter == null || !adapter.isEnabled()) {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, REQUEST);
}
}
listAdapter = new ListAdapter(this, list);
listView.setAdapter(listAdapter);
}
//定义的按钮点击的处理方法
public void scan(View view) {
Log.i("scan", "点击了扫描");
final BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi,
byte[] scanRecord) {
Log.i("scan","终于回调了");
}
};
if (adapter.isEnabled()) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
adapter.stopLeScan(mLeScanCallback);
Toast.makeText(MainActivity.this, "扫描结束", Toast.LENGTH_SHORT).show();
}
}, 15000);
boolean b = adapter.startLeScan(mLeScanCallback);
Log.i("scan", "" + b);
//10秒后停止扫描
} else {
Toast.makeText(MainActivity.this, "蓝牙不可用", Toast.LENGTH_SHORT).show();
}
}
}