天蓝色blue 2017-01-17 12:21 采纳率: 0%
浏览 2997

android studio 中蓝牙BLE设备搜索

同样的代码在eclipse下能执行LeScanCallback的onLeScan方法 而到了android studio 中就不执行了 权限都加了,是不是android studio中还有配置别的东西?
public class Blues {
Context con;
Blues.M_LeScanCallback_interface callback_interface;
private BluetoothAdapter mBluetoothAdapter;
private LeScanCallback mLeScanCallback = new LeScanCallback() {
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
Blues.this.callback_interface.get_address(device.getAddress());
}
};

public Blues(Context con, Blues.M_LeScanCallback_interface callback_interface) {
    //在eclipse中 就加执行到这里   在android studio中就不会
    BluetoothManager bluetoothManager = (BluetoothManager)con.getSystemService("bluetooth");
    this.mBluetoothAdapter = bluetoothManager.getAdapter();
    this.con = con;
    this.callback_interface = callback_interface;
}

public void scanLeDevice(boolean enable) {
    if(enable) {
        this.mBluetoothAdapter.startLeScan(this.mLeScanCallback);
    } else {
        this.mBluetoothAdapter.stopLeScan(this.mLeScanCallback);
    }

}

public interface M_LeScanCallback_interface {
    void get_address(String var1);
}

}

//下面是activity中的调用
public class MainActivity extends Activity implements Blues.M_LeScanCallback_interface {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}


@Override
protected void onResume() {
    super.onResume();
    Blues blues = new Blues(this, this);
    blues.scanLeDevice(true);
}

@Override
public void get_address(String str) {
    Toast.makeText(getApplicationContext(), "" + str, Toast.LENGTH_SHORT).show();
}

}

  • 写回答

2条回答 默认 最新

  • 小菜陈 2017-01-18 07:49
    关注

    1,查看有没有错误log,
    2,查看api版本是否一致;
    3,android6.0蓝牙需要Manifest.permission.ACCESS_COARSE_LOCATION定位权限。
    么有具体信息,只能猜测了

    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿