Bluetooth discovery without prompt in Android

前端 未结 1 1992
生来不讨喜
生来不讨喜 2021-02-10 12:15

I am able to turn on/off Bluetooth without any prompt using the following code. It requires BLUETOOTH and BLUETOOTH_ADMIN permissions.

b         


        
1条回答
  •  没有蜡笔的小新
    2021-02-10 12:57

    Finally I have found a way to do this using reflection.

    Method method;
    try {
        method = bluetoothAdapter.getClass().getMethod("setScanMode", int.class, int.class);
        method.invoke(bluetoothAdapter,BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE,120);
        Log.e("invoke","method invoke successfully");
    }
    catch (Exception e){
        e.printStackTrace();
    }
    

    Warning: Above method is trying to invoke hidden method. So in future maybe it will not work.

    0 讨论(0)
提交回复
热议问题