One_Month 2017-01-21 12:23 采纳率: 22.2%
浏览 4287
已结题

急 求助android蓝牙扫描问题onLeScan()不被调用

开启蓝牙扫描,开启显示成功但是回调接口里面的方法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();
    }
}

}

  • 写回答

2条回答 默认 最新

  • oyljerry 2017-01-21 14:32
    关注
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?