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();
    }
}
阅读 4.7k
1 个回答
新手上路,请多包涵

哈喽 有木有人啊

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进