android studio 中蓝牙BLE设备搜索

发布于 2022-09-04 10:42:11 字数 1862 浏览 44 评论 0

同样的代码在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();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

緦唸λ蓇 2022-09-11 10:42:11

哈喽 有木有人啊

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文